Search in sources :

Example 6 with Slf4JLogger

use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.

the class ContextTest method testVectorNormalize.

@Test
public void testVectorNormalize() throws Exception {
    Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
    final Float2[] theA = { new Float2(10f, 20f) };
    final Float2[] theResult = new Float2[] { new Float2(-1f, -1f) };
    try (Context theContext = thePlatform.createContext()) {
        theContext.compute(1, new Kernel() {

            public void processWorkItem() {
                int id = get_global_id(0);
                Float2 theVec = VectorFunctions.normalize(theA[id]);
                theResult[id].s1 = theVec.s1;
            }
        });
    }
    for (Float2 aTheResult : theResult) {
        System.out.println(aTheResult);
    }
}
Also used : Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 7 with Slf4JLogger

use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.

the class AliceBobCarolDaveTest method testSimilarityWithMethodInKernel.

@Test
public void testSimilarityWithMethodInKernel() throws Exception {
    // The data of our four friends
    Float4 theAlice = new Float4(5f, 1f, 0f, 6f);
    Float4 theBob = new Float4(0f, 10f, 3f, 0f);
    Float4 theCarol = new Float4(2f, 6f, 3f, 2f);
    Float4 theDave = new Float4(7f, 2f, 1f, 8f);
    // We need an input for our kernel, a list of vectors
    Float4[] theInputs = new Float4[] { theAlice, theCarol, theBob, theDave };
    // This is the computed output
    int[] theMostSimilar = new int[theInputs.length];
    float[] theMostSimilarity = new float[theInputs.length];
    // We obtain a platform
    Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
    // used to cache memory buffers and compiled kernels
    try (Context theContext = thePlatform.createContext()) {
        // We fire up the computations
        theContext.compute(theInputs.length, new Kernel() {

            private float similarityOf(Float4 a, Float4 b) {
                return dot(a, b) / length(a) * length(b);
            }

            // This method is called for every workitem
            @Override
            public void processWorkItem() {
                // This is the id of the current work item
                int theCurrentWorkItemId = get_global_id(0);
                // This is the total number of work items
                int theMax = get_global_size(0);
                // We obtain the current work item from the list
                Float4 theCurrent = theInputs[theCurrentWorkItemId];
                float theMaxSimilarity = -1;
                int theMaxIndex = -1;
                // except itself
                for (int i = 0; i < theMax; i++) {
                    if (i != theCurrentWorkItemId) {
                        Float4 theOther = theInputs[i];
                        float theSimilarity = similarityOf(theCurrent, theOther);
                        if (theSimilarity > theMaxSimilarity) {
                            theMaxSimilarity = theSimilarity;
                            theMaxIndex = i;
                        }
                    }
                }
                // The highest similarity is written to the output
                theMostSimilar[theCurrentWorkItemId] = theMaxIndex;
                theMostSimilarity[theCurrentWorkItemId] = theMaxSimilarity;
            }
        });
    }
    // Output the results
    for (int i = 0; i < theInputs.length; i++) {
        System.out.println("Most similar match for input " + i + " is " + theMostSimilar[i] + " with a similarity of " + theMostSimilarity[i]);
    }
}
Also used : Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 8 with Slf4JLogger

use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.

the class ContextTest method testSimpleAdd.

@Test
public void testSimpleAdd() throws Exception {
    Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
    final float[] theA = { 10f, 20f, 30f, 40f };
    final float[] theB = { 100f, 200f, 300f, 400f };
    final float[] theResult = new float[4];
    try (Context theContext = thePlatform.createContext()) {
        theContext.compute(4, new Kernel() {

            public void processWorkItem() {
                int id = get_global_id(0);
                float a = theA[id];
                float b = theB[id];
                theResult[id] = a + b;
            }
        });
    }
    for (float aTheResult : theResult) {
        System.out.println(aTheResult);
    }
}
Also used : Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 9 with Slf4JLogger

use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.

the class ContextTest method testComplexAdd.

@Test
public void testComplexAdd() throws Exception {
    Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
    final Float2[] theA = { new Float2(10f, 20f) };
    final Float2[] theB = { new Float2(10f, 20f) };
    final Float2[] theResult = new Float2[] { new Float2(-1f, -1f) };
    try (Context theContext = thePlatform.createContext()) {
        theContext.compute(1, new Kernel() {

            public void processWorkItem() {
                int id = get_global_id(0);
                float aS0 = theA[id].s0;
                float aS1 = theB[id].s1;
                theResult[id].s0 = aS0 + 100;
                theResult[id].s1 = aS1 + 200;
            }
        });
    }
    for (Float2 aTheResult : theResult) {
        System.out.println(aTheResult);
    }
}
Also used : Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Aggregations

Slf4JLogger (de.mirkosertic.bytecoder.unittest.Slf4JLogger)9 Test (org.junit.Test)8 CompileOptions (de.mirkosertic.bytecoder.backend.CompileOptions)3 BytecodeMethodSignature (de.mirkosertic.bytecoder.core.BytecodeMethodSignature)3 OpenCLCompileBackend (de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileBackend)2 OpenCLCompileResult (de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileResult)2 BytecodeLinkerContext (de.mirkosertic.bytecoder.core.BytecodeLinkerContext)2 BytecodeLoader (de.mirkosertic.bytecoder.core.BytecodeLoader)2 Method (java.lang.reflect.Method)2 Compiler (com.google.javascript.jscomp.Compiler)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 CompileResult (de.mirkosertic.bytecoder.backend.CompileResult)1 CompileTarget (de.mirkosertic.bytecoder.backend.CompileTarget)1 WASMCompileResult (de.mirkosertic.bytecoder.backend.wasm.WASMCompileResult)1 BytecodeArrayTypeRef (de.mirkosertic.bytecoder.core.BytecodeArrayTypeRef)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1