public static void main(String[] _args) { final int size = 512; final float[] a = new float[size]; final float[] b = new float[size]; for (int i = 0; i < size; i++) { a[i] = (float) (Math.random() * 100); b[i] = (float) (Math.random() * 100); } final float[] sum = new float[size]; Kernel kernel = new Kernel() { @Override public void run() { int gid = getGlobalId(); sum[gid] = a[gid] + b[gid]; } }; kernel.execute(Range.create(512)); for (int i = 0; i < size; i++) { System.out.printf("%6.2f + %6.2f = %8.2f\n", a[i], b[i], sum[i]); } kernel.dispose(); }
public static void main(String[] _args) { final int size = 512; /** Input float array for which square values need to be computed. */ final float[] values = new float[size]; /** Initialize input array. */ for (int i = 0; i < size; i++) { values[i] = i; } /** * Output array which will be populated with square values of corresponding input array * elements. */ final float[] squares = new float[size]; /** * Aparapi Kernel which computes squares of input array elements and populates them in * corresponding elements of output array. */ Kernel kernel = new Kernel() { @Override public void run() { int gid = getGlobalId(); squares[gid] = values[gid] * values[gid]; } }; // Execute Kernel. kernel.execute(512); // Report target execution mode: GPU or JTP (Java Thread Pool). System.out.println("Execution mode=" + kernel.getExecutionMode()); // Display computed square values. for (int i = 0; i < size; i++) { System.out.printf("%6.0f %8.0f\n", values[i], squares[i]); } // Dispose Kernel resources. kernel.dispose(); }