将函数从C移植到OPENCL

时间:2019-02-25 04:02:59

标签: c kernel opencl

我用C语言编写了此功能:

void SimpleGraphDrawing(unsigned char *image, int dim[2],double range[2][2])
{
    for (int j=0;j<dim[1];j++)
        for (int i=0;i<dim[0];i++)
        {
            float x=range[0][0]+(i+0.5)*(range[0][1]-range[0][0])/dim[0]; 
            float y=range[1][0]+(j+0.5)*(range[1][1]-range[1][0])/dim[1]; 
            float val=(x*x+y*y-1);
            val=val*val*val-x*x*y*y*y;
            image[j*dim[0]+i]=(val>0)*255; 
        }
}

我想将其移植到OPENCL。

这是我的新功能:

void SimpleGraphDrawingGPU(OpenCLContext &context, unsigned char *image, int dim[2],double range[2][2])
{
    cl_int error;
    int blocking = true;
    SetConsoleColor(15);
    cl_int myKernel = GRAPH_DRAWING;

    cl_mem imageKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, dim[0]*dim[1] * sizeof(char), NULL, &error);
    cl_mem dimKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, 2*sizeof(int), NULL, &error);
    cl_mem rangeKernel = clCreateBuffer(context.GetContext(), CL_MEM_READ_WRITE, 4 * sizeof(double), NULL, &error);

    error = clEnqueueWriteBuffer(context.GetCommandQueue(0), imageKernel, blocking, 0, dim[0] * dim[1]* sizeof(int), image, 0, NULL, NULL);
    error = clEnqueueWriteBuffer(context.GetCommandQueue(0), dimKernel, blocking, 0, 2*sizeof(int), &dim, 0, NULL, NULL);
    error = clEnqueueWriteBuffer(context.GetCommandQueue(0), rangeKernel, blocking, 0, 4 * sizeof(double), &range, 0, NULL, NULL);

    error = clSetKernelArg(context.GetKernel(myKernel), 0, sizeof(cl_mem), &imageKernel);
    error = clSetKernelArg(context.GetKernel(myKernel), 1, sizeof(cl_mem), &dimKernel);
    error = clSetKernelArg(context.GetKernel(myKernel), 2, sizeof(cl_mem), &rangeKernel);

    size_t globalWorkSize[1] = { 1024 };
    error = clEnqueueNDRangeKernel(context.GetCommandQueue(0), context.GetKernel(myKernel), 1, NULL, globalWorkSize, NULL, 0, NULL, NULL);

    error = clReleaseMemObject(imageKernel);
    error = clReleaseMemObject(dimKernel);
    error = clReleaseMemObject(rangeKernel);
    ;
}

这也是MyKernels的功能:

__kernel void GRAPH_DRAWING ()
{

}

输出确实很奇怪,我不知道如何解决:“ PROGRAM -33”

1 个答案:

答案 0 :(得分:0)

-33是无效设备。您应该观看此示例以了解它们如何管理和处理OCL设备:https://software.intel.com/en-us/articles/gemm