Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROOT::TExecutorCRTP< SubC > Class Template Reference

template<class SubC>
class ROOT::TExecutorCRTP< SubC >

This class defines an interface to execute the same task multiple times, possibly in parallel and with different arguments every time.

ROOT::TExecutorCRTP<SubC>::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

The Map function forwards the call to MapImpl, to be implemented by the child classes.

For either signature, func is executed as many times as needed by a pool of n workers, where n typically defaults to the number of available 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::TExecutorCRTP derived classes never delete what they return, they simply forget 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. The ordering of the elements corresponds to the ordering of the arguments.

ROOT::TExecutorCRTP<SubC>::Reduce

These set of methods combine all elements from a std::vector into a single value.

Parameters
redfunca callable object, such as a lambda expression, an std::function, a functor object or a function that takes an std::vector and combines all its elements into a single result.
[args]a standard vector

ROOT::TExecutorCRTP<SubC>::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. This function should be independent of the size of the vector returned by Map due to optimization of the number of chunks.

Examples:

Generate 1 ten times and sum those tens
root[] ROOT::TProcessExecutor pool; auto ten = pool.MapReduce([]() { return 1; }, 10, [](const std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); })
root[] ROOT::TProcessExecutor pool; auto tenOnes = pool.Map([]() { return 1; }, 10); auto ten = Reduce([](const std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); }, tenOnes)
Create 10 histograms and merge them into one
root[] ROOT::TThreadExecutor pool; auto hist = pool.MapReduce(CreateAndFillHists, 10, PoolUtils::ReduceObjects);
Merge collection of TObjects.
Definition PoolUtils.h:35
auto Map(F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >
Execute a function without arguments several times.
T * Reduce(const std::vector< T * > &mergeObjs)
"Reduce" an std::vector into a single object by using the object's Merge method.
This class provides a simple interface to execute the same task multiple times in parallel,...
auto MapReduce(F func, unsigned nTimes, R redfunc) -> InvokeResult_t< F >
Execute a function nTimes in parallel (Map) and accumulate the results into a single value (Reduce).
This class provides a simple interface to execute the same task multiple times in parallel threads,...
auto MapReduce(F func, unsigned nTimes, R redfunc) -> InvokeResult_t< F >
Execute a function nTimes in parallel (Map) and accumulate the results into a single value (Reduce).
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345

Definition at line 103 of file TExecutorCRTP.hxx.

Public Member Functions

 TExecutorCRTP ()=default
 
 TExecutorCRTP (const TExecutorCRTP &)=delete
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
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.
 
template<class F , class INTEGER , class Cond = validMapReturnCond<F, INTEGER>>
auto Map (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 = validMapReturnCond<F, T>>
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.
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
auto Map (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 = validMapReturnCond<F>>
auto Map (F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >
 Execute a function without arguments several times.
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
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).
 
template<class F , class T , class R , class Cond = validMapReturnCond<F, T>>
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).
 
template<class F , class INTEGER , class R , class Cond = validMapReturnCond<F, INTEGER>>
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).
 
template<class F , class T , class R , class Cond = validMapReturnCond<F, T>>
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).
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
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).
 
template<class F , class T , class R , class Cond = validMapReturnCond<F, T>>
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).
 
template<class F , class R , class Cond = validMapReturnCond<F>>
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
 
template<class T >
T * Reduce (const std::vector< T * > &mergeObjs)
 "Reduce" an std::vector into a single object by using the object's Merge method.
 
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.
 

Protected Types

template<typename F , typename... Args>
using InvokeResult_t = ROOT::TypeTraits::InvokeResult_t< F, Args... >
 
template<class F , class... T>
using validMapReturnCond = std::enable_if_t<!std::is_reference< InvokeResult_t< F, T... > >::value &&!std::is_void< InvokeResult_t< F, T... > >::value >
 type definition used in templated functions for not allowing mapping functions that return references or void.
 

Private Member Functions

SubC & Derived ()
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
auto MapImpl (F func, const std::vector< T > &args) -> std::vector< InvokeResult_t< F, T > >=delete
 Implementation of the Map method, left to the derived classes.
 
template<class F , class INTEGER , class Cond = validMapReturnCond<F, INTEGER>>
auto MapImpl (F func, ROOT::TSeq< INTEGER > args) -> std::vector< InvokeResult_t< F, INTEGER > >=delete
 Implementation of the Map method, left to the derived classes.
 
template<class F , class T , class Cond = validMapReturnCond<F, T>>
auto MapImpl (F func, std::vector< T > &args) -> std::vector< InvokeResult_t< F, T > >=delete
 Implementation of the Map method, left to the derived classes.
 
template<class F , class Cond = validMapReturnCond<F>>
auto MapImpl (F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >=delete
 Implementation of the Map method, left to the derived classes.
 

#include <ROOT/TExecutorCRTP.hxx>

Member Typedef Documentation

◆ InvokeResult_t

template<class SubC >
template<typename F , typename... Args>
using ROOT::TExecutorCRTP< SubC >::InvokeResult_t = ROOT::TypeTraits::InvokeResult_t<F, Args...>
protected

Definition at line 107 of file TExecutorCRTP.hxx.

◆ validMapReturnCond

template<class SubC >
template<class F , class... T>
using ROOT::TExecutorCRTP< SubC >::validMapReturnCond = std::enable_if_t<!std::is_reference<InvokeResult_t<F, T...> >::value && !std::is_void<InvokeResult_t<F, T...> >::value>
protected

type definition used in templated functions for not allowing mapping functions that return references or void.

The resulting vector elements must be assignable, references aren't.

Definition at line 112 of file TExecutorCRTP.hxx.

Constructor & Destructor Documentation

◆ TExecutorCRTP() [1/2]

template<class SubC >
ROOT::TExecutorCRTP< SubC >::TExecutorCRTP ( )
default

◆ TExecutorCRTP() [2/2]

template<class SubC >
ROOT::TExecutorCRTP< SubC >::TExecutorCRTP ( const TExecutorCRTP< SubC > &  )
delete

Member Function Documentation

◆ Derived()

template<class SubC >
SubC & ROOT::TExecutorCRTP< SubC >::Derived ( )
inlineprivate

Definition at line 156 of file TExecutorCRTP.hxx.

◆ Map() [1/5]

template<class SubC >
template<class F , class T , class Cond >
auto ROOT::TExecutorCRTP< SubC >::Map ( F  func,
const std::vector< T > &  args 
) -> std::vector<InvokeResult_t<F, T>>

Execute a function over the elements of an immutable vector.

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 240 of file TExecutorCRTP.hxx.

◆ Map() [2/5]

template<class SubC >
template<class F , class INTEGER , class Cond >
auto ROOT::TExecutorCRTP< SubC >::Map ( F  func,
ROOT::TSeq< INTEGER >  args 
) -> std::vector<InvokeResult_t<F, INTEGER>>

Execute a function over a sequence of indexes.

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 198 of file TExecutorCRTP.hxx.

◆ Map() [3/5]

template<class SubC >
template<class F , class T , class Cond >
auto ROOT::TExecutorCRTP< SubC >::Map ( F  func,
std::initializer_list< T >  args 
) -> std::vector<InvokeResult_t<F, T>>

Execute a function over the elements of an initializer_list.

Parameters
funcFunction to be executed on the elements of the initializer_list passed as second parameter.
argsinitializer_list for a vector to apply func on.
Returns
A vector with the results of the function calls.

Definition at line 211 of file TExecutorCRTP.hxx.

◆ Map() [4/5]

template<class SubC >
template<class F , class T , class Cond >
auto ROOT::TExecutorCRTP< SubC >::Map ( F  func,
std::vector< T > &  args 
) -> std::vector<InvokeResult_t<F, T>>

Execute a function over the elements of a vector.

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 226 of file TExecutorCRTP.hxx.

◆ Map() [5/5]

template<class SubC >
template<class F , class Cond >
auto ROOT::TExecutorCRTP< SubC >::Map ( F  func,
unsigned  nTimes 
) -> std::vector<InvokeResult_t<F>>

Execute a function without arguments several times.

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 185 of file TExecutorCRTP.hxx.

◆ MapImpl() [1/4]

template<class SubC >
template<class F , class T , class Cond = validMapReturnCond<F, T>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
const std::vector< T > &  args 
) -> std::vector< InvokeResult_t< F, T > >=delete
privatedelete

Implementation of the Map method, left to the derived classes.

◆ MapImpl() [2/4]

template<class SubC >
template<class F , class INTEGER , class Cond = validMapReturnCond<F, INTEGER>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
ROOT::TSeq< INTEGER >  args 
) -> std::vector< InvokeResult_t< F, INTEGER > >=delete
privatedelete

Implementation of the Map method, left to the derived classes.

◆ MapImpl() [3/4]

template<class SubC >
template<class F , class T , class Cond = validMapReturnCond<F, T>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
std::vector< T > &  args 
) -> std::vector< InvokeResult_t< F, T > >=delete
privatedelete

Implementation of the Map method, left to the derived classes.

◆ MapImpl() [4/4]

template<class SubC >
template<class F , class Cond = validMapReturnCond<F>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
unsigned  nTimes 
) -> std::vector< InvokeResult_t< F > >=delete
privatedelete

Implementation of the Map method, left to the derived classes.

◆ MapReduce() [1/7]

template<class SubC >
template<class F , class T , class Cond >
T * ROOT::TExecutorCRTP< SubC >::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).

Parameters
funcFunction to be executed on the elements of the vector passed as second parameter.
argsImmutable vector of elements passed as an argument to func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 336 of file TExecutorCRTP.hxx.

◆ MapReduce() [2/7]

template<class SubC >
template<class F , class T , class R , class Cond >
auto ROOT::TExecutorCRTP< SubC >::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).

Parameters
funcFunction to be executed on the elements of the vector passed as second parameter.
argsImmutable vector of elements passed as an argument to func.
redfuncReduction function to combine the results of the calls to func. Must return the same type as func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 312 of file TExecutorCRTP.hxx.

◆ MapReduce() [3/7]

template<class SubC >
template<class F , class INTEGER , class R , class Cond >
auto ROOT::TExecutorCRTP< SubC >::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).

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. Must return the same type as func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 269 of file TExecutorCRTP.hxx.

◆ MapReduce() [4/7]

template<class SubC >
template<class F , class T , class R , class Cond >
auto ROOT::TExecutorCRTP< SubC >::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).

Parameters
funcFunction to be executed on the elements of the initializer_list passed as second parameter.
argsinitializer_list for a vector to apply func on.
redfuncReduction function to combine the results of the calls to func. Must return the same type as func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 283 of file TExecutorCRTP.hxx.

◆ MapReduce() [5/7]

template<class SubC >
template<class F , class T , class Cond >
T * ROOT::TExecutorCRTP< SubC >::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).

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 value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 324 of file TExecutorCRTP.hxx.

◆ MapReduce() [6/7]

template<class SubC >
template<class F , class T , class R , class Cond >
auto ROOT::TExecutorCRTP< SubC >::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).

Parameters
funcFunction to be executed on the elements of the vector passed as second parameter.
argsVector of elements passed as an argument to func.
redfuncReduction function to combine the results of the calls to func. Must return the same type as func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 298 of file TExecutorCRTP.hxx.

◆ MapReduce() [7/7]

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

Parameters
funcFunction to be executed.
nTimesNumber of times function should be called.
Returns
A vector with the results of the function calls.
Parameters
redfuncReduction function to combine the results of the calls to func. Must return the same type as func.
Returns
A value result of "reducing" the vector returned by the Map operation into a single object.

Definition at line 255 of file TExecutorCRTP.hxx.

◆ operator=()

template<class SubC >
TExecutorCRTP & ROOT::TExecutorCRTP< SubC >::operator= ( const TExecutorCRTP< SubC > &  )
delete

◆ Reduce() [1/2]

template<class SubC >
template<class T >
T * ROOT::TExecutorCRTP< SubC >::Reduce ( const std::vector< T * > &  mergeObjs)

"Reduce" an std::vector into a single object by using the object's Merge method.

Parameters
mergeObjsA vector of ROOT objects implementing the Merge method
Returns
An object result of merging the vector elements into one.

Definition at line 347 of file TExecutorCRTP.hxx.

◆ Reduce() [2/2]

template<class SubC >
template<class T , class R >
auto ROOT::TExecutorCRTP< SubC >::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.

Parameters
objsA vector of elements to combine.
redfuncReduction function to combine the elements of the vector objs
Returns
A value result of combining the vector elements into a single object of the same type.

Definition at line 373 of file TExecutorCRTP.hxx.


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