Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Executor.h
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Lorenzo Moneta
3/*************************************************************************
4 * Copyright (C) 2019, ROOT/TMVA *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11//////////////////////////////////////////////////////////////////////////
12//
13// Defining Executor classes to be used in TMVA
14// wrapping the functionality of the ROOT TThreadExecutor and
15// ROOT TSequential Executor
16//
17/////////////////////////////////////////////////////////////////////////
18#ifndef ROOT_TMVA_Executor
19#define ROOT_TMVA_Executor
20
21#include <memory>
22#include <vector>
23
25#ifdef R__USE_IMT
27#endif
28
29#include <TROOT.h>
30#include <TError.h>
31
32namespace TMVA {
33
34
35/// Base Executor class
36class Executor {
37
38 template <typename F, typename... Args>
39 using InvokeResult_t = ROOT::TypeTraits::InvokeResult_t<F, Args...>;
40
41public:
42 template <class F, class... T>
43 using noReferenceCond = typename std::enable_if_t<"Function can't return a reference" &&
44 !(std::is_reference<InvokeResult_t<F, T...>>::value)>;
45
46 //////////////////////////////////////
47 /// Default constructor of TMVA Executor class
48 /// if ROOT::EnableImplicitMT has not been called then by default a serial executor will be created
49 /// A user can create a thread pool and enable multi-thread execution by calling TMVA::Config::Instance()::EnableMT(nthreads)
50 /// For releasing the thread pool used by TMVA one can do it by calling TMVA::Config::Instance()::DisableMT() or
51 /// calling TMVA::Config::Instance()::EnableMT() with only one thread
52 ////////////////////////////////////////////
54 // enable MT in TMVA if ROOT::IsImplicitMT is enabled
56#ifdef R__USE_IMT
57 fMTExecImpl = std::unique_ptr< ROOT::TThreadExecutor>(new ROOT::TThreadExecutor());
58#else
59 ::Error("Executor","Cannot have TMVA in multi-threads mode when ROOT is built without IMT");
60#endif
61 }
62 // case of single thread usage
63 if (!fMTExecImpl)
64 fSeqExecImpl = std::unique_ptr<ROOT::TSequentialExecutor>(new ROOT::TSequentialExecutor());
65 }
66
67 //////////////////////////////////////
68 /// Constructor of TMVA Executor class
69 /// Explicit specify the number of threads. In this case if nthreads is > 1 a multi-threaded executor will be created and
70 /// TMVA will run in MT.
71 /// If nthreads = 1 instead TMVA will run in sequential mode
72 /// If nthreads = 0 TMVA will use the default thread pool size
73 ////////////////////////////////////////////
74 explicit Executor(int nthreads) {
75 // enable MT in TMVA if :
76 // - no specific MT
77 if ( nthreads != 1 ) {
78#ifdef R__USE_IMT
79 fMTExecImpl = std::unique_ptr< ROOT::TThreadExecutor>(new ROOT::TThreadExecutor(nthreads));
80#else
81 ::Error("Executor","Cannot have TMVA in multi-threads mode when ROOT is built without IMT");
82#endif
83 }
84 // case of single thread usage
85 if (!fMTExecImpl)
86 fSeqExecImpl = std::unique_ptr<ROOT::TSequentialExecutor>(new ROOT::TSequentialExecutor());
87 }
88
89#ifdef R__USE_IMT
91 if (fMTExecImpl) return fMTExecImpl.get();
92 else {
93 fMTExecImpl = std::unique_ptr< ROOT::TThreadExecutor>(new ROOT::TThreadExecutor());
94 Info("GetThreadExecutor","Creating a TThread executor with a pool with a default size of %d",fMTExecImpl->GetPoolSize());
95 return fMTExecImpl.get();
96 }
97 }
98#endif
99
100 unsigned int GetPoolSize() const {
101 if (!fMTExecImpl) return 1;
102#ifdef R__USE_IMT
103 return fMTExecImpl->GetPoolSize();
104#else
105 return 1;
106#endif
107 }
108
109 /// wrap TExecutor::Foreach
110 template<class Function>
111 void Foreach(Function func, unsigned int nTimes, unsigned nChunks = 0) {
112 if (fMTExecImpl) fMTExecImpl->Foreach(func,nTimes, nChunks);
113 else fSeqExecImpl->Foreach(func,nTimes);
114 }
115 template<class Function, class T>
116 void Foreach(Function func, std::vector<T> & args, unsigned nChunks = 0) {
117 if (fMTExecImpl) fMTExecImpl->Foreach(func,args, nChunks);
118 else fSeqExecImpl->Foreach(func, args);
119 }
120 template<class Function, class INTEGER>
121#ifdef R__USE_IMT
122 void Foreach(Function func, ROOT::TSeq<INTEGER> args, unsigned nChunks = 0){
123 if (fMTExecImpl) fMTExecImpl->Foreach(func,args, nChunks);
124 else fSeqExecImpl->Foreach(func, args);
125 }
126#else
127 void Foreach(Function func, ROOT::TSeq<INTEGER> args, unsigned /*nChunks*/ = 0){
128 fSeqExecImpl->Foreach(func, args);
129 }
130#endif
131
132 /// Wrap TExecutor::Map functions
133 template <class F, class Cond = noReferenceCond<F>>
134 auto Map(F func, unsigned nTimes) -> std::vector<InvokeResult_t<F>>
135 {
136 if (fMTExecImpl) return fMTExecImpl->Map(func,nTimes);
137 else return fSeqExecImpl->Map(func, nTimes);
138 }
139 template <class F, class INTEGER, class Cond = noReferenceCond<F, INTEGER>>
140 auto Map(F func, ROOT::TSeq<INTEGER> args) -> std::vector<InvokeResult_t<F, INTEGER>>
141 {
142 if (fMTExecImpl) return fMTExecImpl->Map(func,args);
143 else return fSeqExecImpl->Map(func, args);
144 }
145
146 /// Wrap TExecutor::MapReduce functions
147 template <class F, class INTEGER, class R, class Cond = noReferenceCond<F, INTEGER>>
149 {
150 if (fMTExecImpl) return fMTExecImpl->MapReduce(func, args, redfunc);
151 else return fSeqExecImpl->MapReduce(func, args, redfunc);
152 }
153 template <class F, class INTEGER, class R, class Cond = noReferenceCond<F, INTEGER>>
154 auto MapReduce(F func, ROOT::TSeq<INTEGER> args, R redfunc, unsigned nChunks) -> InvokeResult_t<F, INTEGER>
155 {
156 if (fMTExecImpl) return fMTExecImpl->MapReduce(func, args, redfunc, nChunks);
157 else return fSeqExecImpl->MapReduce(func, args, redfunc);
158 }
159
160 ///Wrap Reduce function
161 template<class T, class R>
162 auto Reduce(const std::vector<T> &objs, R redfunc) -> decltype(redfunc(objs)) {
163 if (fMTExecImpl) return fMTExecImpl->Reduce(objs, redfunc);
164 else return fSeqExecImpl->Reduce(objs, redfunc);
165 }
166 //template<class T> T* Reduce(const std::vector<T*> &mergeObjs);
167
168#ifdef R__USE_IMT
169 std::unique_ptr<ROOT::TThreadExecutor> fMTExecImpl;
170#else
171 std::unique_ptr<ROOT::TSequentialExecutor> fMTExecImpl; // if not using MT the two pointers will be of same type
172#endif
173 std::unique_ptr<ROOT::TSequentialExecutor> fSeqExecImpl;
174};
175
176} // end namespace TMVA
177
178#endif
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:230
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:197
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Double_t(* Function)(Double_t)
Definition Functor.C:4
A pseudo container class which is a generator of indices.
Definition TSeq.hxx:67
This class provides a simple interface to execute the same task multiple times in parallel threads,...
Base Executor class.
Definition Executor.h:36
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
Definition Executor.h:111
auto Map(F func, ROOT::TSeq< INTEGER > args) -> std::vector< InvokeResult_t< F, INTEGER > >
Definition Executor.h:140
auto MapReduce(F func, ROOT::TSeq< INTEGER > args, R redfunc, unsigned nChunks) -> InvokeResult_t< F, INTEGER >
Definition Executor.h:154
std::unique_ptr< ROOT::TSequentialExecutor > fSeqExecImpl
Definition Executor.h:173
auto Map(F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >
Wrap TExecutor::Map functions.
Definition Executor.h:134
auto MapReduce(F func, ROOT::TSeq< INTEGER > args, R redfunc) -> InvokeResult_t< F, INTEGER >
Wrap TExecutor::MapReduce functions.
Definition Executor.h:148
auto Reduce(const std::vector< T > &objs, R redfunc) -> decltype(redfunc(objs))
Wrap Reduce function.
Definition Executor.h:162
unsigned int GetPoolSize() const
Definition Executor.h:100
typename std::enable_if_t<"Function can't return a reference" &&!(std::is_reference< InvokeResult_t< F, T... > >::value)> noReferenceCond
Definition Executor.h:44
void Foreach(Function func, std::vector< T > &args, unsigned nChunks=0)
Definition Executor.h:116
Executor()
Default constructor of TMVA Executor class if ROOT::EnableImplicitMT has not been called then by defa...
Definition Executor.h:53
std::unique_ptr< ROOT::TThreadExecutor > fMTExecImpl
Definition Executor.h:169
ROOT::TypeTraits::InvokeResult_t< F, Args... > InvokeResult_t
Definition Executor.h:39
void Foreach(Function func, ROOT::TSeq< INTEGER > args, unsigned nChunks=0)
Definition Executor.h:122
Executor(int nthreads)
Constructor of TMVA Executor class Explicit specify the number of threads.
Definition Executor.h:74
ROOT::TThreadExecutor * GetMultiThreadExecutor()
Definition Executor.h:90
#define F(x, y, z)
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:558
create variable transformations