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 (TThreadExecutor &)=delete | |
TThreadExecutor (UInt_t nThreads=0u) | |
Class constructor. More... | |
template<class F , class T > | |
void | Foreach (F func, const std::vector< T > &args, unsigned nChunks=0) |
Execute func in parallel, taking an element of a std::vector as argument. More... | |
template<class F , class INTEGER > | |
void | Foreach (F func, ROOT::TSeq< INTEGER > args, unsigned nChunks=0) |
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, unsigned nChunks=0) |
Execute func in parallel, taking an element of an std::vector as argument. More... | |
template<class F > | |
void | Foreach (F func, unsigned nTimes, unsigned nChunks=0) |
Execute func (with no arguments) nTimes in parallel. More... | |
unsigned | GetPoolSize () |
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 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 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 |
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 |
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, 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::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 | 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, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type > |
Execute func (with no arguments) nTimes in parallel. 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... | |
auto | MapReduce (F func, std::initializer_list< T > args, R redfunc) -> typename std::result_of< F(T)>::type |
T * | MapReduce (F func, std::vector< T * > &args) |
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 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::initializer_list< 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::vector< T > &args, R redfunc, unsigned nChunks) -> std::vector< typename std::result_of< F(T)>::type > |
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... | |
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::RTaskArenaWrapper > | fTaskArenaW = 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 (e.g. because another TThreadExecutor is in flight, or ROOT::EnableImplicitMT() was called), work with the current pool of threads. If not, initialize the pool of threads, spawning nThreads. nThreads' default value, 0, initializes the pool with as many logical threads as are available in the system (see NLogicalCores in RTaskArenaWrapper.cxx).
At construction time, TThreadExecutor automatically enables ROOT's thread-safety locks as per calling ROOT::EnableThreadSafety().
Definition at line 138 of file TThreadExecutor.cxx.
|
delete |
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
const std::vector< T > & | args, | ||
unsigned | nChunks = 0 |
||
) |
Execute func in parallel, taking an element of a std::vector as argument.
Definition at line 191 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
ROOT::TSeq< INTEGER > | args, | ||
unsigned | nChunks = 0 |
||
) |
Execute func in parallel, taking an element of a sequence as argument.
Definition at line 137 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
std::vector< T > & | args, | ||
unsigned | nChunks = 0 |
||
) |
Execute func in parallel, taking an element of an std::vector as argument.
Definition at line 171 of file TThreadExecutor.hxx.
void ROOT::TThreadExecutor::Foreach | ( | F | func, |
unsigned | nTimes, | ||
unsigned | nChunks = 0 |
||
) |
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 117 of file TThreadExecutor.hxx.
unsigned ROOT::TThreadExecutor::GetPoolSize | ( | ) |
Definition at line 183 of file TThreadExecutor.cxx.
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 231 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 306 of file TThreadExecutor.hxx.
|
protected |
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 283 of file TThreadExecutor.hxx.
|
protected |
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 214 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 252 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 400 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 411 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 416 of file TThreadExecutor.hxx.
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 390 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 395 of file TThreadExecutor.hxx.
|
delete |
|
private |
Definition at line 143 of file TThreadExecutor.cxx.
|
private |
Definition at line 159 of file TThreadExecutor.cxx.
|
private |
Definition at line 171 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 424 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 435 of file TThreadExecutor.hxx.
|
private |
Definition at line 443 of file TThreadExecutor.hxx.
|
private |
Definition at line 107 of file TThreadExecutor.hxx.