diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java index a404194..023283d 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/Main.java @@ -25,7 +25,7 @@ public class Main { }); utils.setInputImage(Imgcodecs.imread("src/main/resources/car.jpg")); utils.initializeCommands(); -// utils.warmupGPUTests(10); - utils.runGPUTests(2); + utils.warmupGPUTests(5); + utils.runGPUTests(10); } } diff --git a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/TestUtils.java b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/TestUtils.java index 322e416..4004b04 100644 --- a/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/TestUtils.java +++ b/gpu-app/src/main/java/de/unidue/nes/gpu/testframework/TestUtils.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Objects; import java.util.function.Consumer; +import org.bytedeco.opencv.opencv_core.GpuMat; import org.opencv.core.Mat; import de.unidue.nes.gpu.testframework.internal.MatPool; @@ -81,13 +82,29 @@ public class TestUtils {// TODO: reduce code duplication if (!this.initialized) { throw new RuntimeException(); } - warmupUploadAndDownload(); - + warmupUploadAndDownload(iterations); + for (int i = 0; i < iterations; i++) { + List tasks = TestPipeline.INSTANCE.getPipeline(); + this.originalInput.copyTo(this.input); + for (Task t : tasks) { + GpuTask task = t.toGpuTask(); + task.init(); + task.run(); + task.cleanUp(); + } + MatPool.reset(); + } } - private void warmupUploadAndDownload() { - // TODO Auto-generated method stub - + private void warmupUploadAndDownload(int iterations) { + for (int i = 0; i < iterations; i++) { + this.originalInput.copyTo(this.input); + try (org.bytedeco.opencv.opencv_core.Mat jcvMat = new org.bytedeco.opencv.opencv_core.Mat(); GpuMat gpuMat = new GpuMat()) { + copyToJavaCvMat(this.input, jcvMat); + gpuMat.upload(jcvMat); + gpuMat.download(jcvMat); + } + } } public void runCPUTests(int iterations) { @@ -121,20 +138,72 @@ public class TestUtils {// TODO: reduce code duplication } public void runGPUTests(int iterations) { + testUploadAndDownload(iterations); List tasks = TestPipeline.INSTANCE.getPipeline(); for (int i = 0; i < tasks.size(); i++) { testGPUTaskInIsolation(tasks, i, iterations); } } - private void testUploadAndDownload() { - // TODO Auto-generated method stub + private void testUploadAndDownload(int iterations) { + testUpload(iterations); + testDownload(iterations); } - private void testGPUPipeline(List tasks, int iterations) { - // TODO Auto-generated method stub + private void testDownload(int iterations) { + org.bytedeco.opencv.opencv_core.Mat[] jcvMat = new org.bytedeco.opencv.opencv_core.Mat[1]; + GpuMat[] gpuMat = new GpuMat[1]; + Runnable setUp = () -> { + jcvMat[0] = new org.bytedeco.opencv.opencv_core.Mat(); + gpuMat[0] = new GpuMat(); + this.originalInput.copyTo(this.input); + copyToJavaCvMat(this.input, jcvMat[0]); + gpuMat[0].upload(jcvMat[0]); + }; + Runnable test = () -> { + gpuMat[0].download(jcvMat[0]); + }; + Runnable cleanUp = () -> { + gpuMat[0].close(); + jcvMat[0].close(); + }; + TimeDto time = TimingUtil.testPerformance(setUp, test, cleanUp, iterations); + System.out.println("download was " + time + "ms"); + } + + private void testUpload(int iterations) { + org.bytedeco.opencv.opencv_core.Mat[] jcvMat = new org.bytedeco.opencv.opencv_core.Mat[1]; + GpuMat[] gpuMat = new GpuMat[1]; + + Runnable setUp = () -> { + jcvMat[0] = new org.bytedeco.opencv.opencv_core.Mat(); + gpuMat[0] = new GpuMat(); + this.originalInput.copyTo(this.input); + copyToJavaCvMat(this.input, jcvMat[0]); + }; + Runnable test = () -> { + gpuMat[0].upload(jcvMat[0]); + }; + Runnable cleanUp = () -> { + gpuMat[0].close(); + jcvMat[0].close(); + }; + TimeDto time = TimingUtil.testPerformance(setUp, test, cleanUp, iterations); + System.out.println("upload was " + time + "ms"); + } + + private void copyToJavaCvMat(Mat mat, org.bytedeco.opencv.opencv_core.Mat jcvMat) { + final int cols = mat.cols(); + final int rows = mat.rows(); + final int type = mat.type(); + byte[] buffer = new byte[(int) (mat.total() * mat.channels())]; + mat.get(0, 0, buffer); + jcvMat.create(rows, cols, type); + if (buffer.length != 0) { + jcvMat.data().put(buffer); + } } private void testGPUTaskInIsolation(List tasks, int index, int iterations) {