The Bug from Hell

>> Wednesday, June 1, 2011

I just spent three hours riddling out why I was getting a segmentation fault every time I enqueued a kernel. The application uses OpenGL-OpenCL interoperability, so I assumed the problem involved the shared resources. But when I tested clEnqueueAcquireGLObjects and clEnqueueReleaseGLObjects, everything worked fine.

As it turned out, the problem had nothing to do with interoperability. I was setting the kernel argument incorrectly. Here's my original code:

clSetKernelArg(kernel, 0, sizeof(cl_mem), buffer);

Anyone familiar with OpenCL will know that I'm trying to make a buffer object into a kernel argument. But can you spot my terrible error? If not, here it is: the last argument should be a reference to the memory object, not the memory object itself. The function should be called as follows:
clSetKernelArg(kernel, 0, sizeof(cl_mem), &buffer);

That missing ampersand cost me three hours of coding. If the compiler had caught this, I could have fixed the problem in three seconds. But no, the last argument of clSetKernelArg can be set to anything, and the only way you'll know something's wrong is when the kernel execution raises a segmentation fault.


5028178082 January 1, 2013 at 4:59 PM  

This was so incredibly helpful. I made this mistake too and was completely stumped until I saw this. Thanks!

PS, in your book you mention that you get Invalid Image Format errors when you try to create a CL Image from a GL texture.
I had this as well, the problem was that my texture was unsigned-byte RGB format, but OpenCL doesn't support this - changing the format of the texture to RGBA during the glTexImage2D call fixed this.

Anonymous,  January 10, 2013 at 4:29 PM  

No a 'reference' but a constant pointer to any data type (const void *). So if u got an object u gotta pass its memory address by adding '&'.
If u are using oepncl c++ wrapers wouldnt have this problem

Robert Theed January 28, 2013 at 3:12 PM  

haha, legend, thank you so much. You just saved me a significant portion of my life.

Post a Comment

  © Blogger template Werd by 2009

Back to TOP