javaopenclgpgpumatrix-multiplicationjocl

CL_INVALID_KERNEL_ARGS in JOCL (a Java Binding to OpenCL).


Anybody encounter this kind of error on doing Matrix Multiplication in JOCL?

Exception in thread "main" org.jocl.CLException: CL_INVALID_KERNEL_ARGS
at org.jocl.CL.checkResult(CL.java:787)
at org.jocl.CL.clEnqueueNDRangeKernel(CL.java:20802)
at org.jocl.samples.JOCLSample.main(JOCLSample.java:147)

I edited their sample HelloJOCL.java to do a Matrix Multiplication calculations also the matrixMul.cl (Kernel Code). Here is the Kernel arguments that cause the error:

// Create the kernel
    cl_kernel kernel = clCreateKernel(program, "matrixMul", null);

    long time = nanoTime();
    // Set the arguments for the kernel
    clSetKernelArg(kernel, 0, 
        Sizeof.cl_mem, Pointer.to(memObjects[0]));
    clSetKernelArg(kernel, 1, 
        Sizeof.cl_mem, Pointer.to(memObjects[1]));
    clSetKernelArg(kernel, 2, 
        Sizeof.cl_mem, Pointer.to(memObjects[2]));

The work item dimensions code:

// Set the work-item dimensions
    long global_work_size[] = new long[]{n};
    long local_work_size[] = new long[]{1};


// Execute the kernel
    clEnqueueNDRangeKernel(commandQueue, kernel, 1, null,
    global_work_size, null, 0, null, null);

And the Kernel Code:

private static String programSource =
        "__kernel void "+
                "matrixMul(__global float* C,"+ 
                "          __global float* A,"+ 
                "          __global float* B,"+ 
                "          int wA, int wB)"+
                "{"+
                   "int x = get_global_id(0);"+ 
                   "int y = get_global_id(1);"+

                   "float value = 0;"+
                   "for (int k = 0; k < wA; ++k)"+
                   "{"+
                   "   float elementA = A[y * wA + k];"+
                   "   float elementB = B[k * wB + x];"+
                   "   value += elementA * elementB;"+
                   "}"+
                  "C[y * wA + x] = value;"+
                "}";

Solution

  • The kernel function is defined as

    __kernel void matrixMul(__global float* C,
                            __global float* A,
                            __global float* B,
                            int wA, int wB)
    

    and thus expects five arguments. You are only providing the first three arguments, namely the memory objects that represent the float* values. In order to launch this kernel, you will have to pass in values for all arguments. In your case, this could roughly look like this:

    int a=0;
    clSetKernelArg(kernel, a++, 
        Sizeof.cl_mem, Pointer.to(memObjects[0]));
    clSetKernelArg(kernel, a++, 
        Sizeof.cl_mem, Pointer.to(memObjects[1]));
    clSetKernelArg(kernel, a++, 
        Sizeof.cl_mem, Pointer.to(memObjects[2]));
    
    // These have been missing:
    clSetKernelArg(kernel, a++, 
        Sizeof.cl_int, Pointer.to(new int[]{ wA }));
    clSetKernelArg(kernel, a++, 
        Sizeof.cl_int, Pointer.to(new int[]{ wB }));