CUDA: Using realloc inside kernel

时光毁灭记忆、已成空白 提交于 2019-12-19 09:38:23

问题


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?


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/5322583/cuda-using-realloc-inside-kernel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!