خرید بک لینک

Vote count: 0

My goal is to write a code that takes an image (image) as entry variable and does an analysis and puts the result in an array (tab1) using java and jocl , as a begiing I wanted to see if the image is accepted so I wrote this program bellow and the keel "basic"

My class

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 keel which will actually compute the Mandelbrot
 * set and store the pixel data in a CL memory object
 */
private static cl_keel keel;
/**
 * The OpenCL memory object which stores the pixel data */  private static String input;
@SuppressWaings("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 keel.
 */   
    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);
        retu;
    }       
    // 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_chael_order = CL_RGBA;
            imageFormat.image_chael_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("keels/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 keel
    keel = clCreateKeel(cpProgram, "basic", null);
        sizeX =image.getWidth();  Pointer dst = Pointer.to(tab1);


err=   clSetKeelArg(keel, 0, Sizeof.cl_mem,Pointer.to(inputImageMem));
        if (err!=CL_SUCCESS)
        {
            System.out.println("error4"+"  "+err);
        }
        err=   clSetKeelArg(keel, 1, Sizeof.cl_mem, Pointer.to( memObjects[0]));
        if (err!=CL_SUCCESS)
        {
            System.out.println("error6"+"  "+err);
        }    
err=   clEnqueueNDRangeKeel(commandQueue, keel, 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();
            retu null;
        }

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

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

        retu result;
    }
@SuppressWaings("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");
        }
        retu sb.toString();
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.exit(1);
        retu null;
    }
}

My keel "basic.cl"

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

    }

When I compile my code the result is:

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.clEnqueueNDRangeKeel(CL.java:18456)

What is the problem? In the documentation of OpenCL they said that the error CL_INVALID_KERNEL_ARGS appears if the keel argument values have not been specified. but I specified them any suggestions ?

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 6 تير 1395 ساعت: 5:48

صفحه بندی