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:
parent
b6d665ff0e
commit
7411f21b59
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user