feat: Matrix Reduce

master
qhga 3 years ago
parent 1a7f32b9db
commit 80dce07bae
Signed by: phga
GPG Key ID: 5249548AA705F019

@ -143,6 +143,9 @@ public class App {
case 90: case 90:
System.out.println("Matrix Gauss"); System.out.println("Matrix Gauss");
MatrixGauss.start(); MatrixGauss.start();
case 100:
System.out.println("Matrix Reduzieren (WS2019)");
MatrixReduce.start();
} }
} }
} }

@ -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);
}
}
Loading…
Cancel
Save