CL_INVALID_KERNEL_ARGS 在 JOCL 中(Java 绑定到 OpenCL)。

CL_INVALID_KERNEL_ARGS in JOCL (a Java Binding to OpenCL).

有人在 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)

我编辑了他们的样本 HelloJOCL.java 来做矩阵乘法计算 matrixMul.cl (内核代码)。这是导致错误的内核参数:

// 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]));

工作项维度代码:

// 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);

以及内核代码:

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;"+
                "}";

您的内核代码显示了 5 个输入参数,C,A,B,wA wB。 但我只看到此处列出了 3 个 clSetKernelSrg 调用。

核函数定义为

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

因此需要 五个 个参数。您只提供前 three 个参数,即表示 float* 值的内存对象。为了启动这个内核,你必须为 all 参数传递值。在您的情况下,这可能大致如下所示:

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 }));