added basic CPU test functionality

This commit is contained in:
2020-04-29 15:23:58 +02:00
parent 25cdc5bd71
commit e1c3d2750a
10 changed files with 448 additions and 0 deletions
@@ -0,0 +1,31 @@
package de.unidue.nes.gpu.testframework;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import de.unidue.nes.gpu.testframework.opencv.Imgproc;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class Main {
static {
System.loadLibrary("opencv_java411");
}
// main method only used to test
public static void main(String[] args) {
TestUtils utils = new TestUtils();
utils.setCommandPipeline(mat -> {
Mat edges = new Mat();
Imgproc.cvtColor(mat, mat, org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(mat, edges, 15, 45);
});
utils.setInputImage(Imgcodecs.imread("src/main/resources/car.jpg"));
utils.initializeCommands();
utils.warmupCPUTests(10);
utils.runCPUTests(100);
}
}
@@ -0,0 +1,145 @@
package de.unidue.nes.gpu.testframework;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import org.opencv.core.Mat;
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;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class TestUtils {// TODO: reduce code duplication
private Consumer<Mat> commands;
private Mat originalInput;
private Mat input;
private boolean initialized;
public TestUtils() {
this.initialized = false;
}
/**
* @param commands
*/
public void setCommandPipeline(Consumer<Mat> commands) {
if (this.initialized) {
throw new RuntimeException();
}
this.commands = commands;
}
public void setInputImage(Mat input) {
if (this.initialized) {
throw new RuntimeException();
}
this.originalInput = input;
this.input = new Mat();
this.originalInput.copyTo(this.input);
}
public void initializeCommands() {
if (this.initialized) {
throw new RuntimeException();
}
Objects.requireNonNull(this.commands);
Objects.requireNonNull(this.originalInput);
Objects.requireNonNull(this.input);
this.commands.accept(this.input);
this.initialized = true;
}
public void warmupCPUTests(int iterations) {
if (!this.initialized) {
throw new RuntimeException();
}
for (int i = 0; i < iterations; i++) {
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
this.originalInput.copyTo(this.input);
for (Task t : tasks) {
t.toCpuTask().run();
}
}
}
public void warmupGPUTests(int iterations) {
if (!this.initialized) {
throw new RuntimeException();
}
warmupUploadAndDownload();
for (int i = 0; i < iterations; i++) {
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
this.originalInput.copyTo(this.input);
for (Task t : tasks) {
try (GpuTask task = t.toGpuTask()) {
task.init();
task.run();
}
}
}
}
private void warmupUploadAndDownload() {
// TODO Auto-generated method stub
}
public void runCPUTests(int iterations) {
if (!this.initialized) {
throw new RuntimeException();
}
List<Task> tasks = TestPipeline.INSTANCE.getPipeline();
for (int i = 0; i < tasks.size(); i++) {
testCPUTaskInIsolation(tasks, i, iterations);
}
testCPUPipeline(tasks, iterations);
}
private void testCPUPipeline(List<Task> tasks, int iterations) {
Runnable setup = () -> this.originalInput.copyTo(this.input);
Runnable toTest = () -> tasks.forEach(t -> t.toCpuTask().run());
TimingUtil.testPerformance(setup, toTest, iterations);
}
private void testCPUTaskInIsolation(List<Task> tasks, int index, int iterations) {
Runnable setup = () -> {
this.originalInput.copyTo(this.input);
for (int j = 0; j < index; j++) {
tasks.get(j).toCpuTask().run();
}
};
TimeDto time = TimingUtil.testPerformance(setup, tasks.get(index).toCpuTask(), iterations);
System.out.println(tasks.get(index).toCpuTask() + ":" + time);
}
public void runGPUTests(int iterations) {
}
private void testUploadAndDownload() {
// TODO Auto-generated method stub
}
private void testGPUPipeline(List<Task> tasks, int iterations) {
// TODO Auto-generated method stub
}
private void testGPUTaskInIsolation(List<Task> tasks, int index, int iterations) {
}
}
@@ -0,0 +1,27 @@
package de.unidue.nes.gpu.testframework.internal;
import java.util.ArrayList;
import java.util.List;
import de.unidue.nes.gpu.testframework.internal.tasks.Task;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class TestPipeline {
public static final TestPipeline INSTANCE = new TestPipeline();
private TestPipeline() {
}
private List<Task> pipeline = new ArrayList<>();
public void addTask(Task task) {
this.pipeline.add(task);
}
public List<Task> getPipeline() {
return new ArrayList<>(this.pipeline);
}
}
@@ -0,0 +1,47 @@
package de.unidue.nes.gpu.testframework.internal;
import java.util.concurrent.TimeUnit;
import de.unidue.nes.gpu.utils.TimeDto;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class TimingUtil {
private TimingUtil() {
}
private static long measureTime(Runnable run, TimeUnit unit) {
long start = System.nanoTime();
run.run();
long end = System.nanoTime();
return unit.convert(end - start, TimeUnit.NANOSECONDS);
}
public static TimeDto testPerformance(Runnable setup, Runnable toTest, int iterations) {
long[] times = new long[iterations];
for (int i = 0; i < iterations; i++) {
setup.run();
times[i] = measureTime(toTest, TimeUnit.MILLISECONDS);
}
double avg = 0;
for (int i = 0; i < times.length; i++) {
avg += times[i];
}
avg /= times.length;
double var = 0;
for (int i = 0; i < times.length; i++) {
var += (times[i] - avg) * (times[i] - avg);
}
var /= times.length;
double stdAbw = Math.sqrt(var);
TimeDto dto = new TimeDto();
dto.avgTime = avg;
dto.varTime = var;
dto.stdAbweichung = stdAbw;
return dto;
}
}
@@ -0,0 +1,14 @@
package de.unidue.nes.gpu.testframework.internal.tasks;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public interface CpuTask extends Runnable {
@Override
void run();
@Override
String toString();
}
@@ -0,0 +1,16 @@
package de.unidue.nes.gpu.testframework.internal.tasks;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public interface GpuTask extends Runnable {
void init();
@Override
void run();
@Override
String toString();
}
@@ -0,0 +1,11 @@
package de.unidue.nes.gpu.testframework.internal.tasks;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public interface Task {
CpuTask toCpuTask();
GpuTask toGpuTask();
}
@@ -0,0 +1,53 @@
package de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc;
import java.util.Objects;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
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;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class Canny implements Task {
private Mat image;
private Mat edges;
private double threshold1;
private double threshold2;
public Canny(Mat image, Mat edges, double threshold1, double threshold2) {
this.image = image;
this.edges = edges;
this.threshold1 = threshold1;
this.threshold2 = threshold2;
}
@Override
public CpuTask toCpuTask() {
return new CpuTask() {
@Override
public void run() {
Imgproc.Canny(Canny.this.image, Canny.this.edges, Canny.this.threshold1, Canny.this.threshold2);
}
@Override
public String toString() {
return "Imgproc.Canny(" + Objects.toString(Canny.this.image) + ", " + Objects.toString(Canny.this.edges) + ", " + Canny.this.threshold1 + ", "
+ Canny.this.threshold2 + ")";
}
};
}
@Override
public GpuTask toGpuTask() {
return null;
}
}
@@ -0,0 +1,74 @@
package de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc;
import java.util.Objects;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
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;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class CvtColor implements Task {
private final int numParams;
private Mat src;
private Mat dst;
private int code;
private int dstCn;
public CvtColor(Mat src, Mat dst, int code) {
this.src = src;
this.dst = dst;
this.code = code;
this.numParams = 3;
}
public CvtColor(Mat src, Mat dst, int code, int dstCn) {
this.src = src;
this.dst = dst;
this.code = code;
this.dstCn = dstCn;
this.numParams = 4;
}
@Override
public CpuTask toCpuTask() {
return new CpuTask() {
@Override
public void run() {
if (CvtColor.this.numParams == 3) {
Imgproc.cvtColor(CvtColor.this.src, CvtColor.this.dst, CvtColor.this.code);
} else if (CvtColor.this.numParams == 4) {
Imgproc.cvtColor(CvtColor.this.src, CvtColor.this.dst, CvtColor.this.code, CvtColor.this.dstCn);
}
}
@Override
public String toString() {
if (CvtColor.this.numParams == 3) {
return "Imgproc.cvtColor(" + Objects.toString(CvtColor.this.src) + ", " + Objects.toString(CvtColor.this.dst) + ", " + CvtColor.this.code
+ ")";
} else if (CvtColor.this.numParams == 4) {
return "Imgproc.cvtColor(" + Objects.toString(CvtColor.this.src) + ", " + Objects.toString(CvtColor.this.dst) + ", " + CvtColor.this.code
+ ", " + CvtColor.this.dstCn + ")";
}
return "";
}
};
}
@Override
public GpuTask toGpuTask() {
return null;
}
}
@@ -0,0 +1,30 @@
package de.unidue.nes.gpu.testframework.opencv;
import org.opencv.core.Mat;
import de.unidue.nes.gpu.testframework.internal.TestPipeline;
import de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc.Canny;
import de.unidue.nes.gpu.testframework.internal.tasks.model.imgproc.CvtColor;
/**
* @author Roch P., Shahbaz Nejad B.
*
*/
public class Imgproc {
private Imgproc() {
}
public static void cvtColor(Mat src, Mat dst, int code) {
TestPipeline.INSTANCE.addTask(new CvtColor(src, dst, code));
}
public static void cvtColor(Mat src, Mat dst, int code, int dstCn) {
TestPipeline.INSTANCE.addTask(new CvtColor(src, dst, code, dstCn));
}
public static void Canny(Mat image, Mat edges, double threshold1, double threshold2) {
TestPipeline.INSTANCE.addTask(new Canny(image, edges, threshold1, threshold2));
}
}