Scope of __device__ variable

On http://docs.nvidia.com/cuda/cuda-c-programming-guide/#device-variable-qualifier it says that a device qualifyer 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 apropriate way of doing it?

Device variables can only be declared at global scope (i.e. file scope), not within the body of any function.

Since they are at global scope, they are global to all kernels in your application (or specifically, all kernels within the compilation unit.)

Otherwise I think your question has already been addressed in your cross-posting here:

[url]cuda - Scope of __device__ qualifier - Stack Overflow