Aparapi: Open source API for data parallel Java on GPU
Existing Java OpenCL bindings require developers to code their data parallel algorithms in OpenCL and write Java host code to explicitly manage buffer transfers and execution requests. In contrast, Aparapi allows developers to code entirely in Java (via a data parallel API) and at runtime is capable of converting Java bytecode to OpenCL so that it can be executed on the GPU. If OpenCL is unavailable, Aparapi will fall back to executing using a Java thread pool.
Aparapi authors like to think that for the appropriate workload this extends Java’s ‘Write Once Run Anywhere’ to include GPU devices. With Aparapi we can take a sequential loop such as this (which adds each element from inA and inB arrays and puts the result in result).
final float inA[] = .... // get a float array of data from somewhere
final float inB[] = .... // get a float array of data from somewhere (inA.length==inB.length)
final float result = new float[inA.length];
for (int i=0; i<array.length; i++){
result[i]=intA[i]+inB[i];
}
And refactor the sequential loop to the following form:
Kernel kernel = new Kernel(){
@Override public void run(){
int i= getGlobalId();
result[i]=intA[i]+inB[i];
}
};
Range range = Range.create(result.length);
kernel.execute(range);
In the above code we extend com.amd.aparapi.Kernel base class and override the Kernel.run() method to express our data parallel algorithm. We initiate the execution of the Kernel(over a specific range 0..results.length) using Kernel.execute(range).
If you would like to download and try Aparapi grab it from Google Code. Alternatively if you would like to contribute or access the code you can check out the code from the SVN repository and jump right in by reading the DevelopersGuide pages.
Category: Software






