From 1728c04205136644b345ec8a6f8a406138eb7e08 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 5 Oct 2024 12:02:17 -0400 Subject: create balls in parallel --- balls.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'balls.cpp') diff --git a/balls.cpp b/balls.cpp index 2a0f5b9..22b9f0d 100644 --- a/balls.cpp +++ b/balls.cpp @@ -6,6 +6,7 @@ #include "balls.h" using namespace std; +using namespace oneapi::tbb; #define VMAX_INIT 0.05 /* max initial velocity [m/frame] */ @@ -142,22 +143,17 @@ reshape(int w, int h) { vector makeBalls(unsigned int n) { vector balls(n); - vector ps = noOverlapCircles(n); - unsigned int i; + vector positions = noOverlapCircles(n); - for (i = 0; i < n; i++) { + parallel_for(size_t(0), balls.size(), [&balls, positions] (size_t i) { cout << "Creating ball " << i << "\n"; - balls[i].p = ps[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