如何在 CUDA 内核中调用主机函数? [英] How to call a host function in a CUDA kernel?
问题描述
正如以下错误所暗示的,内核中不允许调用主机函数('rand'),如果我确实需要这样做,我想知道是否有解决方案.
As the following error implies, calling a host function ('rand') is not allowed in kernel, and I wonder whether there is a solution for it if I do need to do that.
error: calling a host function("rand") from a __device__/__global__ function("xS_v1_cuda") is not allowed
推荐答案
很遗憾,您不能在设备中调用未使用 __device__
修饰符指定的函数.如果您需要设备代码中的随机数,请查看 cuda 随机生成器 curand http://developer.nvidia.com/库兰
Unfortunately you can not call functions in device that are not specified with __device__
modifier. If you need in random numbers in device code look at cuda random generator curand http://developer.nvidia.com/curand
如果您想从内核调用自己的主机函数,请同时使用 __host__
和 __device__
修饰符:
If you have your own host function that you want to call from a kernel use both the __host__
and __device__
modifiers on it:
__host__ __device__ int add( int a, int b )
{
return a + b;
}
当这个文件被 NVCC 编译器驱动编译时,编译了两个版本的函数:一个可以被主机代码调用,另一个可以被设备代码调用.这就是为什么现在主机和设备代码都可以调用这个函数的原因.
When this file is compiled by the NVCC compiler driver, two versions of the functions are compiled: one callable by host code and another callable by device code. And this is why this function can now be called both by host and device code.
这篇关于如何在 CUDA 内核中调用主机函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!