summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-05 12:02:17 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-05 12:02:17 -0400
commit1728c04205136644b345ec8a6f8a406138eb7e08 (patch)
tree28526c00cf7e83bf178640d02f992c149dd6cd50
parentb2d3054379f1691600c0e36163b42d14972ede40 (diff)
downloadballs-1728c04205136644b345ec8a6f8a406138eb7e08.zip
create balls in parallel
-rw-r--r--balls.cpp14
1 files changed, 5 insertions, 9 deletions
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<Ball>
makeBalls(unsigned int n) {
vector<Ball> balls(n);
- vector<Point> ps = noOverlapCircles(n);
- unsigned int i;
+ vector<Point> 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;
}