Problem with __constant__ can't use same name for __constant__ in different files

Hi

I searched the Forum but the search engine didnt turn up with any answers. We are working on a project involving a lot of kernels which in essence belong to several C++ Objects. We wanted to put pointers to data on the GPU which is used by several kernels into the constant space. Now I understand that constant declarations are actually file-scope, so we defined them in every distinctive cuda object file seperately. Since they shall all hold the same pointer to the same data on the GPU we actually named them the same way for programming convenience (i.e. device constant float* x). Now the funny thing is that doesnt work. As soon as the same symbol name (i.e. x) is used in two different CUDA modules the whole thing breaks down. It still compiles but the x holds a value of (nil) despite copying an address to it just right before the kernel call. Indeed renaming x to xa in one of the cuda modules solves the problem for that module.

I only can guess that constant variables are to be taking literally existent on the GPU. Is there a way around this problem (something like putting an extern in front of all but one declaration or so?).

If not would it be possible to come up with some solution in a future release of cuda?

For now we can solve the problem, by writing one *.cu file where all other cu files are #included. That way we have basically only a single file and can work with a single declaration of our constants. But it also means that changing one thing in a kernel, requires recompilation of the whole cuda code - which is a bit inconvinient.

best regards
Ceearem

This is an old question, but I think that now that CUDA 5.0 (or even better 5.5) is available we can answer the two main questions:

As the poster noticed, constant symbols are implicitly static in CUDA. CUDA 5.0 enables the separate compilation mode and the linker will detect the duplicate symbol definitions. To be linked in a separate compilation mode, one should define the constant symbol in one compilation units, as for example

File1.cu
__constant__ DataType const_name;

and declare it as extern in any other compilation unit, as for example

File2.cu
extern __constant__ DataType const_name;

Perhaps it could be useful for someone finding this post to clarify things.