Different __device__ variables behind the one name

I am working on my first big CUDA project and I am really frustrated by a problem I do not understand.

My project is structured as follows:

    [*]In "barnes_hut_cuda.h" there are several "global" pointers to device memory, e. g. ``` __device__ float3* positions = NULL;. ``` [*]In "barnes_hut_cuda.cu" they are initialized by »initCuda()«. »setRandomValues()« generates some useful data and copies it to the device memory.[*]Every kernel is located in "kernel1.cu" to "kernel6.cu" and every "kernel[i].h" provides a function to run the kernel. -> In "kernel1.cu" the function »initKernel1()« does some initialization work and »void kernel1(void)« runs the kernel.

The main-function simply calls some functions in the following order:

[list=1][]»initCuda()« -> invokes »initKernel1()«[]»setRandomValues()«»kernel1()«.

Now the following problem occurs: After initializing the pointers, they have a specific address and everything is fine. Then, »initKernel1()« is invoked, where the pointers have the value »NULL«. After that in »setRandomValues()« again the pointers are initialized. Invoking »kernel1()« shows NULL-pointers again.

I wrote a main function, that invokes the functions in the right order and in every function I print the value of one pointer (where it points to on device Memory) and the location of the variable on the host memory (in brackets). Here is the output:

positions = 	310000 (&61f5b8) (initCuda())

positions = 	0 (&61f648) (initKernel1())

positions = 	310000 (&61f5b8) (setRandomValues())

positions = 	0 (&61f648) (kernel1())

The result clearly shows that for the functions in “barnes_hut_cuda.cu” the variables defined in “barnes_hut_cuda.h” are different to those seen by the functions defined in “kernel1.h”, although they should be the same.

It seems that every time “barnes_hut_cuda.h” is included, all variables are defined again. Maybe this is an issue with the »__device__«-macro.

(I compile the source files one by one to object code and then link them together. Normally that should work. (The makefile of NVIDIA does the same.))