Update MatrixMul1D to measure time spent executing the kernel
@@ -3,7 +3,10 @@ package uk.ac.manchester.tornado.examples.matrices;
import uk.ac.manchester.tornado.api.TaskSchedule;
import uk.ac.manchester.tornado.api.annotations.Parallel;
+import java.util.Arrays;
+import java.util.OptionalDouble;
import java.util.stream.IntStream;
+import java.util.stream.LongStream;
public class MatrixMul1D {
@@ -19,17 +22,12 @@ public class MatrixMul1D {
}
}
- private static void printMatrix(final int N, float[] matrixC) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- System.out.printf(" %f |", matrixC[i * N + j]);
- }
- System.out.println();
- }
+ public static void main(String[] args) throws Exception {
+ int N = 512;
+ if (args.length == 1) {
+ N = Integer.parseInt(args[0]);
}
- public static void main(String[] args) {
- final int N = 64;
float[] matrixA = new float[N * N];
float[] matrixB = new float[N * N];
float[] matrixC = new float[N * N];
@@ -39,11 +37,29 @@ public class MatrixMul1D {
matrixB[idx] = 3.5f;
});
- new TaskSchedule("s0") //
- .task("t0", MatrixMul1D::matrixMultiplication, matrixA, matrixB, matrixC, N) //
- .streamOut(matrixC) //
- .execute();
+ TaskSchedule s = new TaskSchedule("s0")
+ .task("t0", MatrixMul1D::matrixMultiplication, matrixA, matrixB, matrixC, N)
+ .streamOut(matrixC);
+
+ // Warm up
+ for (int i = 0; i < 15; i++) {
+ s.execute();
+ }
+
+ long start, stop;
+ long[] execTimes = new long[100];
+ for (int i = 0; i < execTimes.length; i++) {
+ start = System.nanoTime();
+ s.execute();
+ stop = System.nanoTime();
+ execTimes[i] = stop - start;
+ }
+
+ OptionalDouble avg = Arrays.stream(execTimes).average();
+ double average;
+ if (avg.isPresent()) average = avg.getAsDouble();
+ else throw new Exception("Could not get average execution time");
- printMatrix(N, matrixC);
+ System.out.printf("Average time: %.2f\n", average);
}
}
