Search in sources :

Example 6 with Kernel

use of com.jme3.network.kernel.Kernel in project jmonkeyengine by jMonkeyEngine.

the class TestMultipleApplications method simpleInitApp.

@Override
public void simpleInitApp() {
    clContext = context.getOpenCLContext();
    if (clContext == null) {
        LOG.severe("No OpenCL context found");
        stop();
        return;
    }
    Device device = clContext.getDevices().get(0);
    clQueue = clContext.createQueue(device);
    clQueue.register();
    String source = "" + "__kernel void Fill(__global float* vb, float v)\n" + "{\n" + "  int idx = get_global_id(0);\n" + "  vb[idx] = v;\n" + "}\n";
    Program program = clContext.createProgramFromSourceCode(source);
    program.build();
    program.register();
    kernel = program.createKernel("Fill");
    kernel.register();
    buffer = clContext.createBuffer(4);
    buffer.register();
    flyCam.setEnabled(false);
    inputManager.setCursorVisible(true);
    BitmapFont fnt = assetManager.loadFont("Interface/Fonts/Default.fnt");
    infoText = new BitmapText(fnt, false);
    //infoText.setBox(new Rectangle(0, 0, settings.getWidth(), settings.getHeight()));
    infoText.setText("Device: " + clContext.getDevices());
    infoText.setLocalTranslation(0, settings.getHeight(), 0);
    guiNode.attachChild(infoText);
    statusText = new BitmapText(fnt, false);
    //statusText.setBox(new Rectangle(0, 0, settings.getWidth(), settings.getHeight()));
    statusText.setText("Running");
    statusText.setLocalTranslation(0, settings.getHeight() - infoText.getHeight() - 2, 0);
    guiNode.attachChild(statusText);
}
Also used : BitmapText(com.jme3.font.BitmapText) BitmapFont(com.jme3.font.BitmapFont)

Example 7 with Kernel

use of com.jme3.network.kernel.Kernel in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testMatrix4f.

private boolean testMatrix4f(Context clContext, CommandQueue clQueue) {
    try {
        String code = "" + "#import \"Common/OpenCL/Matrix4f.clh\"\n" + "\n" + "__kernel void TestMatrix4f_1(mat4 m1, __global char* result)\n" + "{\n" + "  mat4 id = mat4Identity();\n" + "  mat4 m1Inv = mat4Invert(m1);\n" + "  mat4 m1Res = mat4Mult(m1, m1Inv);\n" + "  result[0] = mat4Equals(id, m1Res, 0.0001f) ? 1 : 0;\n" + "}\n" + "\n" + "__kernel void TestMatrix4f_2(mat4 m1, float d, mat4 m2, mat4 m3, __global char* result)\n" + "{\n" + "  float d2 = mat4Determinant(m1);\n" + "  result[0] = fabs(d - d2) < 0.0001f ? 1 : 0;\n" + "  mat4 res = mat4Transpose(m1);\n" + "  result[1] = mat4Equals(res, m2, 0.0001f) ? 1 : 0;\n" + "  res = mat4Adjoint(m1);\n" + "  result[2] = mat4Equals(res, m3, 0.0001f) ? 1 : 0;\n" + "}\n";
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        com.jme3.opencl.Buffer buffer = clContext.createBuffer(3);
        Random rand = new Random(1561);
        Kernel testMatrix4fKernel1 = program.createKernel("TestMatrix4f_1");
        Matrix4f m1 = new Matrix4f();
        do {
            for (int i = 0; i < 4; ++i) {
                for (int j = 0; j < 4; ++j) {
                    m1.set(i, j, rand.nextFloat() * 20 - 10);
                }
            }
        } while (FastMath.abs(m1.determinant()) < 0.00001f);
        testMatrix4fKernel1.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, buffer);
        ByteBuffer bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix inversion failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix4fKernel1.release();
        Kernel testMatrix4fKernel2 = program.createKernel("TestMatrix4f_2");
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                m1.set(i, j, rand.nextFloat() * 20 - 10);
            }
        }
        testMatrix4fKernel2.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, m1.determinant(), m1.transpose(), m1.adjoint(), buffer);
        bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix determinant computation failed");
            return false;
        }
        if (bb.get() == 0) {
            LOG.severe("Matrix transposing failed");
            return false;
        }
        if (bb.get() == 0) {
            LOG.severe("Matrix adjoint computation failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix4fKernel2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "matrix4f test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "matrix4f test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Matrix4f(com.jme3.math.Matrix4f) Random(java.util.Random)

Example 8 with Kernel

use of com.jme3.network.kernel.Kernel in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testMatrix3f.

private boolean testMatrix3f(Context clContext, CommandQueue clQueue) {
    try {
        String code = "" + "#import \"Common/OpenCL/Matrix3f.clh\"\n" + "\n" + "__kernel void TestMatrix3f_1(__global char* result)\n" + "{\n" + "  mat3 id = mat3Identity();\n" + "  mat3 m1 = mat3FromRows( (float3)(23,-3,10.4f), (float3)(5,-8,2.22f), (float3)(-1,0,34) );\n" + "  mat3 m1Inv = mat3Invert(m1);\n" + "  mat3 m1Res = mat3Mult(m1, m1Inv);\n" + "  result[0] = mat3Equals(id, m1Res, 0.0001f) ? 1 : 0;\n" + "}\n" + "\n" + "__kernel void TestMatrix3f_2(mat3 m1, float a, mat3 m2, mat3 mRes, __global char* result)\n" + "{\n" + "  mat3 m = mat3Transpose(m1);\n" + "  m = mat3Add(mat3Scale(m, a), m2);\n" + "  result[0] = mat3Equals(mRes, m, 0.01f) ? 1 : 0;\n" + "}\n";
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        com.jme3.opencl.Buffer buffer = clContext.createBuffer(1);
        Kernel testMatrix3fKernel1 = program.createKernel("TestMatrix3f_1");
        testMatrix3fKernel1.Run1NoEvent(clQueue, new Kernel.WorkSize(1), buffer);
        ByteBuffer bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix inversion failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel1.release();
        Kernel testMatrix3fKernel2 = program.createKernel("TestMatrix3f_2");
        Matrix3f m1 = new Matrix3f(13.24f, -0.234f, 42, 83.23f, -34.2f, 3.2f, 0.25f, -42, 7.64f);
        Matrix3f m2 = new Matrix3f(-5.2f, 0.757f, 2.01f, 12.0f, -6, 2, 0.01f, 9, 2.255f);
        Matrix3f mRes = new Matrix3f(-31.68f, -165.703f, 1.51f, 12.468f, 62.4f, 86, -83.99f, 2.6f, -13.025f);
        testMatrix3fKernel2.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, -2.0f, m2, mRes, buffer);
        bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix add, multiply, transpose failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Matrix3f(com.jme3.math.Matrix3f)

Example 9 with Kernel

use of com.jme3.network.kernel.Kernel in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testRandom.

private boolean testRandom(Context clContext, CommandQueue clQueue) {
    try {
        //test for doubles
        boolean supportsDoubles = clContext.getDevices().get(0).hasDouble();
        //create code
        String code = "" + "#import \"Common/OpenCL/Random.clh\"\n" + "__kernel void TestBool(__global ulong* seeds, __global uchar* results) {\n" + "  results[get_global_id(0)] = randBool(seeds + get_global_id(0)) ? 1 : 0;\n" + "}\n" + "__kernel void TestInt(__global ulong* seeds, __global int* results) {\n" + "  results[get_global_id(0)] = randInt(seeds + get_global_id(0));\n" + "}\n" + "__kernel void TestIntN(__global ulong* seeds, int n, __global int* results) {\n" + "  results[get_global_id(0)] = randIntN(n, seeds + get_global_id(0));\n" + "}\n" + "__kernel void TestLong(__global ulong* seeds, __global long* results) {\n" + "  results[get_global_id(0)] = randLong(seeds + get_global_id(0));\n" + "}\n" + "__kernel void TestFloat(__global ulong* seeds, __global float* results) {\n" + "  results[get_global_id(0)] = randFloat(seeds + get_global_id(0));\n" + "}\n" + "#ifdef RANDOM_DOUBLES\n" + "__kernel void TestDouble(__global ulong* seeds, __global double* results) {\n" + "  results[get_global_id(0)] = randDouble(seeds + get_global_id(0));\n" + "}\n" + "#endif\n";
        if (supportsDoubles) {
            code = "#define RANDOM_DOUBLES\n" + code;
        }
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        int count = 256;
        Kernel.WorkSize ws = new Kernel.WorkSize(count);
        //create seeds
        Random initRandom = new Random();
        long[] seeds = new long[count];
        Random[] randoms = new Random[count];
        for (int i = 0; i < count; ++i) {
            seeds[i] = initRandom.nextLong();
            randoms[i] = new Random(seeds[i]);
            //needed because the Random constructor scrambles the initial seed
            seeds[i] = (seeds[i] ^ 0x5DEECE66DL) & ((1L << 48) - 1);
        }
        com.jme3.opencl.Buffer seedsBuffer = clContext.createBuffer(8 * count);
        ByteBuffer tmpByteBuffer = BufferUtils.createByteBuffer(8 * count);
        tmpByteBuffer.asLongBuffer().put(seeds);
        seedsBuffer.write(clQueue, tmpByteBuffer);
        //test it
        ByteBuffer resultByteBuffer = BufferUtils.createByteBuffer(8 * count);
        IntBuffer resultIntBuffer = resultByteBuffer.asIntBuffer();
        LongBuffer resultLongBuffer = resultByteBuffer.asLongBuffer();
        FloatBuffer resultFloatBuffer = resultByteBuffer.asFloatBuffer();
        DoubleBuffer resultDoubleBuffer = resultByteBuffer.asDoubleBuffer();
        com.jme3.opencl.Buffer resultBuffer = clContext.createBuffer(8 * count);
        //boolean
        Kernel testBoolKernel = program.createKernel("TestBool");
        testBoolKernel.Run1NoEvent(clQueue, ws, seedsBuffer, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextBoolean() ? 1 : 0, resultByteBuffer.get(i), "randBool at i=" + i);
        }
        testBoolKernel.release();
        //int
        Kernel testIntKernel = program.createKernel("TestInt");
        testIntKernel.Run1NoEvent(clQueue, ws, seedsBuffer, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextInt(), resultIntBuffer.get(i), "randInt at i=" + i);
        }
        testIntKernel.release();
        //int n
        Kernel testIntNKernel = program.createKernel("TestIntN");
        testIntNKernel.Run1NoEvent(clQueue, ws, seedsBuffer, 186, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextInt(186), resultIntBuffer.get(i), "randInt at i=" + i + " with n=" + 186);
        }
        testIntNKernel.Run1NoEvent(clQueue, ws, seedsBuffer, 97357, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextInt(97357), resultIntBuffer.get(i), "randInt at i=" + i + " with n=" + 97357);
        }
        testIntNKernel.release();
        //long
        Kernel testLongKernel = program.createKernel("TestLong");
        testLongKernel.Run1NoEvent(clQueue, ws, seedsBuffer, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextLong(), resultLongBuffer.get(i), "randLong at i=" + i);
        }
        testLongKernel.release();
        //float
        Kernel testFloatKernel = program.createKernel("TestFloat");
        testFloatKernel.Run1NoEvent(clQueue, ws, seedsBuffer, resultBuffer);
        resultByteBuffer.rewind();
        resultBuffer.read(clQueue, resultByteBuffer);
        for (int i = 0; i < count; ++i) {
            assertEquals(randoms[i].nextFloat(), resultFloatBuffer.get(i), "randFloat at i=" + i);
        }
        testFloatKernel.release();
        //double
        if (supportsDoubles) {
            Kernel testDoubleKernel = program.createKernel("TestDouble");
            testDoubleKernel.Run1NoEvent(clQueue, ws, seedsBuffer, resultBuffer);
            resultByteBuffer.rewind();
            resultBuffer.read(clQueue, resultByteBuffer);
            for (int i = 0; i < count; ++i) {
                assertEquals(randoms[i].nextDouble(), resultDoubleBuffer.get(i), "randLong at i=" + i);
            }
            testDoubleKernel.release();
        }
        seedsBuffer.release();
        resultBuffer.release();
        program.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "random test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "random test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Random(java.util.Random)

Aggregations

com.jme3.opencl (com.jme3.opencl)3 IOException (java.io.IOException)3 BitmapFont (com.jme3.font.BitmapFont)2 BitmapText (com.jme3.font.BitmapText)2 KernelException (com.jme3.network.kernel.KernelException)2 Random (java.util.Random)2 Matrix3f (com.jme3.math.Matrix3f)1 Matrix4f (com.jme3.math.Matrix4f)1 Vector2f (com.jme3.math.Vector2f)1 Endpoint (com.jme3.network.kernel.Endpoint)1 Kernel (com.jme3.network.kernel.Kernel)1 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1