How can I create global variables in CUDA??
Could you please give me an example?
How can create arrays inside a CUDA function for example
__global__ void test()
{
int *a = new int[10];
}
or How can I create a global array and access it from this function. for example
__device__ int *a;
__global__ void test()
{
a[0] = 2;
}
Or How can I use like the following..
__global__ void ProcessData(int img)
{
int *neighborhood = new int[8];
getNeighbourhood(img, neighbourhood);
}
Still I have some problem with this. I found that compare to
__device__
if I define
"__device__ __constant__" (read only)
will improve the memory access.
But my problem is I have an array in host memory say
float *arr = new float[sizeOfTheArray];
I want to make it as a variable array in device and I need to modify this in device memory and I need to copy this back to host. How can I do it??
The C++
newoperator is supported on compute capability 2.0 and 2.1 (ie. Fermi) with CUDA 4.0, so you could usenewto allocate global memory onto a device symbol, although neither of your first two code snippets are how it would be done in practice.On older hardware, and/or with pre CUDA 4.0 toolkits, the standard approach is to use the
cudaMemcpyToSymbolAPI in host code:which copies a dynamically allocated device pointer onto a symbol which can be used directly in device code.
EDIT: Answering this question is a bit like hitting a moving target. For the constant memory case you now seem interested in, here is a complete working example:
This shows copying data onto a constant memory symbol, and using that data inside a kernel.