Declaring array inside OpenCL kernel not working. (clBuildProgram(-11))

1k views Asked by At

I have an OpenCL code that works perfect on my MAC. But when I port it to an EC2 g2.2xlarge instance, it gives the

Exception
ERROR: clBuildProgram(-11)

Now after a lot of debugging, I found out that the kernel has a syntax error. After narrowing it down further, I found that the error is in the following line:

int size_a = POP_SIZE / 4 / numberOfDevices;
int aliveIndividualsIndex[size_a];

If I try:

int aliveIndividualsIndex[40];

then it works on the EC2 Instance too.

Why is there a difference between how a MAC and EC2 instance are handling the same kernel code? Why is one of them recognizing the code and why is the other one giving a syntax error? How do I fix it? Because I need to use [size_a].

Both of them are using OpenCL 1.2. If you need any more information about the versions of gcc or g++ or anything else, please ask, I'll provide them.

1

There are 1 answers

0
faken On

The size of arrays within OpenCL kernels should be known at compile time. As such, to solve your problem, make numberOfDevices a compile time constant by passing it to the build options of the clBuildProgram() function with the "-D name=definition" option, e.g.:

char * buildOptions;     // Should be allocated with enough space
unsigned int numDevices; // Initialize with number of devices
...
// Use snprint instead if available
sprintf(buildOption, "-D NUMBER_OF_DEVICES=%u", numDevices);
...
// Build program with specified compiler options
clBuildProgram(prog, numDevices, deviceLst, buildOptions, NULL, NULL);

It may also be necessary to make size_a a constant in your kernel source code, e.g.:

#define SIZE_A POP_SIZE / 4 / NUMBER_OF_DEVICES;

You can now declare the array within the kernel:

aliveIndividualsIndex[SIZE_A];