adjusted time measurements

This commit is contained in:
2020-03-13 11:28:44 +01:00
parent 2477a941a3
commit 45ae7f4e73
11 changed files with 152 additions and 46 deletions
@@ -13,7 +13,9 @@ import de.unidue.nes.gpu.events.ApplicationFinishedEvent;
import de.unidue.nes.gpu.events.GpuEventBus;
import de.unidue.nes.gpu.initialization.Initializer;
import de.unidue.nes.gpu.tasks.Task;
import de.unidue.nes.gpu.tasks.taskdefinitions.PageLockedUploadDownloadTask;
import de.unidue.nes.gpu.tasks.taskdefinitions.PixelWiseTask;
import de.unidue.nes.gpu.tasks.taskdefinitions.UploadDownloadTask;
/**
* Class containing the main method of this project.
@@ -49,11 +51,12 @@ public class GpuTestSystem {
LOG.info("Running tasks so dlls get loaded");
muteLogger();
// run tasks for the first time if some dlls are loaded lazily
tasks.forEach(Task::runTask);
// run tasks for the first time to load dlls and initialize everything
// tasks.forEach(Task::runTask);
// run again to be sure that nothing is loaded anymore
tasks.forEach(Task::runTask);// I'm not sure if this is necessary
// 1 run should already be sufficient
// tasks.forEach(Task::runTask);// I'm not sure if this is necessary
resetLogger();
@@ -80,7 +83,11 @@ public class GpuTestSystem {
private List<Task> getTasks() {
return Arrays.asList(new PixelWiseTask());
return Arrays.asList(//
new UploadDownloadTask(), //
new PageLockedUploadDownloadTask(), //
new PixelWiseTask()//
);
}
}
@@ -31,6 +31,10 @@ import com.google.common.eventbus.Subscribe;
*/
public class EventHandler {
private static final String HLINE_LINEBREAK = "\\hline\n";
private static final String LATEX_TABLE_END_OF_LINE = " \\\\\n";
private static final Logger LOG = LogManager.getLogger(EventHandler.class);
private List<TaskResultEvent> events;
@@ -64,61 +68,91 @@ public class EventHandler {
}
private void printTable(String tablename, TaskType[] types) {
System.out.println(tablename + ": ");
final boolean addHorizontalLines = true;
List<TaskResultEvent> eventList = this.events.stream().filter(ev -> Arrays.asList(types).contains(ev.getType())).collect(Collectors.toList());
if (eventList.isEmpty()) {
return;
}
System.out.println(tablename + ": ");
List<Size> sizes = eventList.stream().map(TaskResultEvent::getImageSize).distinct().collect(Collectors.toList());
if (sizes.size() != 10) {// currently only 10 elements
if (sizes.size() != 10 && sizes.size() != 5) {// currently only 5 or 10 elements
throw new RuntimeException(sizes.size() + "");
}
StringBuilder headline = new StringBuilder();
StringBuilder content = new StringBuilder();
buildDividedTable(addHorizontalLines, eventList, sizes, headline, content);
StringBuilder table = new StringBuilder();
table.append("\\begin{tabular}{|l|rr|rr|rr|rr|rr|}\n\\hline\n");
table.append(headline.toString());
table.append(content.toString());
table.append("\\end{tabular}");
System.out.println(table.toString());
}
private void buildDividedTable(boolean addHorizontalLines, List<TaskResultEvent> eventList, List<Size> sizes, StringBuilder headline,
StringBuilder content) {
headline.append("Task");
for (int i = 0; i < 5; i++) {
headline.append(" & ");
headline.append("\\multicolumn{2}{c|}{");
headline.append(sizes.get(i).toString());
headline.append("}");
}
headline.append(" \\\\\n");
for (int i = 5; i < 10; i++) {
if (sizes.size() == 10) {
headline.append(LATEX_TABLE_END_OF_LINE);
for (int i = 5; i < 10; i++) {
headline.append(" & ");
headline.append("\\multicolumn{2}{c|}{");
headline.append(sizes.get(i).toString());
headline.append("}");
}
}
headline.append(LATEX_TABLE_END_OF_LINE);
for (int i = 0; i < 5; i++) {
headline.append(" & ");
headline.append(sizes.get(i).toString());
headline.append("\\multicolumn{1}{r}{");
headline.append("$\\overline{time}$");
headline.append("}");
headline.append(" & ");
headline.append("\\multicolumn{1}{r|}{");
headline.append("$\\sigma$");
headline.append("}");
}
headline.append("\\\\\n\\hline\n");
Map<TaskType, List<TaskResultEvent>> mappedEvents = new EnumMap<>(TaskType.class);
for (TaskResultEvent event : eventList) {
mappedEvents.computeIfAbsent(event.getType(), k -> new ArrayList<>()).add(event);
}
mappedEvents.entrySet().stream().sorted(Comparator.comparingInt(entry -> entry.getKey().ordinal())).forEach(entry -> {
content.append(entry.getKey().getTitle());
entry.getValue().sort(Comparator.comparingDouble(evt -> evt.getImageSize().area()));
for (int i = 0; i < 5; i++) {
content.append(" & ");
content.append(entry.getValue().get(i).getExecutionTime());
content.append(" ms");
}
content.append(" \\\\\n");
for (int i = 5; i < 10; i++) {
content.append((int) entry.getValue().get(i).getTimeDto().avgTime);
content.append(" & ");
content.append(entry.getValue().get(i).getExecutionTime());
content.append(" ms");
content.append((int) entry.getValue().get(i).getTimeDto().stdAbweichung);
}
content.append(" \\\\\n");
if (sizes.size() == 10) {
content.append(LATEX_TABLE_END_OF_LINE);
for (int i = 5; i < 10; i++) {
content.append(" & ");
content.append((int) entry.getValue().get(i).getTimeDto().avgTime);
content.append(" & ");
content.append((int) entry.getValue().get(i).getTimeDto().stdAbweichung);
}
}
content.append(LATEX_TABLE_END_OF_LINE);
if (addHorizontalLines) {
content.append("\\hline\n");
content.append(HLINE_LINEBREAK);
}
});
if (!addHorizontalLines) {
content.append("\\hline\n");
content.append(HLINE_LINEBREAK);
}
StringBuilder table = new StringBuilder();
table.append("\\begin{tabular}{|l|r|r|r|r|r|}\n\\hline\n");
table.append(headline.toString());
table.append(content.toString());
table.append("\\end{tabular}");
System.out.println(table.toString());
}
@Subscribe
@@ -4,6 +4,8 @@ import java.util.concurrent.TimeUnit;
import org.opencv.core.Size;
import de.unidue.nes.gpu.utils.TimeDto;
/**
* @author Roch P., Shahbaz Nejad B.
*/
@@ -21,10 +23,17 @@ public class TaskResultEvent {
private TaskType type;
private TimeDto timeDto;
public TaskResultEvent(long executionTime, String description, int executionRuns, Size imageSize, TaskType type) {
this(executionTime, TimeUnit.MILLISECONDS, description, executionRuns, imageSize, type);
}
public TaskResultEvent(String description, int executionRuns, Size imageSize, TaskType type, TimeDto time) {
this(0, TimeUnit.MILLISECONDS, description, executionRuns, imageSize, type);
this.timeDto = time;
}
private TaskResultEvent(long executionTime, TimeUnit unit, String description, int executionRuns, Size imageSize, TaskType type) {
this.executionTime = executionTime;
this.unit = unit;
@@ -34,6 +43,10 @@ public class TaskResultEvent {
this.type = type;
}
public TaskResultEvent(TimeDto time, String description, int iterations, Size imageSize, TaskType type) {
this(description, iterations, imageSize, type, time);
}
/**
* @return the executionTime
*/
@@ -69,4 +82,8 @@ public class TaskResultEvent {
public TaskType getType() {
return this.type;
}
public TimeDto getTimeDto() {
return this.timeDto;
}
}
@@ -9,6 +9,7 @@ import de.unidue.nes.gpu.events.GpuEventBus;
import de.unidue.nes.gpu.events.TaskResultEvent;
import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.taskdefinitions.PixelWiseTask;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -22,10 +23,10 @@ public class CPUColoring {
LOG.debug(() -> "grayscaling an image of size " + image.size().toString());
Mat gray = new Mat();
long time = TimingService.measureTime(() -> Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY), PixelWiseTask.TEST_RUNS);
TimeDto time = TimingService.testPerformance(() -> Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY), PixelWiseTask.TEST_RUNS);
LOG.info(() -> "Grayscaling the image of size " + image.size() + " took " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "", PixelWiseTask.TEST_RUNS, image.size(), TaskType.CPU_GRAYSCALE));
GpuEventBus.INSTANCE.post(new TaskResultEvent("", PixelWiseTask.TEST_RUNS, image.size(), TaskType.CPU_GRAYSCALE, time));
}
@@ -9,6 +9,7 @@ import de.unidue.nes.gpu.events.GpuEventBus;
import de.unidue.nes.gpu.events.TaskResultEvent;
import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.taskdefinitions.PixelWiseTask;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -21,10 +22,10 @@ public class CPUThresholding {
public void threshold(Mat mat) {
Mat res = new Mat();
long time = TimingService.measureTime(() -> Imgproc.threshold(mat, res, 128, 255, Imgproc.THRESH_BINARY), PixelWiseTask.TEST_RUNS);
TimeDto time = TimingService.testPerformance(() -> Imgproc.threshold(mat, res, 128, 255, Imgproc.THRESH_BINARY), PixelWiseTask.TEST_RUNS);
LOG.info(() -> "thresholding image from size " + mat.size() + " took " + time + "ms.");
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "", PixelWiseTask.TEST_RUNS, mat.size(), TaskType.CPU_THRESHOLD));
GpuEventBus.INSTANCE.post(new TaskResultEvent("", PixelWiseTask.TEST_RUNS, mat.size(), TaskType.CPU_THRESHOLD, time));
// Mat show = new Mat();
// Imgproc.resize(mat, show, new Size(1000, 662));
// Imgproc.resize(res, res, new Size(1000, 662));
@@ -12,6 +12,7 @@ import de.unidue.nes.gpu.events.TaskResultEvent;
import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.taskdefinitions.PixelWiseTask;
import de.unidue.nes.gpu.utils.MatConverter;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -30,16 +31,16 @@ public class GPUColoring {
public void grayScale(Mat image) {
LOG.debug(() -> "grayscaling an image of size " + image.size().toString());
long time;
TimeDto time;
try (GpuMat gpuImg = new GpuMat();
GpuMat grayImg = new GpuMat();
org.bytedeco.opencv.opencv_core.Mat javacvMat = new org.bytedeco.opencv.opencv_core.Mat()) {
this.converter.convert(image, javacvMat);
gpuImg.upload(javacvMat);
time = TimingService.measureTime(() -> opencv_cudaimgproc.cvtColor(gpuImg, grayImg, opencv_imgproc.COLOR_BGR2GRAY), PixelWiseTask.TEST_RUNS);
time = TimingService.testPerformance(() -> opencv_cudaimgproc.cvtColor(gpuImg, grayImg, opencv_imgproc.COLOR_BGR2GRAY), PixelWiseTask.TEST_RUNS);
}
LOG.info(() -> "Grayscaling the image of size " + image.size() + " took " + time + "ms");
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "", PixelWiseTask.TEST_RUNS, image.size(), TaskType.GPU_GRAYSCALE));
GpuEventBus.INSTANCE.post(new TaskResultEvent("", PixelWiseTask.TEST_RUNS, image.size(), TaskType.GPU_GRAYSCALE, time));
}
}
@@ -12,6 +12,7 @@ import de.unidue.nes.gpu.events.TaskResultEvent;
import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.taskdefinitions.PixelWiseTask;
import de.unidue.nes.gpu.utils.MatConverter;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -33,10 +34,11 @@ public class GPUThresholding {
this.converter.convert(mat, javacvMat);
src.upload(javacvMat);
long time = TimingService.measureTime(() -> opencv_cudaarithm.threshold(src, dst, 128, 255, opencv_imgproc.THRESH_BINARY), PixelWiseTask.TEST_RUNS);
TimeDto time = TimingService.testPerformance(() -> opencv_cudaarithm.threshold(src, dst, 128, 255, opencv_imgproc.THRESH_BINARY),
PixelWiseTask.TEST_RUNS);
LOG.info(() -> "thresholding image from size " + mat.size() + " took " + time + "ms.");
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "", PixelWiseTask.TEST_RUNS, mat.size(), TaskType.GPU_THRESHOLD));
GpuEventBus.INSTANCE.post(new TaskResultEvent("", PixelWiseTask.TEST_RUNS, mat.size(), TaskType.GPU_THRESHOLD, time));
// dst.download(javacvMat);
// opencv_highgui.imshow("GPU result", javacvMat);
// opencv_highgui.waitKey();
@@ -17,6 +17,7 @@ import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.Task;
import de.unidue.nes.gpu.utils.ImageLoader;
import de.unidue.nes.gpu.utils.MatConverter;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -48,26 +49,26 @@ public class PageLockedUploadDownloadTask implements Task {
private void testUpload(Mat m) {
try (GpuMat gpuMat = new GpuMat()) {
long time = TimingService.measureTime(() -> gpuMat.upload(m), ITERATIONS);
TimeDto time = TimingService.testPerformance(() -> gpuMat.upload(m), ITERATIONS);
LOG.info(() -> "uploading" + MAT_OF_SIZE + getSizeOfMat(m) + TOOK + time + MS);
GpuEventBus.INSTANCE
.post(new TaskResultEvent(time, "uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.PAGE_LOCKED_GPU_UPLOAD));
.post(new TaskResultEvent("uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.PAGE_LOCKED_GPU_UPLOAD, time));
}
}
private void testDownload(Mat m) {
try (GpuMat gpuMat = new GpuMat(); Mat res = new Mat()) {
gpuMat.upload(m);
long time = TimingService.measureTime(() -> gpuMat.download(res), ITERATIONS);
TimeDto time = TimingService.testPerformance(() -> gpuMat.download(res), ITERATIONS);
LOG.info(() -> "downloading" + MAT_OF_SIZE + getSizeOfMat(m) + TOOK + time + MS);
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m),
TaskType.PAGE_LOCKED_GPU_DOWNLOAD));
GpuEventBus.INSTANCE.post(
new TaskResultEvent(time, "downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.PAGE_LOCKED_GPU_DOWNLOAD));
}
}
private void testUploadAndDownload(Mat m) {
try (GpuMat gpuMat = new GpuMat()) {
long time = TimingService.measureTime(() -> {
TimeDto time = TimingService.testPerformance(() -> {
gpuMat.upload(m);
gpuMat.download(m);
}, ITERATIONS);
@@ -16,6 +16,7 @@ import de.unidue.nes.gpu.events.TaskType;
import de.unidue.nes.gpu.tasks.Task;
import de.unidue.nes.gpu.utils.ImageLoader;
import de.unidue.nes.gpu.utils.MatConverter;
import de.unidue.nes.gpu.utils.TimeDto;
import de.unidue.nes.gpu.utils.TimingService;
/**
@@ -46,31 +47,32 @@ public class UploadDownloadTask implements Task {
private void testUpload(Mat m) {
try (GpuMat gpuMat = new GpuMat()) {
long time = TimingService.measureTime(() -> gpuMat.upload(m), ITERATIONS);
TimeDto time = TimingService.testPerformance(() -> gpuMat.upload(m), ITERATIONS);
// long time = TimingService.measureTime(() -> gpuMat.upload(m), ITERATIONS);
LOG.info(() -> "uploading" + MAT_OF_SIZE + getSizeOfMat(m) + TOOK + time + MS);
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_UPLOAD));
GpuEventBus.INSTANCE.post(new TaskResultEvent("uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_UPLOAD, time));
}
}
private void testDownload(Mat m) {
try (GpuMat gpuMat = new GpuMat(); Mat res = new Mat()) {
gpuMat.upload(m);
long time = TimingService.measureTime(() -> gpuMat.download(res), ITERATIONS);
TimeDto time = TimingService.testPerformance(() -> gpuMat.download(res), ITERATIONS);
LOG.info(() -> "downloading" + MAT_OF_SIZE + getSizeOfMat(m) + TOOK + time + MS);
GpuEventBus.INSTANCE
.post(new TaskResultEvent(time, "downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_DOWNLOAD));
.post(new TaskResultEvent("downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_DOWNLOAD, time));
}
}
private void testUploadAndDownload(Mat m) {
try (GpuMat gpuMat = new GpuMat()) {
long time = TimingService.measureTime(() -> {
TimeDto time = TimingService.testPerformance(() -> {
gpuMat.upload(m);
gpuMat.download(m);
}, ITERATIONS);
LOG.info(() -> "uploading and downloading" + MAT_OF_SIZE + getSizeOfMat(m) + TOOK + time + MS);
GpuEventBus.INSTANCE.post(
new TaskResultEvent(time, "uploading and downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_COMBINED));
new TaskResultEvent("uploading and downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_COMBINED, time));
}
}
@@ -0,0 +1,16 @@
package de.unidue.nes.gpu.utils;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public class TimeDto {
public double avgTime;
public double varTime;// sigma²
public double stdAbweichung;// sqrt(varTime)
@Override
public String toString() {
return this.avgTime + "";
}
}
@@ -22,4 +22,28 @@ public class TimingService {
public static long measureTime(Runnable run, int iterations) {
return measureTime(run, iterations, TimeUnit.MILLISECONDS);
}
public static TimeDto testPerformance(Runnable run, int iterations) {
final int iterations2 = 10;
long[] times = new long[iterations2];
for (int i = 0; i < iterations2; i++) {
times[i] = measureTime(run, iterations);
}
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;
}
}