You can use malloc inside the kernel. check the following, which is taken from the nvidia cuda manual:
__global__ void mallocTest()
{
char* ptr = (char*)malloc(123);
printf("Thread %d got pointer: %p\n", threadIdx.x, ptr);
free(ptr);
}
void main()
{
cudaThreadSetLimit(cudaLimitMallocHeapSize, 128*1024*1024);
mallocTest<<<1, 5>>>();
cudaThreadSynchronize();
}
will output:
Thread 0 got pointer: 00057020
Thread 1 got pointer: 0005708c
Thread 2 got pointer: 000570f8
Thread 3 got pointer: 00057164
source
share