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 template<
class F,
class INTEGER>
48 template<
class F,
class T>
49 void Foreach(
F func, std::initializer_list<T> args);
51 template<
class F,
class T>
52 void Foreach(
F func, std::vector<T> &args);
55 template<
class F,
class Cond = noReferenceCond<F>>
57 template<
class F,
class INTEGER,
class Cond = noReferenceCond<F, INTEGER>>
59 template<
class F,
class T,
class Cond = noReferenceCond<F, T>>
67 template<
class F,
class R,
class Cond = noReferenceCond<F>>
69 template<
class F,
class INTEGER,
class R,
class Cond = noReferenceCond<F, INTEGER>>
72 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
75 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
79 template<
class T,
class BINARYOP>
auto Reduce(
const std::vector<T> &objs, BINARYOP redfunc) -> decltype(redfunc(objs.front(), objs.front()));
80 template<
class T,
class R>
auto Reduce(
const std::vector<T> &objs,
R redfunc) -> decltype(redfunc(objs));
84 template<
class F,
class R,
class Cond = noReferenceCond<F>>
86 template<
class F,
class INTEGER,
class R,
class Cond = noReferenceCond<F, INTEGER>>
88 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
90 template<
class F,
class T,
class R,
class Cond = noReferenceCond<F, T>>
97 template<
class T,
class R>
98 auto SeqReduce(
const std::vector<T> &objs,
R redfunc) -> decltype(redfunc(objs));
100 std::shared_ptr<ROOT::Internal::TPoolManager>
fSched =
nullptr;
117 template<
class F,
class INTEGER>
126 template<
class F,
class T>
128 std::vector<T> vargs(std::move(args));
136 template<
class F,
class T>
138 unsigned int nToProcess = args.size();
147 template<
class F,
class Cond>
149 using retType = decltype(
func());
150 std::vector<retType> reslist(nTimes);
151 auto lambda = [&](
unsigned int i)
164 template<
class F,
class INTEGER,
class Cond>
166 unsigned start = *args.begin();
167 unsigned end = *args.end();
168 unsigned seqStep = args.step();
170 using retType = decltype(
func(start));
171 std::vector<retType> reslist(end - start);
172 auto lambda = [&](
unsigned int i)
174 reslist[i] =
func(i);
185 template<
class F,
class R,
class Cond>
189 return Map(func, nTimes);
192 using retType = decltype(
func());
193 std::vector<retType> reslist(nChunks);
194 unsigned step = (nTimes + nChunks - 1) / nChunks;
195 auto lambda = [&](
unsigned int i)
197 std::vector<retType> partialResults(step);
198 for (
unsigned j = 0; j < step && (i + j) < nTimes; j++) {
199 partialResults[j] =
func();
201 reslist[i / step] = redfunc(partialResults);
214 template<
class F,
class T,
class Cond>
217 using retType = decltype(
func(args.front()));
219 unsigned int nToProcess = args.size();
220 std::vector<retType> reslist(nToProcess);
222 auto lambda = [&](
unsigned int i)
224 reslist[i] =
func(args[i]);
236 template<
class F,
class INTEGER,
class R,
class Cond>
240 return Map(func, args);
243 unsigned start = *args.begin();
244 unsigned end = *args.end();
245 unsigned seqStep = args.step();
246 unsigned step = (end - start + nChunks - 1) / nChunks;
248 using retType = decltype(
func(start));
249 std::vector<retType> reslist(nChunks);
250 auto lambda = [&](
unsigned int i)
252 std::vector<retType> partialResults(step);
253 for (
unsigned j = 0; j < step && (i + j) < end; j+=seqStep) {
254 partialResults[j] =
func(i + j);
256 reslist[i / step] = redfunc(partialResults);
268 template<
class F,
class T,
class R,
class Cond>
272 return Map(func, args);
275 using retType = decltype(
func(args.front()));
277 unsigned int nToProcess = args.size();
278 std::vector<retType> reslist(nChunks);
279 unsigned step = (nToProcess + nChunks - 1) / nChunks;
281 auto lambda = [&](
unsigned int i)
283 std::vector<T> partialResults(step);
284 for (
unsigned j = 0; j < step && (i + j) < nToProcess; j++) {
285 partialResults[j] =
func(args[i + j]);
287 reslist[i / step] = redfunc(partialResults);
299 template<
class F,
class T,
class R,
class Cond>
301 std::vector<T> vargs(std::move(args));
302 const auto &reslist =
Map(func, vargs, redfunc, nChunks);
315 template<
class F,
class R,
class Cond>
317 return Reduce(
Map(func, nTimes, redfunc, nChunks), redfunc);
320 template<
class F,
class INTEGER,
class R,
class Cond>
322 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
325 template<
class F,
class T,
class R,
class Cond>
327 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
330 template<
class F,
class T,
class R,
class Cond>
332 return Reduce(
Map(func, args, redfunc, nChunks), redfunc);
338 template<
class T,
class BINARYOP>
342 static_assert(std::is_same<decltype(redfunc(objs.front(), objs.front())),
T>::value,
"redfunc does not have the correct signature");
349 template<
class T,
class R>
353 static_assert(std::is_same<decltype(redfunc(objs)),
T>::value,
"redfunc does not have the correct signature");
357 template<
class T,
class R>
360 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.
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.
double func(double *x, double *p)
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
auto MapReduce(F func, unsigned nTimes, R redfunc, unsigned nChunks) -> typename std::result_of< F()>::type
This method behaves just like Map, but an additional redfunc function must be provided.