Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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:38
auto MapReduce(F func, unsigned nTimes, R redfunc, unsigned nChunks) -> InvokeResult_t< F >
Execute a function nTimes (Map) and accumulate the results into a single value (Reduce).

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

Private Types

template<typename F , typename... Args>
using InvokeResult_t = ROOT::TypeTraits::InvokeResult_t< F, Args... >
 
using Unused_t = ROOT::TSequentialExecutor
 

Private Member Functions

template<class F , class T , class Cond = noReferenceCond<F, T>>
auto MapImpl (F func, const std::vector< T > &args) -> std::vector< InvokeResult_t< F, T > >
 Execute a function over the elements of an immutable vector.
 
template<class F , class INTEGER , class Cond = noReferenceCond<F, INTEGER>>
auto MapImpl (F func, ROOT::TSeq< INTEGER > args) -> std::vector< InvokeResult_t< F, INTEGER > >
 Execute a function over a sequence of indexes.
 
template<class F , class T , class Cond = noReferenceCond<F, T>>
auto MapImpl (F func, std::vector< T > &args) -> std::vector< InvokeResult_t< F, T > >
 Execute a function over the elements of a vector.
 
template<class F , class Cond = noReferenceCond<F>>
auto MapImpl (F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >
 Execute a function without arguments several times.
 
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.
 

Private Attributes

ROOT::EExecutionPolicy fExecPolicy
 
std::unique_ptr< ROOT::TProcessExecutorfProcessExecutor
 
std::unique_ptr< ROOT::TSequentialExecutorfSequentialExecutor
 
std::unique_ptr< ROOT::TThreadExecutorfThreadExecutor
 
friend TExecutorCRTP
 

Additional Inherited Members

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

#include <ROOT/TExecutor.hxx>

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

Member Typedef Documentation

◆ InvokeResult_t

template<typename F , typename... Args>
using ROOT::Internal::TExecutor::InvokeResult_t = ROOT::TypeTraits::InvokeResult_t<F, Args...>
private

Definition at line 42 of file TExecutor.hxx.

◆ 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 49 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 309 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<InvokeResult_t<F, T>>
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 199 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<InvokeResult_t<F, INTEGER>>
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 177 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<InvokeResult_t<F, T>>
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 188 of file TExecutor.hxx.

◆ MapImpl() [4/4]

template<class F , class Cond >
auto ROOT::Internal::TExecutor::MapImpl ( F  func,
unsigned  nTimes 
) -> std::vector<InvokeResult_t<F>>
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 166 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 
) -> InvokeResult_t<F, T>

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 296 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 
) -> InvokeResult_t<F, INTEGER>

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 236 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 
) -> InvokeResult_t<F, T>

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 256 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 
) -> InvokeResult_t<F, T>

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 276 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 
) -> InvokeResult_t<F>

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 216 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 61 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 140 of file TExecutor.hxx.

Member Data Documentation

◆ fExecPolicy

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

Definition at line 101 of file TExecutor.hxx.

◆ fProcessExecutor

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

Definition at line 118 of file TExecutor.hxx.

◆ fSequentialExecutor

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

Definition at line 119 of file TExecutor.hxx.

◆ fThreadExecutor

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

Definition at line 117 of file TExecutor.hxx.

◆ TExecutorCRTP

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

Definition at line 39 of file TExecutor.hxx.

Libraries for ROOT::Internal::TExecutor:

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