1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-24 17:10:36 +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

@ -16,30 +16,6 @@
// press 'o' when flocking active to change the parameters.
#ifdef USE_THREADS
#include <thread>
int threads = 1;
#endif
template<class T> auto parallelize(long long N, T action) -> decltype(action(0,0)) {
#ifndef USE_THREADS
return action(0,N);
#else
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
}
#include "rogueviz.h"
namespace rogueviz {
@ -372,11 +348,9 @@ namespace flocking {
shift(); ini_speed = argf();
shift(); max_speed = argf();
}
#ifdef USE_THREADS
else if(argis("-threads")) {
shift(); threads = argi();
}
#endif
else return 1;
return 0;
}

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