added generic Gpu task
This commit is contained in:
@@ -16,17 +16,20 @@ public class Main {
|
||||
|
||||
// main method only used to test
|
||||
public static void main(String[] args) {
|
||||
|
||||
TestUtils<Mat> utils = new TestUtils<>();
|
||||
utils.setCommandPipeline(matInput -> {
|
||||
Mat mat = matInput.get();
|
||||
Mat edges = new Mat();
|
||||
Imgproc.cvtColor(mat, mat, org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY);
|
||||
Imgproc.medianBlur(mat, mat, 15);
|
||||
Imgproc.Canny(mat, edges, 15, 45);
|
||||
});
|
||||
utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/skyscraper_large.jpg")));
|
||||
// utils.setInput(new MatInput(Imgcodecs.imread("src/main/resources/car.jpg")));
|
||||
|
||||
utils.initializeCommands();
|
||||
utils.warmup(5);
|
||||
utils.runTests(10);
|
||||
utils.warmupCPUTests(1);
|
||||
utils.runCPUTests(1);
|
||||
utils.warmupGPUTests(5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ 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;
|
||||
@@ -102,9 +103,13 @@ public class TestUtils<T> {
|
||||
this.originalInput.copyTo(this.input);
|
||||
for (Task t : tasks) {
|
||||
GpuTask task = t.toGpuTask();
|
||||
if (task == null) {
|
||||
task = new GenericGpuTask(t);
|
||||
}
|
||||
task.init();
|
||||
task.run();
|
||||
task.cleanUp();
|
||||
|
||||
}
|
||||
MatPool.reset();
|
||||
}
|
||||
|
||||
+8
-1
@@ -21,7 +21,7 @@ public class MatPool {
|
||||
CPU, GPU
|
||||
}
|
||||
|
||||
public static GpuMat getGpuMat(org.opencv.core.Mat mat) {
|
||||
public static GpuMat getUploadedGpuMat(org.opencv.core.Mat mat) {
|
||||
if (gpuMats.containsKey(mat.getNativeObjAddr())) {
|
||||
if (states.get(mat.getNativeObjAddr()) == State.CPU) {
|
||||
gpuMats.get(mat.getNativeObjAddr()).upload(mats.get(mat.getNativeObjAddr()));
|
||||
@@ -39,6 +39,13 @@ public class MatPool {
|
||||
return gpuMat;
|
||||
}
|
||||
|
||||
public static void download(org.opencv.core.Mat mat) {
|
||||
if (gpuMats.containsKey(mat.getNativeObjAddr()) && states.get(mat.getNativeObjAddr()) == State.GPU) {
|
||||
gpuMats.get(mat.getNativeObjAddr()).download(mats.get(mat.getNativeObjAddr()));
|
||||
states.put(mat.getNativeObjAddr(), State.CPU);
|
||||
}
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
gpuMats.values().forEach(GpuMat::close);
|
||||
mats.values().forEach(Mat::close);
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package de.unidue.nes.gpu.testframework.internal.tasks.general;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.internal.mathandling.MatPool;
|
||||
import de.unidue.nes.gpu.testframework.tasks.GpuTask;
|
||||
import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class GenericGpuTask implements GpuTask {
|
||||
|
||||
private Task task;
|
||||
|
||||
public GenericGpuTask(Task task) {
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
try {
|
||||
Class<?> classType = this.task.getClass();
|
||||
Field[] fields = classType.getDeclaredFields();
|
||||
for (Field f : fields) {
|
||||
f.setAccessible(true);
|
||||
if (f.getType().equals(Mat.class)) {
|
||||
Mat m = (Mat) f.get(this.task);
|
||||
MatPool.download(m);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.task.toCpuTask().run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
// No clean up needed
|
||||
}
|
||||
|
||||
}
|
||||
+6
-6
@@ -19,10 +19,10 @@ import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
*/
|
||||
public class Canny implements Task {
|
||||
|
||||
private Mat image;
|
||||
private Mat edges;
|
||||
private double threshold1;
|
||||
private double threshold2;
|
||||
private final Mat image;
|
||||
private final Mat edges;
|
||||
private final double threshold1;
|
||||
private final double threshold2;
|
||||
|
||||
public Canny(Mat image, Mat edges, double threshold1, double threshold2) {
|
||||
this.image = image;
|
||||
@@ -55,8 +55,8 @@ public class Canny implements Task {
|
||||
@Override
|
||||
public void init() {
|
||||
this.detector = opencv_cudaimgproc.createCannyEdgeDetector(Canny.this.threshold1, Canny.this.threshold2);
|
||||
this.gpuImage = MatPool.getGpuMat(Canny.this.image);
|
||||
this.gpuEdges = MatPool.getGpuMat(Canny.this.edges);
|
||||
this.gpuImage = MatPool.getUploadedGpuMat(Canny.this.image);
|
||||
this.gpuEdges = MatPool.getUploadedGpuMat(Canny.this.edges);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+7
-6
@@ -20,19 +20,20 @@ public class CvtColor implements Task {
|
||||
|
||||
private final int numParams;
|
||||
|
||||
private Mat src;
|
||||
private final Mat src;
|
||||
|
||||
private Mat dst;
|
||||
private final Mat dst;
|
||||
|
||||
private int code;
|
||||
private final int code;
|
||||
|
||||
private int dstCn;
|
||||
private final int dstCn;
|
||||
|
||||
public CvtColor(Mat src, Mat dst, int code) {
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.code = code;
|
||||
this.numParams = 3;
|
||||
this.dstCn = 0;
|
||||
}
|
||||
|
||||
public CvtColor(Mat src, Mat dst, int code, int dstCn) {
|
||||
@@ -76,8 +77,8 @@ public class CvtColor implements Task {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.gpuSrc = MatPool.getGpuMat(CvtColor.this.src);
|
||||
this.gpuDst = MatPool.getGpuMat(CvtColor.this.dst);
|
||||
this.gpuSrc = MatPool.getUploadedGpuMat(CvtColor.this.src);
|
||||
this.gpuDst = MatPool.getUploadedGpuMat(CvtColor.this.dst);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package de.unidue.nes.gpu.testframework.internal.tasks.imgproc;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.tasks.CpuTask;
|
||||
import de.unidue.nes.gpu.testframework.tasks.GpuTask;
|
||||
import de.unidue.nes.gpu.testframework.tasks.Task;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class MedianBlur implements Task {
|
||||
|
||||
private final Mat src;
|
||||
private final Mat dst;
|
||||
private final int ksize;
|
||||
|
||||
public MedianBlur(Mat src, Mat dst, int ksize) {
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.ksize = ksize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CpuTask toCpuTask() {
|
||||
return () -> Imgproc.medianBlur(this.src, this.dst, this.ksize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GpuTask toGpuTask() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Imgproc.medianBlur(" + Objects.toString(this.src) + ", " + Objects.toString(this.dst) + ", " + this.ksize + ")";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import org.opencv.core.Mat;
|
||||
import de.unidue.nes.gpu.testframework.internal.pipeline.TestPipeline;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.Canny;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.CvtColor;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.imgproc.MedianBlur;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
@@ -27,4 +28,8 @@ public class Imgproc {
|
||||
public static void Canny(Mat image, Mat edges, double threshold1, double threshold2) {
|
||||
TestPipeline.INSTANCE.addTask(new Canny(image, edges, threshold1, threshold2));
|
||||
}
|
||||
|
||||
public static void medianBlur(Mat src, Mat dst, int ksize) {
|
||||
TestPipeline.INSTANCE.addTask(new MedianBlur(src, dst, ksize));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user