Logo ROOT   6.10/09
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 R, class Cond = noReferenceCond<F>>
88  auto MapReduce(F func, unsigned nTimes, R redfunc) -> typename std::result_of<F()>::type;
89  template<class F, class INTEGER, class R, class Cond = noReferenceCond<F, INTEGER>>
90  auto MapReduce(F func, ROOT::TSeq<INTEGER> args, R redfunc) -> typename std::result_of<F(INTEGER)>::type;
91  /// \cond
92  template<class F, class T, class R, class Cond = noReferenceCond<F, T>>
93  auto MapReduce(F func, std::initializer_list<T> args, R redfunc) -> typename std::result_of<F(T)>::type;
94  /// \endcond
95  template<class F, class T, class R, class Cond = noReferenceCond<F, T>>
96  auto MapReduce(F func, std::vector<T> &args, R redfunc) -> typename std::result_of<F(T)>::type;
97  template<class F, class T, class Cond = noReferenceCond<F, T>>
98  T* MapReduce(F func, std::vector<T*> &args);
99 
100  template<class T> T* Reduce(const std::vector<T*> &mergeObjs);
101 
102 private:
103  inline subc & Derived()
104  {
105  return *static_cast<subc*>(this);
106  }
107 };
108 
109 //////////////////////////////////////////////////////////////////////////
110 /// Execute func (with no arguments) nTimes in parallel.
111 /// A vector containg executions' results is returned.
112 /// Functions that take more than zero arguments can be executed (with
113 /// fixed arguments) by wrapping them in a lambda or with std::bind.
114 template<class subc> template<class F, class Cond>
116 {
117  return Derived().Map(func, nTimes);
118 }
119 
120 //////////////////////////////////////////////////////////////////////////
121 /// Execute func in parallel, taking an element of a
122 /// sequence as argument. Divides and groups the executions in nChunks with partial reduction;
123 /// A vector containg partial reductions' results is returned.
124 template<class subc> template<class F, class INTEGER, class Cond>
126 {
127  return Derived().Map(func, args);
128 }
129 
130 //////////////////////////////////////////////////////////////////////////
131 /// Execute func in parallel, taking an element of the std::initializer_list
132 /// as argument. Divides and groups the executions in nChunks with partial reduction;
133 /// A vector containg partial reductions' results is returned.
134 template<class subc> template<class F, class T, class Cond>
136 {
137  std::vector<T> vargs(std::move(args));
138  const auto &reslist = Map(func, vargs);
139  return reslist;
140 }
141 
142 //////////////////////////////////////////////////////////////////////////
143 /// Execute func in parallel, taking an element of an
144 /// std::vector as argument.
145 /// A vector containg executions' results is returned.
146 // actual implementation of the Map method. all other calls with arguments eventually
147 // call this one
148 template<class subc> template<class F, class T, class Cond>
150 {
151  return Derived().Map(func, args);
152 }
153 
154 //////////////////////////////////////////////////////////////////////////
155 /// This method behaves just like Map, but an additional redfunc function
156 /// must be provided. redfunc is applied to the vector Map would return and
157 /// must return the same type as func. In practice, redfunc can be used to
158 /// "squash" the vector returned by Map into a single object by merging,
159 /// adding, mixing the elements of the vector.
160 template<class subc> template<class F, class R, class Cond>
161 auto TExecutor<subc>::MapReduce(F func, unsigned nTimes, R redfunc) -> typename std::result_of<F()>::type
162 {
163  return Derived().Reduce(Map(func, nTimes), redfunc);
164 }
165 
166 template<class subc> template<class F, class INTEGER, class R, class Cond>
168 {
169  return Derived().Reduce(Map(func, args), redfunc);
170 }
171 
172 template<class subc> template<class F, class T, class R, class Cond>
173 auto TExecutor<subc>::MapReduce(F func, std::initializer_list<T> args, R redfunc) -> typename std::result_of<F(T)>::type
174 {
175  return Derived().Reduce(Map(func, args), redfunc);
176 }
177 
178 template<class subc> template<class F, class T, class R, class Cond>
179 auto TExecutor<subc>::MapReduce(F func, std::vector<T> &args, R redfunc) -> typename std::result_of<F(T)>::type
180 {
181  return Derived().Reduce(Map(func, args), redfunc);
182 }
183 
184 template<class subc> template<class F, class T, class Cond>
185 T* TExecutor<subc>::MapReduce(F func, std::vector<T*> &args)
186 {
187  return Derived().Reduce(Map(func, args));
188 }
189 
190 //////////////////////////////////////////////////////////////////////////
191 /// "Reduce" an std::vector into a single object by using the object's Merge
192 //Reduction for objects with the Merge() method
193 template<class subc> template<class T>
194 T* TExecutor<subc>::Reduce(const std::vector<T*> &mergeObjs)
195 {
196  TList l;
197  for(unsigned i =1; i<mergeObjs.size(); i++){
198  l.Add(mergeObjs[i]);
199  }
200  // use clone to return a new object
201  auto retHist = dynamic_cast<T*>((mergeObjs.front())->Clone());
202  if (retHist) retHist->Merge(&l);
203  return retHist;
204 }
205 
206 } // end namespace ROOT
207 
208 #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
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:43
#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:115
TLine * l
Definition: textangle.C:4
subc & Derived()
Definition: TExecutor.hxx:103
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
auto MapReduce(F func, unsigned nTimes, R redfunc) -> typename std::result_of< F()>::type
This method behaves just like Map, but an additional redfunc function must be provided.
Definition: TExecutor.hxx:161
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual void Add(TObject *obj)
Definition: TList.h:77
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:194
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28