0

EDIT: Realized that I was reinstantiating the cl_context on every iteration. It works now.

Although this is a JOCL specific question, I believe it is applicable to OpenCL in general.

I have a program that sends int arrays to an OpenCL kernel to be processed on the GPU, then returns the results to Java. The program works fine on the first iteration, but when clSetKernelArg is called a second time (with a new array), it throws a CL_INVALID_MEM_OBJECT error.

I don't see why setting a kernel parameter crashes the program on the second iteration only.

Here is a simplified version of the Java code:

... bunch of kernel initialization code that works correctly ...

void doSingleIteration() {
    int[] array = new int[length];
    fillArrayWithData(array);

    cl_mem buf = CL.clCreateBuffer(context, CL.CL_MEM_READ_ONLY,
              length * Sizeof.cl_float, null, null);
    CL.clEnqueueWriteBuffer(commandQueue, buf, true, 0,
              array.length * Sizeof.cl_float, Pointer.to(array), 0, null, null);

    CL.clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(buf)); // This crashes on the second iteration ONLY??

    CL.clEnqueueNDRangeKernel(... this works fine ...)

    ... some other unrelated code ...
}

doSingleIteration(); // This one is ok
doSingleIteration(); // This one crashes

Here is the header for the corresponding kernel in OpenCL:

kernel void myKernel(global const int* myArray) {
    ...
}

The only way I have gotten this to work is by reinstantiating the command queue on each iteration. But that is very inefficient. How can I call clSetKernelArg a second time without crashing the program?

Thanks

Daniel Williams
  • 635
  • 7
  • 14
  • Did you register the callback function while calling clCreateContext in order to get error messages? It might give you more information about the one you are getting right now. How big is your buffer, how big is your available memory? Have you tried reusing the same buffer? – AlexG May 02 '19 at 17:15
  • I feel silly, but I just realized that I was resetting the cl_context on every iteration. I realized after getting an CL_INVALID_CONTEXT error while trying to reuse the buffer as you suggested. Thanks for that suggestion. – Daniel Williams May 03 '19 at 00:28

0 Answers0