added resetability of tasks and capturing of uploads and downloads
This commit is contained in:
@@ -4,23 +4,16 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_core;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.exception.AlreadyInitializedException;
|
||||
import de.unidue.nes.gpu.testframework.exception.NotInitializedException;
|
||||
import de.unidue.nes.gpu.testframework.input.Input;
|
||||
import de.unidue.nes.gpu.testframework.internal.mathandling.MatConverter;
|
||||
import de.unidue.nes.gpu.testframework.internal.mathandling.MatPool;
|
||||
import de.unidue.nes.gpu.testframework.internal.pipeline.TestPipeline;
|
||||
import de.unidue.nes.gpu.testframework.internal.resulthandling.Result;
|
||||
import de.unidue.nes.gpu.testframework.internal.resulthandling.ResultHandler;
|
||||
import de.unidue.nes.gpu.testframework.internal.resulthandling.TestExecution;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.general.DownloadTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.general.GenericGpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.general.PagelockedUploadTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.general.PipelineTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.general.UploadTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.timing.TimeDto;
|
||||
import de.unidue.nes.gpu.testframework.internal.timing.TimingUtil;
|
||||
import de.unidue.nes.gpu.testframework.tasks.GpuTask;
|
||||
@@ -93,12 +86,11 @@ public class TestUtils<T> {
|
||||
t.toCpuTask().run();
|
||||
}
|
||||
}
|
||||
resetTasks();
|
||||
}
|
||||
|
||||
public void warmupGPUTests(int iterations) {
|
||||
checkInitialized();
|
||||
warmupUploadAndDownload(iterations);
|
||||
warmupPageLockedUpload(iterations);
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
|
||||
this.originalInput.copyTo(this.input);
|
||||
@@ -110,33 +102,10 @@ public class TestUtils<T> {
|
||||
task.init();
|
||||
task.run();
|
||||
task.cleanUp();
|
||||
|
||||
}
|
||||
MatPool.reset();
|
||||
}
|
||||
}
|
||||
|
||||
private void warmupPageLockedUpload(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()) {
|
||||
MatConverter.copyMat(this.input.toMat(), jcvMat);
|
||||
opencv_core.registerPageLocked(jcvMat);
|
||||
gpuMat.upload(jcvMat);
|
||||
opencv_core.unregisterPageLocked(jcvMat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
MatConverter.copyMat(this.input.toMat(), jcvMat);
|
||||
gpuMat.upload(jcvMat);
|
||||
gpuMat.download(jcvMat);
|
||||
}
|
||||
}
|
||||
resetTasks();
|
||||
}
|
||||
|
||||
public void runCPUTests(int iterations) {
|
||||
@@ -144,8 +113,10 @@ public class TestUtils<T> {
|
||||
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
testCPUTaskInIsolation(tasks, i, iterations);
|
||||
resetTasks();
|
||||
}
|
||||
testCPUPipeline(tasks, iterations);
|
||||
resetTasks();
|
||||
}
|
||||
|
||||
private void testCPUPipeline(List<Task> tasks, int iterations) {
|
||||
@@ -168,43 +139,13 @@ public class TestUtils<T> {
|
||||
}
|
||||
|
||||
public void runGPUTests(int iterations) {
|
||||
testUploadAndDownload(iterations);
|
||||
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
testGPUTaskInIsolation(tasks, i, iterations);
|
||||
resetTasks();
|
||||
}
|
||||
}
|
||||
|
||||
private void testUploadAndDownload(int iterations) {
|
||||
testUpload(iterations);
|
||||
testPageLockedUpload(iterations);
|
||||
testDownload(iterations);
|
||||
}
|
||||
|
||||
private void testPageLockedUpload(int iterations) {
|
||||
PagelockedUploadTask task = new PagelockedUploadTask(this.input.toMat());
|
||||
testSpecialGpuMethod(iterations, task);
|
||||
}
|
||||
|
||||
private void testDownload(int iterations) {
|
||||
DownloadTask task = new DownloadTask(this.input.toMat());
|
||||
testSpecialGpuMethod(iterations, task);
|
||||
}
|
||||
|
||||
private void testUpload(int iterations) {
|
||||
UploadTask task = new UploadTask(this.input.toMat());
|
||||
testSpecialGpuMethod(iterations, task);
|
||||
}
|
||||
|
||||
private void testSpecialGpuMethod(int iterations, Task task) {
|
||||
GpuTask gpuTask = task.toGpuTask();
|
||||
TimeDto time = TimingUtil.testPerformance(() -> {
|
||||
this.originalInput.copyTo(this.input);
|
||||
gpuTask.init();
|
||||
}, gpuTask::run, gpuTask::cleanUp, iterations);
|
||||
ResultHandler.newResult(new Result(task, TestExecution.GPU, time));
|
||||
}
|
||||
|
||||
private void testGPUTaskInIsolation(List<Task> tasks, int index, int iterations) {
|
||||
Task task = tasks.get(index);
|
||||
GpuTask t = task.toGpuTask();
|
||||
@@ -244,4 +185,13 @@ public class TestUtils<T> {
|
||||
throw new NotInitializedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void evaluate() {
|
||||
ResultHandler.evaluate();
|
||||
}
|
||||
|
||||
private void resetTasks() {
|
||||
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
|
||||
tasks.forEach(Task::reset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.unidue.nes.gpu.testframework.input;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
@@ -12,6 +10,4 @@ public interface Input<T> {
|
||||
Input<T> createCopy();
|
||||
|
||||
T get();
|
||||
|
||||
Mat toMat();
|
||||
}
|
||||
|
||||
@@ -29,10 +29,4 @@ public class MatInput implements Input<Mat> {
|
||||
public Mat get() {
|
||||
return this.mat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mat toMat() {
|
||||
return get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-2
@@ -6,6 +6,8 @@ import java.util.Map;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
@@ -21,9 +23,10 @@ public class MatPool {
|
||||
CPU, GPU
|
||||
}
|
||||
|
||||
public static GpuMat getUploadedGpuMat(org.opencv.core.Mat mat) {
|
||||
public static GpuMat getUploadedGpuMat(org.opencv.core.Mat mat, Task task) {
|
||||
if (gpuMats.containsKey(mat.getNativeObjAddr())) {
|
||||
if (states.get(mat.getNativeObjAddr()) == State.CPU) {
|
||||
UploadDownloadCaptor.captureUpload(mat, task);
|
||||
MatConverter.copyMat(mat, mats.get(mat.getNativeObjAddr()));
|
||||
gpuMats.get(mat.getNativeObjAddr()).upload(mats.get(mat.getNativeObjAddr()));
|
||||
states.put(mat.getNativeObjAddr(), State.GPU);
|
||||
@@ -37,14 +40,16 @@ public class MatPool {
|
||||
gpuMats.put(mat.getNativeObjAddr(), gpuMat);
|
||||
mats.put(mat.getNativeObjAddr(), jcvMat);
|
||||
states.put(mat.getNativeObjAddr(), State.GPU);
|
||||
UploadDownloadCaptor.captureUpload(mat, task);
|
||||
return gpuMat;
|
||||
}
|
||||
|
||||
public static void download(org.opencv.core.Mat mat) {
|
||||
public static void download(org.opencv.core.Mat mat, Task task) {
|
||||
if (gpuMats.containsKey(mat.getNativeObjAddr()) && states.get(mat.getNativeObjAddr()) == State.GPU) {
|
||||
gpuMats.get(mat.getNativeObjAddr()).download(mats.get(mat.getNativeObjAddr()));
|
||||
MatConverter.copyMat(mats.get(mat.getNativeObjAddr()), mat);
|
||||
states.put(mat.getNativeObjAddr(), State.CPU);
|
||||
UploadDownloadCaptor.captureDownload(mat, task);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package de.unidue.nes.gpu.testframework.internal.mathandling;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
|
||||
public final class MatTaskTuple {
|
||||
|
||||
private final Mat mat;
|
||||
|
||||
private final Task task;
|
||||
|
||||
private final long nativeMat;
|
||||
|
||||
MatTaskTuple(Mat mat, Task task) {
|
||||
this.mat = new Mat();
|
||||
mat.copyTo(this.mat);
|
||||
this.task = task;
|
||||
this.nativeMat = mat.getNativeObjAddr();
|
||||
}
|
||||
|
||||
public Mat getMat() {
|
||||
return this.mat;
|
||||
}
|
||||
|
||||
public Task getTask() {
|
||||
return this.task;
|
||||
}
|
||||
|
||||
public long getNativeMat() {
|
||||
return this.nativeMat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof MatTaskTuple)) {
|
||||
return false;
|
||||
}
|
||||
MatTaskTuple other = (MatTaskTuple) obj;
|
||||
return other.nativeMat == this.nativeMat && other.task == this.task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.nativeMat, this.task);
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package de.unidue.nes.gpu.testframework.internal.mathandling;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class UploadDownloadCaptor {
|
||||
|
||||
private UploadDownloadCaptor() {
|
||||
|
||||
}
|
||||
|
||||
private static final List<MatTaskTuple> uploads = new ArrayList<>();
|
||||
|
||||
private static final List<MatTaskTuple> downloads = new ArrayList<>();
|
||||
|
||||
public static void captureUpload(Mat mat, Task task) {
|
||||
MatTaskTuple tuple = new MatTaskTuple(mat, task);
|
||||
if (!uploads.contains(tuple)) {
|
||||
uploads.add(tuple);
|
||||
}
|
||||
}
|
||||
|
||||
public static void captureDownload(Mat mat, Task task) {
|
||||
MatTaskTuple tuple = new MatTaskTuple(mat, task);
|
||||
if (!downloads.contains(tuple)) {
|
||||
downloads.add(tuple);
|
||||
}
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
uploads.forEach(tuple -> tuple.getMat().release());
|
||||
downloads.forEach(tuple -> tuple.getMat().release());
|
||||
uploads.clear();
|
||||
downloads.clear();
|
||||
}
|
||||
|
||||
public static List<MatTaskTuple> getUploads() {
|
||||
return new ArrayList<>(uploads);
|
||||
}
|
||||
|
||||
public static List<MatTaskTuple> getDownloads() {
|
||||
return new ArrayList<>(downloads);
|
||||
}
|
||||
}
|
||||
+38
@@ -1,14 +1,52 @@
|
||||
package de.unidue.nes.gpu.testframework.internal.resulthandling;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class ResultHandler {
|
||||
|
||||
private static final List<Result> cpuResults = new ArrayList<>();
|
||||
|
||||
private static final List<Result> gpuResults = new ArrayList<>();
|
||||
|
||||
private static Result upload;
|
||||
|
||||
private static Result pagelockedUpload;
|
||||
|
||||
private static Result download;
|
||||
|
||||
private static Result cpuPipelineResult;
|
||||
|
||||
private ResultHandler() {
|
||||
}
|
||||
|
||||
public static void newResult(Result result) {
|
||||
System.out.println(result);
|
||||
// if (result.getTask().getClass() == PipelineTask.class) {
|
||||
// cpuPipelineResult = result;
|
||||
// }
|
||||
// if (result.getExecution() == TestExecution.CPU) {
|
||||
// cpuResults.add(result);
|
||||
// } else if (result.getExecution() == TestExecution.GPU) {
|
||||
// if (result.getTask().getClass() == UploadTask.class) {
|
||||
// upload = result;
|
||||
// } else if (result.getTask().getClass() == DownloadTask.class) {
|
||||
// pagelockedUpload = result;
|
||||
// } else if (result.getTask().getClass() == PagelockedUploadTask.class) {
|
||||
// download = result;
|
||||
// } else {
|
||||
// gpuResults.add(result);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public static void evaluate() {
|
||||
// List<Task> pipeline = TestPipeline.INSTANCE.getPipeline();
|
||||
// for (Task t : pipeline) {
|
||||
// System.out.println(t.getDescription());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -59,4 +59,10 @@ public class DownloadTask implements Task {
|
||||
return "Download of " + this.mat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ public class GenericGpuTask implements GpuTask {
|
||||
f.setAccessible(true);
|
||||
if (f.getType().equals(Mat.class)) {
|
||||
Mat m = (Mat) f.get(this.task);
|
||||
MatPool.download(m);
|
||||
MatPool.download(m, this.task);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
+6
@@ -61,4 +61,10 @@ public class PagelockedUploadTask implements Task {
|
||||
return "Page-locked upload of " + this.mat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -50,4 +50,10 @@ public class PipelineTask implements Task {
|
||||
return "Pipeline";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -58,4 +58,10 @@ public class UploadTask implements Task {
|
||||
return "Upload of " + this.mat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+15
-2
@@ -24,11 +24,18 @@ public class Canny implements Task {
|
||||
private final double threshold1;
|
||||
private final double threshold2;
|
||||
|
||||
private final Mat originalImage;
|
||||
private final Mat originalEdges;
|
||||
|
||||
public Canny(Mat image, Mat edges, double threshold1, double threshold2) {
|
||||
this.image = image;
|
||||
this.edges = edges;
|
||||
this.threshold1 = threshold1;
|
||||
this.threshold2 = threshold2;
|
||||
this.originalImage = new Mat();
|
||||
this.originalEdges = new Mat();
|
||||
this.image.copyTo(this.originalImage);
|
||||
this.edges.copyTo(this.originalEdges);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,8 +60,8 @@ public class Canny implements Task {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.gpuImage = MatPool.getUploadedGpuMat(Canny.this.image);
|
||||
this.gpuEdges = MatPool.getUploadedGpuMat(Canny.this.edges);
|
||||
this.gpuImage = MatPool.getUploadedGpuMat(Canny.this.image, Canny.this);
|
||||
this.gpuEdges = MatPool.getUploadedGpuMat(Canny.this.edges, Canny.this);
|
||||
this.detector = opencv_cudaimgproc.createCannyEdgeDetector(Canny.this.threshold1, Canny.this.threshold2);
|
||||
}
|
||||
|
||||
@@ -70,4 +77,10 @@ public class Canny implements Task {
|
||||
return "Imgproc.Canny(" + Objects.toString(this.image) + ", " + Objects.toString(this.edges) + ", " + this.threshold1 + ", " + this.threshold2 + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
this.originalImage.copyTo(this.image);
|
||||
this.originalEdges.copyTo(this.edges);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-2
@@ -28,12 +28,20 @@ public class CvtColor implements Task {
|
||||
|
||||
private final int dstCn;
|
||||
|
||||
private final Mat originalSrc;
|
||||
|
||||
private final Mat originalDst;
|
||||
|
||||
public CvtColor(Mat src, Mat dst, int code) {
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.code = code;
|
||||
this.numParams = 3;
|
||||
this.dstCn = 0;
|
||||
this.originalSrc = new Mat();
|
||||
this.originalDst = new Mat();
|
||||
this.src.copyTo(this.originalSrc);
|
||||
this.dst.copyTo(this.originalDst);
|
||||
}
|
||||
|
||||
public CvtColor(Mat src, Mat dst, int code, int dstCn) {
|
||||
@@ -42,6 +50,10 @@ public class CvtColor implements Task {
|
||||
this.code = code;
|
||||
this.dstCn = dstCn;
|
||||
this.numParams = 4;
|
||||
this.originalSrc = new Mat();
|
||||
this.originalDst = new Mat();
|
||||
this.src.copyTo(this.originalSrc);
|
||||
this.dst.copyTo(this.originalDst);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,8 +86,8 @@ public class CvtColor implements Task {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.gpuSrc = MatPool.getUploadedGpuMat(CvtColor.this.src);
|
||||
this.gpuDst = MatPool.getUploadedGpuMat(CvtColor.this.dst);
|
||||
this.gpuSrc = MatPool.getUploadedGpuMat(CvtColor.this.src, CvtColor.this);
|
||||
this.gpuDst = MatPool.getUploadedGpuMat(CvtColor.this.dst, CvtColor.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,4 +107,10 @@ public class CvtColor implements Task {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
this.originalSrc.copyTo(this.src);
|
||||
this.originalDst.copyTo(this.dst);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+16
-2
@@ -22,10 +22,18 @@ public class MedianBlur implements Task {
|
||||
private final Mat dst;
|
||||
private final int ksize;
|
||||
|
||||
private final Mat originalSrc;
|
||||
private final Mat originalDst;
|
||||
|
||||
public MedianBlur(Mat src, Mat dst, int ksize) {
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.ksize = ksize;
|
||||
|
||||
this.originalSrc = new Mat();
|
||||
this.originalDst = new Mat();
|
||||
this.src.copyTo(this.originalSrc);
|
||||
this.dst.copyTo(this.originalDst);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,8 +58,8 @@ public class MedianBlur implements Task {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.gpuSrc = MatPool.getUploadedGpuMat(MedianBlur.this.src);
|
||||
this.gpuDst = MatPool.getUploadedGpuMat(MedianBlur.this.dst);
|
||||
this.gpuSrc = MatPool.getUploadedGpuMat(MedianBlur.this.src, MedianBlur.this);
|
||||
this.gpuDst = MatPool.getUploadedGpuMat(MedianBlur.this.dst, MedianBlur.this);
|
||||
this.filter = opencv_cudafilters.createMedianFilter(this.gpuSrc.type(), MedianBlur.this.ksize);
|
||||
}
|
||||
|
||||
@@ -67,4 +75,10 @@ public class MedianBlur implements Task {
|
||||
return "Imgproc.medianBlur(" + Objects.toString(this.src) + ", " + Objects.toString(this.dst) + ", " + this.ksize + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
this.originalSrc.copyTo(this.src);
|
||||
this.originalDst.copyTo(this.dst);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ public interface Task {
|
||||
GpuTask toGpuTask();
|
||||
|
||||
String getDescription();
|
||||
|
||||
void reset();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user