added automatic table generation

This commit is contained in:
2020-03-12 15:29:53 +01:00
parent 2ea7b68e78
commit ea79a9fd8c
7 changed files with 155 additions and 16 deletions
@@ -14,7 +14,6 @@ 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;
/**
@@ -46,6 +45,8 @@ public class GpuTestSystem {
public void run() {
List<Task> tasks = getTasks();
GpuEventBus.INSTANCE.post(() -> tasks);
GpuEventBus.INSTANCE.stopListening();
LOG.info("Running tasks so dlls get loaded");
muteLogger();
@@ -59,6 +60,7 @@ public class GpuTestSystem {
LOG.info("Now starting tasks to measure performance");
// now run tasks to get the runtime performance
GpuEventBus.INSTANCE.startListening();
tasks.forEach(Task::runTask);
GpuEventBus.INSTANCE.post(ApplicationFinishedEvent.EVENT);
@@ -79,7 +81,7 @@ public class GpuTestSystem {
private List<Task> getTasks() {
return Arrays.asList(new UploadDownloadTask(), new PageLockedUploadDownloadTask(), new PixelWiseTask());
return Arrays.asList(new UploadDownloadTask(), new PageLockedUploadDownloadTask());
}
}
@@ -1,7 +1,23 @@
package de.unidue.nes.gpu.events;
import static de.unidue.nes.gpu.events.TaskType.GPU_COMBINED;
import static de.unidue.nes.gpu.events.TaskType.GPU_DOWNLOAD;
import static de.unidue.nes.gpu.events.TaskType.GPU_UPLOAD;
import static de.unidue.nes.gpu.events.TaskType.PAGE_LOCKED_GPU_COMBINED;
import static de.unidue.nes.gpu.events.TaskType.PAGE_LOCKED_GPU_DOWNLOAD;
import static de.unidue.nes.gpu.events.TaskType.PAGE_LOCKED_GPU_UPLOAD;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opencv.core.Size;
import com.google.common.eventbus.DeadEvent;
import com.google.common.eventbus.Subscribe;
@@ -13,8 +29,12 @@ public class EventHandler {
private static final Logger LOG = LogManager.getLogger(EventHandler.class);
private List<TaskResultEvent> events;
public EventHandler() {
GpuEventBus.INSTANCE.register(this);
this.events = new ArrayList<>();
}
@Subscribe
@@ -26,13 +46,72 @@ public class EventHandler {
@Subscribe
public void onApplicationFinished(ApplicationFinishedEvent event) {
LOG.info("All tasks are finished");
// TODO: save results, maybe export as csv
printUploadDownloadStuff();
}
private void printUploadDownloadStuff() {
System.out.println("uploading downloading table:");
final boolean addHorizontalLines = true;
List<TaskResultEvent> uploadDownloadEvents = this.events.stream()
.filter(ev -> Arrays.asList(GPU_UPLOAD, GPU_DOWNLOAD, GPU_COMBINED, PAGE_LOCKED_GPU_UPLOAD, PAGE_LOCKED_GPU_DOWNLOAD, PAGE_LOCKED_GPU_COMBINED)
.contains(ev.getType()))
.collect(Collectors.toList());
List<Size> sizes = uploadDownloadEvents.stream().map(TaskResultEvent::getImageSize).distinct().collect(Collectors.toList());
if (sizes.size() != 10) {
throw new RuntimeException(sizes.size() + "");
}
StringBuilder headline = new StringBuilder();
StringBuilder content = new StringBuilder();
headline.append("Task");
for (int i = 0; i < 5; i++) {
headline.append(" & ");
headline.append(sizes.get(i).toString());
}
headline.append(" \\\\\n");
for (int i = 5; i < 10; i++) {
headline.append(" & ");
headline.append(sizes.get(i).toString());
}
headline.append("\\\\\n\\hline\n");
Map<TaskType, List<TaskResultEvent>> mappedEvents = new EnumMap<>(TaskType.class);
for (TaskResultEvent event : uploadDownloadEvents) {
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(" & ");
content.append(entry.getValue().get(i).getExecutionTime());
content.append(" ms");
}
content.append(" \\\\\n");
if (addHorizontalLines) {
content.append("\\hline\n");
}
});
if (!addHorizontalLines) {
content.append("\\hline\n");
}
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
public void onTaskResult(TaskResultEvent event) {
LOG.debug(() -> "Task finished: " + event.getDescription());
// TODO: save result
this.events.add(event);
}
@Subscribe
@@ -15,6 +15,7 @@ public class GpuEventBus {
}
private EventBus eventBus;
private boolean listening = true;
private GpuEventBus() {
this.eventBus = new EventBus();
@@ -25,14 +26,28 @@ public class GpuEventBus {
}
public void post(Object event) {
this.eventBus.post(event);
if (this.listening) {
this.eventBus.post(event);
}
}
public void post(ApplicationStartupEvent event) {
this.eventBus.post(event);
if (this.listening) {
this.eventBus.post(event);
}
}
public void post(ApplicationFinishedEvent event) {
this.eventBus.post(event);
if (this.listening) {
this.eventBus.post(event);
}
}
public void stopListening() {
this.listening = false;
}
public void startListening() {
this.listening = true;
}
}
@@ -2,6 +2,8 @@ package de.unidue.nes.gpu.events;
import java.util.concurrent.TimeUnit;
import org.opencv.core.Size;
/**
* @author Roch P., Shahbaz Nejad B.
*/
@@ -15,15 +17,21 @@ public class TaskResultEvent {
private int executionRuns;
public TaskResultEvent(long executionTime, String description, int executionRuns) {
this(executionTime, TimeUnit.MILLISECONDS, description, executionRuns);
private Size imageSize;
private TaskType type;
public TaskResultEvent(long executionTime, String description, int executionRuns, Size imageSize, TaskType type) {
this(executionTime, TimeUnit.MILLISECONDS, description, executionRuns, imageSize, type);
}
public TaskResultEvent(long executionTime, TimeUnit unit, String description, int executionRuns) {
private TaskResultEvent(long executionTime, TimeUnit unit, String description, int executionRuns, Size imageSize, TaskType type) {
this.executionTime = executionTime;
this.unit = unit;
this.description = description;
this.executionRuns = executionRuns;
this.imageSize = imageSize;
this.type = type;
}
/**
@@ -53,4 +61,12 @@ public class TaskResultEvent {
public int getExecutionRuns() {
return this.executionRuns;
}
public Size getImageSize() {
return this.imageSize;
}
public TaskType getType() {
return this.type;
}
}
@@ -0,0 +1,20 @@
package de.unidue.nes.gpu.events;
/**
* @author Roch P., Shahbaz Nejad B.
*/
public enum TaskType {
GPU_UPLOAD("uploading"), GPU_DOWNLOAD("downloading"), GPU_COMBINED("combined"), //
PAGE_LOCKED_GPU_UPLOAD("page-locked uploading"), PAGE_LOCKED_GPU_DOWNLOAD("page-locked downloading"),
PAGE_LOCKED_GPU_COMBINED("page-locked combined");
private String title;
private TaskType(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
@@ -13,6 +13,7 @@ import org.bytedeco.opencv.opencv_core.Mat;
import de.unidue.nes.gpu.GpuTestSystem;
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.Task;
import de.unidue.nes.gpu.utils.ImageLoader;
import de.unidue.nes.gpu.utils.MatConverter;
@@ -49,7 +50,8 @@ public class PageLockedUploadDownloadTask implements Task {
try (GpuMat gpuMat = new GpuMat()) {
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));
GpuEventBus.INSTANCE
.post(new TaskResultEvent(time, "uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.PAGE_LOCKED_GPU_UPLOAD));
}
}
@@ -58,7 +60,8 @@ public class PageLockedUploadDownloadTask implements Task {
gpuMat.upload(m);
long time = TimingService.measureTime(() -> 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));
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m),
TaskType.PAGE_LOCKED_GPU_DOWNLOAD));
}
}
@@ -69,7 +72,8 @@ public class PageLockedUploadDownloadTask implements Task {
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));
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "uploading and downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m),
TaskType.PAGE_LOCKED_GPU_COMBINED));
}
}
@@ -12,6 +12,7 @@ import org.bytedeco.opencv.opencv_core.Mat;
import de.unidue.nes.gpu.GpuTestSystem;
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.Task;
import de.unidue.nes.gpu.utils.ImageLoader;
import de.unidue.nes.gpu.utils.MatConverter;
@@ -47,7 +48,7 @@ public class UploadDownloadTask implements Task {
try (GpuMat gpuMat = new GpuMat()) {
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));
GpuEventBus.INSTANCE.post(new TaskResultEvent(time, "uploading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_UPLOAD));
}
}
@@ -56,7 +57,8 @@ public class UploadDownloadTask implements Task {
gpuMat.upload(m);
long time = TimingService.measureTime(() -> 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));
GpuEventBus.INSTANCE
.post(new TaskResultEvent(time, "downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_DOWNLOAD));
}
}
@@ -67,7 +69,8 @@ public class UploadDownloadTask implements Task {
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));
GpuEventBus.INSTANCE.post(
new TaskResultEvent(time, "uploading and downloading Mat of size " + getSizeOfMat(m), ITERATIONS, getSizeOfMat(m), TaskType.GPU_COMBINED));
}
}