I am having a hard time on using constant memory space.
(1) According to Programming Guide (2.3) B.2.5, constant variables are only allowed at file scope, and they can not be defined by the extern keyword. So the question is: is it possible to make some constant data visible across multiple files?
(2) According to Programming Guide (2.3) B.2.5, the function cudaGetSymbolAddress can be used to take the address of a constant variable. However, the following code complains “cannot take the address of constant data”. What is wrong with it? Or simply we could not take the address of a constant variable, not as stated in the programming guide.
#include
using namespace std; constant int DATA;
int main(void)
{
int d = 1;
cudaMemcpyToSymbol(“DATA”, &d, sizeof(int));
int data_ptr;
cudaError_t err = cudaGetSymbolAddress((void*)&data_ptr, “DATA”);
cout << cudaGetErrorString(err) << endl;
return 0;
}
About #2 I also had som minor issues when trying to implement it the first time. I finally didn’t use the cudaGetSymbolAddress stuff… You can simply do:
No, you cannot. The same is true of device and texture declarations, by the way. They are implicitly declared static for that compilation unit. The root cause of this behavior is that there is no linker for CUDA kernel code.
This will not generate any compile errors, no. But if you compile each .cu files in separate compilation units, each gets its own private version of the constant variable or texture reference. If you, say, bind the texture ref in one compilation unit and then try to use it in a kernel in another, you will get a texture not bound error.
I haven’t tried it with a constant, but the reference manual and doxygen pages clearly say cudaGetSymbolAddress() works for symbols in global memory. No mention of constant memory.