diff --git a/src/main/java/edu/thi/phga/aparapi_test/App.java b/src/main/java/edu/thi/phga/aparapi_test/App.java index 45d861e..47f83d7 100644 --- a/src/main/java/edu/thi/phga/aparapi_test/App.java +++ b/src/main/java/edu/thi/phga/aparapi_test/App.java @@ -143,6 +143,9 @@ public class App { case 90: System.out.println("Matrix Gauss"); MatrixGauss.start(); + case 100: + System.out.println("Matrix Reduzieren (WS2019)"); + MatrixReduce.start(); } } } diff --git a/src/main/java/edu/thi/phga/aparapi_test/MatrixReduce.java b/src/main/java/edu/thi/phga/aparapi_test/MatrixReduce.java new file mode 100644 index 0000000..fddc47c --- /dev/null +++ b/src/main/java/edu/thi/phga/aparapi_test/MatrixReduce.java @@ -0,0 +1,53 @@ +package edu.thi.phga.aparapi_test; + +import java.util.Random; + +import com.aparapi.Kernel; + +public class MatrixReduce { + static final int N = 6; + static final int F = 2; + public static void start() { + var in = new Random().ints(N * N, 1, 10).toArray(); + var out = new float[N / F * N / F]; + + System.out.println("IN:"); + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + System.out.printf("%2d ", in[i * N + j]); + } + System.out.println(); + } + + reduziere(in, out); + + System.out.println("OUT:"); + + for (int i = 0; i < N / F; i++) { + for (int j = 0; j < N / F; j++) { + System.out.printf("%.1f ", out[i * N / F + j]); + } + System.out.println(); + } + } + + public static void reduziere(int[] in, float[] out) { + class MyKernel extends Kernel { + @Override public void run() { + int i = getGlobalId(0); + float sum = 0; + + for (int k = 0; k < F; k++) { + for (int x = 0; x < F; x++) { + sum += in[(i % F) * F + k * N + ((int) i / F) * N + x]; + } + } + out[i] = sum / (F * F); + } + } + + MyKernel k = new MyKernel(); + k.execute(out.length); + } +}