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);
}
}
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());
}
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());
}
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);
}
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]);
}
}
Aggregations