设备或主机均可调用的 CUDA 函数

2022-01-10 00:00:00 function cuda scope c++

我在一些 CUDA 代码中有一个可重用的函数,需要从设备和主机调用.有合适的限定词吗?

I have a re-useable function in some CUDA code that needs to be called from both the device and the host. Is there an appropriate qualifier for this?

例如在这种情况下,func1 的正确定义是什么:

e.g. what's the correct definition for func1 in this case:

int func1 (int a, int b) {
    return a+b;
}

__global__ devicecode (float *A) {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    A[i] = func1(i,i);
}

void main() {
    // Normal cuda memory set-up

    // Call func1 from inside main:
    int j = func1(2,4)

    // Normal cuda memory copy / program run / retrieve data
}

到目前为止,我只能通过两次使用该功能来使其工作:一次明确用于设备,一次用于主机.有没有更好的办法?

So far I can only get this to work by having the function twice: once explicitly for the device and once for the host. Is there a better way?

推荐答案

来自 CUDA 编程指南:

From the CUDA Programming Guide:

__device____host__ 限定词可以一起使用,但是,在在这种情况下,函数会同时为主机和设备编译.

The __device__ and __host__ qualifiers can be used together however, in which case the function is compiled for both the host and the device.

相关文章