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.))