VLA : workaround to get Variable Length Arrays
to compensate the lack of VLAs in OpenCL, this tiny trick offers the ability to create them though:
add some extra space characters at the very top of your kernel file (in this case 20)
after reading the kernel file into char* str, but before building the program, do the following:
then inside your kernel, the line
const char * define = "#define M 0x00000008";
memcpy(str, define, 20);
allocates an array of the size you entered in the host application. the hex value can be created dynamically, so you really have a VLA in OpenCL
Re: VLA : workaround to get Variable Length Arrays
Another approach is to pass in a compiler flag to effectively insert #defines. E.g., "-define M=8" passed in as the options string to clBuildProgram will build the program as if you had #define M 8 in the code.
I personally think using string manipulation on code is one of the cooler parts of OpenCL. Since kernel programs start out as strings you can modify and compose them all you want before building them. This makes it easy to change a kernel to use float vs. int, or change the size of an array or anything else like that. It's really very powerful. I've written a lot of kernels where I write them as three or four strings and then compose them the way I need them before building the program.