Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable length array declaration not allowed in OpenCL - why?

I want to create a local array inside my OpenCL kernel, whose size depends on a parameter of the kernel. It seems that's not allowed - at least with AMD APP.

Is your experience different? Perhaps it's just the APP? Or is is there some rationale here?

Edit: I would now suggest variable length arrays should be allowed in CPU-side code too, and it was an unfortunate call by the C standard committee; but the question stands.

like image 419
einpoklum Avatar asked Aug 01 '13 13:08

einpoklum


2 Answers

You can dynamically allocate the size of a local block. You need to take it as a parameter to your kernel, and define its size when you call clSetKernelArg.

definition example:

__kernel void kernelName(__local float* myLocalFloats, ...)

host code:

clSetKernelArg(kernel, 0, myLocalFloatCount * sizeof(float), NULL); // <-- set the size to the correct number of bytes to allocate, but use NULL for the data.

Make sure you know what the limit for local memory is on your device before you do this. Call clGetDeviceInfo, and poll for the 'CL_DEVICE_LOCAL_MEM_SIZE' value.

like image 79
mfa Avatar answered Nov 10 '22 19:11

mfa


Not sure why people are saying you can't do this as it is something many people do with OpenCL (Yes, I understand it's not exactly the same but it works well enough for many cases).

Since OpenCL kernels are compiled at runtime and, just like text, you can just simply set the size to whatever size you want and then recompile your kernel. This obviously won't be perfect in cases where you have huge variability in sizes but usually I compile several different sizes at startup and then just call the correct one as needed (in your case based on the kernel argument). If I get a new size I don't have a kernel for I will compile it right then and cache the kernel in case it comes up again.

like image 3
Jim V Avatar answered Nov 10 '22 20:11

Jim V