12 #ifndef ROOT_TThreadExecutor 13 #define ROOT_TThreadExecutor 15 #include "RConfigure.h" 20 # if !defined(__ROOTCLING__) && !defined(G__DICTIONARY) 21 # error "Cannot use ROOT::TThreadExecutor without defining R__USE_IMT." 45 void Foreach(
F func,
unsigned nTimes);
46 template<
class F,
class INTEGER>
49 template<
class F,
class T>
50 void Foreach(
F func, std::initializer_list<T> args);
52 template<
class F,
class T>
53 void Foreach(
F func, std::vector<T> &args);
54 template<
class F,
class T>
55 void Foreach(
F func,
const std::vector<T> &args);
58 template<
class F,
class Cond = noReferenceCond<F>>
60 template<
class F,
class INTEGER,
class Cond = noReferenceCond<F, INTEGER>>
62 template<
class F,
class T,
class Cond = noReferenceCond<F, T>>
70 template<
class F,
class R,
class Cond = noReferenceCond<F>>
72 template<
class F,
class R,
class Cond = noReferenceCond<F>>
74 template<
class F,
class INTEGER,
class R,
class Cond = noReferenceCond<F, INTEGER>>
77 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
80 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
82 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
86 template<
class T,
class BINARYOP>
auto Reduce(
const std::vector<T> &objs, BINARYOP redfunc) -> decltype(redfunc(objs.front(), objs.front()));
87 template<
class T,
class R>
auto Reduce(
const std::vector<T> &objs,
R redfunc) -> decltype(redfunc(objs));
90 template<
class F,
class R,
class Cond = noReferenceCond<F>>
92 template<
class F,
class INTEGER,
class R,
class Cond = noReferenceCond<F, INTEGER>>
94 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
96 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
103 template<
class T,
class R>
104 auto SeqReduce(
const std::vector<T> &objs,
R redfunc) -> decltype(redfunc(objs));
106 std::shared_ptr<ROOT::Internal::TPoolManager>
fSched =
nullptr;
117 ParallelFor(0U, nTimes, 1, [&](
unsigned int){func();});
123 template<
class F,
class INTEGER>
132 template<
class F,
class T>
134 std::vector<T> vargs(std::move(args));
142 template<
class F,
class T>
144 unsigned int nToProcess = args.size();
145 ParallelFor(0U, nToProcess, 1, [&](
unsigned int i){func(args[i]);});
150 template<
class F,
class T>
152 unsigned int nToProcess = args.size();
153 ParallelFor(0U, nToProcess, 1, [&](
unsigned int i){func(args[i]);});
161 template<
class F,
class Cond>
163 using retType = decltype(func());
164 std::vector<retType> reslist(nTimes);
165 auto lambda = [&](
unsigned int i)
178 template<
class F,
class INTEGER,
class Cond>
180 unsigned start = *args.begin();
181 unsigned end = *args.end();
182 unsigned seqStep = args.step();
184 using retType = decltype(func(start));
185 std::vector<retType> reslist(end - start);
186 auto lambda = [&](
unsigned int i)
188 reslist[i] = func(i);
199 template<
class F,
class R,
class Cond>
203 return Map(func, nTimes);
206 unsigned step = (nTimes + nChunks - 1) / nChunks;
208 unsigned actualChunks = (nTimes + step - 1) / step;
209 using retType = decltype(func());
210 std::vector<retType> reslist(actualChunks);
211 auto lambda = [&](
unsigned int i)
213 std::vector<retType> partialResults(std::min(nTimes-i, step));
214 for (
unsigned j = 0; j < step && (i + j) < nTimes; j++) {
215 partialResults[j] = func();
217 reslist[i / step] = redfunc(partialResults);
230 template<
class F,
class T,
class Cond>
233 using retType = decltype(func(args.front()));
235 unsigned int nToProcess = args.size();
236 std::vector<retType> reslist(nToProcess);
238 auto lambda = [&](
unsigned int i)
240 reslist[i] = func(args[i]);
253 template<
class F,
class INTEGER,
class R,
class Cond>
257 return Map(func, args);
260 unsigned start = *args.begin();
261 unsigned end = *args.end();
262 unsigned seqStep = args.step();
263 unsigned step = (end - start + nChunks - 1) / nChunks;
265 unsigned actualChunks = (end - start + step - 1) / step;
267 using retType = decltype(func(start));
268 std::vector<retType> reslist(actualChunks);
269 auto lambda = [&](
unsigned int i)
271 std::vector<retType> partialResults(std::min(end-i, step));
272 for (
unsigned j = 0; j < step && (i + j) < end; j+=seqStep) {
273 partialResults[j] = func(i + j);
275 reslist[i / step] = redfunc(partialResults);
288 template<
class F,
class T,
class R,
class Cond>
292 return Map(func, args);
295 unsigned int nToProcess = args.size();
296 unsigned step = (nToProcess + nChunks - 1) / nChunks;
298 unsigned actualChunks = (nToProcess + step - 1) / step;
300 using retType = decltype(func(args.front()));
301 std::vector<retType> reslist(actualChunks);
302 auto lambda = [&](
unsigned int i)
304 std::vector<T> partialResults(step);
305 for (
unsigned j = 0; j < step && (i + j) < nToProcess; j++) {
306 partialResults[j] = func(args[i + j]);
308 reslist[i / step] = redfunc(partialResults);
321 template<
class F,
class T,
class R,
class Cond>
323 std::vector<T> vargs(std::move(args));
324 const auto &reslist =
Map(func, vargs, redfunc, nChunks);
337 template<
class F,
class R,
class Cond>
339 return Reduce(
Map(func, nTimes), redfunc);
342 template<
class F,
class R,
class Cond>
344 return Reduce(
Map(func, nTimes, redfunc, nChunks), redfunc);
347 template<
class F,
class INTEGER,
class R,
class Cond>
349 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
352 template<
class F,
class T,
class R,
class Cond>
354 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
358 template<
class F,
class T,
class R,
class Cond>
363 template<
class F,
class T,
class R,
class Cond>
365 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
371 template<
class T,
class BINARYOP>
375 static_assert(std::is_same<decltype(redfunc(objs.front(), objs.front())),
T>::value,
"redfunc does not have the correct signature");
382 template<
class T,
class R>
386 static_assert(std::is_same<decltype(redfunc(objs)),
T>::value,
"redfunc does not have the correct signature");
390 template<
class T,
class R>
393 return redfunc(objs);
void Foreach(F func, unsigned nTimes)
Execute func (with no arguments) nTimes in parallel.
Namespace for new ROOT classes and functions.
auto SeqReduce(const std::vector< T > &objs, R redfunc) -> decltype(redfunc(objs))
void ParallelFor(unsigned start, unsigned end, unsigned step, const std::function< void(unsigned int i)> &f)
This class defines an interface to execute the same task multiple times in parallel, possibly with different arguments every time.
#define R(a, b, c, d, e, f, g, h, i)
double ParallelReduce(const std::vector< double > &objs, const std::function< double(double a, double b)> &redfunc)
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
auto Reduce(const std::vector< T > &objs, BINARYOP redfunc) -> decltype(redfunc(objs.front(), objs.front()))
"Reduce" an std::vector into a single object in parallel by passing a binary operator as the second a...
This class provides a simple interface to execute the same task multiple times in parallel...
TThreadExecutor & operator=(TThreadExecutor &)=delete
TThreadExecutor()
Class constructor.
A pseudo container class which is a generator of indices.
auto MapReduce(F func, unsigned nTimes, R redfunc) -> typename std::result_of< F()>::type
This method behaves just like Map, but an additional redfunc function must be provided.
auto Map(F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
Execute func (with no arguments) nTimes in parallel.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
std::shared_ptr< ROOT::Internal::TPoolManager > fSched