summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-10 18:15:02 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-10 18:15:02 -0400
commit96e13851f64b1fc364cc5ee701713706a81fc647 (patch)
treeace864bbe73019da145e5e17706b3c2e45a30cbe
parent6a18f738c8dd6e3fe9f21c27f70f7df201e54d24 (diff)
downloadballs-96e13851f64b1fc364cc5ee701713706a81fc647.zip
parallelize makeBalls()
-rw-r--r--balls.cpp15
1 files changed, 9 insertions, 6 deletions
diff --git a/balls.cpp b/balls.cpp
index b3f245d..2c4ee6b 100644
--- a/balls.cpp
+++ b/balls.cpp
@@ -136,11 +136,9 @@ vector<Ball *>
makeBalls(int n) {
size_t i;
- vector<Point> positions = noOverlapCircles(n);
-
vector<Ball *> balls(n);
- /* TODO: parallel */
- for(i = 0; i < balls.size(); i++) {
+
+ for (i = 0; i < balls.size(); i++) {
cout << "Creating ball " << i << "\n";
if ((balls[i] = (Ball *) malloc(sizeof(Ball))) == NULL) {
cerr << "failed to allocate ball\n";
@@ -148,14 +146,19 @@ makeBalls(int n) {
free(balls[i]);
exit(1);
}
-
+ }
+
+ vector<Point> positions = noOverlapCircles(n);
+
+ parallel_for(size_t(0), balls.size(), [balls, positions] (size_t i) {
balls[i]->p = positions[i];
balls[i]->v.x = randDouble(-VMAX_INIT, VMAX_INIT);
balls[i]->v.y = randDouble(-VMAX_INIT, VMAX_INIT);
balls[i]->r = randDouble(RMIN, RMAX);
balls[i]->m = mass(balls[i]->r);
balls[i]->color = randColor();
- };
+ });
+
return balls;
}