Package com.jogamp.opencl

Examples of com.jogamp.opencl.CLContext


        CLMultiContext mc = new CLMultiContext();
        for (Map.Entry<CLPlatform, List<CLDevice>> entry : platformDevicesMap.entrySet()) {
            List<CLDevice> list = entry.getValue();
            // one context per device to workaround driver bugs
            for (CLDevice device : list) {
                CLContext context = CLContext.create(device);
                mc.contexts.add(context);
            }
        }

        return mc;
View Full Code Here


       
        assertNotNull(glcontext);
        makeGLCurrent();
        assertTrue(glcontext.isCurrent());
       
        CLContext context = CLGLContext.create(glcontext, device);
        assertNotNull(context);

        try{
            out.println(context);
            /*
            CLDevice currentDevice = context.getCurrentGLCLDevice();
            assertNotNull(currentDevice);
            out.println(currentDevice);
             */
        }finally{
            // destroy cl context, gl context still current
            context.release();
           
            deinitGL();
        }

    }
View Full Code Here

        @Override
        public IntBuffer execute(CLSingleProgramQueueContext qc) {
           
            CLCommandQueue queue = qc.getQueue();
            CLContext context = qc.getCLContext();
            CLKernel kernel = qc.getKernel("compute");

//            System.out.println(Thread.currentThread().getName()+" / "+queue);
            assertFalse(qc.isReleased());
            assertFalse(queue.isReleased());
            assertFalse(context.isReleased());
            assertFalse(kernel.isReleased());

            CLBuffer<IntBuffer> buffer = null;
            try{

                buffer = context.createBuffer(data);
                int gws = buffer.getCLCapacity();

                kernel.putArg(buffer).putArg(gws).rewind();

                queue.putWriteBuffer(buffer, false);
View Full Code Here

        }
        if(deviceIndex != -1) {
            device = platform.listCLDevices()[deviceIndex];
        }

        CLContext context = CLContext.create(device);

        System.out.println();
        System.out.println(platform);
        System.out.println(context);
        System.out.println();

        // Run tests
        testBandwidth(context, start, end, increment, mode, COPY.HOST_TO_DEVICE, accMode, memMode);
        testBandwidth(context, start, end, increment, mode, COPY.DEVICE_TO_HOST, accMode, memMode);
        testBandwidth(context, start, end, increment, mode, COPY.DEVICE_TO_DEVICE, accMode, memMode);

        context.release();
    }
View Full Code Here

        ByteBuffer h_data = null;
        CLBuffer<?> cmPinnedData = null;
        CLBuffer<?> cmDevData;

        CLContext context = queue.getContext();

        //allocate and init host memory, pinned or conventional
        if (memMode == memMode.PINNED) {
            // Create a host buffer
            cmPinnedData = context.createBuffer(memSize, Mem.READ_WRITE, Mem.ALLOCATE_BUFFER);

            // Get a mapped pointer
            h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
            fill(h_data);

            // unmap and make data in the host buffer valid
            queue.putUnmapMemory(cmPinnedData, h_data);
        } else { // PAGED
            // standard host alloc
            h_data = Buffers.newDirectByteBuffer(memSize);
            fill(h_data);
        }

        // allocate device memory
        cmDevData = context.createBuffer(memSize, Mem.READ_WRITE);

        // initialize device memory
        if (memMode == memMode.PINNED) {
            // Get a mapped pointer
            h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
View Full Code Here

        ByteBuffer h_data;
        CLBuffer<?> cmPinnedData = null;
        CLBuffer<?> cmDevData;

        CLContext context = queue.getContext();

        // Allocate and init host memory, pinned or conventional
        if (memMode == memMode.PINNED) {
            // Create a host buffer
            cmPinnedData = context.createBuffer(memSize, Mem.READ_WRITE, Mem.ALLOCATE_BUFFER);

            // Get a mapped pointer
            h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);

            //initialize
            fill(h_data);

            // unmap and make data in the host buffer valid
            queue.putUnmapMemory(cmPinnedData, h_data);
        } else { // PAGED
            // standard host alloc
            h_data = Buffers.newDirectByteBuffer(memSize);
            fill(h_data);
        }

        // allocate device memory
        cmDevData = context.createBuffer(memSize, Mem.READ_WRITE);

        // Sync queue to host, start timer 0, and copy data from Host to GPU
        queue.finish();

        long delta = System.nanoTime();
View Full Code Here

    /**
     *  test the bandwidth of a device to host memcopy of a specific size
     */
    private static double testDeviceToDeviceTransfer(CLCommandQueue queue, int memSize) {

        CLContext context = queue.getContext();

        //allocate host memory
        ByteBuffer h_idata = Buffers.newDirectByteBuffer(memSize);
        fill(h_idata);

        // allocate device input and output memory and initialize the device input memory
        CLBuffer<?> d_idata = context.createBuffer(memSize, READ_ONLY);
        CLBuffer<?> d_odata = context.createBuffer(memSize, WRITE_ONLY);

        d_idata = d_idata.cloneWith(h_idata);
        queue.putWriteBuffer(d_idata, true);

        // Sync queue to host, start timer 0, and copy data from one GPU buffer to another GPU bufffer
View Full Code Here

public class HelloJOCL {

    public static void main(String[] args) throws IOException {

        // set up (uses default CLPlatform and creates context for all devices)
        CLContext context = CLContext.create();
        out.println("created "+context);
       
        // always make sure to release the context under all circumstances
        // not needed for this particular sample but recommented
        try{
           
            // select fastest device
            CLDevice device = context.getMaxFlopsDevice();
            out.println("using "+device);

            // create command queue on device.
            CLCommandQueue queue = device.createCommandQueue();

            int elementCount = 1444477;                                  // Length of arrays to process
            int localWorkSize = min(device.getMaxWorkGroupSize(), 256)// Local work size dimensions
            int globalWorkSize = roundUp(localWorkSize, elementCount);   // rounded up to the nearest multiple of the localWorkSize

            // load sources, create and build program
            CLProgram program = context.createProgram(HelloJOCL.class.getResourceAsStream("VectorAdd.cl")).build();

            // A, B are input buffers, C is for the result
            CLBuffer<FloatBuffer> clBufferA = context.createFloatBuffer(globalWorkSize, READ_ONLY);
            CLBuffer<FloatBuffer> clBufferB = context.createFloatBuffer(globalWorkSize, READ_ONLY);
            CLBuffer<FloatBuffer> clBufferC = context.createFloatBuffer(globalWorkSize, WRITE_ONLY);

            out.println("used device memory: "
                + (clBufferA.getCLSize()+clBufferB.getCLSize()+clBufferC.getCLSize())/1000000 +"MB");

            // fill input buffers with random numbers
            // (just to have test data; seed is fixed -> results will not change between runs).
            fillBuffer(clBufferA.getBuffer(), 12345);
            fillBuffer(clBufferB.getBuffer(), 67890);

            // get a reference to the kernel function with the name 'VectorAdd'
            // and map the buffers to its input parameters.
            CLKernel kernel = program.createCLKernel("VectorAdd");
            kernel.putArgs(clBufferA, clBufferB, clBufferC).putArg(elementCount);

            // asynchronous write of data to GPU device,
            // followed by blocking read to get the computed results back.
            long time = nanoTime();
            queue.putWriteBuffer(clBufferA, false)
                 .putWriteBuffer(clBufferB, false)
                 .put1DRangeKernel(kernel, 0, globalWorkSize, localWorkSize)
                 .putReadBuffer(clBufferC, true);
            time = nanoTime() - time;

            // print first few elements of the resulting buffer to the console.
            out.println("a+b=c results snapshot: ");
            for(int i = 0; i < 10; i++)
                out.print(clBufferC.getBuffer().get() + ", ");
            out.println("...; " + clBufferC.getBuffer().remaining() + " more");

            out.println("computation took: "+(time/1000000)+"ms");
           
        }finally{
            // cleanup all resources associated with this context.
            context.release();
        }

    }
View Full Code Here

    public static void main(String[] args) throws IOException {
       
        // find a CL implementation
        CLPlatform platform = CLPlatform.getDefault(/*type(CPU)*/);
       
        CLContext context = CLContext.create(platform.getMaxFlopsDevice());
       
        try{
            //load and compile program for the chosen device
            CLProgram program = context.createProgram(getStreamFor("Gamma.cl"));
            program.build(CompilerOptions.FAST_RELAXED_MATH);
            assert program.isExecutable();
           
            // load image
            BufferedImage image = readImage("lena.png");
            assert image.getColorModel().getNumComponents() == 3;
           
            float[] pixels = image.getRaster().getPixels(0, 0, image.getWidth(), image.getHeight(), (float[])null);
           
            // copy to direct float buffer
            FloatBuffer fb = Buffers.newDirectFloatBuffer(pixels);
           
            // allocate a OpenCL buffer using the direct fb as working copy
            CLBuffer<FloatBuffer> buffer = context.createBuffer(fb, CLBuffer.Mem.READ_WRITE);
           
            // creade a command queue with benchmarking flag set
            CLCommandQueue queue = context.getDevices()[0].createCommandQueue(Mode.PROFILING_MODE);
           
            int localWorkSize = queue.getDevice().getMaxWorkGroupSize(); // Local work size dimensions
            int globalWorkSize = roundUp(localWorkSize, fb.capacity())// rounded up to the nearest multiple of the localWorkSize
           
            // create kernel and set function parameters
            CLKernel kernel = program.createCLKernel("gamma");
           
            // original lenna
            show(image, 0, 50, "reference");
           
            // a few gamma corrected versions
            float gamma = 0.5f;
            gammaCorrection(gamma, queue, kernel, buffer, localWorkSize, globalWorkSize);
            show(createImage(image.getWidth(), image.getHeight(), buffer), image.getWidth()/2, 50, "gamma="+gamma);
           
            gamma = 1.5f;
            gammaCorrection(gamma, queue, kernel, buffer, localWorkSize, globalWorkSize);
            show(createImage(image.getWidth(), image.getHeight(), buffer), image.getWidth()/2*2, 50, "gamma="+gamma);
           
            gamma = 2.0f;
            gammaCorrection(gamma, queue, kernel, buffer, localWorkSize, globalWorkSize);
            show(createImage(image.getWidth(), image.getHeight(), buffer), image.getWidth()/2*3, 50, "gamma="+gamma);
           
        }finally{
            context.release();
        }
       
    }
View Full Code Here

        final int maxvalue = 1000000;

        out.println("Initializing OpenCL...");

        //Create the context
        CLContext context = null;

        try{

            context = CLContext.create();
            CLCommandQueue queue = context.getMaxFlopsDevice().createCommandQueue();

            out.println("Initializing OpenCL bitonic sorter...");
            kernels = initBitonicSort(queue);

            out.println("Creating OpenCL memory objects...");
            CLBuffer<IntBuffer> keyBuffer = context.createIntBuffer(elements, READ_ONLY, USE_BUFFER);
            System.out.println(keyBuffer.getCLSize()/1000000.0f);

            out.println("Initializing data...\n");
            Random random = new Random();
            for (int i = 0; i < elements; i++) {
                int rnd = random.nextInt(maxvalue);
                keyBuffer.getBuffer().put(i, rnd);
            }

            int arrayLength = elements;
            int batch = elements / arrayLength;

            out.printf("Test array length %d (%d arrays in the batch)...\n", arrayLength, batch);

            long time = currentTimeMillis();

            bitonicSort(queue, keyBuffer, keyBuffer, batch, arrayLength, sortDir);
            queue.putReadBuffer(keyBuffer, true);

            out.println(currentTimeMillis() - time+"ms");

            IntBuffer keys = keyBuffer.getBuffer();
            printSnapshot(keys, 20);
            checkIfSorted(keys);

            out.println("\nTEST PASSED");
       
        }finally{
            if(context!=null) {
                context.release();
            }
        }

    }
View Full Code Here

TOP

Related Classes of com.jogamp.opencl.CLContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.