Logo ROOT   6.12/07
Reference Guide
TExecutor.hxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Xavier Valls March 2016
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TExecutor
13 #define ROOT_TExecutor
14 
15 #include "ROOT/TSeq.hxx"
16 #include "TList.h"
17 #include <vector>
18 
19 //////////////////////////////////////////////////////////////////////////
20 ///
21 /// \class ROOT::TExecutor
22 /// \brief This class defines an interface to execute the same task
23 /// multiple times in parallel, possibly with different arguments every
24 /// time. The classes implementing it mimic the behaviour of python's pool.Map method.
25 ///
26 /// ###ROOT::TExecutor::Map
27 /// The two possible usages of the Map method are:\n
28 /// * Map(F func, unsigned nTimes): func is executed nTimes with no arguments
29 /// * Map(F func, T& args): func is executed on each element of the collection of arguments args
30 ///
31 /// For either signature, func is executed as many times as needed by a pool of
32 /// nThreads threads; It defaults to the number of cores.\n
33 /// A collection containing the result of each execution is returned.\n
34 /// **Note:** the user is responsible for the deletion of any object that might
35 /// be created upon execution of func, returned objects included: ROOT::TExecutor never
36 /// deletes what it returns, it simply forgets it.\n
37 ///
38 /// \param func
39 /// \parblock
40 /// a lambda expression, an std::function, a loaded macro, a
41 /// functor class or a function that takes zero arguments (for the first signature)
42 /// or one (for the second signature).
43 /// \endparblock
44 /// \param args
45 /// \parblock
46 /// a standard vector, a ROOT::TSeq of integer type or an initializer list for the second signature.
47 /// An integer only for the first.\n
48 /// \endparblock
49 ///
50 /// **Note:** in cases where the function to be executed takes more than
51 /// zero/one argument but all are fixed except zero/one, the function can be wrapped
52 /// in a lambda or via std::bind to give it the right signature.\n
53 ///
54 /// #### Return value:
55 /// An std::vector. The elements in the container
56 /// will be the objects returned by func.
57 
58 namespace ROOT {
59 
60 template<class subc>
61 class TExecutor {
62 public:
63  explicit TExecutor() = default;
64  explicit TExecutor(size_t /* nThreads */ ){};
65 
66  template< class F, class... T>
67  using noReferenceCond = typename std::enable_if<"Function can't return a reference" && !(std::is_reference<typename std::result_of<F(T...)>::type>::value)>::type;
68 
69  // // Map
70  // //these late return types allow for a compile-time check of compatibility between function signatures and args,
71  // //and a compile-time check that the argument list implements a front() method (all STL sequence containers have it)
72  template<class F, class Cond = noReferenceCond<F>>
73  auto Map(F func, unsigned nTimes) -> std::vector<typename std::result_of<F()>::type>;
74  template<class F, class INTEGER, class Cond = noReferenceCond<F, INTEGER>>
76  /// \cond
77  template<class F, class T, class Cond = noReferenceCond<F, T>>
78  auto Map(F func, std::initializer_list<T> args) -> std::vector<typename std::result_of<F(T)>::type>;
79  /// \endcond
80  template<class F, class T, class Cond = noReferenceCond<F, T>>
81  auto Map(F func, std::vector<T> &args) -> std::vector<typename std::result_of<F(T)>::type>;
82 
83  // // MapReduce
84  // // the late return types also check at compile-time whether redfunc is compatible with func,
85  // // other than checking that func is compatible with the type of arguments.
86  // // a static_assert check in TExecutor<subc>::Reduce is used to check that redfunc is compatible with the type returned by func
87  template<class F, class INTEGER, class R, class Cond = noReferenceCond<F, INTEGER>>
88  auto MapReduce(F func, ROOT::TSeq<INTEGER> args, R redfunc) -> typename std::result_of<F(INTEGER)>::type;
89  /// \cond
90  template<class F, class T, class R, class Cond = noReferenceCond<F, T>>
91  auto MapReduce(F func, std::initializer_list<T> args, R redfunc) -> typename std::result_of<F(T)>::type;
92  /// \endcond
93  template<class F, class T, class Cond = noReferenceCond<F, T>>
94  T* MapReduce(F func, std::vector<T*> &args);
95 
96  template<class T> T* Reduce(const std::vector<T*> &mergeObjs);
97 
98 private:
99  inline subc & Derived()
100  {
101  return *static_cast<subc*>(this);
102  }
103 };
104 
105 //////////////////////////////////////////////////////////////////////////
106 /// Execute func (with no arguments) nTimes in parallel.
107 /// A vector containg executions' results is returned.
108 /// Functions that take more than zero arguments can be executed (with
109 /// fixed arguments) by wrapping them in a lambda or with std::bind.
110 template<class subc> template<class F, class Cond>
112 {
113  return Derived().Map(func, nTimes);
114 }
115 
116 //////////////////////////////////////////////////////////////////////////
117 /// Execute func in parallel, taking an element of a
118 /// sequence as argument. Divides and groups the executions in nChunks with partial reduction;
119 /// A vector containg partial reductions' results is returned.
120 template<class subc> template<class F, class INTEGER, class Cond>
122 {
123  return Derived().Map(func, args);
124 }
125 
126 //////////////////////////////////////////////////////////////////////////
127 /// Execute func in parallel, taking an element of the std::initializer_list
128 /// as argument. Divides and groups the executions in nChunks with partial reduction;
129 /// A vector containg partial reductions' results is returned.
130 template<class subc> template<class F, class T, class Cond>
131 auto TExecutor<subc>::Map(F func, std::initializer_list<T> args) -> std::vector<typename std::result_of<F(T)>::type>
132 {
133  std::vector<T> vargs(std::move(args));
134  const auto &reslist = Map(func, vargs);
135  return reslist;
136 }
137 
138 //////////////////////////////////////////////////////////////////////////
139 /// Execute func in parallel, taking an element of an
140 /// std::vector as argument.
141 /// A vector containg executions' results is returned.
142 // actual implementation of the Map method. all other calls with arguments eventually
143 // call this one
144 template<class subc> template<class F, class T, class Cond>
146 {
147  return Derived().Map(func, args);
148 }
149 
150 //////////////////////////////////////////////////////////////////////////
151 /// This method behaves just like Map, but an additional redfunc function
152 /// must be provided. redfunc is applied to the vector Map would return and
153 /// must return the same type as func. In practice, redfunc can be used to
154 /// "squash" the vector returned by Map into a single object by merging,
155 /// adding, mixing the elements of the vector.
156 template<class subc> template<class F, class INTEGER, class R, class Cond>
158 {
159  std::vector<INTEGER> vargs(args.size());
160  std::copy(args.begin(), args.end(), vargs.begin());
161  return Derived().MapReduce(func, vargs, redfunc);
162 }
163 
164 template<class subc> template<class F, class T, class R, class Cond>
165 auto TExecutor<subc>::MapReduce(F func, std::initializer_list<T> args, R redfunc) -> typename std::result_of<F(T)>::type
166 {
167  std::vector<T> vargs(std::move(args));
168  return Derived().MapReduce(func, vargs, redfunc);
169 }
170 
171 template<class subc> template<class F, class T, class Cond>
172 T* TExecutor<subc>::MapReduce(F func, std::vector<T*> &args)
173 {
174  return Derived().Reduce(Map(func, args));
175 }
176 
177 //////////////////////////////////////////////////////////////////////////
178 /// "Reduce" an std::vector into a single object by using the object's Merge
179 //Reduction for objects with the Merge() method
180 template<class subc> template<class T>
181 T* TExecutor<subc>::Reduce(const std::vector<T*> &mergeObjs)
182 {
183  TList l;
184  for(unsigned i =1; i<mergeObjs.size(); i++){
185  l.Add(mergeObjs[i]);
186  }
187  // use clone to return a new object
188  auto retHist = dynamic_cast<T*>((mergeObjs.front())->Clone());
189  if (retHist) retHist->Merge(&l);
190  return retHist;
191 }
192 
193 } // end namespace ROOT
194 
195 #endif
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
This class defines an interface to execute the same task multiple times in parallel, possibly with different arguments every time.
Definition: TExecutor.hxx:61
auto MapReduce(F func, ROOT::TSeq< INTEGER > args, R redfunc) -> typename std::result_of< F(INTEGER)>::type
This method behaves just like Map, but an additional redfunc function must be provided.
Definition: TExecutor.hxx:157
TExecutor()=default
typename std::enable_if<"Function can't return a reference" &&!(std::is_reference< typename std::result_of< F(T...)>::type >::value)>::type noReferenceCond
Definition: TExecutor.hxx:67
A doubly linked list.
Definition: TList.h:44
#define F(x, y, z)
auto Map(F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
Execute func (with no arguments) nTimes in parallel.
Definition: TExecutor.hxx:111
subc & Derived()
Definition: TExecutor.hxx:99
TExecutor(size_t)
Definition: TExecutor.hxx:64
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
int type
Definition: TGX11.cxx:120
virtual void Add(TObject *obj)
Definition: TList.h:87
auto * l
Definition: textangle.C:4
T * Reduce(const std::vector< T *> &mergeObjs)
"Reduce" an std::vector into a single object by using the object&#39;s Merge
Definition: TExecutor.hxx:181
constexpr Double_t R()
Definition: TMath.h:213