Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CUDA: Using realloc inside kernel

Tags:

cuda

I know that it is possible to use malloc inside the kernel to allocate memory on GPU's global memory. Is it also possible to use realloc?

like image 496
kil Avatar asked Sep 14 '26 18:09

kil


2 Answers

You could write you own realloc device function for your data type.

Just allocate the new space for a new array, copy the old values to the new, free the old array space, return the new with more space.

Approximately like the following code fragment:

__device__ MY_TYPE* myrealloc(int oldsize, int newsize, MY_TYPE* old)
{
    MY_TYPE* newT = (MY_TYPE*) malloc (newsize*sizeof(MY_TYPE));

    int i;

    for(i=0; i<oldsize; i++)
    {
        newT[i] = old[i];
    }

    free(old);
    return newT;
}

But be sure to call it, if you really need it. Also add proper error checking.

like image 57
YesThatIsMyName Avatar answered Sep 19 '26 02:09

YesThatIsMyName


In the Cuda Programming Guide, when they introduce malloc and free functions, there is no mention of realloc. I would assume that it does not exist.

If you want to know it for sure, why don't you write a simple kernel and try using it?

like image 44
CygnusX1 Avatar answered Sep 19 '26 03:09

CygnusX1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!