added Gpu evaluation
This commit is contained in:
@@ -25,7 +25,7 @@ public class Main {
|
||||
});
|
||||
utils.setInputImage(Imgcodecs.imread("src/main/resources/car.jpg"));
|
||||
utils.initializeCommands();
|
||||
utils.warmupCPUTests(10);
|
||||
utils.runCPUTests(100);
|
||||
// utils.warmupGPUTests(10);
|
||||
utils.runGPUTests(2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.internal.MatPool;
|
||||
import de.unidue.nes.gpu.testframework.internal.TestPipeline;
|
||||
import de.unidue.nes.gpu.testframework.internal.TimingUtil;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.GpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.Task;
|
||||
import de.unidue.nes.gpu.utils.TimeDto;
|
||||
|
||||
@@ -17,6 +19,9 @@ import de.unidue.nes.gpu.utils.TimeDto;
|
||||
*/
|
||||
public class TestUtils {// TODO: reduce code duplication
|
||||
|
||||
private static final Runnable NOOP = () -> {
|
||||
};
|
||||
|
||||
private Consumer<Mat> commands;
|
||||
|
||||
private Mat originalInput;
|
||||
@@ -100,7 +105,7 @@ public class TestUtils {// TODO: reduce code duplication
|
||||
Runnable setup = () -> this.originalInput.copyTo(this.input);
|
||||
Runnable toTest = () -> tasks.forEach(t -> t.toCpuTask().run());
|
||||
|
||||
TimingUtil.testPerformance(setup, toTest, iterations);
|
||||
TimingUtil.testPerformance(setup, toTest, NOOP, iterations);
|
||||
}
|
||||
|
||||
private void testCPUTaskInIsolation(List<Task> tasks, int index, int iterations) {
|
||||
@@ -110,13 +115,16 @@ public class TestUtils {// TODO: reduce code duplication
|
||||
tasks.get(j).toCpuTask().run();
|
||||
}
|
||||
};
|
||||
TimeDto time = TimingUtil.testPerformance(setup, tasks.get(index).toCpuTask(), iterations);
|
||||
TimeDto time = TimingUtil.testPerformance(setup, tasks.get(index).toCpuTask(), NOOP, iterations);
|
||||
System.out.println(tasks.get(index).toCpuTask() + ":" + time);
|
||||
|
||||
}
|
||||
|
||||
public void runGPUTests(int iterations) {
|
||||
|
||||
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
testGPUTaskInIsolation(tasks, i, iterations);
|
||||
}
|
||||
}
|
||||
|
||||
private void testUploadAndDownload() {
|
||||
@@ -130,6 +138,22 @@ public class TestUtils {// TODO: reduce code duplication
|
||||
}
|
||||
|
||||
private void testGPUTaskInIsolation(List<Task> tasks, int index, int iterations) {
|
||||
|
||||
GpuTask toTest = tasks.get(index).toGpuTask();
|
||||
Runnable setup = () -> {
|
||||
this.originalInput.copyTo(this.input);
|
||||
for (int j = 0; j < index; j++) {
|
||||
GpuTask task = tasks.get(j).toGpuTask();
|
||||
task.init();
|
||||
task.run();
|
||||
task.cleanUp();
|
||||
}
|
||||
toTest.init();
|
||||
};
|
||||
Runnable cleanUp = () -> {
|
||||
toTest.cleanUp();
|
||||
MatPool.reset();
|
||||
};
|
||||
TimeDto time = TimingUtil.testPerformance(setup, toTest, cleanUp, iterations);
|
||||
System.out.println(tasks.get(index).toGpuTask() + ":" + time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package de.unidue.nes.gpu.testframework.internal;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
|
||||
/**
|
||||
* @author Roch P., Shahbaz Nejad B.
|
||||
*/
|
||||
public class MatPool {
|
||||
|
||||
private static final Map<Long, GpuMat> gpuMats = new HashMap<>();
|
||||
|
||||
private static final Map<Long, Mat> mats = new HashMap<>();
|
||||
|
||||
private static final Map<Long, State> states = new HashMap<>();
|
||||
|
||||
private enum State {
|
||||
CPU, GPU
|
||||
}
|
||||
|
||||
public static GpuMat getGpuMat(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()));
|
||||
states.put(mat.getNativeObjAddr(), State.GPU);
|
||||
}
|
||||
return gpuMats.get(mat.getNativeObjAddr());
|
||||
}
|
||||
GpuMat gpuMat = new GpuMat();
|
||||
Mat jcvMat = new Mat();
|
||||
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);
|
||||
}
|
||||
gpuMat.upload(jcvMat);
|
||||
gpuMats.put(mat.getNativeObjAddr(), gpuMat);
|
||||
mats.put(mat.getNativeObjAddr(), jcvMat);
|
||||
states.put(mat.getNativeObjAddr(), State.GPU);
|
||||
return gpuMat;
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
gpuMats.values().forEach(GpuMat::close);
|
||||
mats.values().forEach(Mat::close);
|
||||
gpuMats.clear();
|
||||
mats.clear();
|
||||
states.clear();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,12 @@ public class TimingUtil {
|
||||
return unit.convert(end - start, TimeUnit.NANOSECONDS);
|
||||
}
|
||||
|
||||
public static TimeDto testPerformance(Runnable setup, Runnable toTest, int iterations) {
|
||||
public static TimeDto testPerformance(Runnable setup, Runnable toTest, Runnable cleanUp, int iterations) {
|
||||
long[] times = new long[iterations];
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
setup.run();
|
||||
times[i] = measureTime(toTest, TimeUnit.MILLISECONDS);
|
||||
cleanUp.run();
|
||||
}
|
||||
double avg = 0;
|
||||
for (int i = 0; i < times.length; i++) {
|
||||
|
||||
@@ -11,6 +11,8 @@ public interface GpuTask extends Runnable {
|
||||
@Override
|
||||
void run();
|
||||
|
||||
void cleanUp();
|
||||
|
||||
@Override
|
||||
String toString();
|
||||
}
|
||||
|
||||
+29
-1
@@ -2,9 +2,13 @@ package de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_cudaimgproc;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.bytedeco.opencv.opencv_cudaimgproc.CannyEdgeDetector;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.internal.MatPool;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.CpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.GpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.Task;
|
||||
@@ -47,7 +51,31 @@ public class Canny implements Task {
|
||||
|
||||
@Override
|
||||
public GpuTask toGpuTask() {
|
||||
return null;
|
||||
return new GpuTask() {
|
||||
|
||||
private GpuMat gpuImage;
|
||||
|
||||
private GpuMat gpuEdges;
|
||||
|
||||
private CannyEdgeDetector detector;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.detector.detect(this.gpuImage, this.gpuEdges);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
this.detector.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+29
-1
@@ -2,9 +2,12 @@ package de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bytedeco.opencv.global.opencv_cudaimgproc;
|
||||
import org.bytedeco.opencv.opencv_core.GpuMat;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import de.unidue.nes.gpu.testframework.internal.MatPool;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.CpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.GpuTask;
|
||||
import de.unidue.nes.gpu.testframework.internal.tasks.Task;
|
||||
@@ -68,7 +71,32 @@ public class CvtColor implements Task {
|
||||
|
||||
@Override
|
||||
public GpuTask toGpuTask() {
|
||||
return null;
|
||||
return new GpuTask() {
|
||||
|
||||
private GpuMat gpuSrc;
|
||||
|
||||
private GpuMat gpuDst;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (CvtColor.this.numParams == 3) {
|
||||
opencv_cudaimgproc.cvtColor(this.gpuSrc, this.gpuDst, CvtColor.this.code);
|
||||
} else if (CvtColor.this.numParams == 4) {
|
||||
opencv_cudaimgproc.cvtColor(this.gpuSrc, this.gpuDst, CvtColor.this.code, CvtColor.this.dstCn, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.gpuSrc = MatPool.getGpuMat(CvtColor.this.src);
|
||||
this.gpuDst = MatPool.getGpuMat(CvtColor.this.dst);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
// nothing to clean up
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user