package com.jogamp.opencl.demos.hellojocl; import com.jogamp.opencl.CLBuffer; import com.jogamp.opencl.CLCommandQueue; import com.jogamp.opencl.CLContext; import com.jogamp.opencl.CLDevice; import com.jogamp.opencl.CLKernel; import com.jogamp.opencl.CLProgram; import java.io.IOException; import java.nio.FloatBuffer; import java.util.Random; import static java.lang.System.*; import static com.jogamp.opencl.CLMemory.Mem.*; import static java.lang.Math.*; /** * Hello Java OpenCL example. Adds all elements of buffer A to buffer B * and stores the result in buffer C.
* Sample was inspired by the Nvidia VectorAdd example written in C/C++
* which is bundled in the Nvidia OpenCL SDK.
* @author Michael Bien
*/
public class HelloJOCL {
public static void main(String[] args) throws IOException {
// set up (uses default CLPlatform and creates context for all devices)
CLContext context = CLContext.create();
out.println("created "+context);
// always make sure to release the context under all circumstances
// not needed for this particular sample but recommented
try{
// select fastest device
CLDevice device = context.getMaxFlopsDevice();
out.println("using "+device);
// create command queue on device.
CLCommandQueue queue = device.createCommandQueue();
int elementCount = 1444477; // Length of arrays to process
int localWorkSize = min(device.getMaxWorkGroupSize(), 256); // Local work size dimensions
int globalWorkSize = roundUp(localWorkSize, elementCount); // rounded up to the nearest multiple of the localWorkSize
// load sources, create and build program
CLProgram program = context.createProgram(HelloJOCL.class.getResourceAsStream("VectorAdd.cl")).build();
// A, B are input buffers, C is for the result
CLBuffer