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< typename std::result_of< F()>::type >
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) -> typename std::result_of< F()>::type
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) -> typename std::result_of< F()>::type
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 102 of file TExecutorCRTP.hxx.

Public Types

template<class F , class... T>
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.
 

Public Member Functions

 TExecutorCRTP ()=default
 
 TExecutorCRTP (const TExecutorCRTP &)=delete
 
template<class F , class T , class Cond = noReferenceCond<F, T>>
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.
 
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 a function over a sequence of indexes.
 
template<class F , class T , class Cond = noReferenceCond<F, T>>
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.
 
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 a function over the elements of a vector.
 
template<class F , class Cond = noReferenceCond<F>>
auto Map (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
 Execute a function without arguments several times.
 
template<class F , class T , class Cond = noReferenceCond<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 = noReferenceCond<F, T>>
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).
 
template<class F , class INTEGER , class R , class Cond = noReferenceCond<F, INTEGER>>
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).
 
template<class F , class T , class R , class Cond = noReferenceCond<F, T>>
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).
 
template<class F , class T , class Cond = noReferenceCond<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 = noReferenceCond<F, T>>
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).
 
template<class F , class R , class Cond = noReferenceCond<F>>
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).
 
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.
 

Private Member Functions

SubC & Derived ()
 
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 >=delete
 Implementation of the Map method, left to the derived classes.
 
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 >=delete
 Implementation of the Map method, left to the derived classes.
 
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 >=delete
 Implementation of the Map method, left to the derived classes.
 
template<class F , class Cond = noReferenceCond<F>>
auto MapImpl (F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >=delete
 Implementation of the Map method, left to the derived classes.
 

#include <ROOT/TExecutorCRTP.hxx>

Member Typedef Documentation

◆ noReferenceCond

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

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 150 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<typename std::result_of<F(T)>::type>

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 230 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<typename std::result_of<F(INTEGER)>::type>

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 191 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<typename std::result_of<F(T)>::type>

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 203 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<typename std::result_of<F(T)>::type>

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 217 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<typename std::result_of<F()>::type>

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

◆ MapImpl() [1/4]

template<class SubC >
template<class F , class T , class Cond = noReferenceCond<F, T>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
const std::vector< T > &  args 
) -> std::vector< typename std::result_of< F(T)>::type >=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 = noReferenceCond<F, INTEGER>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
ROOT::TSeq< INTEGER >  args 
) -> std::vector< typename std::result_of< F(INTEGER)>::type >=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 = noReferenceCond<F, T>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
std::vector< T > &  args 
) -> std::vector< typename std::result_of< F(T)>::type >=delete
privatedelete

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

◆ MapImpl() [4/4]

template<class SubC >
template<class F , class Cond = noReferenceCond<F>>
auto ROOT::TExecutorCRTP< SubC >::MapImpl ( F  func,
unsigned  nTimes 
) -> std::vector< typename std::result_of< F()>::type >=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.
argsImmutableector 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 321 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 
) -> 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).

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 296 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 
) -> 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).

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 257 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 
) -> 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).

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 270 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 308 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 
) -> 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).

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 284 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 
) -> typename std::result_of<F()>::type

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 244 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 332 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 358 of file TExecutorCRTP.hxx.


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