How to create __global__ array at runtime?

Hai,

In my program I need to create a global array at runtime whose size is varying in each iteration. Is this possible?

My device compute capability is 2.1.

Please give any suggestion
Thank you in advance

In other words You want to allocate an array of data using cudaMalloc and then bind it to device pointer (global memory) defined within CUDA kernel, right?

This topic may be helpful.

MK

Thank you for your reply

I have a global function and from within the function I need to create another global array with varying size, ie, like

global void myMethod(int *a)

{
//code
global float arr; // the “size” is determined at runtime not passed from host
// and content of “arr” should be retained to the next call
//code
}

main()
{
int a=(int*) malloc(N*sizeof(int));
cudaMemcpy(dev_a, a,sizeof(int) * N, cudaMemcpyHostToDevice);
while (k< threshold)
{
myMethod<<<M,N>>>(dev_a);
//calculate k using “arr”
}

}
is this possible?

global is a function type qualifier. It cannot be applied to variables. To make a global variable/buffer use device qualifier. Refer to variable type qualifiers here.

You could run two kernals. First determines the size of the buffer and returns it to host. Then host code realloc proper amount of global memory. Finally the second one, ‘myMethod’ kernel, doing Your stuff on the buffer. One can use malloc or new operator with CC 2.x and higher (but I don’t remember exactly) but it is not a good idea.

MK