Use cpu function in cuda

我的梦境 提交于 2020-01-25 06:12:25

问题


I would like to include a C++ function in a CUDA Kernel, but this function is written for CPU like this:

inline float random(int rangeMin,int rangeMax){
    return rand(rangeMin,rangeMax);
}

Assume that the rand() function use either curand.h or Thrust cuda library.

I thought in use a Kernel function (with only one GPU thread) that would include this function as inline, so the cuda compiler would generate the binary for the GPU.

Is this possible? If so I would like to include another inlines functions written for the cpu in the CUDA kernel function.

Something like this:

-- InlineCpuFunction.h and InlineCpuFunction.cpp

-- CudaKernel.cuh and CudaKernel.cu (this one include the above header and uses it's function in the CUDA kernel)

If you need some more explanation (as this may look confusing) please ask me.


回答1:


You can tag the functions you want to use on both the device and the host with both the __host__ __device__ decorators that way it's compiled for your cpu and gpu.



来源:https://stackoverflow.com/questions/18315796/use-cpu-function-in-cuda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!