From 1a7f32b9db37536b3f25c8abb849887c848c5795 Mon Sep 17 00:00:00 2001 From: qhga Date: Sun, 23 Jan 2022 19:38:28 +0100 Subject: [PATCH] feat: Klausur SS2018 --- .../java/edu/thi/phga/aparapi_test/App.java | 4 ++ .../thi/phga/aparapi_test/PraefixSumme.java | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/edu/thi/phga/aparapi_test/PraefixSumme.java 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 98a36d0..45d861e 100644 --- a/src/main/java/edu/thi/phga/aparapi_test/App.java +++ b/src/main/java/edu/thi/phga/aparapi_test/App.java @@ -116,6 +116,10 @@ public class App { java.util.Arrays.fill(b, 1); OpenCLPrefix2.start(b); break; + case 33: + // Klausur SS2018 + PraefixSumme.start(); + break; case 40: // multiParam defines smoothing System.out.println("Spawning window Filter"); diff --git a/src/main/java/edu/thi/phga/aparapi_test/PraefixSumme.java b/src/main/java/edu/thi/phga/aparapi_test/PraefixSumme.java new file mode 100644 index 0000000..663adbc --- /dev/null +++ b/src/main/java/edu/thi/phga/aparapi_test/PraefixSumme.java @@ -0,0 +1,52 @@ +package edu.thi.phga.aparapi_test; + +import java.util.Arrays; + +import com.aparapi.Kernel; + +public class PraefixSumme { + public static void start() { + int[] a = {4, 3, 6, 2, 1, 5, 9, 7}; + praefixsummme(a); + System.out.println(Arrays.toString(a)); + } + + public static void praefixsummme(int[] a) { + int N = a.length; + int[] b = Arrays.copyOf(a, N * 2); + + class PraefixKernel extends Kernel { + private int offset = 0; + private int stepSize; + + public void setOffset() { + offset = N - offset; + } + + public int getOffset() { + return offset; + } + + public void setStepSize(int s) { + stepSize = s; + } + + @Override public void run() { + int i = getGlobalId(0); + if (i >= stepSize) { + b[offset + i] = b[N - offset + i] + b[N - offset + i - stepSize]; + } + } + } + + PraefixKernel k = new PraefixKernel(); + + for (int j = 1; j < N; j *= 2) { + k.setOffset(); + k.setStepSize(j); + k.execute(N); + } + + Arrays.parallelSetAll(a, i -> b[k.getOffset() + i]); + } +}