ROOT 6.14/05 Reference Guide |
This class provides a simple interface to execute the same task multiple times in parallel, possibly with different arguments every time.
This mimics the behaviour of python's pool.Map method.
This class inherits its interfaces from ROOT::TExecutor
. The two possible usages of the Map method are:
For either signature, func is executed as many times as needed by a pool of nThreads threads; It defaults to the number of cores.
A collection containing the result of each execution is returned.
Note: the user is responsible for the deletion of any object that might be created upon execution of func, returned objects included: ROOT::TThreadExecutor never deletes what it returns, it simply forgets it.
func | a lambda expression, an std::function, a loaded macro, a functor class or a function that takes zero arguments (for the first signature) or one (for the second signature). |
args | a standard vector, a ROOT::TSeq of integer type or an initializer list for the second signature. An integer only for the first. |
Note: in cases where the function to be executed takes more than zero/one argument but all are fixed except zero/one, the function can be wrapped in a lambda or via std::bind to give it the right signature.
An std::vector. The elements in the container will be the objects returned by func.
This set of methods behaves exactly like Map, but takes an additional function as a third argument. This function is applied to the set of objects returned by the corresponding Map execution to "squash" them to a single object. This function should be independent of the size of the vector returned by Map due to optimization of the number of chunks.
If this function is a binary operator, the "squashing" will be performed in parallel. This is exclusive to ROOT::TThreadExecutor and not any other ROOT::TExecutor-derived classes.
An integer can be passed as the fourth argument indicating the number of chunks we want to divide our work in. This may be useful to avoid the overhead introduced when running really short tasks.
Definition at line 35 of file TThreadExecutor.hxx.
Public Member Functions | |
TThreadExecutor () | |
Class constructor. More... | |
TThreadExecutor (UInt_t nThreads) | |
Class constructor. More... | |
TThreadExecutor (TThreadExecutor &)=delete | |
template<class F > | |
void | Foreach (F func, unsigned nTimes) |
Execute func (with no arguments) nTimes in parallel. More... | |
template<class F , class INTEGER > | |
void | Foreach (F func, ROOT::TSeq< INTEGER > args) |
Execute func in parallel, taking an element of a sequence as argument. More... | |
template<class F , class T > | |
void | Foreach (F func, std::vector< T > &args) |
Execute func in parallel, taking an element of an std::vector as argument. More... | |
template<class F , class T > | |
void | Foreach (F func, const std::vector< T > &args) |
Execute func in parallel, taking an element of a std::vector as argument. More... | |
template<class F , class Cond = noReferenceCond<F>> | |
auto | Map (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type > |
Execute func (with no arguments) nTimes in parallel. More... | |
template<class F , class INTEGER , class Cond = noReferenceCond<F, INTEGER>> | |
auto | Map (F func, ROOT::TSeq< INTEGER > args) -> std::vector< typename std::result_of< F(INTEGER)>::type > |
Execute func in parallel, taking an element of a sequence as argument. More... | |
template<class F , class T , class Cond = noReferenceCond<F, T>> | |
auto | Map (F func, std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type > |
Execute func in parallel, taking an element of an std::vector as argument. More... | |
template<class F , class R , class Cond = noReferenceCond<F>> | |
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. More... | |
template<class F , class R , class Cond = noReferenceCond<F>> | |
auto | MapReduce (F func, unsigned nTimes, R redfunc, unsigned nChunks) -> typename std::result_of< F()>::type |
template<class F , class INTEGER , class R , class Cond = noReferenceCond<F, INTEGER>> | |
auto | MapReduce (F func, ROOT::TSeq< INTEGER > args, R redfunc, unsigned nChunks) -> typename std::result_of< F(INTEGER)>::type |
template<class F , class T , class R , class Cond = noReferenceCond<F, T>> | |
auto | MapReduce (F func, std::vector< T > &args, R redfunc) -> typename std::result_of< F(T)>::type |
template<class F , class T , class R , class Cond = noReferenceCond<F, T>> | |
auto | MapReduce (F func, std::vector< T > &args, R redfunc, unsigned nChunks) -> typename std::result_of< F(T)>::type |
TThreadExecutor & | operator= (TThreadExecutor &)=delete |
template<class T , class BINARYOP > | |
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 argument to act on pairs of elements of the std::vector. More... | |
template<class T , class R > | |
auto | Reduce (const std::vector< T > &objs, R redfunc) -> decltype(redfunc(objs)) |
"Reduce" an std::vector into a single object by passing a function as the second argument defining the reduction operation. More... | |
Public Member Functions inherited from ROOT::TExecutor< TThreadExecutor > | |
TExecutor ()=default | |
TExecutor (size_t) | |
auto | Map (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type > |
Execute func (with no arguments) nTimes in parallel. More... | |
auto | Map (F func, ROOT::TSeq< INTEGER > args) -> std::vector< typename std::result_of< F(INTEGER)>::type > |
Execute func in parallel, taking an element of a sequence as argument. More... | |
auto | Map (F func, std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type > |
Execute func in parallel, taking an element of an std::vector as argument. More... | |
auto | Map (F func, std::initializer_list< T > args) -> std::vector< typename std::result_of< F(T)>::type > |
Execute func in parallel, taking an element of the std::initializer_list as argument. More... | |
auto | MapReduce (F func, ROOT::TSeq< INTEGER > args, R redfunc) -> typename std::result_of< F(INTEGER)>::type |
This method behaves just like Map, but an additional redfunc function must be provided. More... | |
T * | MapReduce (F func, std::vector< T * > &args) |
auto | MapReduce (F func, std::initializer_list< T > args, R redfunc) -> typename std::result_of< F(T)>::type |
T * | Reduce (const std::vector< T * > &mergeObjs) |
"Reduce" an std::vector into a single object by using the object's Merge More... | |
Protected Member Functions | |
template<class F , class R , class Cond = noReferenceCond<F>> | |
auto | Map (F func, unsigned nTimes, R redfunc, unsigned nChunks) -> std::vector< typename std::result_of< F()>::type > |
Execute func (with no arguments) nTimes in parallel. More... | |
template<class F , class INTEGER , class R , class Cond = noReferenceCond<F, INTEGER>> | |
auto | Map (F func, ROOT::TSeq< INTEGER > args, R redfunc, unsigned nChunks) -> std::vector< typename std::result_of< F(INTEGER)>::type > |
Execute func in parallel, taking an element of a sequence as argument. More... | |
template<class F , class T , class R , class Cond = noReferenceCond<F, T>> | |
auto | Map (F func, std::vector< T > &args, R redfunc, unsigned nChunks) -> std::vector< typename std::result_of< F(T)>::type > |
template<class F , class T , class R , class Cond = noReferenceCond<F, T>> | |
auto | Map (F func, std::initializer_list< T > args, R redfunc, unsigned nChunks) -> std::vector< typename std::result_of< F(T)>::type > |
Private Member Functions | |
void | ParallelFor (unsigned start, unsigned end, unsigned step, const std::function< void(unsigned int i)> &f) |
double | ParallelReduce (const std::vector< double > &objs, const std::function< double(double a, double b)> &redfunc) |
float | ParallelReduce (const std::vector< float > &objs, const std::function< float(float a, float b)> &redfunc) |
template<class T , class R > | |
auto | SeqReduce (const std::vector< T > &objs, R redfunc) -> decltype(redfunc(objs)) |
Private Attributes | |
std::shared_ptr< ROOT::Internal::TPoolManager > | fSched = nullptr |
Additional Inherited Members | |
Public Types inherited from ROOT::TExecutor< TThreadExecutor > | |
using | noReferenceCond = typename std::enable_if<"Function can't return a reference" &&!(std::is_reference< typename std::result_of< F(T...)>::type >::value)>::type |
#include <ROOT/TThreadExecutor.hxx>
|
explicit |
Class constructor.
If the scheduler is active, gets a pointer to it. If not, initializes the pool of threads with the number of logical threads supported by the hardware.
Definition at line 79 of file TThreadExecutor.cxx.
|
explicit |
Class constructor.
nThreads is the number of threads that will be spawned. If the scheduler is active (ImplicitMT enabled, another TThreadExecutor instance), it won't change the number of threads.
Definition at line 84 of file TThreadExecutor.cxx.
|
delete |
Execute func (with no arguments) nTimes in parallel.
Functions that take more than zero arguments can be executed (with fixed arguments) by wrapping them in a lambda or with std::bind.
Definition at line 116 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
ROOT::TSeq< INTEGER > | args | ||
) |
Execute func in parallel, taking an element of a sequence as argument.
Definition at line 124 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
std::vector< T > & | args | ||
) |
Execute func in parallel, taking an element of an std::vector as argument.
Definition at line 143 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
const std::vector< T > & | args | ||
) |
Execute func in parallel, taking an element of a std::vector as argument.
Definition at line 151 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::Map | ( | F | func, |
unsigned | nTimes | ||
) | -> std::vector<typename std::result_of<F()>::type> |
Execute func (with no arguments) nTimes in parallel.
A vector containg executions' results is returned. Functions that take more than zero arguments can be executed (with fixed arguments) by wrapping them in a lambda or with std::bind.
Definition at line 162 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::Map | ( | F | func, |
ROOT::TSeq< INTEGER > | args | ||
) | -> std::vector<typename std::result_of<F(INTEGER)>::type> |
Execute func in parallel, taking an element of a sequence as argument.
A vector containg executions' results is returned.
Definition at line 179 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::Map | ( | F | func, |
std::vector< T > & | args | ||
) | -> std::vector<typename std::result_of<F(T)>::type> |
Execute func in parallel, taking an element of an std::vector as argument.
A vector containg executions' results is returned.
Definition at line 231 of file TThreadExecutor.hxx.
|
protected |
Execute func (with no arguments) nTimes in parallel.
Divides and groups the executions in nChunks (if it doesn't make sense will reduce the number of chunks) with partial reduction; A vector containg partial reductions' results is returned.
Definition at line 200 of file TThreadExecutor.hxx.
|
protected |
Execute func in parallel, taking an element of a sequence as argument.
Divides and groups the executions in nChunks (if it doesn't make sense will reduce the number of chunks) with partial reduction
A vector containg partial reductions' results is returned.
Definition at line 254 of file TThreadExecutor.hxx.
|
protected |
|
protected |
auto ROOT::TThreadExecutor::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.
redfunc is applied to the vector Map would return and must return the same type as func. In practice, redfunc can be used to "squash" the vector returned by Map into a single object by merging, adding, mixing the elements of the vector.
The fourth argument indicates the number of chunks we want to divide our work in.
Definition at line 338 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::MapReduce | ( | F | func, |
unsigned | nTimes, | ||
R | redfunc, | ||
unsigned | nChunks | ||
) | -> typename std::result_of<F()>::type |
Definition at line 343 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::MapReduce | ( | F | func, |
ROOT::TSeq< INTEGER > | args, | ||
R | redfunc, | ||
unsigned | nChunks | ||
) | -> typename std::result_of<F(INTEGER)>::type |
Definition at line 348 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::MapReduce | ( | F | func, |
std::vector< T > & | args, | ||
R | redfunc | ||
) | -> typename std::result_of<F(T)>::type |
Definition at line 359 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::MapReduce | ( | F | func, |
std::vector< T > & | args, | ||
R | redfunc, | ||
unsigned | nChunks | ||
) | -> typename std::result_of<F(T)>::type |
Definition at line 364 of file TThreadExecutor.hxx.
|
delete |
|
private |
Definition at line 89 of file TThreadExecutor.cxx.
|
private |
Definition at line 94 of file TThreadExecutor.cxx.
|
private |
Definition at line 102 of file TThreadExecutor.cxx.
auto ROOT::TThreadExecutor::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 argument to act on pairs of elements of the std::vector.
Definition at line 372 of file TThreadExecutor.hxx.
auto ROOT::TThreadExecutor::Reduce | ( | const std::vector< T > & | objs, |
R | redfunc | ||
) | -> decltype(redfunc(objs)) |
"Reduce" an std::vector into a single object by passing a function as the second argument defining the reduction operation.
Definition at line 383 of file TThreadExecutor.hxx.
|
private |
Definition at line 391 of file TThreadExecutor.hxx.
|
private |
Definition at line 106 of file TThreadExecutor.hxx.