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
?
Asked
Active
Viewed 3,831 times
3

karlphillip
- 92,053
- 36
- 243
- 426

kil
- 41
- 1
- 3
-
Where did you get that information? AFAIK there is no such thing as memory management inside a kernel. You have to prepare everything upfront. – Jonas Bötel Mar 16 '11 at 16:33
-
malloc and free are supported according to the 3.2 programming guide. – Ade Miller Mar 23 '11 at 05:16
-
1Also `new` and `delete` since 4.0 – Manolete Sep 20 '12 at 09:48
2 Answers
5
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.

YesThatIsMyName
- 1,585
- 3
- 23
- 30
-
2if newsize < oldsize, the loop should be until newsize right? also, old can be null. – shaoyl85 Feb 02 '14 at 18:44
4
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?

CygnusX1
- 20,968
- 5
- 65
- 109