Logo ROOT  
Reference Guide
ROOT::Internal::TExecutor Class Reference

This class implements the interface to execute the same task multiple times, sequentially or in parallel depending on the execution policy passed as a first parameter on construction, and possibly with different arguments every time.

ROOT::Internal::TExecutor::Map

The two possible usages of the Map method are:

  • Map(F func, unsigned nTimes): func is executed nTimes with no arguments
  • Map(F func, T& args): func is executed on each element of the collection of arguments args

For either signature, func is executed as many times as needed by a pool of n workers; where n tipically 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::::Internal::TExecutor never deletes what it returns, it simply forgets it.

Parameters
funca callable object, such as a lambda expression, an std::function, a functor object 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.

ROOT::Internal::TExecutor::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 into a single object.

An integer can be passed as the fourth argument indicating the number of chunks we want to divide our work in. (Note: Please be aware that chunking is only available when the policy is kMultiThread, ignoring this argument in other cases) This may be useful to avoid the overhead introduced when running really short tasks. In this case, the reduction function should be independent of the size of the vector returned by Map due to optimization of the number of chunks.

Examples:

root[] ROOT::Internal::TExecutor pool; auto ten = pool.MapReduce([]() { return 1; }, 10, [](const std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); })
Merge collection of TObjects.
Definition: PoolUtils.h:35
This class implements the interface to execute the same task multiple times, sequentially or in paral...
Definition: TExecutor.hxx:37
auto MapReduce(F func, unsigned nTimes, R redfunc, unsigned nChunks) -> typename std::result_of< F()>::type
Execute a function nTimes (Map) and accumulate the results into a single value (Reduce).
Definition: TExecutor.hxx:207

Definition at line 37 of file TExecutor.hxx.

Classes

struct  MapRetType
 Helper class to get the correct return type from the Map function, necessary to infer the ResolveExecutorAndMap function type. More...
 
struct  MapRetType< F, unsigned >
 

Public Member Functions

 TExecutor (const TExecutor &)=delete
  More...
 
 TExecutor (ROOT::EExecutionPolicy execPolicy, unsigned nWorkers=0)
 Class constructor. More...
 
 TExecutor (unsigned nWorkers=0)
 Class constructor. More...
 
unsigned GetPoolSize () const
 Return the number of pooled workers. More...
 
template<class F , class T , class R , class Cond = noReferenceCond<F, T>>
auto MapReduce (F func, const std::vector< T > &args, R redfunc, unsigned nChunks) -> typename std::result_of< F(T)>::type
 Execute a function over the elements of an immutable vector (Map) and accumulate the results into a single value (Reduce). 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
 Execute a function over a sequence of indexes (Map) and accumulate the results into a single value (Reduce). More...
 
template<class F , class T , class R , class Cond = noReferenceCond<F, T>>
auto MapReduce (F func, std::initializer_list< T > args, R redfunc, unsigned nChunks) -> typename std::result_of< F(T)>::type
 Execute a function over the elements of an initializer_list (Map) and accumulate the results into a single value (Reduce). More...
 
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
 Execute a function over the elements of a vector (Map) and accumulate the results into a single value (Reduce). 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
 Execute a function nTimes (Map) and accumulate the results into a single value (Reduce). More...
 
TExecutoroperator= (const TExecutor &)=delete
  More...
 
ROOT::EExecutionPolicy Policy () const
 Return the execution policy the executor is set to. More...
 
- Public Member Functions inherited from ROOT::TExecutorCRTP< TExecutor >
 TExecutorCRTP ()=default
  More...
 
 TExecutorCRTP (const TExecutorCRTP &)=delete
  More...
 
auto Map (F func, const std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type >
 Execute a function over the elements of an immutable vector. More...
 
auto Map (F func, ROOT::TSeq< INTEGER > args) -> std::vector< typename std::result_of< F(INTEGER)>::type >
 Execute a function over a sequence of indexes. More...
 
auto Map (F func, std::initializer_list< T > args) -> std::vector< typename std::result_of< F(T)>::type >
 Execute a function over the elements of an initializer_list. More...
 
auto Map (F func, std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type >
 Execute a function over the elements of a vector. More...
 
auto Map (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
 Execute a function without arguments several times. More...
 
T * MapReduce (F func, const std::vector< T * > &args)
 Execute a function over the TObject-inheriting elements of an immutable vector (Map) and merge the objects into a single one (Reduce). More...
 
auto MapReduce (F func, const std::vector< T > &args, R redfunc) -> typename std::result_of< F(T)>::type
 Execute a function over the elements of an immutable vector (Map) and accumulate the results into a single value (Reduce). More...
 
auto MapReduce (F func, ROOT::TSeq< INTEGER > args, R redfunc) -> typename std::result_of< F(INTEGER)>::type
 Execute a function over a sequence of indexes (Map) and accumulate the results into a single value (Reduce). More...
 
auto MapReduce (F func, std::initializer_list< T > args, R redfunc) -> typename std::result_of< F(T)>::type
 Execute a function over the elements of an initializer_list (Map) and accumulate the results into a single value (Reduce). More...
 
T * MapReduce (F func, std::vector< T * > &args)
 Execute a function over the TObject-inheriting elements of a vector (Map) and merge the objects into a single one (Reduce). More...
 
auto MapReduce (F func, std::vector< T > &args, R redfunc) -> typename std::result_of< F(T)>::type
 Execute a function over the elements of a vector (Map) and accumulate the results into a single value (Reduce). More...
 
auto MapReduce (F func, unsigned nTimes, R redfunc) -> typename std::result_of< F()>::type
 Execute a function without arguments several times (Map) and accumulate the results into a single value (Reduce). More...
 
TExecutorCRTPoperator= (const TExecutorCRTP &)=delete
  More...
 
T * Reduce (const std::vector< T * > &mergeObjs)
 "Reduce" an std::vector into a single object by using the object's Merge method. More...
 
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...
 

Private Types

using Unused_t = ROOT::TSequentialExecutor
  More...
 

Private Member Functions

template<class F , class T , class Cond = noReferenceCond<F, T>>
auto MapImpl (F func, const std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type >
 Execute a function over the elements of an immutable vector. More...
 
template<class F , class INTEGER , class Cond = noReferenceCond<F, INTEGER>>
auto MapImpl (F func, ROOT::TSeq< INTEGER > args) -> std::vector< typename std::result_of< F(INTEGER)>::type >
 Execute a function over a sequence of indexes. More...
 
template<class F , class T , class Cond = noReferenceCond<F, T>>
auto MapImpl (F func, std::vector< T > &args) -> std::vector< typename std::result_of< F(T)>::type >
 Execute a function over the elements of a vector. More...
 
template<class F , class Cond = noReferenceCond<F>>
auto MapImpl (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
 Execute a function without arguments several times. More...
 
template<class F , class T >
auto ResolveExecutorAndMap (F func, T &&args) -> std::vector< typename MapRetType< F, typename std::decay< T >::type >::type >
 Function called from Map to select and execute the correct Executor according to the set Execution Policy. More...
 

Private Attributes

ROOT::EExecutionPolicy fExecPolicy
  More...
 
std::unique_ptr< R__EXECUTOR_PROCESSfProcessExecutor
  More...
 
std::unique_ptr< ROOT::TSequentialExecutorfSequentialExecutor
  More...
 
std::unique_ptr< R__EXECUTOR_THREADfThreadExecutor
  More...
 
friend TExecutorCRTP
  More...
 

Additional Inherited Members

- Public Types inherited from ROOT::TExecutorCRTP< TExecutor >
using noReferenceCond = typename std::enable_if<"Function can't return a reference" &&!(std::is_reference< typename std::result_of< F(T...)>::type >::value)>::type
 type definition in used in templated functions for not allowing mapping functions that return references. More...
 

#include <ROOT/TExecutor.hxx>

Inheritance diagram for ROOT::Internal::TExecutor:
[legend]

Member Typedef Documentation

◆ Unused_t

Constructor & Destructor Documentation

◆ TExecutor() [1/3]

ROOT::Internal::TExecutor::TExecutor ( unsigned  nWorkers = 0)
inlineexplicit

Class constructor.

Sets the default execution policy and initializes the corresponding executor. Defaults to multithreaded execution policy if ROOT is compiled with IMT=ON and IsImplicitMTEnabled. Otherwise it defaults to a serial execution policy

Parameters
nWorkers[optional] Number of parallel workers, only taken into account if the execution policy is kMultiThread

Definition at line 44 of file TExecutor.hxx.

◆ TExecutor() [2/3]

ROOT::Internal::TExecutor::TExecutor ( ROOT::EExecutionPolicy  execPolicy,
unsigned  nWorkers = 0 
)
explicit

Class constructor.

Sets the execution policy and initializes the corresponding executor.

Parameters
execPolicyExecution policy(kMultiThread, kMultiprocess, kSerial) to process the data
nWorkers[optional] Number of parallel workers, only taken into account if the execution policy is kMultiThread

Definition at line 76 of file TExecutor.cxx.

◆ TExecutor() [3/3]

ROOT::Internal::TExecutor::TExecutor ( const TExecutor )
delete

Member Function Documentation

◆ GetPoolSize()

unsigned ROOT::Internal::TExecutor::GetPoolSize ( ) const
inline

Return the number of pooled workers.

Returns
The number of workers in the pool in the executor used as a backend.

Definition at line 295 of file TExecutor.hxx.

◆ MapImpl() [1/4]

template<class F , class T , class Cond >
auto ROOT::Internal::TExecutor::MapImpl ( F  func,
const std::vector< T > &  args 
) -> std::vector<typename std::result_of<F(T)>::type>
private

Execute a function over the elements of an immutable vector.

Implementation of the Map method.

Parameters
funcFunction to be executed on the elements of the vector passed as second parameter.
argsVector of elements passed as an argument to func.
Returns
A vector with the results of the function calls.

Definition at line 191 of file TExecutor.hxx.

◆ MapImpl() [2/4]

template<class F , class INTEGER , class Cond >
auto ROOT::Internal::TExecutor::MapImpl ( F  func,
ROOT::TSeq< INTEGER >  args 
) -> std::vector<typename std::result_of<F(INTEGER)>::type>
private

Execute a function over a sequence of indexes.

Implementation of the Map method.

Parameters
funcFunction to be executed. Must take an element of the sequence passed assecond argument as a parameter.
argsSequence of indexes to execute func on.
Returns
A vector with the results of the function calls.

Definition at line 171 of file TExecutor.hxx.

◆ MapImpl() [3/4]

template<class F , class T , class Cond >
auto ROOT::Internal::TExecutor::MapImpl ( F  func,
std::vector< T > &  args 
) -> std::vector<typename std::result_of<F(T)>::type>
private

Execute a function over the elements of a vector.

Implementation of the Map method.

Parameters
funcFunction to be executed on the elements of the vector passed as second parameter.
argsVector of elements passed as an argument to func.
Returns
A vector with the results of the function calls.

Definition at line 181 of file TExecutor.hxx.

◆ MapImpl() [4/4]

template<class F , class Cond >
auto ROOT::Internal::TExecutor::MapImpl ( F  func,
unsigned  nTimes 
) -> std::vector<typename std::result_of<F()>::type>
private

Execute a function without arguments several times.

Implementation of the Map method.

Parameters
funcFunction to be executed.
nTimesNumber of times function should be called.
Returns
A vector with the results of the function calls. Functions that take arguments can be executed (with fixed arguments) by wrapping them in a lambda or with std::bind.

Definition at line 161 of file TExecutor.hxx.

◆ MapReduce() [1/5]

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

Execute a function over the elements of an immutable vector (Map) and accumulate the results into a single value (Reduce).

Benefits from partial reduction into nChunks intermediate results if the execution policy is multithreaded. Otherwise, it ignores the nChunks argument and performs a normal MapReduce operation.

Parameters
funcFunction to be executed. Must take an element of the sequence passed assecond argument as a parameter.
argsImmutable vector, whose elements are passed as an argument to func.
redfuncReduction function to combine the results of the calls to func into partial results, and these into a final result. Must return the same type as func.
nChunksNumber of chunks to split the input data for processing.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 283 of file TExecutor.hxx.

◆ MapReduce() [2/5]

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

Execute a function over a sequence of indexes (Map) and accumulate the results into a single value (Reduce).

Benefits from partial reduction into nChunks intermediate results if the execution policy is multithreaded. Otherwise, it ignores the nChunks argument and performs a normal MapReduce operation.

Parameters
funcFunction to be executed. Must take an element of the sequence passed assecond argument as a parameter.
argsSequence of indexes to execute func on.
redfuncReduction function to combine the results of the calls to func into partial results, and these into a final result. Must return the same type as func.
nChunksNumber of chunks to split the input data for processing.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 226 of file TExecutor.hxx.

◆ MapReduce() [3/5]

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

Execute a function over the elements of an initializer_list (Map) and accumulate the results into a single value (Reduce).

Benefits from partial reduction into nChunks intermediate results if the execution policy is multithreaded. Otherwise, it ignores the nChunks argument and performs a normal MapReduce operation.

Parameters
funcFunction to be executed. Must take an element of the sequence passed assecond argument as a parameter.
argsinitializer_list for a vector to apply func on.
redfuncReduction function to combine the results of the calls to func into partial results, and these into a final result. Must return the same type as func.
nChunksNumber of chunks to split the input data for processing.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 245 of file TExecutor.hxx.

◆ MapReduce() [4/5]

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

Execute a function over the elements of a vector (Map) and accumulate the results into a single value (Reduce).

Benefits from partial reduction into nChunks intermediate results if the execution policy is multithreaded. Otherwise, it ignores the nChunks argument and performs a normal MapReduce operation.

Parameters
funcFunction to be executed. Must take an element of the sequence passed assecond argument as a parameter.
argsVector of elements passed as an argument to func.
redfuncReduction function to combine the results of the calls to func into partial results, and these into a final result. Must return the same type as func.
nChunksNumber of chunks to split the input data for processing.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 264 of file TExecutor.hxx.

◆ MapReduce() [5/5]

template<class F , class R , class Cond >
auto ROOT::Internal::TExecutor::MapReduce ( F  func,
unsigned  nTimes,
R  redfunc,
unsigned  nChunks 
) -> typename std::result_of<F()>::type

Execute a function nTimes (Map) and accumulate the results into a single value (Reduce).

Benefits from partial reduction into nChunks intermediate results if the execution policy is multithreaded. Otherwise, it ignores the nChunks argument and performs a normal MapReduce operation.

Parameters
funcFunction to be executed. Must take an element of the sequence passed as second argument as a parameter.
nTimesNumber of times function should be called.
redfuncReduction function to combine the results of the calls to func into partial results, and these into a final result. Must return the same type as func.
nChunksNumber of chunks to split the input data for processing.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 207 of file TExecutor.hxx.

◆ operator=()

TExecutor& ROOT::Internal::TExecutor::operator= ( const TExecutor )
delete

◆ Policy()

ROOT::EExecutionPolicy ROOT::Internal::TExecutor::Policy ( ) const
inline

Return the execution policy the executor is set to.

Definition at line 56 of file TExecutor.hxx.

◆ ResolveExecutorAndMap()

template<class F , class T >
auto ROOT::Internal::TExecutor::ResolveExecutorAndMap ( F  func,
T &&  args 
) -> std::vector<typename MapRetType<F, typename std::decay<T>::type>::type>
inlineprivate

Function called from Map to select and execute the correct Executor according to the set Execution Policy.

Definition at line 135 of file TExecutor.hxx.

Member Data Documentation

◆ fExecPolicy

ROOT::EExecutionPolicy ROOT::Internal::TExecutor::fExecPolicy
private

Definition at line 96 of file TExecutor.hxx.

◆ fProcessExecutor

std::unique_ptr<R__EXECUTOR_PROCESS> ROOT::Internal::TExecutor::fProcessExecutor
private

Definition at line 113 of file TExecutor.hxx.

◆ fSequentialExecutor

std::unique_ptr<ROOT::TSequentialExecutor> ROOT::Internal::TExecutor::fSequentialExecutor
private

Definition at line 114 of file TExecutor.hxx.

◆ fThreadExecutor

std::unique_ptr<R__EXECUTOR_THREAD> ROOT::Internal::TExecutor::fThreadExecutor
private

Definition at line 112 of file TExecutor.hxx.

◆ TExecutorCRTP

friend ROOT::Internal::TExecutor::TExecutorCRTP
private

Definition at line 38 of file TExecutor.hxx.

Libraries for ROOT::Internal::TExecutor:
[legend]

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