Search in sources :

Example 1 with CLContext

use of com.nativelibs4java.opencl.CLContext in project suite by stupidsing.

the class GpuTest method test.

@Test
public void test() {
    String openCl = // 
    "" + // 
    "__kernel void add_floats(__global float *a, __global float *b, __global float *o, int n) { \n" + // 
    "    int i = get_global_id(0); \n" + // 
    "    if (i < n) o[i] = a[i] * a[i] + b[i] * b[i]; \n" + // 
    "} \n";
    CLContext context = JavaCL.createBestContext();
    CLQueue queue = context.createDefaultQueue();
    ByteOrder byteOrder = context.getByteOrder();
    int n = 1024;
    Pointer<Float> inp0 = Pointer.allocateFloats(n).order(byteOrder);
    Pointer<Float> inp1 = Pointer.allocateFloats(n).order(byteOrder);
    for (int i = 0; i < n; i++) {
        inp0.set(i, (float) Math.cos(i));
        inp1.set(i, (float) Math.sin(i));
    }
    CLBuffer<Float> out = context.createBuffer(Usage.Output, Float.class, n);
    CLKernel kernel = context.createProgram(openCl).createKernel("add_floats");
    kernel.setArgs(context.createBuffer(Usage.Input, inp0), context.createBuffer(Usage.Input, inp1), out, n);
    Pointer<Float> outp = out.read(queue, kernel.enqueueNDRange(queue, new int[] { n }));
    for (CLDevice device : context.getDevices()) System.out.println(device);
    for (int i = 0; i < min(10, n); i++) System.out.println("out[" + i + "] = " + outp.get(i));
}
Also used : CLContext(com.nativelibs4java.opencl.CLContext) CLQueue(com.nativelibs4java.opencl.CLQueue) CLDevice(com.nativelibs4java.opencl.CLDevice) ByteOrder(java.nio.ByteOrder) CLKernel(com.nativelibs4java.opencl.CLKernel) Test(org.junit.Test)

Aggregations

CLContext (com.nativelibs4java.opencl.CLContext)1 CLDevice (com.nativelibs4java.opencl.CLDevice)1 CLKernel (com.nativelibs4java.opencl.CLKernel)1 CLQueue (com.nativelibs4java.opencl.CLQueue)1 ByteOrder (java.nio.ByteOrder)1 Test (org.junit.Test)1