aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-05-16 00:07:29 +0200
committerAdrian Kummerlaender2016-05-16 00:07:29 +0200
commitd87a9b0960ddd4397325fe7240b484ec9f79e7af (patch)
tree8c6fac831e3ca5f033dfa58e31047bf1e8cd5591
parentf7d5b04e07a874903969d8c55b78ab727d6a1769 (diff)
downloadvoronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar.gz
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar.bz2
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar.lz
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar.xz
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.tar.zst
voronoi-d87a9b0960ddd4397325fe7240b484ec9f79e7af.zip
Generalize parallel generation of Voronoi diagrams
-rw-r--r--example.cc53
1 files changed, 42 insertions, 11 deletions
diff --git a/example.cc b/example.cc
index d815e07..294c86a 100644
--- a/example.cc
+++ b/example.cc
@@ -1,8 +1,9 @@
#include <imgen.h>
#include <cmath>
-#include <algorithm>
#include <thread>
+#include <vector>
+#include <algorithm>
int minkowski_metric(double p, int refX, int refY, int x, int y) {
return static_cast<int>(std::nearbyint(
@@ -38,7 +39,7 @@ void generate_minkowski_voronoi(
imgen::colored_vector{-240, -20, imgen::olive() }
};
- for ( double p = lower; p < upper; p = p + epsilon ) {
+ for ( double p = lower; p < upper + epsilon; p = p + epsilon ) {
imgen::write_ppm(
"voronoi_" + std::to_string(p) + ".ppm",
512,
@@ -72,14 +73,44 @@ void generate_minkowski_voronoi(
}
}
+void generate_parallel_minkowski_voronoi(
+ const unsigned int thread_count,
+ const double lower,
+ const double upper,
+ const double epsilon
+) {
+ std::vector<std::thread> threads;
+
+ const double step = ( ( upper - lower ) / thread_count ) - epsilon;
+ double offset = lower;
+
+ threads.emplace_back([offset, step, epsilon]{
+ generate_minkowski_voronoi(
+ offset,
+ offset + step + epsilon,
+ epsilon
+ );
+ });
+
+ offset += step + epsilon + epsilon;
+
+ while ( threads.size() < thread_count ) {
+ threads.emplace_back([offset, step, epsilon]{
+ generate_minkowski_voronoi(
+ offset,
+ offset + step,
+ epsilon
+ );
+ });
+
+ offset += step + epsilon;
+ }
+
+ for ( auto& thread : threads ) {
+ thread.join();
+ }
+}
+
int main(int, char*[]) {
- std::thread worker1([](){ generate_minkowski_voronoi(0.8, 1.1, 0.002); });
- std::thread worker2([](){ generate_minkowski_voronoi(1.102, 1.4, 0.002); });
- std::thread worker3([](){ generate_minkowski_voronoi(1.402, 1.7, 0.002); });
- std::thread worker4([](){ generate_minkowski_voronoi(1.702, 2.0, 0.002); });
-
- worker1.join();
- worker2.join();
- worker3.join();
- worker4.join();
+ generate_parallel_minkowski_voronoi(4, 1.0, 16.0, 0.05);
}