use of de.mirkosertic.bytecoder.core.BytecodeLoader 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.core.BytecodeLoader 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.core.BytecodeLoader in project Bytecoder by mirkosertic.
the class OpenCLContext method kernelFor.
private CachedKernel kernelFor(Kernel aKernel) {
Class theKernelClass = aKernel.getClass();
CachedKernel theCachedKernel = cachedKernels.get(theKernelClass);
if (theCachedKernel != null) {
return theCachedKernel;
}
OpenCLCompileResult theResult = ALREADY_COMPILED.get(theKernelClass);
if (theResult == null) {
Method theMethod;
try {
theMethod = aKernel.getClass().getDeclaredMethod("processWorkItem");
} catch (Exception e) {
throw new IllegalArgumentException("Error resolving kernel method", e);
}
BytecodeMethodSignature theSignature = backend.signatureFrom(theMethod);
BytecodeLoader theLoader = new BytecodeLoader(theKernelClass.getClassLoader());
BytecodeLinkerContext theLinkerContext = new BytecodeLinkerContext(theLoader, compileOptions.getLogger());
theResult = backend.generateCodeFor(compileOptions, theLinkerContext, aKernel.getClass(), theMethod.getName(), theSignature);
logger.debug("Generated Kernel code : {}", theResult.getData());
ALREADY_COMPILED.put(theKernelClass, theResult);
}
// Construct the program
cl_program theCLProgram = clCreateProgramWithSource(context, 1, new String[] { theResult.getData() }, null, null);
clBuildProgram(theCLProgram, 0, null, null, null, null);
cl_kernel theKernel = clCreateKernel(theCLProgram, "BytecoderKernel", null);
CachedKernel theCached = new CachedKernel(theResult.getInputOutputs(), theCLProgram, theKernel);
cachedKernels.put(theKernelClass, theCached);
return theCached;
}
Aggregations