diff --git a/README.md b/README.md index c673adc..4555f2d 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ DEVICE=1 # 60 = RGBHistogramm (Spawns a javafx window) # 70 = Radixsort # 80 = Matrix Multiplication +# 90 = Gauss Matrix # DEFAULT = 0 TARGET=31 @@ -29,6 +30,7 @@ TARGET=31 # Target 60 filename of a picture (Place in folder $PROJECT_ROOT/pictures/my_pic.jpg) [jpg,bmp,png] # Target 70 Array lenth (N) # Target 80 Matrices height and width (NxN) +# Target 90 Nothing # DEFAULT = 25 / "lena.bmp" MULTI_PARAM=3 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 1b8ca97..98a36d0 100644 --- a/src/main/java/edu/thi/phga/aparapi_test/App.java +++ b/src/main/java/edu/thi/phga/aparapi_test/App.java @@ -136,6 +136,9 @@ public class App { case 80: System.out.println("Matrix Mult"); MatrixMult.start(); + case 90: + System.out.println("Matrix Gauss"); + MatrixGauss.start(); } } } diff --git a/src/main/java/edu/thi/phga/aparapi_test/MatrixGauss.java b/src/main/java/edu/thi/phga/aparapi_test/MatrixGauss.java new file mode 100644 index 0000000..cb7b421 --- /dev/null +++ b/src/main/java/edu/thi/phga/aparapi_test/MatrixGauss.java @@ -0,0 +1,98 @@ +package edu.thi.phga.aparapi_test; + +import com.aparapi.Kernel; +import com.aparapi.Range; + +class MatrixGauss { + public static void start() { + double[] a = { + 2, 8, 1, + 4, 4, -1, + -1, 2, 12 + }; + print(a); + // gauss_muloe(a); + gauss(a); + print(a); + } + + private static void print(double[] a) { + int n = (int) Math.round(Math.sqrt(a.length)); + for (int z = 0; z < n; z++) { + for (int s = 0; s < n; s++) + System.out.printf("%5.1f ", a[z * n + s]); + System.out.println(); + } + System.out.println(); + } + + private static void gauss(double[] a) { + int n = (int) Math.sqrt(a.length); + double[] c = new double[n]; + Range r = Range.create(n * n); + + class SubKernel extends Kernel { + int i; + double[] c; + public void setParams(int i, double[] c) { + this.i = i; + this.c = c; + } + @Override public void run() { + int j = getGlobalId(); + int x = (int) j / n; + // if (j % n != i) + a[j] = a[j] - c[x] * a[i * n + (j % n)]; + } + } + + SubKernel k = new SubKernel(); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + c[j] = a[j * n + i] / a[i * n + i]; + } + c[i] = 0; // removes the if (j % n != i) + k.setParams(i, c); + k.execute(r); + // for (int j = 0; j < n * n; j++) { + // int x = (int) j / n; + // int Ai = i * n + (j % n); + // System.out.printf("j: %d, i: %d, j / n: %d -> ", j, i, x); + // System.out.printf("a[%d] -= c[%d] * a[%d] (%.2f * %.2f)", j, x, Ai, c[x], a[Ai]); + // System.out.println(""); + // } + } + } + + // private static void gauss_muloe(double[] a) { + // int n = (int) Math.round(Math.sqrt(a.length)); + // double[] c = new double[n]; + // // Koeffizienten + // class Zeilensubtraktion extends Kernel { + // double[] c; + // int i; + // void setC(double[] c) { this.c = c; } + // void setI(int + // i) { this.i = i; } + // @Override public void run() { + // int j = getGlobalId(0); + // // Zeilenindex + // int k = getGlobalId(1); + // // Spaltenindex + // a[j * n + k] -= c[j] * a[i * n + k]; + // } + // } + // Zeilensubtraktion kernel = new Zeilensubtraktion(); + // Range range = Range.create2D(App.device, n, n); + // for (int i = 0; i < n; i++) { + // for (int j = 0; j < n; j++) + // c[j] = a[j * n + i] / a[i * n + i]; + + // c[i] = 0; // vermeidet Fallunterscheidung im Kernel + // kernel.setI(i); kernel.setC(c); + // kernel.execute(range); + // } + // } + +} \ No newline at end of file