传输参数在openCL中的函数

时间:2012-04-09 15:48:47

标签: opencl gpu gpu-programming

具有以下内核功能:

   private static String programSource =
        "__kernel void sampleKernel(__global float *Y, __global  float *param) "
        + "{ int index = get_global_id(0); "
        + "  Y[index]=param[0]-Y[index]/param[1]-param[2]; "
        + "} ";

第一个参数“Y”工作正常,但第二个参数“param”工作不正确,我收到空值。第二个参数必须是一个数组,由3个单元组成。

带传输参数的代码片段:

        float[] arr_params = new float[3];
        arr_params[0] = (float) h_c;
        arr_params[1] = (float) sy;
        arr_params[2] = (float) dy;

        //pointers
        Pointer Pvy = Pointer.to(vy);
        Pointer Parr_params = Pointer.to(arr_params);

        cl_mem memObjects[] = new cl_mem[2];

        memObjects[0] = clCreateBuffer(context,
                CL_MEM_READ_WRITE,
                Sizeof.cl_float * vy.length, Pvy, null);
        memObjects[1] = clCreateBuffer(context,
                CL_MEM_READ_ONLY,
                Sizeof.cl_float * arr_params.length, Parr_params, null);

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

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

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

        // Read the output data
        clEnqueueReadBuffer(commandQueue, memObjects[0], CL_TRUE, 0, vy.length * Sizeof.cl_float, Pvy, 0, null, null);
        // Release kernel, program, and memory objects
        clReleaseMemObject(memObjects[0]);
        clReleaseMemObject(memObjects[1]);

1 个答案:

答案 0 :(得分:2)

第二个缓冲区全部为零,因为在clCreateBuffer调用中,您没有告诉OpenCL从何处获取数据。使用CL_MEM_USE_HOST_PTR或CL_MEM_COPY_HOST_PTR。