CL_INVALID_KERNEL_ARGS错误与图像jocl

时间:2016-06-25 22:33:54

标签: java opencl jocl

我的目标是编写一个代码,将图像(图像)作为入口变量并进行分析,并使用java和jocl将结果放入数组(tab1),作为一个开始,我想看看图像是否是接受所以我写了这个程序贝娄和内核"基本"

我的班级

public class Openclnkkh {    private static BufferedImage image;
private static int[]tab1=null;
private static int[]tab2=null;
    private static int sizeX = 0;
private static int sizeY = 0;
    private static cl_mem inputImageMem;
    private static int err;
/** 
 * The OpenCL context
 */
private static cl_context context;
/**
 * The OpenCL command queue
 */
private static cl_command_queue commandQueue;
/**
 * The OpenCL kernel which will actually compute the Mandelbrot
 * set and store the pixel data in a CL memory object
 */
private static cl_kernel kernel;
/**
 * The OpenCL memory object which stores the pixel data */  private static String input;
@SuppressWarnings("deprecation")
public static void main(String args[])
{
     input ="12.jpg";
    image = createBufferedImage(input);
      sizeX =image.getWidth();
   sizeY = image.getHeight();
   tab1=new int[sizeX];
   tab2=new int[sizeX];    
/**
 * Initialize OpenCL: Create the context, the command queue
 * and the kernel.
 */   
    final int platformIndex = 0;
    final long deviceType = CL_DEVICE_TYPE_GPU;
    final int deviceIndex = 0;     
    // Enable exceptions and subsequently omit error checks in this sample  
    CL.setExceptionsEnabled(true);
         // Obtain the number of platforms
    int numPlatformsArray[] = new int[1];
    clGetPlatformIDs(0, null, numPlatformsArray);
    int numPlatforms = numPlatformsArray[0];
    // Obtain a platform ID
    cl_platform_id platforms[] = new cl_platform_id[numPlatforms];
    clGetPlatformIDs(platforms.length, platforms, null);
    cl_platform_id platform = platforms[platformIndex];
    // Initialize the context properties
    cl_context_properties contextProperties = new cl_context_properties();
    contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);       
    // Obtain the number of devices for the platform
    int numDevicesArray[] = new int[1];
   err= clGetDeviceIDs(platform, deviceType, 0, null, numDevicesArray);
   if (err!=CL_SUCCESS)
   {
    System.out.println("error0"+"  "+err);
   }
    int numDevices = numDevicesArray[0];        
    // Obtain a device ID 
    cl_device_id devices[] = new cl_device_id[numDevices];
    err=clGetDeviceIDs(platform, deviceType, numDevices, devices, null);
    if (err!=CL_SUCCESS)
    {
        System.out.println("error1"+"  "+err);
    }
    cl_device_id device = devices[deviceIndex];
    // Create a context for the selected device
    context = clCreateContext(
        contextProperties, 1, new cl_device_id[]{device}, 
        null, null, null);       
 // Check if images are supported
    int imageSupport[] = new int[1];
    err=clGetDeviceInfo (device, CL.CL_DEVICE_IMAGE_SUPPORT,
        Sizeof.cl_int, Pointer.to(imageSupport), null);
    if (err!=CL_SUCCESS)
    {
        System.out.println("error2"+"  "+err);
    }
    System.out.println("Images supported: "+(imageSupport[0]==1));
    if (imageSupport[0]==0)
    {
        System.out.println("Images are not supported");
        System.exit(1);
        return;
    }       
    // Create a command-queue for the selected device
    System.out.println("Creating command queue...");
        commandQueue = clCreateCommandQueue(context, devices[0], 0, null);
    // Program Setup
        //err=clCreateCommandQueue(context, device, 0, null);
        DataBufferInt dataBufferSrc =
                (DataBufferInt)image.getRaster().getDataBuffer();
            int dataSrc[] = dataBufferSrc.getData();
            cl_image_format imageFormat = new cl_image_format();
            imageFormat.image_channel_order = CL_RGBA;
            imageFormat.image_channel_data_type = CL_UNSIGNED_INT8;
            inputImageMem = clCreateImage2D(
                context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
                new cl_image_format[]{imageFormat}, sizeX, sizeY,
                sizeX * Sizeof.cl_uint, Pointer.to(dataSrc), null);
        cl_mem memObjects[] = new cl_mem[2];
        memObjects[0] = clCreateBuffer(context, 
                CL_MEM_READ_WRITE,   Sizeof.cl_int * sizeX, null, null);
        long globalWorkSize[] = new long[2];
        globalWorkSize[0] = sizeX;
        globalWorkSize[1] = sizeY;         
    String source = readFile("kernels/basic.cl"); 
 // Create the program
    System.out.println("Creating program...");
    cl_program cpProgram = clCreateProgramWithSource(context, 1, 
        new String[]{ source }, null, null);
    // Build the program
    System.out.println("Building program...");
   err= clBuildProgram(cpProgram, 0, null, null, null, null);
   if (err!=CL_SUCCESS)
   {
    System.out.println("error3"+"  "+err);
   }
    // Create the kernel
    kernel = clCreateKernel(cpProgram, "basic", null);
        sizeX =image.getWidth();  Pointer dst = Pointer.to(tab1);


err=   clSetKernelArg(kernel, 0, Sizeof.cl_mem,Pointer.to(inputImageMem));
        if (err!=CL_SUCCESS)
        {
            System.out.println("error4"+"  "+err);
        }
        err=   clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to( memObjects[0]));
        if (err!=CL_SUCCESS)
        {
            System.out.println("error6"+"  "+err);
        }    
err=   clEnqueueNDRangeKernel(commandQueue, kernel, 1, null,
              globalWorkSize,null, 0, null, null);     
 err=clEnqueueReadBuffer(commandQueue, memObjects[0], CL_TRUE, 0,Sizeof.cl_int*sizeX ,dst, 0, null, null);
 if (err!=CL_SUCCESS)
 {
    System.out.println("error8"+"   "+err);
 }
               if (sizeX !=0)
        { if (err!=CL_SUCCESS)
        {
            System.out.println("error9");
        }

            System.out.println("Result: "+java.util.Arrays.toString(tab1));
        }
               clReleaseMemObject(inputImageMem);
               clReleaseMemObject(memObjects[0]); 
}
    private static BufferedImage createBufferedImage(String fileName)
    {
        BufferedImage image = null;
        try
        {
            image = ImageIO.read(new File(fileName));
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }

        int sizeX = image.getWidth();
        int sizeY = image.getHeight();

        BufferedImage result = new BufferedImage(
            sizeX, sizeY, BufferedImage.TYPE_INT_RGB);

        return result;
    }
@SuppressWarnings("resource")
private static String readFile(String fileName)
{
    try
    {
        BufferedReader br = new BufferedReader(
            new InputStreamReader(new FileInputStream(fileName)));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while (true)
        {
            line = br.readLine();
            if (line == null)
            {
                break;
            }
            sb.append(line).append("\n");
        }
        return sb.toString();
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.exit(1);
        return null;
    }
}

我的内核" basic.cl"

  __kernel void basic(__read_only image2d_t input,__global int *result) 
{

   int gidX = get_global_id(0);
    int gidY = get_global_id(1);
    // const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE |
      //    CLK_ADDRESS_NONE | //Clamp to zeros
        //  CLK_FILTER_NEAREST; 


    // int2 coord = (int2)(gidX, gidY);
    //uint4 pixel = read_imageui(input, smp, coord);
    // result[gidX] = pixel.s0;
 result[gidX] = get_global_id(0);

    }

编译代码时,结果为:

Images supported: true
Creating command queue...
Creating program...
Building program...
Exception in thread "main" org.jocl.CLException: CL_INVALID_KERNEL_ARGS
    at org.jocl.CL.checkResult(CL.java:686)
    at org.jocl.CL.clEnqueueNDRangeKernel(CL.java:18456)

有什么问题?在OpenCL的文档中,他们说如果没有指定内核参数值,则会出现错误CL_INVALID_KERNEL_ARGS。 但我指定了他们的任何建议吗?

0 个答案:

没有答案