Logo ROOT   6.10/09
Reference Guide
List of all members | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
ROOT::TThreadExecutor Class Reference

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.

ROOT::TThreadExecutor::Map

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.

Parameters
funca 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).
argsa 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.

Return value:

An std::vector. The elements in the container will be the objects returned by func.

Examples:

root[] ROOT::TThreadExecutor pool; auto hists = pool.Map(CreateHisto, 10);
root[] ROOT::TThreadExecutor pool(2); auto squares = pool.Map([](int a) { return a*a; }, {1,2,3});

ROOT::TThreadExecutor::MapReduce

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.

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.

Examples:

root[] ROOT::TThreadExecutor pool; auto ten = pool.MapReduce([]() { return 1; }, 10, [](std::vector<int> v) { return std::accumulate(v.begin(), v.end(), 0); })
root[] ROOT::TThreadExecutor pool; auto hist = pool.MapReduce(CreateAndFillHists, 10, PoolUtils::ReduceObjects);

Definition at line 34 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 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, unsigned nChunks) -> 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 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, unsigned nChunks) -> typename std::result_of< F(T)>::type
 
TThreadExecutoroperator= (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, 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...
 
auto MapReduce (F func, ROOT::TSeq< INTEGER > args, R redfunc) -> typename std::result_of< F(INTEGER)>::type
 
auto MapReduce (F func, std::vector< T > &args, R redfunc) -> typename std::result_of< F(T)>::type
 
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::TPoolManagerfSched = 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>

Inheritance diagram for ROOT::TThreadExecutor:
[legend]

Constructor & Destructor Documentation

◆ TThreadExecutor() [1/3]

ROOT::TThreadExecutor::TThreadExecutor ( )
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 77 of file TThreadExecutor.cxx.

◆ TThreadExecutor() [2/3]

ROOT::TThreadExecutor::TThreadExecutor ( UInt_t  nThreads)
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 82 of file TThreadExecutor.cxx.

◆ TThreadExecutor() [3/3]

ROOT::TThreadExecutor::TThreadExecutor ( TThreadExecutor )
delete

Member Function Documentation

◆ Foreach() [1/3]

template<class F >
void ROOT::TThreadExecutor::Foreach ( F  func,
unsigned  nTimes 
)

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 110 of file TThreadExecutor.hxx.

◆ Foreach() [2/3]

template<class F , class INTEGER >
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 118 of file TThreadExecutor.hxx.

◆ Foreach() [3/3]

template<class F , class T >
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 137 of file TThreadExecutor.hxx.

◆ Map() [1/7]

template<class F , class Cond >
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 148 of file TThreadExecutor.hxx.

◆ Map() [2/7]

template<class F , class INTEGER , class Cond >
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 165 of file TThreadExecutor.hxx.

◆ Map() [3/7]

template<class F , class T , class Cond >
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 215 of file TThreadExecutor.hxx.

◆ Map() [4/7]

template<class F , class R , class Cond >
auto ROOT::TThreadExecutor::Map ( F  func,
unsigned  nTimes,
R  redfunc,
unsigned  nChunks 
) -> std::vector<typename std::result_of<F()>::type>
protected

Execute func (with no arguments) nTimes in parallel.

Divides and groups the executions in nChunks with partial reduction; A vector containg partial reductions' results is returned.

Definition at line 186 of file TThreadExecutor.hxx.

◆ Map() [5/7]

template<class F , class INTEGER , class R , class Cond >
auto ROOT::TThreadExecutor::Map ( F  func,
ROOT::TSeq< INTEGER >  args,
R  redfunc,
unsigned  nChunks 
) -> std::vector<typename std::result_of<F(INTEGER)>::type>
protected

Execute func in parallel, taking an element of a sequence as argument.

Divides and groups the executions in nChunks with partial reduction; A vector containg partial reductions' results is returned.

Definition at line 237 of file TThreadExecutor.hxx.

◆ Map() [6/7]

template<class F , class T , class R , class Cond = noReferenceCond<F, T>>
auto ROOT::TThreadExecutor::Map ( F  func,
std::vector< T > &  args,
R  redfunc,
unsigned  nChunks 
) -> std::vector< typename std::result_of< F(T)>::type >
protected

◆ Map() [7/7]

template<class F , class T , class R , class Cond = noReferenceCond<F, T>>
auto ROOT::TThreadExecutor::Map ( F  func,
std::initializer_list< T >  args,
R  redfunc,
unsigned  nChunks 
) -> std::vector< typename std::result_of< F(T)>::type >
protected

◆ MapReduce() [1/3]

template<class F , class R , class Cond >
auto ROOT::TThreadExecutor::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.

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 316 of file TThreadExecutor.hxx.

◆ MapReduce() [2/3]

template<class F , class INTEGER , class R , class Cond >
auto ROOT::TThreadExecutor::MapReduce ( F  func,
ROOT::TSeq< INTEGER >  args,
R  redfunc,
unsigned  nChunks 
) -> typename std::result_of<F(INTEGER)>::type

Definition at line 321 of file TThreadExecutor.hxx.

◆ MapReduce() [3/3]

template<class F , class T , class R , class Cond >
auto ROOT::TThreadExecutor::MapReduce ( F  func,
std::vector< T > &  args,
R  redfunc,
unsigned  nChunks 
) -> typename std::result_of<F(T)>::type

Definition at line 331 of file TThreadExecutor.hxx.

◆ operator=()

TThreadExecutor& ROOT::TThreadExecutor::operator= ( TThreadExecutor )
delete

◆ ParallelFor()

void ROOT::TThreadExecutor::ParallelFor ( unsigned  start,
unsigned  end,
unsigned  step,
const std::function< void(unsigned int i)> &  f 
)
private

Definition at line 87 of file TThreadExecutor.cxx.

◆ ParallelReduce() [1/2]

double ROOT::TThreadExecutor::ParallelReduce ( const std::vector< double > &  objs,
const std::function< double(double a, double b)> &  redfunc 
)
private

Definition at line 92 of file TThreadExecutor.cxx.

◆ ParallelReduce() [2/2]

float ROOT::TThreadExecutor::ParallelReduce ( const std::vector< float > &  objs,
const std::function< float(float a, float b)> &  redfunc 
)
private

Definition at line 100 of file TThreadExecutor.cxx.

◆ Reduce() [1/2]

template<class T , class BINARYOP >
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 339 of file TThreadExecutor.hxx.

◆ Reduce() [2/2]

template<class T , class R >
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 350 of file TThreadExecutor.hxx.

◆ SeqReduce()

template<class T , class R >
auto ROOT::TThreadExecutor::SeqReduce ( const std::vector< T > &  objs,
R  redfunc 
) -> decltype(redfunc(objs))
private

Definition at line 358 of file TThreadExecutor.hxx.

Member Data Documentation

◆ fSched

std::shared_ptr<ROOT::Internal::TPoolManager> ROOT::TThreadExecutor::fSched = nullptr
private

Definition at line 100 of file TThreadExecutor.hxx.


The documentation for this class was generated from the following files: