>> 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.