GaudiParallelizer.cpp
Go to the documentation of this file.
1 // Include files
2 // From Gaudi
8 
9 #include "GaudiParallelizer.h"
10 
11 // ----------------------------------------------------------------------------
12 // Implementation file for class: GaudiParallelizer
13 //
14 // 09/12/2011: Illya Shapoval
15 // ----------------------------------------------------------------------------
17 
18 // ============================================================================
19 // Standard constructor, initializes variables
20 // ============================================================================
21 GaudiParallelizer::GaudiParallelizer(const std::string& name, ISvcLocator* pSvcLocator)
22  : GaudiAlgorithm(name, pSvcLocator)
23  , m_timerTool( 0 )
24 {
25  declareProperty( "Members" , m_names );
26  declareProperty( "ModeOR" , m_modeOR = false );
27  declareProperty( "MeasureTime" , m_measureTime = false );
28  declareProperty( "ReturnOK" , m_returnOK = false );
29  declareProperty( "NumberOfThreads", m_nthreads = 0 );
30 
31  m_names.declareUpdateHandler (&GaudiParallelizer::membershipHandler, this );
32 }
33 
34 // ============================================================================
35 // Destructor
36 // ============================================================================
38 
39 // ============================================================================
40 // Initialization
41 // ============================================================================
43  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
44  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
45  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
46 
47  StatusCode status = decodeNames();
48  if ( !status.isSuccess() ) return status;
49 
50  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
51  if ( m_timerTool->globalTiming() ) m_measureTime = true;
52 
53  if ( m_measureTime ) {
56  } else {
58  m_timerTool = 0;
59  }
60 
61  //== Initialize the algorithms
63  for ( itE = m_entries.begin(); m_entries.end() != itE; itE++ ) {
64  Algorithm* myAlg = itE->algorithm();
65  if ( m_measureTime ) {
66  itE->setTimer( m_timerTool->addTimer( myAlg->name() ) );
67  }
68 
69  status = myAlg->sysInitialize();
70  if ( !status.isSuccess() ) {
71  return Error( "Can not initialize " + myAlg->name(),
72  status );
73  }
74  }
75 
77 
78  if ( m_nthreads != 0 ) {
79  // Construct the TBB task scheduler with m_nthreads threads
80  tbb::task_scheduler_init init( m_nthreads );
81  }
82  else {
83  m_nthreads = tbb::task_scheduler_init::default_num_threads();
84  }
85 
86  if ( msgLevel(MSG::DEBUG) ) debug() << "Number of threads set to be used in the TBB thread pool is "
87  << m_nthreads << endmsg;
88  return StatusCode::SUCCESS;
89 }
90 
91 // ============================================================================
92 // Main execution
93 // ============================================================================
96 
97  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute algorithms in parallel" << endmsg;
98 
99  for (std::vector<AlgorithmEntry>::iterator itE = m_entries.begin(); m_entries.end() != itE; ++itE ) {
100  Algorithm* myAlg = itE->algorithm();
101  if ( ! myAlg->isEnabled() ) continue;
102  if ( ! myAlg->isExecuted() ) {
103 
104  m_task_group.run(boost::bind(&AlgorithmEntry::run,
105  boost::ref(*itE),
106  boost::ref(*this)));
107  }
108  }
109 
110  m_task_group.wait();
111  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Joining parallel algorithm tasks" << endmsg;
112 
113  for( std::vector<AlgorithmEntry>::const_iterator it = m_entries.begin(); it != m_entries.end(); ++it ){
114  if ( msgLevel(MSG::DEBUG) ) debug() << "Algorithm wrapper " << &*it
115  << " around the algorithm " << it->algorithm()->name()
116  << " received return status code " << it->m_returncode
117  << endmsg;
118  }
119 
120  for( std::vector<AlgorithmEntry>::const_iterator it = m_entries.begin(); it != m_entries.end(); ++it )
121  if ( !(it->m_returncode.isSuccess()) ) return it->m_returncode;
122 
123  setExecuted( true );
125  return StatusCode::SUCCESS;
126 }
127 
128 // ============================================================================
129 // Finalize
130 // ============================================================================
132  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
133 
134  return GaudiAlgorithm::finalize(); // must be called after all other actions
135 }
136 
137 // ============================================================================
138 
140 
142  m_entries.clear();
143 
144  //== Get the "Context" option if in the file...
145  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
146  bool addedContext = false; //= Have we added the context ?
147  bool addedRootInTES = false; //= Have we added the rootInTES ?
148 
149 
150  //= Get the Application manager, to see if algorithm exist
151  auto appMgr = service<IAlgManager>("ApplicationMgr");
152  const std::vector<std::string>& nameVector = m_names.value();
154  for ( it = nameVector.begin(); nameVector.end() != it; it++ ) {
156  const std::string &theName = typeName.name();
157  const std::string &theType = typeName.type();
158 
159  //== Check whether the specified algorithm already exists. If not, create it
161  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm(typeName, false); // do not create it now
162  if ( !myIAlg.isValid() ) {
163  //== Set the Context if not in the jobOptions list
164  if ( !context().empty() || !rootInTES().empty() ) {
165  bool foundContext = false;
166  bool foundRootInTES = false;
167  const auto properties = jos->getProperties( theName );
168  if ( properties ) {
169  // Iterate over the list to set the options
170  for ( const auto& p : *properties ) {
171  if ( "Context" == p->name() ) {
172  foundContext = true;
173  }
174  if ( "RootInTES" == p->name() ) {
175  foundRootInTES = true;
176  }
177  }
178  }
179  if ( !foundContext && !context().empty() ) {
180  StringProperty contextProperty( "Context", context() );
181  jos->addPropertyToCatalogue( theName, contextProperty ).ignore();
182  addedContext = true;
183  }
184  if ( !foundRootInTES && !rootInTES().empty() ) {
185  StringProperty rootInTESProperty( "RootInTES", rootInTES() );
186  jos->addPropertyToCatalogue( theName, rootInTESProperty ).ignore();
187  addedRootInTES = true;
188  }
189  }
190 
191  Algorithm *myAlg = nullptr;
192  result = createSubAlgorithm( theType, theName, myAlg );
193  // (MCl) this should prevent bug #35199... even if I didn't manage to
194  // reproduce it with a simple test.
195  if (result.isSuccess()) myIAlg = myAlg;
196  } else {
197  Algorithm *myAlg = dynamic_cast<Algorithm*>(myIAlg.get());
198  if (myAlg) {
199  subAlgorithms()->push_back(myAlg);
200  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
201  myAlg->addRef();
202  }
203  }
204 
205  //== Remove the property, in case this is not a GaudiAlgorithm...
206  if ( addedContext ) {
207  jos->removePropertyFromCatalogue( theName, "Context" ).ignore();
208  addedContext = false;
209  }
210  if ( addedRootInTES ) {
211  jos->removePropertyFromCatalogue( theName, "RootInTES" ).ignore();
212  addedRootInTES = false;
213  }
214 
215  // propagate the sub-algorithm into own state.
216  if ( result.isSuccess () &&
218  myIAlg.isValid () &&
219  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() )
220  {
221  StatusCode sc = myIAlg->sysInitialize() ;
222  if ( sc.isFailure() ) { result = sc ; }
223  }
224 
225  // propagate the sub-algorithm into own state.
226  if ( result.isSuccess () &&
228  myIAlg.isValid () &&
229  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() )
230  {
231  StatusCode sc = myIAlg->sysStart () ;
232  if ( sc.isFailure() ) { result = sc ; }
233  }
234 
235  //== Is it an Algorithm ? Strange test...
236  if ( result.isSuccess() ) {
237  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
238  // cases by keeping the result of createSubAlgorithm.
239  Algorithm* myAlg = dynamic_cast<Algorithm*>(myIAlg.get());
240  if (myAlg!=0) {
241  // Note: The reference counting is kept by the system of sub-algorithms
242  m_entries.push_back( AlgorithmEntry( myAlg ) );
243  if (msgLevel(MSG::DEBUG)) debug () << "Added algorithm " << theName << endmsg;
244  } else {
245  warning() << theName << " is not an Algorithm - failed dynamic_cast"
246  << endmsg;
247  final = StatusCode::FAILURE;
248  }
249  } else {
250  warning() << "Unable to find or create " << theName << endmsg;
251  final = result;
252  }
253 
254  }
255  //== Print the list of algorithms
256  MsgStream& msg = info();
257  if ( m_modeOR ) msg << "OR ";
258  msg << "Member list: ";
260  ostream_joiner( msg, m_entries, ", ",
261  [](MsgStream& msg,const AlgorithmEntry& entry) -> MsgStream& {
262  Algorithm* myAlg = entry.algorithm();
263  auto myAlgType = System::typeinfoName( typeid(*myAlg) ) ;
264  if ( myAlg->name() != myAlgType ) {
265  msg << myAlgType << "/" ;
266  }
267  return msg << myAlg->name();
268  } );
269  if ( !context().empty() ) msg << ", with context '" << context() << "'";
270  if ( !rootInTES().empty() ) msg << ", with rootInTES '" << rootInTES() << "'";
271  msg << endmsg;
272  return final;
273 
274 }
275 
276 //=========================================================================
277 // Interface for the Property manager
278 //=========================================================================
280 {
281  // no action for not-yet initialized sequencer
282  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) { return ; } // RETURN
283 
284  decodeNames().ignore();
285 
286  if ( !m_measureTime ) { return ; } // RETURN
287 
288  // add the entries into timer table:
289 
290  if ( 0 == m_timerTool )
291  { m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" ) ; }
292 
293  if ( m_timerTool->globalTiming() ) m_measureTime = true;
294 
297 
299  m_entries.end() != itE; ++itE )
300  {
301  itE->setTimer( m_timerTool->addTimer( itE->algorithm()->name() ) );
302  }
303 
305 
306 }
307 //=============================================================================
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual void decreaseIndent()=0
Decrease the indentation of the name.
StatusCode createSubAlgorithm(const std::string &type, const std::string &name, Algorithm *&pSubAlg)
Create a sub algorithm.
Definition: Algorithm.cpp:955
Algorithm * algorithm() const
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:716
virtual ~GaudiParallelizer()
Destructor.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
virtual int addTimer(const std::string &name)=0
add a timer entry with the specified name
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
StatusCode Error(const std::string &msg, const StatusCode st=StatusCode::FAILURE, const size_t mx=10) const
Print the error message and return with the given StatusCode.
STL namespace.
StatusCode decodeNames()
Decode a vector of string.
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:712
virtual void increaseIndent()=0
Increase the indentation of the name.
T end(T...args)
bool m_modeOR
Indicates that the OR is wanted instead of AND.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
virtual bool globalTiming()=0
returns the flag telling that global timing is wanted
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
virtual StatusCode finalize()
Algorithm finalization.
STL class.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:820
StringArrayProperty m_names
Input string, list of algorithms.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
T push_back(T...args)
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
void setExecuted(bool state) override
Set the executed flag to the specified state.
Definition: Algorithm.cpp:844
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:840
StatusCode finalize() override
standard finalization method
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:777
The useful base class for data processing algorithms.
const TYPE & value() const
explicit conversion
Definition: Property.h:341
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
tbb::task_group m_task_group
TBB task group.
virtual void start(int index)=0
start the counter, i.e.
const std::vector< Algorithm * > * subAlgorithms() const
List of sub-algorithms. Returns a pointer to a vector of (sub) Algorithms.
Definition: Algorithm.cpp:865
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:74
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
STL class.
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:193
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
virtual StatusCode execute()
Algorithm execution.
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:853
T begin(T...args)
const std::string & type() const
std::vector< AlgorithmEntry > m_entries
List of algorithms to process.
void membershipHandler(Property &theProp)
for asynchronous changes in the list of algorithms
tuple appMgr
Definition: IOTest.py:83
ISequencerTimerTool * m_timerTool
Pointer to the timer tool.
bool m_measureTime
Flag to measure time.
int m_timer
Timer number for the sequencer.
const std::string & name() const
void ignore() const
Definition: StatusCode.h:108
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
unsigned short m_nthreads
Number of threads in the thread pool.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
StatusCode sysInitialize() override
Initialization method invoked by the framework.
Definition: Algorithm.cpp:105
virtual double stop(int index)=0
stop the counter, return the elapsed time
StatusCode release(const IInterface *interface) const
Manual forced (and 'safe') release of the active tool or service.
void run(GaudiParallelizer &prlzr)
Thread task executor method to wrap an algorithm execution in.
virtual StatusCode initialize()
Algorithm initialization.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244