The Gaudi Framework  v33r1 (b1225454)
PrecedenceRulesGraph.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "PrecedenceRulesGraph.h"
12 #include "Visitors/Promoters.h"
13 
15 
16 #include <boost/property_map/transform_value_property_map.hpp>
17 #include <fstream>
18 
19 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
20 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
21 
22 namespace {
23  //---------------------------------------------------------------------------
25  const char* stateToString( const int& stateId ) {
26  switch ( stateId ) {
27  case 0:
28  return "FALSE";
29  case 1:
30  return "TRUE";
31  default:
32  return "UNDEFINED";
33  }
34  }
35 } // namespace
36 
37 namespace concurrency {
38 
39  //---------------------------------------------------------------------------
41 
42  if ( std::find( m_parents.begin(), m_parents.end(), node ) == m_parents.end() ) m_parents.push_back( node );
43  }
44 
45  //--------------------------------------------------------------------------
47 
48  if ( std::find( m_children.begin(), m_children.end(), node ) == m_children.end() ) m_children.push_back( node );
49  }
50 
51  //---------------------------------------------------------------------------
53  const unsigned int& recursionLevel ) const {
54 
55  auto& node_decisions = slot.controlFlowState;
56  output << std::string( recursionLevel, ' ' ) << m_nodeName << " (" << m_nodeIndex << ")"
57  << ", w/ decision: " << stateToString( node_decisions[m_nodeIndex] ) << "(" << node_decisions[m_nodeIndex]
58  << ")" << std::endl;
59 
60  for ( auto daughter : m_children ) daughter->printState( output, slot, recursionLevel + 2 );
61  }
62 
63  //---------------------------------------------------------------------------
65 
66  if ( visitor.visitEnter( *this ) ) {
67  // try to aggregate a decision
68  bool result = visitor.visit( *this );
69  return !result;
70  }
71 
72  return false; // visitor was rejected (since the decision node has an aggregated decision already)
73  }
74 
75  //---------------------------------------------------------------------------
77  const unsigned int& recursionLevel ) const {
78 
79  auto& node_decisions = slot.controlFlowState;
80  auto& states = slot.algsStates;
81  std::string indent( recursionLevel, ' ' );
82  output << indent << m_nodeName << " (" << m_nodeIndex << ")"
83  << ", w/ decision: " << stateToString( node_decisions[m_nodeIndex] ) << "(" << node_decisions[m_nodeIndex]
84  << ")"
85  << ", in state: " << states[m_algoIndex] << std::endl;
86 
87  // In a stall, CONTROLREADY nodes are interesting
88  if ( states[m_algoIndex] == AlgsExecutionStates::State::CONTROLREADY ) {
89 
90  // Check all data dependencies
91  output << indent << "========" << std::endl;
92  for ( auto dataNode : this->getInputDataNodes() ) {
93 
94  // Was the data produced?
95  ConditionNode* conditionNode = dynamic_cast<ConditionNode*>( dataNode );
96  DataReadyPromoter visitor( slot, {} );
97  bool wasProduced = false;
98  if ( conditionNode ) {
99  // ConditionNodes always request data on visit()
100  // Instead take the opposite of visitEnter(), since you may not enter if it already exists
101  wasProduced = !visitor.visitEnter( *conditionNode );
102  } else {
103  // For DataNodes, the check is done in visit()
104  wasProduced = visitor.visit( *dataNode );
105  }
106 
107  // Print out states of producer algs if data is missing
108  if ( !wasProduced ) {
109 
110  // Say if it's conditions data or not
111  if ( conditionNode )
112  output << indent << "missing conditions data: " << dataNode->name() << std::endl;
113  else
114  output << indent << "missing data: " << dataNode->name() << std::endl;
115 
116  // Find out if the algorithm needs it because of a tool
117  DataHandleFinder finder( dataNode->name() );
118  this->getAlgorithm()->acceptDHVisitor( &finder );
119  if ( finder.holderNames().size() > 1 ) {
120  output << indent << "required by tool:";
121  for ( auto const& holderName : finder.holderNames() ) {
122  if ( holderName != this->name() ) output << " " << holderName;
123  }
124  output << std::endl;
125  }
126 
127  if ( conditionNode ) {
128  // State which IOVs the data exists for
129  output << indent << "current EventID: " << EventIDBase( slot.eventContext->eventID() ) << std::endl;
130  std::vector<EventIDRange> validRanges;
131  conditionNode->m_condSvc->validRanges( validRanges, dataNode->name() )
132  .ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
133  for ( auto& range : validRanges ) { output << indent << "interval of validity: " << range << std::endl; }
134  if ( validRanges.empty() ) output << indent << "no interval(s) of validity" << std::endl;
135  } else {
136  // State which algs produce this data
137  output << indent << "can be produced by alg(s): ";
138  for ( auto algoNode : dataNode->getProducers() ) {
139  output << "( " << algoNode->name() << " in state: " << states[algoNode->getAlgoIndex()] << " ) ";
140  }
141  output << std::endl;
142  }
143 
144  // See where data is available (ignore conditions, since these are top-level)
145  if ( !conditionNode ) {
146  std::vector<EventSlot>* testSubSlots = &slot.allSubSlots;
147  auto* subSlotMap = &slot.subSlotsByNode;
148 
149  // Examine the top-level slot if you did not start there
150  if ( slot.parentSlot ) {
151  visitor.m_slot = slot.parentSlot;
152  testSubSlots = &slot.parentSlot->allSubSlots;
153  subSlotMap = &slot.parentSlot->subSlotsByNode;
154  if ( visitor.visit( *dataNode ) ) {
155  output << indent << "data is available at whole-event level" << std::endl;
156  }
157  }
158 
159  // Examine all sub slots, grouped by entry point
160  for ( auto& pair : *subSlotMap ) {
161  if ( pair.second.size() > 0 ) {
162  bool madeLine = false;
163 
164  // Loop over the slots for this entry point
165  for ( int slotIndex : pair.second ) {
166 
167  EventSlot* subSlot = &testSubSlots->at( slotIndex );
168  visitor.m_slot = subSlot;
169  if ( visitor.visit( *dataNode ) ) {
170 
171  if ( !madeLine ) {
172  // Only mention this set of sub-slots at all if one has the data
173  output << indent << "data is available in sub-slot(s) ";
174  madeLine = true;
175  }
176  output << slotIndex << ", ";
177  }
178  }
179  if ( madeLine ) { output << "entered from " << pair.first << std::endl; }
180  }
181  }
182  }
183  }
184  }
185  output << indent << "========" << std::endl;
186  }
187  }
188 
189  //---------------------------------------------------------------------------
191 
192  if ( visitor.visitEnter( *this ) ) {
193  visitor.visit( *this );
194  return true; // visitor was accepted to promote the algorithm
195  }
196 
197  return false; // visitor was rejected (since the algorithm already produced a decision)
198  }
199 
200  //---------------------------------------------------------------------------
202 
203  if ( std::find( m_parents.begin(), m_parents.end(), node ) == m_parents.end() ) m_parents.push_back( node );
204  }
205 
206  //---------------------------------------------------------------------------
208 
209  if ( std::find( m_outputs.begin(), m_outputs.end(), node ) == m_outputs.end() ) m_outputs.push_back( node );
210  }
211 
212  //---------------------------------------------------------------------------
214 
215  if ( std::find( m_inputs.begin(), m_inputs.end(), node ) == m_inputs.end() ) m_inputs.push_back( node );
216  }
217 
218  //---------------------------------------------------------------------------
220  if ( serviceLocator()->existsService( "CondSvc" ) ) {
221  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc" )};
222  if ( condSvc.isValid() ) {
223  info() << "CondSvc found. DF precedence rules will be augmented with 'Conditions'" << endmsg;
225  }
226  }
227 
228  // Detach condition algorithms from the CF realm
229  if ( m_conditionsRealmEnabled ) {
230  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc", false )};
231  auto& condAlgs = condSvc->condAlgs();
232  for ( const auto algo : condAlgs ) {
233  auto itA = m_algoNameToAlgoNodeMap.find( algo->name() );
234  if ( itA != m_algoNameToAlgoNodeMap.end() ) {
235  concurrency::AlgorithmNode* algoNode = itA->second.get();
236  debug() << "Detaching condition algorithm '" << algo->name() << "' from the CF realm.." << endmsg;
237  for ( auto parent : algoNode->getParentDecisionHubs() ) {
238  parent->m_children.erase( std::remove( parent->m_children.begin(), parent->m_children.end(), algoNode ),
239  parent->m_children.end() );
240  // clean up also auxiliary BGL-based graph of precedence rules
241  if ( m_enableAnalysis ) boost::remove_edge( node( algoNode->name() ), node( parent->name() ), m_PRGraph );
242  }
243  algoNode->m_parents.clear();
244 
245  } else {
246  warning() << "Algorithm '" << algo->name() << "' is not registered in the graph" << endmsg;
247  }
248  }
249  }
250 
252 
253  if ( !sc.isSuccess() ) error() << "Could not build the data dependency realm." << endmsg;
254 
255  ON_DEBUG debug() << dumpDataFlow() << endmsg;
256 
257  return sc;
258  }
259 
260  //---------------------------------------------------------------------------
262 
263  const std::string& algoName = algo->name();
264 
265  m_algoNameToAlgoInputsMap[algoName] = algo->inputDataObjs();
266  m_algoNameToAlgoOutputsMap[algoName] = algo->outputDataObjs();
267 
268  ON_VERBOSE {
269  verbose() << " Inputs of " << algoName << ": ";
270  for ( auto tag : algo->inputDataObjs() ) verbose() << tag << " | ";
271  verbose() << endmsg;
272 
273  verbose() << " Outputs of " << algoName << ": ";
274  for ( auto tag : algo->outputDataObjs() ) verbose() << tag << " | ";
275  verbose() << endmsg;
276  }
277  }
278 
279  //---------------------------------------------------------------------------
281 
282  StatusCode global_sc( StatusCode::SUCCESS, true );
283 
284  // Production of DataNodes by AlgorithmNodes (DataNodes are created here)
285  for ( auto& algo : m_algoNameToAlgoNodeMap ) {
286 
287  auto& outputs = m_algoNameToAlgoOutputsMap[algo.first];
288  for ( auto output : outputs ) {
289  const auto sc = addDataNode( output );
290  if ( !sc.isSuccess() ) {
291  error() << "Extra producer (" << algo.first << ") for DataObject @ " << output
292  << " has been detected: this is not allowed." << endmsg;
293  global_sc = sc;
294  }
295  auto dataNode = getDataNode( output );
296  dataNode->addProducerNode( algo.second.get() );
297  algo.second->addOutputDataNode( dataNode );
298 
299  // Mirror the action above in the BGL-based graph
300  if ( m_enableAnalysis ) boost::add_edge( node( algo.second->name() ), node( output.fullKey() ), m_PRGraph );
301  }
302  }
303 
304  // Consumption of DataNodes by AlgorithmNodes
305  for ( auto& algo : m_algoNameToAlgoNodeMap ) {
306 
307  for ( auto input : m_algoNameToAlgoInputsMap[algo.first] ) {
308 
309  auto itP = m_dataPathToDataNodeMap.find( input );
310 
311  DataNode* dataNode = ( itP != m_dataPathToDataNodeMap.end() ? getDataNode( input ) : nullptr );
312  if ( dataNode ) {
313  dataNode->addConsumerNode( algo.second.get() );
314  algo.second->addInputDataNode( dataNode );
315 
316  // Mirror the action above in the BGL-based graph
317  if ( m_enableAnalysis ) boost::add_edge( node( input.fullKey() ), node( algo.second->name() ), m_PRGraph );
318  }
319  }
320  }
321 
322  return global_sc;
323  }
324 
325  //---------------------------------------------------------------------------
327  bool inverted, bool allPass ) {
328 
330 
332 
333  auto& algoName = algo->name();
334 
335  concurrency::AlgorithmNode* algoNode;
336 
337  auto itA = m_algoNameToAlgoNodeMap.find( algoName );
338  if ( itA != m_algoNameToAlgoNodeMap.end() ) {
339  algoNode = itA->second.get();
340  } else {
341  auto r = m_algoNameToAlgoNodeMap.emplace(
342  algoName, std::make_unique<concurrency::AlgorithmNode>( *this, algo, m_nodeCounter, m_algoCounter, inverted,
343  allPass ) );
344  algoNode = r.first->second.get();
345 
346  // Mirror AlgorithmNode in the BGL-based graph
347  if ( m_enableAnalysis ) {
348  boost::add_vertex( AlgoProps( algo, m_nodeCounter, m_algoCounter, inverted, allPass ), m_PRGraph );
349  }
350  ++m_nodeCounter;
351  ++m_algoCounter;
352  ON_VERBOSE verbose() << "AlgorithmNode '" << algoName << "' added @ " << algoNode << endmsg;
353 
354  registerIODataObjects( algo );
355  }
356 
358  auto itP = m_decisionNameToDecisionHubMap.find( parentName );
359  if ( itP != m_decisionNameToDecisionHubMap.end() ) {
360  auto parentNode = itP->second.get();
361 
362  parentNode->addDaughterNode( algoNode );
363  algoNode->addParentNode( parentNode );
364 
365  // Mirror algorithm to CF parent relationship in the BGL-based graph
366  if ( m_enableAnalysis ) boost::add_edge( node( algo->name() ), node( parentName ), m_PRGraph );
367 
368  ON_VERBOSE verbose() << "Attached AlgorithmNode '" << algo->name() << "' to parent DecisionNode '" << parentName
369  << "'" << endmsg;
370  } else {
371  sc = StatusCode::FAILURE;
372  error() << "Parent DecisionNode '" << parentName << "' was not found" << endmsg;
373  }
374 
375  return sc;
376  }
377 
378  //---------------------------------------------------------------------------
380 
381  auto itD = m_dataPathToDataNodeMap.find( dataPath );
382  if ( itD != m_dataPathToDataNodeMap.end() ) return StatusCode::SUCCESS;
383 
385  if ( !m_conditionsRealmEnabled ) {
386  dataNode = std::make_unique<concurrency::DataNode>( *this, dataPath );
387  ON_VERBOSE verbose() << " DataNode " << dataPath << " added @ " << dataNode.get() << endmsg;
388  // Mirror the action above in the BGL-based graph
389  if ( m_enableAnalysis ) boost::add_vertex( DataProps( dataPath ), m_PRGraph );
390  } else {
391  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc", false )};
392  if ( condSvc->isRegistered( dataPath ) ) {
393  dataNode = std::make_unique<concurrency::ConditionNode>( *this, dataPath, condSvc );
394  ON_VERBOSE verbose() << " ConditionNode " << dataPath << " added @ " << dataNode.get() << endmsg;
395  // Mirror the action above in the BGL-based graph
396  if ( m_enableAnalysis ) boost::add_vertex( CondDataProps( dataPath ), m_PRGraph );
397  } else {
398  dataNode = std::make_unique<concurrency::DataNode>( *this, dataPath );
399  ON_VERBOSE verbose() << " DataNode " << dataPath << " added @ " << dataNode.get() << endmsg;
400  // Mirror the action above in the BGL-based graph
401  if ( m_enableAnalysis ) boost::add_vertex( DataProps( dataPath ), m_PRGraph );
402  }
403  }
404  m_dataPathToDataNodeMap.emplace( dataPath, std::move( dataNode ) );
405  return StatusCode::SUCCESS;
406  }
407 
408  //---------------------------------------------------------------------------
410  Concurrent modeConcurrent, PromptDecision modePromptDecision,
411  ModeOr modeOR, AllPass allPass, Inverted isInverted ) {
412 
414 
416 
417  auto& decisionHubName = decisionHubAlgo->name();
418 
419  auto itA = m_decisionNameToDecisionHubMap.find( decisionHubName );
420  concurrency::DecisionNode* decisionHubNode;
421  if ( itA != m_decisionNameToDecisionHubMap.end() ) {
422  decisionHubNode = itA->second.get();
423  } else {
424  auto r = m_decisionNameToDecisionHubMap.emplace(
425  decisionHubName,
426  std::make_unique<concurrency::DecisionNode>( *this, m_nodeCounter, decisionHubName, modeConcurrent,
427  modePromptDecision, modeOR, allPass, isInverted ) );
428  decisionHubNode = r.first->second.get();
429  // Mirror DecisionNode in the BGL-based graph
430  if ( m_enableAnalysis ) {
431  boost::add_vertex( DecisionHubProps( decisionHubName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
432  allPass, isInverted ),
433  m_PRGraph );
434  }
435 
436  ++m_nodeCounter;
437 
438  ON_VERBOSE verbose() << "DecisionNode '" << decisionHubName << "' added @ " << decisionHubNode << endmsg;
439  }
440 
442  auto itP = m_decisionNameToDecisionHubMap.find( parentName );
443  if ( itP != m_decisionNameToDecisionHubMap.end() ) {
444  auto parentNode = itP->second.get();
445  parentNode->addDaughterNode( decisionHubNode );
446  decisionHubNode->addParentNode( parentNode );
447 
448  // Mirror DecisionNode-to-DecisionNode relationship in the BGL-based graph
449  if ( m_enableAnalysis ) boost::add_edge( node( decisionHubName ), node( parentName ), m_PRGraph );
450 
451  ON_VERBOSE verbose() << "Attached DecisionNode '" << decisionHubName << "' to parent DecisionNode '" << parentName
452  << "'" << endmsg;
453  } else {
454  sc = StatusCode::FAILURE;
455  error() << "Parent DecisionNode '" << parentName << "' was not found" << endmsg;
456  }
457 
458  return sc;
459  }
460 
461  //---------------------------------------------------------------------------
463  concurrency::PromptDecision modePromptDecision, concurrency::ModeOr modeOR,
464  concurrency::AllPass allPass, concurrency::Inverted isInverted ) {
465 
466  auto itH = m_decisionNameToDecisionHubMap.find( headName );
467  if ( itH != m_decisionNameToDecisionHubMap.end() ) {
468  m_headNode = itH->second.get();
469  } else {
470  auto r = m_decisionNameToDecisionHubMap.emplace(
471  headName, std::make_unique<concurrency::DecisionNode>( *this, m_nodeCounter, headName, modeConcurrent,
472  modePromptDecision, modeOR, allPass, isInverted ) );
473  m_headNode = r.first->second.get();
474 
475  // Mirror the action above in the BGL-based graph
476  if ( m_enableAnalysis ) {
477  boost::add_vertex( DecisionHubProps( headName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
478  allPass, isInverted ),
479  m_PRGraph );
480  }
481 
482  ++m_nodeCounter;
483  }
484  }
485 
486  //---------------------------------------------------------------------------
488  auto vp = vertices( m_PRGraph );
489  auto i = std::find_if( vp.first, vp.second, [&]( const PRVertexDesc& v ) {
490  return std::visit( precedence::VertexName(), m_PRGraph[v] ) == name;
491  } );
492  return i != vp.second ? *i : PRVertexDesc{};
493  }
494 
495  //---------------------------------------------------------------------------
497  // iterate through Algorithm nodes
498  for ( auto& pr : m_algoNameToAlgoNodeMap ) pr.second->accept( visitor );
499 
500  // iterate through DecisionHub nodes
501  for ( auto& pr : m_decisionNameToDecisionHubMap ) pr.second->accept( visitor );
502 
503  // iterate through Data [and Conditions] nodes
504  for ( auto& pr : m_dataPathToDataNodeMap ) pr.second->accept( visitor );
505  }
506 
507  //---------------------------------------------------------------------------
509 
510  info() << "Starting ranking by data outputs .. " << endmsg;
511  for ( auto& pair : m_algoNameToAlgoNodeMap ) {
512  ON_DEBUG debug() << " Ranking " << pair.first << "... " << endmsg;
513  pair.second->accept( ranker );
514  ON_DEBUG debug() << " ... rank of " << pair.first << ": " << pair.second->getRank() << endmsg;
515  }
516  }
517 
519  std::ostringstream ost;
520  dumpControlFlow( ost, m_headNode, 0 );
521  return ost.str();
522  }
523 
525  const int& indent ) const {
526  ost << std::string( indent * 2, ' ' );
527  DecisionNode* dn = dynamic_cast<DecisionNode*>( node );
528  AlgorithmNode* an = dynamic_cast<AlgorithmNode*>( node );
529  if ( dn != 0 ) {
530  if ( node != m_headNode ) {
531  ost << node->name() << " [Seq] ";
532  ost << ( ( dn->m_modeConcurrent ) ? " [Concurrent] " : " [Sequential] " );
533  ost << ( ( dn->m_modePromptDecision ) ? " [Prompt] " : "" );
534  ost << ( ( dn->m_modeOR ) ? " [OR] " : "" );
535  ost << ( ( dn->m_allPass ) ? " [PASS] " : "" );
536  ost << "\n";
537  }
538  for ( const auto& i : dn->getDaughters() ) dumpControlFlow( ost, i, indent + 1 );
539  } else if ( an != 0 ) {
540  ost << node->name() << " [Alg] ";
541  if ( an != 0 ) {
542  auto ar = an->getAlgorithm();
543  ost << " [n= " << ar->cardinality() << "]";
544  ost << ( ( !ar->isClonable() ) ? " [unclonable] " : "" );
545  }
546  ost << "\n";
547  }
548  }
549 
550  //---------------------------------------------------------------------------
552 
553  const char idt[] = " ";
554  std::ostringstream ost;
555 
556  ost << "\n" << idt << "====================================\n";
557  ost << idt << "Data origins and destinations:\n";
558  ost << idt << "====================================\n";
559 
560  for ( auto& pair : m_dataPathToDataNodeMap ) {
561 
562  for ( auto algoNode : pair.second->getProducers() ) ost << idt << " " << algoNode->name() << "\n";
563 
564  ost << idt << " V\n";
565  ost << idt << " o " << pair.first << "\n";
566  ost << idt << " V\n";
567 
568  for ( auto algoNode : pair.second->getConsumers() ) ost << idt << " " << algoNode->name() << "\n";
569 
570  ost << idt << "====================================\n";
571  }
572 
573  return ost.str();
574  }
575 
576  //---------------------------------------------------------------------------
577 
579  boost::filesystem::ofstream myfile;
580  myfile.open( fileName, std::ios::app );
581 
582  // Declare properties to dump
583  boost::dynamic_properties dp;
584 
585  dp.property( "Entity",
586  boost::make_transform_value_property_map(
587  []( const VariantVertexProps& v ) {
588  return std::visit( []( const auto& w ) { return boost::lexical_cast<std::string>( w ); }, v );
589  },
590  boost::get( boost::vertex_bundle, m_PRGraph ) ) );
591 
592  auto add_prop = [&]( auto name, auto&& vis ) {
593  dp.property( name, boost::make_transform_value_property_map(
594  [vis = std::forward<decltype( vis )>( vis )]( const VariantVertexProps& v ) {
595  return std::visit( vis, v );
596  },
597  boost::get( boost::vertex_bundle, m_PRGraph ) ) );
598  };
599 
600  add_prop( "Name", precedence::VertexName() );
601  add_prop( "Mode", precedence::GroupMode() );
602  add_prop( "Logic", precedence::GroupLogic() );
603  add_prop( "Decision Negation", precedence::DecisionNegation() );
604  add_prop( "Negative Decision Inversion", precedence::AllPass() );
605  add_prop( "Exit Policy", precedence::GroupExit() );
606  add_prop( "Operations", precedence::Operations() );
607  add_prop( "CF Decision", precedence::CFDecision( slot ) );
608  add_prop( "State", precedence::EntityState( slot, serviceLocator(), m_conditionsRealmEnabled ) );
609  add_prop( "Start Time (Epoch ns)", precedence::StartTime( slot, serviceLocator() ) );
610  add_prop( "End Time (Epoch ns)", precedence::EndTime( slot, serviceLocator() ) );
611  add_prop( "Runtime (ns)", precedence::Duration( slot, serviceLocator() ) );
612 
613  boost::write_graphml( myfile, m_PRGraph, dp );
614 
615  myfile.close();
616  }
617 
618  //---------------------------------------------------------------------------
620  boost::filesystem::ofstream myfile;
621  myfile.open( fileName, std::ios::app );
622 
623  // Fill runtimes (as this could not be done on the fly during trace assembling)
624  SmartIF<ITimelineSvc> timelineSvc = m_svcLocator->service<ITimelineSvc>( "TimelineSvc", false );
625  if ( !timelineSvc.isValid() ) {
626  warning() << "Failed to get the TimelineSvc, timing will not be added to "
627  << "the task precedence trace dump" << endmsg;
628  } else {
629 
630  for ( auto vp = vertices( m_precTrace ); vp.first != vp.second; ++vp.first ) {
631  TimelineEvent te{};
632  te.algorithm = m_precTrace[*vp.first].m_name;
633  timelineSvc->getTimelineEvent( te );
634  int runtime = std::chrono::duration_cast<std::chrono::microseconds>( te.end - te.start ).count();
635  m_precTrace[*vp.first].m_runtime = runtime;
636  }
637  }
638 
639  // Declare properties to dump
640  boost::dynamic_properties dp;
641  using boost::get;
643  dp.property( "Name", get( &AlgoTraceProps::m_name, m_precTrace ) );
644  dp.property( "Rank", get( &AlgoTraceProps::m_rank, m_precTrace ) );
645  dp.property( "Runtime", get( &AlgoTraceProps::m_runtime, m_precTrace ) );
646 
647  boost::write_graphml( myfile, m_precTrace, dp );
648 
649  myfile.close();
650  }
651 
653 
654  std::string u_name = u == nullptr ? "ENTRY" : u->name();
655  std::string v_name = v->name();
656 
658 
659  if ( !u ) {
660  auto itT = m_prec_trace_map.find( "ENTRY" );
661  if ( itT != m_prec_trace_map.end() ) {
662  source = itT->second;
663  } else {
664  source = boost::add_vertex( precedence::AlgoTraceProps( "ENTRY", -1, -1, -1.0 ), m_precTrace );
665  m_prec_trace_map["ENTRY"] = source;
666  }
667  } else {
668  auto itS = m_prec_trace_map.find( u_name );
669  if ( itS != m_prec_trace_map.end() ) {
670  source = itS->second;
671  } else {
672 
673  source =
674  boost::add_vertex( precedence::AlgoTraceProps( u_name, u->getAlgoIndex(), u->getRank(), -1 ), m_precTrace );
675  m_prec_trace_map[u_name] = source;
676  }
677  }
678 
680 
681  auto itP = m_prec_trace_map.find( v_name );
682  if ( itP != m_prec_trace_map.end() ) {
683  target = itP->second;
684  } else {
685 
686  target =
687  boost::add_vertex( precedence::AlgoTraceProps( v_name, v->getAlgoIndex(), v->getRank(), -1 ), m_precTrace );
688  m_prec_trace_map[v_name] = target;
689  }
690 
691  boost::add_edge( source, target, m_precTrace );
692 
693  ON_DEBUG debug() << u_name << "-->" << v_name << " precedence trait added" << endmsg;
694  }
695 
696 } // namespace concurrency
std::unordered_map< std::string, DataObjIDColl > m_algoNameToAlgoInputsMap
Indexes: maps of algorithm's name to algorithm's inputs/outputs.
std::vector< DataNode * > m_outputs
Algorithm outputs (DataNodes)
Class representing an event slot.
Definition: EventSlot.h:24
std::vector< DecisionNode * > m_parents
Direct parent nodes.
unsigned int m_algoIndex
The index of the algorithm.
precedence::PrecTrace m_precTrace
facilities for algorithm precedence tracing
void addDaughterNode(ControlFlowNode *node)
Add a daughter node.
boost::graph_traits< PrecTrace >::vertex_descriptor AlgoTraceVertex
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
const std::vector< DecisionNode * > & getParentDecisionHubs() const
Get all parent decision hubs.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
boost::graph_traits< PRGraph >::vertex_descriptor PRVertexDesc
StatusCode addAlgorithmNode(Gaudi::Algorithm *daughterAlgo, const std::string &parentName, bool inverted, bool allPass)
Add algorithm node.
virtual bool visit(DecisionNode &)
Definition: IGraphVisitor.h:26
std::vector< ControlFlowNode * > m_children
All direct daughter nodes in the tree.
virtual bool visitEnter(DecisionNode &) const
Definition: IGraphVisitor.h:25
Gaudi::Algorithm * getAlgorithm() const
get Algorithm representatives
void dumpPrecRules(const boost::filesystem::path &, const EventSlot &slot)
dump to file the precedence rules
T endl(T... args)
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:72
std::map< std::string, precedence::AlgoTraceVertex > m_prec_trace_map
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::vector< DecisionNode * > m_parents
Control flow parents of an AlgorithmNode (DecisionNodes)
virtual bool getTimelineEvent(TimelineEvent &) const =0
std::string algorithm
Definition: ITimelineSvc.h:31
std::vector< int > controlFlowState
State of the control flow.
Definition: EventSlot.h:87
virtual StatusCode validRanges(std::vector< EventIDRange > &ranges, const DataObjID &id) const =0
retrieve all valid ranges for one Object ID
unsigned int m_algoCounter
Total number of algorithm nodes in the graph.
T end(T... args)
std::vector< EventSlot > allSubSlots
Actual sub-slot instances.
Definition: EventSlot.h:100
StatusCode addDataNode(const DataObjID &dataPath)
Add DataNode that represents DataObject.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
#define ON_DEBUG
bool m_allPass
Whether always passing regardless of daughter results.
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
T remove(T... args)
std::unordered_map< DataObjID, std::unique_ptr< DataNode >, DataObjID_Hasher > m_dataPathToDataNodeMap
Index: map of data path to DataNode.
Gaudi::tagged_bool< class ModeOr_tag > ModeOr
bool visitEnter(AlgorithmNode &) const override
Definition: Promoters.cpp:24
STL class.
const unsigned int & getAlgoIndex() const
Get algorithm index.
bool m_modeOR
Whether acting as "and" (false) or "or" node (true)
T at(T... args)
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
void addInputDataNode(DataNode *node)
Associate an AlgorithmNode, which is a data consumer of this one.
bool accept(IGraphVisitor &visitor) override
Visitor entry point.
StatusCode initialize()
Initialize graph.
std::unordered_map< std::string, std::unique_ptr< AlgorithmNode > > m_algoNameToAlgoNodeMap
Index: map of algorithm's name to AlgorithmNode.
void dumpPrecTrace(const boost::filesystem::path &)
dump to file the precedence trace
bool m_conditionsRealmEnabled
Enable conditions realm of precedence rules.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
precedence::PRGraph m_PRGraph
BGL-based graph of precedence rules.
Gaudi::tagged_bool< class Inverted_tag > Inverted
DecisionNode * m_headNode
the head node of the control flow graph
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
std::string dumpControlFlow() const
Print out control flow of Algorithms and Sequences.
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
T str(T... args)
const float & getRank() const
Get Algorithm rank.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
void registerIODataObjects(const Gaudi::Algorithm *algo)
Register algorithm in the Data Dependency index.
std::unordered_map< std::string, DataObjIDColl > m_algoNameToAlgoOutputsMap
bool isSuccess() const
Definition: StatusCode.h:365
const std::vector< ControlFlowNode * > & getDaughters() const
Get children nodes.
T move(T... args)
Gaudi::tagged_bool< class Concurrent_tag > Concurrent
bool m_modePromptDecision
Whether to evaluate the hub decision ASA its child decisions allow to do that.
PRVertexDesc node(const std::string &) const
void addEdgeToPrecTrace(const AlgorithmNode *u, const AlgorithmNode *v)
set cause-effect connection between two algorithms in the precedence trace
T get(T... args)
Implements the IDataHandleVisitor interface Class used to explore heirarchy of nested IDataHandleHold...
void addOutputDataNode(DataNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
const DataObjIDColl & outputDataObjs() const override
T find(T... args)
std::unordered_map< std::string, std::unique_ptr< DecisionNode > > m_decisionNameToDecisionHubMap
Index: map of decision's name to DecisionHub.
STL class.
void acceptDHVisitor(IDataHandleVisitor *) const override
Definition: Algorithm.cpp:199
StatusCode addDecisionHubNode(Gaudi::Algorithm *daughterAlgo, const std::string &parentName, concurrency::Concurrent, concurrency::PromptDecision, concurrency::ModeOr, concurrency::AllPass, concurrency::Inverted)
Add a node, which aggregates decisions of direct daughter nodes.
void accept(IGraphVisitor &visitor) const
An entry point to visit all graph nodes.
Gaudi::tagged_bool< class PromptDecision_tag > PromptDecision
std::vector< DataNode * > m_inputs
Algorithm inputs (DataNodes)
void addParentNode(DecisionNode *node)
Add a parent node.
bool m_modeConcurrent
Whether all daughters will be evaluated concurrently or sequentially.
const std::vector< DataNode * > & getInputDataNodes() const
Get all consumer nodes.
const std::string & name() const override
Retrieve name of the service.
unsigned int m_nodeCounter
Total number of nodes in the graph.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:89
void printState(std::stringstream &output, EventSlot &slot, const unsigned int &recursionLevel) const override
Print a string representing the control flow state.
DataNode * getDataNode(const DataObjID &dataPath) const
Get DataNode by DataObject path using graph index.
Gaudi::tagged_bool< class AllPass_tag > AllPass
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:66
constexpr static const auto FAILURE
Definition: StatusCode.h:101
const Gaudi::Algorithm & parent
EventSlot * parentSlot
Pointer to parent slot (null for top level)
Definition: EventSlot.h:96
void addParentNode(DecisionNode *node)
Add a parent node.
bool accept(IGraphVisitor &visitor) override
Visitor entry point.
std::string dumpDataFlow() const
Print out all data origins and destinations, as reflected in the EF graph.
#define ON_VERBOSE
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
StatusCode buildDataDependenciesRealm()
Build data dependency realm WITH data object nodes participating.
const DataObjIDColl & inputDataObjs() const override
std::unordered_map< std::string, std::vector< unsigned int > > subSlotsByNode
Listing of sub-slots by the node (name) they are attached to.
Definition: EventSlot.h:98
void printState(std::stringstream &output, EventSlot &slot, const unsigned int &recursionLevel) const override
Print a string representing the control flow state.
T forward(T... args)
std::variant< AlgoProps, DecisionHubProps, DataProps, CondDataProps > VariantVertexProps
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::unique_ptr< EventContext > eventContext
Cache for the eventContext.
Definition: EventSlot.h:83
const std::string & name() const
Get node name.
void addHeadNode(const std::string &headName, concurrency::Concurrent, concurrency::PromptDecision, concurrency::ModeOr, concurrency::AllPass, concurrency::Inverted)
Add a node, which has no parents.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:549
AlgsExecutionStates algsStates
Vector of algorithms states.
Definition: EventSlot.h:85
const EventIDBase & eventID() const
Definition: EventContext.h:55
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
void rankAlgorithms(IGraphVisitor &ranker) const
Rank Algorithm nodes by the number of data outputs.
void addConsumerNode(AlgorithmNode *node)
Add relationship to consumer AlgorithmNode.