I read several old topics pretending one can’t dynamically allocate constant memory, even from the host, as this guy wants to do the same thing that I do → http://stackoverflow.com/questions/271273/dynamic-allocation-of-constant-memory-in-cuda
The typical answer is that one should texture memory instead. Anyway, I ran the following test:
__constant__ int* c;
__global__ void kernel()
{
/* UPDATE: ok, the following works, suggesting it's not constant memory... */
c[4] = 666;
for (int i = 0; i < 10; ++i)
{
printf("%d\n", c[i]);
}
}
int main()
{
int host_c[10] = {0,1,2,3,4,5,6,7,8,9};
int* dev_c;
cudaMalloc(&dev_c, 10 * sizeof(int));
cudaMemcpy(dev_c, host_c, 10 * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c, &dev_c, sizeof(int*));
kernel<<<1,1>>>();
}
This seems to work, as I get no error and my 10 numbers are correctly printed.
I know from the book “Programming massively parallel processors” that the constant memory is just global memory that is subsequently cached.
My questions are:
- Just because the "c" pointer is in constant memory, is the area of global memory that c end up pointing automatically considered as "constant memory" by the device? Or am I naive and doing it wrong?
- Is it really the same to use texture memory instead of constant memory? If it is, why did they invent this "constant memory"?
Update: Sorry but I just thought now about this basic test answering this first question: I can modify the data pointed by c. This suggest these data are not considered as constant memory! Ok but my next question still worth answering…
I need to load data from a file. Those data may use more that 65535 bytes, so I actually need an dynamically allocated vector of dynamically allocated constant memory banks to hold those data. But once those data are on the device, they won’t change, ever…
Update: Sorry but I just thought now about this basic test answering my first question: I can modify the data pointed by c. This suggest these data are not considered as constant memory! Ok but my second question still worth answering…