1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2025-09-10 06:16:00 +00:00

rogueviz:: parallelize moved from flocking to rogueviz.h

This commit is contained in:
Zeno Rogue
2022-08-23 21:49:25 +02:00
parent b6d665ff0e
commit 7411f21b59
2 changed files with 19 additions and 26 deletions

View File

@@ -342,6 +342,25 @@ namespace objmodels {
#define addHook_slideshows(x, y) 0
#define addHook_rvtour(x, y) 0
#endif
/* parallelize a computation */
inline int threads = 1;
template<class T> auto parallelize(long long N, T action) -> decltype(action(0,0)) {
if(threads == 1) return action(0,N);
std::vector<std::thread> v;
typedef decltype(action(0,0)) Res;
std::vector<Res> results(threads);
for(int k=0; k<threads; k++)
v.emplace_back([&,k] () {
results[k] = action(N*k/threads, N*(k+1)/threads);
});
for(std::thread& t:v) t.join();
Res res = 0;
for(Res r: results) res += r;
return res;
}
}
#endif