The Gaudi Framework  v33r0 (d5ea422b)
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  for ( auto& range : validRanges ) { output << indent << "interval of validity: " << range << std::endl; }
133  if ( validRanges.empty() ) output << indent << "no interval(s) of validity" << std::endl;
134  } else {
135  // State which algs produce this data
136  output << indent << "can be produced by alg(s): ";
137  for ( auto algoNode : dataNode->getProducers() ) {
138  output << "( " << algoNode->name() << " in state: " << states[algoNode->getAlgoIndex()] << " ) ";
139  }
140  output << std::endl;
141  }
142 
143  // See where data is available (ignore conditions, since these are top-level)
144  if ( !conditionNode ) {
145  std::vector<EventSlot>* testSubSlots = &slot.allSubSlots;
146  auto* subSlotMap = &slot.subSlotsByNode;
147 
148  // Examine the top-level slot if you did not start there
149  if ( slot.parentSlot ) {
150  visitor.m_slot = slot.parentSlot;
151  testSubSlots = &slot.parentSlot->allSubSlots;
152  subSlotMap = &slot.parentSlot->subSlotsByNode;
153  if ( visitor.visit( *dataNode ) ) {
154  output << indent << "data is available at whole-event level" << std::endl;
155  }
156  }
157 
158  // Examine all sub slots, grouped by entry point
159  for ( auto& pair : *subSlotMap ) {
160  if ( pair.second.size() > 0 ) {
161  bool madeLine = false;
162 
163  // Loop over the slots for this entry point
164  for ( int slotIndex : pair.second ) {
165 
166  EventSlot* subSlot = &testSubSlots->at( slotIndex );
167  visitor.m_slot = subSlot;
168  if ( visitor.visit( *dataNode ) ) {
169 
170  if ( !madeLine ) {
171  // Only mention this set of sub-slots at all if one has the data
172  output << indent << "data is available in sub-slot(s) ";
173  madeLine = true;
174  }
175  output << slotIndex << ", ";
176  }
177  }
178  if ( madeLine ) { output << "entered from " << pair.first << std::endl; }
179  }
180  }
181  }
182  }
183  }
184  output << indent << "========" << std::endl;
185  }
186  }
187 
188  //---------------------------------------------------------------------------
190 
191  if ( visitor.visitEnter( *this ) ) {
192  visitor.visit( *this );
193  return true; // visitor was accepted to promote the algorithm
194  }
195 
196  return false; // visitor was rejected (since the algorithm already produced a decision)
197  }
198 
199  //---------------------------------------------------------------------------
201 
202  if ( std::find( m_parents.begin(), m_parents.end(), node ) == m_parents.end() ) m_parents.push_back( node );
203  }
204 
205  //---------------------------------------------------------------------------
207 
208  if ( std::find( m_outputs.begin(), m_outputs.end(), node ) == m_outputs.end() ) m_outputs.push_back( node );
209  }
210 
211  //---------------------------------------------------------------------------
213 
214  if ( std::find( m_inputs.begin(), m_inputs.end(), node ) == m_inputs.end() ) m_inputs.push_back( node );
215  }
216 
217  //---------------------------------------------------------------------------
219  if ( serviceLocator()->existsService( "CondSvc" ) ) {
220  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc" )};
221  if ( condSvc.isValid() ) {
222  info() << "CondSvc found. DF precedence rules will be augmented with 'Conditions'" << endmsg;
224  }
225  }
226 
227  // Detach condition algorithms from the CF realm
228  if ( m_conditionsRealmEnabled ) {
229  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc", false )};
230  auto& condAlgs = condSvc->condAlgs();
231  for ( const auto algo : condAlgs ) {
232  auto itA = m_algoNameToAlgoNodeMap.find( algo->name() );
233  if ( itA != m_algoNameToAlgoNodeMap.end() ) {
234  concurrency::AlgorithmNode* algoNode = itA->second.get();
235  debug() << "Detaching condition algorithm '" << algo->name() << "' from the CF realm.." << endmsg;
236  for ( auto parent : algoNode->getParentDecisionHubs() ) {
237  parent->m_children.erase( std::remove( parent->m_children.begin(), parent->m_children.end(), algoNode ),
238  parent->m_children.end() );
239  // clean up also auxiliary BGL-based graph of precedence rules
240  if ( m_enableAnalysis ) boost::remove_edge( node( algoNode->name() ), node( parent->name() ), m_PRGraph );
241  }
242  algoNode->m_parents.clear();
243 
244  } else {
245  warning() << "Algorithm '" << algo->name() << "' is not registered in the graph" << endmsg;
246  }
247  }
248  }
249 
251 
252  if ( !sc.isSuccess() ) error() << "Could not build the data dependency realm." << endmsg;
253 
254  ON_DEBUG debug() << dumpDataFlow() << endmsg;
255 
256  return sc;
257  }
258 
259  //---------------------------------------------------------------------------
261 
262  const std::string& algoName = algo->name();
263 
264  m_algoNameToAlgoInputsMap[algoName] = algo->inputDataObjs();
265  m_algoNameToAlgoOutputsMap[algoName] = algo->outputDataObjs();
266 
267  ON_VERBOSE {
268  verbose() << " Inputs of " << algoName << ": ";
269  for ( auto tag : algo->inputDataObjs() ) verbose() << tag << " | ";
270  verbose() << endmsg;
271 
272  verbose() << " Outputs of " << algoName << ": ";
273  for ( auto tag : algo->outputDataObjs() ) verbose() << tag << " | ";
274  verbose() << endmsg;
275  }
276  }
277 
278  //---------------------------------------------------------------------------
280 
281  StatusCode global_sc( StatusCode::SUCCESS, true );
282 
283  // Production of DataNodes by AlgorithmNodes (DataNodes are created here)
284  for ( auto& algo : m_algoNameToAlgoNodeMap ) {
285 
286  auto& outputs = m_algoNameToAlgoOutputsMap[algo.first];
287  for ( auto output : outputs ) {
288  const auto sc = addDataNode( output );
289  if ( !sc.isSuccess() ) {
290  error() << "Extra producer (" << algo.first << ") for DataObject @ " << output
291  << " has been detected: this is not allowed." << endmsg;
292  global_sc = sc;
293  }
294  auto dataNode = getDataNode( output );
295  dataNode->addProducerNode( algo.second.get() );
296  algo.second->addOutputDataNode( dataNode );
297 
298  // Mirror the action above in the BGL-based graph
299  if ( m_enableAnalysis ) boost::add_edge( node( algo.second->name() ), node( output.fullKey() ), m_PRGraph );
300  }
301  }
302 
303  // Consumption of DataNodes by AlgorithmNodes
304  for ( auto& algo : m_algoNameToAlgoNodeMap ) {
305 
306  for ( auto input : m_algoNameToAlgoInputsMap[algo.first] ) {
307 
308  auto itP = m_dataPathToDataNodeMap.find( input );
309 
310  DataNode* dataNode = ( itP != m_dataPathToDataNodeMap.end() ? getDataNode( input ) : nullptr );
311  if ( dataNode ) {
312  dataNode->addConsumerNode( algo.second.get() );
313  algo.second->addInputDataNode( dataNode );
314 
315  // Mirror the action above in the BGL-based graph
316  if ( m_enableAnalysis ) boost::add_edge( node( input.fullKey() ), node( algo.second->name() ), m_PRGraph );
317  }
318  }
319  }
320 
321  return global_sc;
322  }
323 
324  //---------------------------------------------------------------------------
326  bool inverted, bool allPass ) {
327 
329 
331 
332  auto& algoName = algo->name();
333 
334  concurrency::AlgorithmNode* algoNode;
335 
336  auto itA = m_algoNameToAlgoNodeMap.find( algoName );
337  if ( itA != m_algoNameToAlgoNodeMap.end() ) {
338  algoNode = itA->second.get();
339  } else {
340  auto r = m_algoNameToAlgoNodeMap.emplace(
341  algoName, std::make_unique<concurrency::AlgorithmNode>( *this, algo, m_nodeCounter, m_algoCounter, inverted,
342  allPass ) );
343  algoNode = r.first->second.get();
344 
345  // Mirror AlgorithmNode in the BGL-based graph
346  if ( m_enableAnalysis ) {
347  boost::add_vertex( AlgoProps( algo, m_nodeCounter, m_algoCounter, inverted, allPass ), m_PRGraph );
348  }
349  ++m_nodeCounter;
350  ++m_algoCounter;
351  ON_VERBOSE verbose() << "AlgorithmNode '" << algoName << "' added @ " << algoNode << endmsg;
352 
353  registerIODataObjects( algo );
354  }
355 
357  auto itP = m_decisionNameToDecisionHubMap.find( parentName );
358  if ( itP != m_decisionNameToDecisionHubMap.end() ) {
359  auto parentNode = itP->second.get();
360 
361  parentNode->addDaughterNode( algoNode );
362  algoNode->addParentNode( parentNode );
363 
364  // Mirror algorithm to CF parent relationship in the BGL-based graph
365  if ( m_enableAnalysis ) boost::add_edge( node( algo->name() ), node( parentName ), m_PRGraph );
366 
367  ON_VERBOSE verbose() << "Attached AlgorithmNode '" << algo->name() << "' to parent DecisionNode '" << parentName
368  << "'" << endmsg;
369  } else {
370  sc = StatusCode::FAILURE;
371  error() << "Parent DecisionNode '" << parentName << "' was not found" << endmsg;
372  }
373 
374  return sc;
375  }
376 
377  //---------------------------------------------------------------------------
379 
380  auto itD = m_dataPathToDataNodeMap.find( dataPath );
381  if ( itD != m_dataPathToDataNodeMap.end() ) return StatusCode::SUCCESS;
382 
384  if ( !m_conditionsRealmEnabled ) {
385  dataNode = std::make_unique<concurrency::DataNode>( *this, dataPath );
386  ON_VERBOSE verbose() << " DataNode " << dataPath << " added @ " << dataNode.get() << endmsg;
387  // Mirror the action above in the BGL-based graph
388  if ( m_enableAnalysis ) boost::add_vertex( DataProps( dataPath ), m_PRGraph );
389  } else {
390  SmartIF<ICondSvc> condSvc{serviceLocator()->service( "CondSvc", false )};
391  if ( condSvc->isRegistered( dataPath ) ) {
392  dataNode = std::make_unique<concurrency::ConditionNode>( *this, dataPath, condSvc );
393  ON_VERBOSE verbose() << " ConditionNode " << dataPath << " added @ " << dataNode.get() << endmsg;
394  // Mirror the action above in the BGL-based graph
395  if ( m_enableAnalysis ) boost::add_vertex( CondDataProps( dataPath ), m_PRGraph );
396  } else {
397  dataNode = std::make_unique<concurrency::DataNode>( *this, dataPath );
398  ON_VERBOSE verbose() << " DataNode " << dataPath << " added @ " << dataNode.get() << endmsg;
399  // Mirror the action above in the BGL-based graph
400  if ( m_enableAnalysis ) boost::add_vertex( DataProps( dataPath ), m_PRGraph );
401  }
402  }
403  m_dataPathToDataNodeMap.emplace( dataPath, std::move( dataNode ) );
404  return StatusCode::SUCCESS;
405  }
406 
407  //---------------------------------------------------------------------------
409  Concurrent modeConcurrent, PromptDecision modePromptDecision,
410  ModeOr modeOR, AllPass allPass, Inverted isInverted ) {
411 
413 
415 
416  auto& decisionHubName = decisionHubAlgo->name();
417 
418  auto itA = m_decisionNameToDecisionHubMap.find( decisionHubName );
419  concurrency::DecisionNode* decisionHubNode;
420  if ( itA != m_decisionNameToDecisionHubMap.end() ) {
421  decisionHubNode = itA->second.get();
422  } else {
423  auto r = m_decisionNameToDecisionHubMap.emplace(
424  decisionHubName,
425  std::make_unique<concurrency::DecisionNode>( *this, m_nodeCounter, decisionHubName, modeConcurrent,
426  modePromptDecision, modeOR, allPass, isInverted ) );
427  decisionHubNode = r.first->second.get();
428  // Mirror DecisionNode in the BGL-based graph
429  if ( m_enableAnalysis ) {
430  boost::add_vertex( DecisionHubProps( decisionHubName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
431  allPass, isInverted ),
432  m_PRGraph );
433  }
434 
435  ++m_nodeCounter;
436 
437  ON_VERBOSE verbose() << "DecisionNode '" << decisionHubName << "' added @ " << decisionHubNode << endmsg;
438  }
439 
441  auto itP = m_decisionNameToDecisionHubMap.find( parentName );
442  if ( itP != m_decisionNameToDecisionHubMap.end() ) {
443  auto parentNode = itP->second.get();
444  parentNode->addDaughterNode( decisionHubNode );
445  decisionHubNode->addParentNode( parentNode );
446 
447  // Mirror DecisionNode-to-DecisionNode relationship in the BGL-based graph
448  if ( m_enableAnalysis ) boost::add_edge( node( decisionHubName ), node( parentName ), m_PRGraph );
449 
450  ON_VERBOSE verbose() << "Attached DecisionNode '" << decisionHubName << "' to parent DecisionNode '" << parentName
451  << "'" << endmsg;
452  } else {
453  sc = StatusCode::FAILURE;
454  error() << "Parent DecisionNode '" << parentName << "' was not found" << endmsg;
455  }
456 
457  return sc;
458  }
459 
460  //---------------------------------------------------------------------------
462  concurrency::PromptDecision modePromptDecision, concurrency::ModeOr modeOR,
463  concurrency::AllPass allPass, concurrency::Inverted isInverted ) {
464 
465  auto itH = m_decisionNameToDecisionHubMap.find( headName );
466  if ( itH != m_decisionNameToDecisionHubMap.end() ) {
467  m_headNode = itH->second.get();
468  } else {
469  auto r = m_decisionNameToDecisionHubMap.emplace(
470  headName, std::make_unique<concurrency::DecisionNode>( *this, m_nodeCounter, headName, modeConcurrent,
471  modePromptDecision, modeOR, allPass, isInverted ) );
472  m_headNode = r.first->second.get();
473 
474  // Mirror the action above in the BGL-based graph
475  if ( m_enableAnalysis ) {
476  boost::add_vertex( DecisionHubProps( headName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
477  allPass, isInverted ),
478  m_PRGraph );
479  }
480 
481  ++m_nodeCounter;
482  }
483  }
484 
485  //---------------------------------------------------------------------------
487  auto vp = vertices( m_PRGraph );
488  auto i = std::find_if( vp.first, vp.second, [&]( const PRVertexDesc& v ) {
489  return std::visit( precedence::VertexName(), m_PRGraph[v] ) == name;
490  } );
491  return i != vp.second ? *i : PRVertexDesc{};
492  }
493 
494  //---------------------------------------------------------------------------
496  // iterate through Algorithm nodes
497  for ( auto& pr : m_algoNameToAlgoNodeMap ) pr.second->accept( visitor );
498 
499  // iterate through DecisionHub nodes
500  for ( auto& pr : m_decisionNameToDecisionHubMap ) pr.second->accept( visitor );
501 
502  // iterate through Data [and Conditions] nodes
503  for ( auto& pr : m_dataPathToDataNodeMap ) pr.second->accept( visitor );
504  }
505 
506  //---------------------------------------------------------------------------
508 
509  info() << "Starting ranking by data outputs .. " << endmsg;
510  for ( auto& pair : m_algoNameToAlgoNodeMap ) {
511  ON_DEBUG debug() << " Ranking " << pair.first << "... " << endmsg;
512  pair.second->accept( ranker );
513  ON_DEBUG debug() << " ... rank of " << pair.first << ": " << pair.second->getRank() << endmsg;
514  }
515  }
516 
518  std::ostringstream ost;
519  dumpControlFlow( ost, m_headNode, 0 );
520  return ost.str();
521  }
522 
524  const int& indent ) const {
525  ost << std::string( indent * 2, ' ' );
526  DecisionNode* dn = dynamic_cast<DecisionNode*>( node );
527  AlgorithmNode* an = dynamic_cast<AlgorithmNode*>( node );
528  if ( dn != 0 ) {
529  if ( node != m_headNode ) {
530  ost << node->name() << " [Seq] ";
531  ost << ( ( dn->m_modeConcurrent ) ? " [Concurrent] " : " [Sequential] " );
532  ost << ( ( dn->m_modePromptDecision ) ? " [Prompt] " : "" );
533  ost << ( ( dn->m_modeOR ) ? " [OR] " : "" );
534  ost << ( ( dn->m_allPass ) ? " [PASS] " : "" );
535  ost << "\n";
536  }
537  for ( const auto& i : dn->getDaughters() ) dumpControlFlow( ost, i, indent + 1 );
538  } else if ( an != 0 ) {
539  ost << node->name() << " [Alg] ";
540  if ( an != 0 ) {
541  auto ar = an->getAlgorithm();
542  ost << " [n= " << ar->cardinality() << "]";
543  ost << ( ( !ar->isClonable() ) ? " [unclonable] " : "" );
544  }
545  ost << "\n";
546  }
547  }
548 
549  //---------------------------------------------------------------------------
551 
552  const char idt[] = " ";
553  std::ostringstream ost;
554 
555  ost << "\n" << idt << "====================================\n";
556  ost << idt << "Data origins and destinations:\n";
557  ost << idt << "====================================\n";
558 
559  for ( auto& pair : m_dataPathToDataNodeMap ) {
560 
561  for ( auto algoNode : pair.second->getProducers() ) ost << idt << " " << algoNode->name() << "\n";
562 
563  ost << idt << " V\n";
564  ost << idt << " o " << pair.first << "\n";
565  ost << idt << " V\n";
566 
567  for ( auto algoNode : pair.second->getConsumers() ) ost << idt << " " << algoNode->name() << "\n";
568 
569  ost << idt << "====================================\n";
570  }
571 
572  return ost.str();
573  }
574 
575  //---------------------------------------------------------------------------
576 
578  boost::filesystem::ofstream myfile;
579  myfile.open( fileName, std::ios::app );
580 
581  // Declare properties to dump
582  boost::dynamic_properties dp;
583 
584  dp.property( "Entity",
585  boost::make_transform_value_property_map(
586  []( const VariantVertexProps& v ) {
587  return std::visit( []( const auto& w ) { return boost::lexical_cast<std::string>( w ); }, v );
588  },
589  boost::get( boost::vertex_bundle, m_PRGraph ) ) );
590 
591  auto add_prop = [&]( auto name, auto&& vis ) {
592  dp.property( name, boost::make_transform_value_property_map(
593  [vis = std::forward<decltype( vis )>( vis )]( const VariantVertexProps& v ) {
594  return std::visit( vis, v );
595  },
596  boost::get( boost::vertex_bundle, m_PRGraph ) ) );
597  };
598 
599  add_prop( "Name", precedence::VertexName() );
600  add_prop( "Mode", precedence::GroupMode() );
601  add_prop( "Logic", precedence::GroupLogic() );
602  add_prop( "Decision Negation", precedence::DecisionNegation() );
603  add_prop( "Negative Decision Inversion", precedence::AllPass() );
604  add_prop( "Exit Policy", precedence::GroupExit() );
605  add_prop( "Operations", precedence::Operations() );
606  add_prop( "CF Decision", precedence::CFDecision( slot ) );
607  add_prop( "State", precedence::EntityState( slot, serviceLocator(), m_conditionsRealmEnabled ) );
608  add_prop( "Start Time (Epoch ns)", precedence::StartTime( slot, serviceLocator() ) );
609  add_prop( "End Time (Epoch ns)", precedence::EndTime( slot, serviceLocator() ) );
610  add_prop( "Runtime (ns)", precedence::Duration( slot, serviceLocator() ) );
611 
612  boost::write_graphml( myfile, m_PRGraph, dp );
613 
614  myfile.close();
615  }
616 
617  //---------------------------------------------------------------------------
619  boost::filesystem::ofstream myfile;
620  myfile.open( fileName, std::ios::app );
621 
622  // Fill runtimes (as this could not be done on the fly during trace assembling)
623  SmartIF<ITimelineSvc> timelineSvc = m_svcLocator->service<ITimelineSvc>( "TimelineSvc", false );
624  if ( !timelineSvc.isValid() ) {
625  warning() << "Failed to get the TimelineSvc, timing will not be added to "
626  << "the task precedence trace dump" << endmsg;
627  } else {
628 
629  for ( auto vp = vertices( m_precTrace ); vp.first != vp.second; ++vp.first ) {
630  TimelineEvent te{};
631  te.algorithm = m_precTrace[*vp.first].m_name;
632  timelineSvc->getTimelineEvent( te );
633  int runtime = std::chrono::duration_cast<std::chrono::microseconds>( te.end - te.start ).count();
634  m_precTrace[*vp.first].m_runtime = runtime;
635  }
636  }
637 
638  // Declare properties to dump
639  boost::dynamic_properties dp;
640  using boost::get;
642  dp.property( "Name", get( &AlgoTraceProps::m_name, m_precTrace ) );
643  dp.property( "Rank", get( &AlgoTraceProps::m_rank, m_precTrace ) );
644  dp.property( "Runtime", get( &AlgoTraceProps::m_runtime, m_precTrace ) );
645 
646  boost::write_graphml( myfile, m_precTrace, dp );
647 
648  myfile.close();
649  }
650 
652 
653  std::string u_name = u == nullptr ? "ENTRY" : u->name();
654  std::string v_name = v->name();
655 
657 
658  if ( !u ) {
659  auto itT = m_prec_trace_map.find( "ENTRY" );
660  if ( itT != m_prec_trace_map.end() ) {
661  source = itT->second;
662  } else {
663  source = boost::add_vertex( precedence::AlgoTraceProps( "ENTRY", -1, -1, -1.0 ), m_precTrace );
664  m_prec_trace_map["ENTRY"] = source;
665  }
666  } else {
667  auto itS = m_prec_trace_map.find( u_name );
668  if ( itS != m_prec_trace_map.end() ) {
669  source = itS->second;
670  } else {
671 
672  source =
673  boost::add_vertex( precedence::AlgoTraceProps( u_name, u->getAlgoIndex(), u->getRank(), -1 ), m_precTrace );
674  m_prec_trace_map[u_name] = source;
675  }
676  }
677 
679 
680  auto itP = m_prec_trace_map.find( v_name );
681  if ( itP != m_prec_trace_map.end() ) {
682  target = itP->second;
683  } else {
684 
685  target =
686  boost::add_vertex( precedence::AlgoTraceProps( v_name, v->getAlgoIndex(), v->getRank(), -1 ), m_precTrace );
687  m_prec_trace_map[v_name] = target;
688  }
689 
690  boost::add_edge( source, target, m_precTrace );
691 
692  ON_DEBUG debug() << u_name << "-->" << v_name << " precedence trait added" << endmsg;
693  }
694 
695 } // 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:96
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:361
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:206
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:97
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:556
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.