diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-10-10 18:15:02 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-10-10 18:15:02 -0400 |
| commit | 96e13851f64b1fc364cc5ee701713706a81fc647 (patch) | |
| tree | ace864bbe73019da145e5e17706b3c2e45a30cbe | |
| parent | 6a18f738c8dd6e3fe9f21c27f70f7df201e54d24 (diff) | |
| download | balls-96e13851f64b1fc364cc5ee701713706a81fc647.zip | |
parallelize makeBalls()
| -rw-r--r-- | balls.cpp | 15 |
1 files changed, 9 insertions, 6 deletions
@@ -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; } |