From 96e13851f64b1fc364cc5ee701713706a81fc647 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 10 Oct 2024 18:15:02 -0400 Subject: parallelize makeBalls() --- balls.cpp | 15 +++++++++------ 1 file 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 makeBalls(int n) { size_t i; - vector positions = noOverlapCircles(n); - vector 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 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; } -- cgit v1.2.3