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