Scope of __device__ qualifier

时间:2016-04-25 09:32:21

标签: cuda

On http://docs.nvidia.com/cuda/cuda-c-programming-guide/#device-variable-qualifier it says that a __device_ qualifier variable has the "lifetime of an application". Does this mean the kernel? If there are several kernels, how can CUDA know which variable belongs to which kernel?

If i declare a __device_ variable like so:

void someHOSTfunction() {
   __device__ int var;
   // Launch kernel etc...
}

Is "var" still global in the sense that it is still accessible from a kernel launched from a different function, even though it is "local" on the stack of someHOSTfunction() and gets scoped (?) when someHOSTfunction() returns? Does it make any difference to write it like this:

__device__ int var;
void someHOSTfunction() {
   // Launch kernel etc...
}

Now var is a global variable. But that means it is also accessible from other translation units. This probably won't work to prevent that:

static __device__ int var;
void someHOSTfunction() {
   // Launch kernel etc...
}

What would be the appropriate way of doing it?

1 个答案:

答案 0 :(得分:3)

此:

void someHOSTfunction() {
   __device__ int var;
   // Launch kernel etc...
}

在CUDA中是非法的。函数体内不允许__device__变量声明,如果您尝试这样做,编译器将发出错误。您必须在翻译单元范围内声明它们。该限制适用于任何功能,无论是__host__还是__device__

如果您需要针对不同内核使用不同的静态声明__device__变量,则为每个内核使用不同的变量名称。或者使用运行时分配的变量并将其作为参数传递给内核,或使用模板参数变量或其他内容。但是你所描述的在今天的CUDA中是不可能的。