简体   繁体   中英

Porting a function from C to OPENCL

I have this function written in 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; 
        }
}

And I want to port it to OPENCL.

This is my new function:

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

This is also the function from MyKernels:

__kernel void GRAPH_DRAWING ()
{

}

The output is really strange and I have no idea how to fix it: "PROGRAM -33"

-33 is invalid device . You should watch this example to see how they manage and handle OCL devices: https://software.intel.com/en-us/articles/gemm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM