Can i allocate memory inside kernel?


I need to use a pointer inside of my kernel.
Is it possible allocate memory to a __global pointer that i put inside of my kernel?


short answer: no

long answer: not yet :wink:

One trick I have seen is to allocate a large block of __global memory using the host API, then use their favorite heap allocation algorithm inside the kernel to carve this memory up into smaller chunks.


Thanks, it works!

I haven’t had recourse to use this yet, so I haven’t tried it. However the CL_MEM_ALLOC_HOST_PTR option for clCreateBuffer() seems to set up precisely what you want: allowing a kernel executing on a device to allocate host accessible (host?) memory. Here’s the link to the specification: … uffer.html

Or am I misunderstanding something entirely?

That means you’re using a host pointer, or CPU memory, for your buffer. But right now you can’t allocate any new memory (or free it) from within a kernel, only through the application API calls. ljbade’s method is the best, until new OpenCL specs allow for dynamic memory within kernels.