Search in sources :

Example 1 with Slf4JLogger

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

the class BytecoderMavenMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    File theBaseDirectory = new File(buldDirectory);
    File theBytecoderDirectory = new File(theBaseDirectory, "bytecoder");
    theBytecoderDirectory.mkdirs();
    try {
        ClassLoader theLoader = prepareClassLoader();
        Class theTargetClass = theLoader.loadClass(mainClass);
        CompileTarget theCompileTarget = new CompileTarget(theLoader, CompileTarget.BackendType.valueOf(backend));
        File theBytecoderFileName = new File(theBytecoderDirectory, theCompileTarget.generatedFileName());
        BytecodeMethodSignature theSignature = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.VOID, new BytecodeTypeRef[] { new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(String.class), 1) });
        CompileOptions theOptions = new CompileOptions(new Slf4JLogger(), debugOutput, KnownOptimizer.ALL);
        CompileResult theCode = theCompileTarget.compileToJS(theOptions, theTargetClass, "main", theSignature);
        try (PrintWriter theWriter = new PrintWriter(new FileWriter(theBytecoderFileName))) {
            theWriter.println(theCode.getData());
        }
        if (optimizeWithGoogleClosure) {
            Compiler theCompiler = new Compiler();
            CompilerOptions theClosureOptions = new CompilerOptions();
            theClosureOptions.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
            theClosureOptions.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
            CompilationLevel.valueOf(closureOptimizationLevel).setOptionsForCompilationLevel(theClosureOptions);
            List<SourceFile> theSourceFiles = CommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.BROWSER);
            theSourceFiles.add(SourceFile.fromCode("bytecoder.js", (String) theCode.getData()));
            theCompiler.compile(new ArrayList<>(), theSourceFiles, theClosureOptions);
            String theClosureCode = theCompiler.toSource();
            File theBytecoderClosureFileName = new File(theBytecoderDirectory, "bytecoder-closure.js");
            try (PrintWriter theWriter = new PrintWriter(new FileWriter(theBytecoderClosureFileName))) {
                theWriter.println(theClosureCode);
            }
        }
        if (theCode instanceof WASMCompileResult) {
            WASMCompileResult theWASMCompileResult = (WASMCompileResult) theCode;
            int[] theWASM = wat2wasm(theWASMCompileResult);
            File theBytecoderWASMFileName = new File(theBytecoderDirectory, "bytecoder.wasm");
            try (FileOutputStream theFos = new FileOutputStream(theBytecoderWASMFileName)) {
                for (int aTheWASM : theWASM) {
                    theFos.write(aTheWASM);
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error running bytecoder", e);
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) WASMCompileResult(de.mirkosertic.bytecoder.backend.wasm.WASMCompileResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) CompileOptions(de.mirkosertic.bytecoder.backend.CompileOptions) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BytecodeArrayTypeRef(de.mirkosertic.bytecoder.core.BytecodeArrayTypeRef) FileOutputStream(java.io.FileOutputStream) CompileTarget(de.mirkosertic.bytecoder.backend.CompileTarget) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) URLClassLoader(java.net.URLClassLoader) WASMCompileResult(de.mirkosertic.bytecoder.backend.wasm.WASMCompileResult) CompileResult(de.mirkosertic.bytecoder.backend.CompileResult) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) PrintWriter(java.io.PrintWriter)

Example 2 with Slf4JLogger

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

the class CompilerTest method testSimpleKernel.

@Test
public void testSimpleKernel() {
    OpenCLCompileBackend backend = new OpenCLCompileBackend();
    CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL);
    Kernel theKernel = createKernel();
    Class theKernelClass = theKernel.getClass();
    System.out.println(theKernelClass);
    Method[] theMethods = theKernelClass.getDeclaredMethods();
    if (theMethods.length != 1) {
        throw new IllegalArgumentException("A kernel must have exactly one declared method!");
    }
    Method theMethod = theMethods[0];
    BytecodeMethodSignature theSignature = backend.signatureFrom(theMethod);
    BytecodeLoader theLoader = new BytecodeLoader(getClass().getClassLoader());
    BytecodeLinkerContext theLinkerContext = new BytecodeLinkerContext(theLoader, compileOptions.getLogger());
    OpenCLCompileResult theCompiledKernel = backend.generateCodeFor(compileOptions, theLinkerContext, theKernelClass, theMethod.getName(), theSignature);
    System.out.println(theCompiledKernel.getData());
}
Also used : OpenCLCompileBackend(de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileBackend) BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) OpenCLCompileResult(de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileResult) BytecodeLoader(de.mirkosertic.bytecoder.core.BytecodeLoader) BytecodeLinkerContext(de.mirkosertic.bytecoder.core.BytecodeLinkerContext) CompileOptions(de.mirkosertic.bytecoder.backend.CompileOptions) Method(java.lang.reflect.Method) Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 3 with Slf4JLogger

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

the class CompilerTest method testKernelWithComplexType.

@Test
public void testKernelWithComplexType() {
    OpenCLCompileBackend backend = new OpenCLCompileBackend();
    CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL);
    Float2[] theIn = new Float2[10];
    Float2[] theOut = new Float2[10];
    Kernel theKernel = new Kernel() {

        public void processWorkItem() {
            int theIndex = get_global_id(0);
            Float2 a = theIn[theIndex];
            Float2 b = theOut[theIndex];
            b.s0 = a.s0;
            b.s1 = a.s1;
        }
    };
    Class theKernelClass = theKernel.getClass();
    System.out.println(theKernelClass);
    Method[] theMethods = theKernelClass.getDeclaredMethods();
    if (theMethods.length != 1) {
        throw new IllegalArgumentException("A kernel must have exactly one declared method!");
    }
    Method theMethod = theMethods[0];
    BytecodeMethodSignature theSignature = backend.signatureFrom(theMethod);
    BytecodeLoader theLoader = new BytecodeLoader(getClass().getClassLoader());
    BytecodeLinkerContext theLinkerContext = new BytecodeLinkerContext(theLoader, compileOptions.getLogger());
    OpenCLCompileResult theCompiedKernel = backend.generateCodeFor(compileOptions, theLinkerContext, theKernelClass, theMethod.getName(), theSignature);
    System.out.println(theCompiedKernel.getData());
}
Also used : BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) BytecodeLoader(de.mirkosertic.bytecoder.core.BytecodeLoader) CompileOptions(de.mirkosertic.bytecoder.backend.CompileOptions) Method(java.lang.reflect.Method) OpenCLCompileBackend(de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileBackend) OpenCLCompileResult(de.mirkosertic.bytecoder.backend.opencl.OpenCLCompileResult) BytecodeLinkerContext(de.mirkosertic.bytecoder.core.BytecodeLinkerContext) Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 4 with Slf4JLogger

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

the class AliceBobCarolDaveTest method testPerformance.

@Test
public void testPerformance() throws Exception {
    int theMaxSize = 100000;
    Float4[] theInputs = new Float4[theMaxSize];
    for (int i = 0; i < theMaxSize; i++) {
        theInputs[i] = new Float4((float) Math.random() * 10, (float) Math.random() * 10, (float) Math.random() * 10, (float) Math.random() * 10);
    }
    int[] theMostSimilar = new int[theInputs.length];
    float[] theMostSimilarity = new float[theInputs.length];
    long theStart = System.currentTimeMillis();
    Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
    // Platform thePlatform = new CPUPlatform(new Slf4JLogger());
    PlatformProperties thePlatformProps = thePlatform.getPlatformProperties();
    System.out.println("Platform is   : " + thePlatformProps.getName());
    DeviceProperties theDevProps = thePlatform.getDeviceProperties();
    System.out.println("Device        : " + theDevProps.getName());
    System.out.println(" # CU         : " + theDevProps.getNumberOfComputeUnits());
    System.out.println(" Clock freq.  : " + theDevProps.getClockFrequency());
    System.out.println(" Max workgroup: " + theDevProps.getMaxWorkGroupSize());
    try (Context theContext = thePlatform.createContext()) {
        theContext.compute(theInputs.length, new Kernel() {

            @Override
            public void processWorkItem() {
                int theCurrentWorkItemId = get_global_id(0);
                int theMax = get_global_size(0);
                Float4 theCurrent = theInputs[theCurrentWorkItemId];
                float theCurrentLength = length(theCurrent);
                float theMaxSimilarity = -1;
                int theMaxIndex = -1;
                for (int i = 0; i < theMax; i++) {
                    if (i != theCurrentWorkItemId) {
                        Float4 theOther = theInputs[i];
                        float theOtherLength = length(theOther);
                        float theLength = theCurrentLength * theOtherLength;
                        if (theLength != 0) {
                            float theSimilarity = dot(theCurrent, theOther) / (theLength);
                            if (theSimilarity > theMaxSimilarity) {
                                theMaxSimilarity = theSimilarity;
                                theMaxIndex = i;
                            }
                        }
                    }
                }
                theMostSimilar[theCurrentWorkItemId] = theMaxIndex;
                theMostSimilarity[theCurrentWorkItemId] = theMaxSimilarity;
            }
        });
    }
    long theDuration = System.currentTimeMillis() - theStart;
    System.out.println("Took " + theDuration);
}
Also used : Slf4JLogger(de.mirkosertic.bytecoder.unittest.Slf4JLogger) Test(org.junit.Test)

Example 5 with Slf4JLogger

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

the class AliceBobCarolDaveTest method testSimilarity.

@Test
public void testSimilarity() 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() {

            // 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 theCurrentLength = length(theCurrent);
                float theMaxSimilarity = -1;
                int theMaxIndex = -1;
                // except itself
                for (int i = 0; i < theMax; i++) {
                    if (i != theCurrentWorkItemId) {
                        Float4 theOther = theInputs[i];
                        float theOtherLength = length(theOther);
                        float theLength = theCurrentLength * theOtherLength;
                        if (theLength != 0) {
                            float theSimilarity = dot(theCurrent, theOther) / (theLength);
                            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)

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