I am writing a library for numerical optimization and one of the features is that the client of the library should be able to pass a (double, double, double) -> double
function into my optimizer on the host side. The function has to run on the GPU inside a kernel (device). The problem is that in cuda it is forbidden to take function pointers in host code of a device function. Since the function, which the client is going to pass, is going to be a device function, I am not sure how to proceed. In essence the optimization library has to compile and build without the (double, double, double) -> double
ever existing. Is that even possible?
Asked
Active
Viewed 21 times
0

Stefan B
- 541
- 2
- 6
- 13
-
"cuda it is forbidden to take function pointers in host code of a device function" -- that isn't correct. You need to use an API, but that is how it is done – talonmies May 02 '20 at 18:41
-
1Yes, its possible. There is a [CUDA sample code that demonstrates function pointer usage](https://docs.nvidia.com/cuda/cuda-samples/index.html#function-pointers) and [this answer](https://stackoverflow.com/questions/31057870/passing-host-function-as-a-function-pointer-in-global-or-device-function/31058123#31058123) links to half a dozen or more variants. – Robert Crovella May 02 '20 at 18:42