4 #include <boost/property_map/transform_value_property_map.hpp> 9 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 10 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 15 const char* stateToString(
const int& stateId ) {
43 const std::vector<int>& node_decisions,
const unsigned int& recursionLevel )
const {
49 for (
auto daughter :
m_children ) daughter->printState( output, states, node_decisions, recursionLevel + 2 );
57 bool result = visitor.
visit( *
this );
66 const std::vector<int>& node_decisions,
const unsigned int& recursionLevel )
const {
70 <<
", in state: " << states[m_algoIndex] <<
std::endl;
77 visitor.
visit( *
this );
93 if (
std::find( m_outputs.begin(), m_outputs.end(), node ) == m_outputs.end() ) m_outputs.push_back( node );
104 if ( serviceLocator()->existsService(
"CondSvc" ) ) {
106 if ( condSvc.isValid() ) {
107 info() <<
"CondSvc found. DF precedence rules will be augmented with 'Conditions'" <<
endmsg;
108 m_conditionsRealmEnabled =
true;
113 if ( m_conditionsRealmEnabled ) {
115 auto& condAlgs = condSvc->
condAlgs();
116 for (
const auto algo : condAlgs ) {
117 auto itA = m_algoNameToAlgoNodeMap.find( algo->name() );
118 if ( itA != m_algoNameToAlgoNodeMap.end() ) {
120 debug() <<
"Detaching condition algorithm '" << algo->name() <<
"' from the CF realm.." <<
endmsg;
122 parent->m_children.erase(
std::remove( parent->m_children.begin(), parent->m_children.end(), algoNode ),
123 parent->m_children.end() );
125 if ( m_enableAnalysis )
126 boost::remove_edge( node( algoNode->
getNodeName() ), node( parent->getNodeName() ), m_PRGraph );
131 warning() <<
"Algorithm '" << algo->name() <<
"' is not registered in the graph" <<
endmsg;
138 if ( !sc.
isSuccess() ) error() <<
"Could not build the data dependency realm." <<
endmsg;
154 verbose() <<
" Inputs of " << algoName <<
": ";
158 verbose() <<
" Outputs of " << algoName <<
": ";
170 for (
auto& algo : m_algoNameToAlgoNodeMap ) {
172 auto& outputs = m_algoNameToAlgoOutputsMap[algo.first];
173 for (
auto output : outputs ) {
174 const auto sc = addDataNode(
output );
175 if ( !sc.isSuccess() ) {
176 error() <<
"Extra producer (" << algo.first <<
") for DataObject @ " <<
output 177 <<
" has been detected: this is not allowed." <<
endmsg;
180 auto dataNode = getDataNode(
output );
181 dataNode->addProducerNode( algo.second.get() );
182 algo.second->addOutputDataNode( dataNode );
185 if ( m_enableAnalysis )
186 boost::add_edge( node( algo.second->getNodeName() ), node(
output.fullKey() ), m_PRGraph );
191 for (
auto& algo : m_algoNameToAlgoNodeMap ) {
193 for (
auto input : m_algoNameToAlgoInputsMap[algo.first] ) {
195 auto itP = m_dataPathToDataNodeMap.find( input );
197 DataNode* dataNode = ( itP != m_dataPathToDataNodeMap.end() ? getDataNode( input ) : nullptr );
200 algo.second->addInputDataNode( dataNode );
203 if ( m_enableAnalysis )
204 boost::add_edge( node( input.fullKey() ), node( algo.second->getNodeName() ), m_PRGraph );
214 bool inverted,
bool allPass ) {
220 auto& algoName = algo->
name();
224 auto itA = m_algoNameToAlgoNodeMap.find( algoName );
225 if ( itA != m_algoNameToAlgoNodeMap.end() ) {
226 algoNode = itA->second.get();
228 auto r = m_algoNameToAlgoNodeMap.emplace(
229 algoName, std::make_unique<concurrency::AlgorithmNode>( *
this, algo, m_nodeCounter, m_algoCounter, inverted,
231 algoNode = r.first->second.get();
234 if ( m_enableAnalysis ) {
235 boost::add_vertex(
AlgoProps( algo, m_nodeCounter, m_algoCounter, inverted, allPass ), m_PRGraph );
241 registerIODataObjects( algo );
245 auto itP = m_decisionNameToDecisionHubMap.find( parentName );
246 if ( itP != m_decisionNameToDecisionHubMap.end() ) {
247 auto parentNode = itP->second.get();
249 parentNode->addDaughterNode( algoNode );
253 if ( m_enableAnalysis ) boost::add_edge( node( algo->
name() ), node( parentName ), m_PRGraph );
255 ON_VERBOSE verbose() <<
"Attached AlgorithmNode '" << algo->
name() <<
"' to parent DecisionNode '" << parentName
259 error() <<
"Parent DecisionNode '" << parentName <<
"' was not found" <<
endmsg;
268 auto itD = m_dataPathToDataNodeMap.find( dataPath );
272 if ( !m_conditionsRealmEnabled ) {
273 dataNode = std::make_unique<concurrency::DataNode>( *
this, dataPath );
276 if ( m_enableAnalysis ) boost::add_vertex(
DataProps( dataPath ), m_PRGraph );
279 if ( condSvc->isRegistered( dataPath ) ) {
280 dataNode = std::make_unique<concurrency::ConditionNode>( *
this, dataPath, condSvc );
283 if ( m_enableAnalysis ) boost::add_vertex(
CondDataProps( dataPath ), m_PRGraph );
285 dataNode = std::make_unique<concurrency::DataNode>( *
this, dataPath );
288 if ( m_enableAnalysis ) boost::add_vertex(
DataProps( dataPath ), m_PRGraph );
291 m_dataPathToDataNodeMap.emplace( dataPath,
std::move( dataNode ) );
304 auto& decisionHubName = decisionHubAlgo->
name();
306 auto itA = m_decisionNameToDecisionHubMap.
find( decisionHubName );
308 if ( itA != m_decisionNameToDecisionHubMap.end() ) {
309 decisionHubNode = itA->second.get();
311 auto r = m_decisionNameToDecisionHubMap.emplace(
313 std::make_unique<concurrency::DecisionNode>( *
this, m_nodeCounter, decisionHubName, modeConcurrent,
314 modePromptDecision, modeOR, allPass, isInverted ) );
315 decisionHubNode = r.first->second.get();
317 if ( m_enableAnalysis ) {
318 boost::add_vertex(
DecisionHubProps( decisionHubName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
319 allPass, isInverted ),
329 auto itP = m_decisionNameToDecisionHubMap.find( parentName );
330 if ( itP != m_decisionNameToDecisionHubMap.end() ) {
331 auto parentNode = itP->second.get();
332 parentNode->addDaughterNode( decisionHubNode );
336 if ( m_enableAnalysis ) boost::add_edge( node( decisionHubName ), node( parentName ), m_PRGraph );
338 ON_VERBOSE verbose() <<
"Attached DecisionNode '" << decisionHubName <<
"' to parent DecisionNode '" << parentName
342 error() <<
"Parent DecisionNode '" << parentName <<
"' was not found" <<
endmsg;
353 auto itH = m_decisionNameToDecisionHubMap.find( headName );
354 if ( itH != m_decisionNameToDecisionHubMap.end() ) {
355 m_headNode = itH->second.get();
357 auto r = m_decisionNameToDecisionHubMap.emplace(
358 headName, std::make_unique<concurrency::DecisionNode>( *
this, m_nodeCounter, headName, modeConcurrent,
359 modePromptDecision, modeOR, allPass, isInverted ) );
360 m_headNode = r.first->second.get();
363 if ( m_enableAnalysis ) {
364 boost::add_vertex(
DecisionHubProps( headName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
365 allPass, isInverted ),
375 auto vp = vertices( m_PRGraph );
377 return boost::apply_visitor( precedence::VertexName(), m_PRGraph[v] ) == name;
385 for (
auto& pr : m_algoNameToAlgoNodeMap ) pr.second->accept( visitor );
388 for (
auto& pr : m_decisionNameToDecisionHubMap ) pr.second->accept( visitor );
391 for (
auto& pr : m_dataPathToDataNodeMap ) pr.second->accept( visitor );
397 info() <<
"Starting ranking by data outputs .. " <<
endmsg;
398 for (
auto& pair : m_algoNameToAlgoNodeMap ) {
399 ON_DEBUG debug() <<
" Ranking " << pair.first <<
"... " <<
endmsg;
400 pair.second->accept( ranker );
401 ON_DEBUG debug() <<
" ... rank of " << pair.first <<
": " << pair.second->getRank() <<
endmsg;
407 dumpControlFlow( ost, m_headNode, 0 );
412 const int& indent )
const {
417 if ( node != m_headNode ) {
421 ost << ( ( dn->
m_modeOR ) ?
" [OR] " :
"" );
422 ost << ( ( dn->
m_allPass ) ?
" [PASS] " :
"" );
425 for (
const auto& i : dn->
getDaughters() ) dumpControlFlow( ost, i, indent + 1 );
426 }
else if ( an != 0 ) {
430 ost <<
" [n= " << ar->cardinality() <<
"]";
431 ost << ( ( !ar->isClonable() ) ?
" [unclonable] " :
"" );
440 const char idt[] =
" ";
443 ost <<
"\n" << idt <<
"====================================\n";
444 ost << idt <<
"Data origins and destinations:\n";
445 ost << idt <<
"====================================\n";
447 for (
auto& pair : m_dataPathToDataNodeMap ) {
449 for (
auto algoNode : pair.second->getProducers() ) ost << idt <<
" " << algoNode->getNodeName() <<
"\n";
451 ost << idt <<
" V\n";
452 ost << idt <<
" o " << pair.first <<
"\n";
453 ost << idt <<
" V\n";
455 for (
auto algoNode : pair.second->getConsumers() ) ost << idt <<
" " << algoNode->getNodeName() <<
"\n";
457 ost << idt <<
"====================================\n";
466 boost::filesystem::ofstream myfile;
467 myfile.open( fileName, std::ios::app );
470 boost::dynamic_properties dp;
472 dp.property(
"Entity", boost::make_transform_value_property_map(
474 boost::get( boost::vertex_bundle, m_PRGraph ) ) );
476 auto add_prop = [&](
auto name,
auto&& vis ) {
477 dp.property(
name, boost::make_transform_value_property_map(
479 return boost::apply_visitor( vis, v );
481 boost::get( boost::vertex_bundle, m_PRGraph ) ) );
497 boost::write_graphml( myfile, m_PRGraph, dp );
504 boost::filesystem::ofstream myfile;
505 myfile.open( fileName, std::ios::app );
509 if ( !timelineSvc.
isValid() ) {
510 warning() <<
"Failed to get the TimelineSvc, timing will not be added to " 511 <<
"the task precedence trace dump" <<
endmsg;
514 for (
auto vp = vertices( m_precTrace ); vp.first != vp.second; ++vp.first ) {
516 te.
algorithm = m_precTrace[*vp.first].m_name;
519 m_precTrace[*vp.first].m_runtime = runtime;
524 boost::dynamic_properties dp;
527 dp.property(
"Name",
get( &AlgoTraceProps::m_name, m_precTrace ) );
528 dp.property(
"Rank",
get( &AlgoTraceProps::m_rank, m_precTrace ) );
529 dp.property(
"Runtime",
get( &AlgoTraceProps::m_runtime, m_precTrace ) );
531 boost::write_graphml( myfile, m_precTrace, dp );
544 auto itT = m_prec_trace_map.
find(
"ENTRY" );
545 if ( itT != m_prec_trace_map.end() ) {
546 source = itT->second;
549 m_prec_trace_map[
"ENTRY"] = source;
552 auto itS = m_prec_trace_map.find( u_name );
553 if ( itS != m_prec_trace_map.end() ) {
554 source = itS->second;
559 m_prec_trace_map[u_name] = source;
565 auto itP = m_prec_trace_map.find( v_name );
566 if ( itP != m_prec_trace_map.end() ) {
567 target = itP->second;
572 m_prec_trace_map[v_name] = target;
575 boost::add_edge( source, target, m_precTrace );
577 ON_DEBUG debug() << u_name <<
"-->" << v_name <<
" precedence trait added" <<
endmsg;
PRVertexDesc node(const std::string &) const
const unsigned int & getAlgoIndex() const
Get algorithm index.
Class representing an event slot.
std::vector< DecisionNode * > m_parents
Direct parent nodes.
void accept(IGraphVisitor &visitor) const
An entry point to visit all graph nodes.
void addDaughterNode(ControlFlowNode *node)
Add a daughter node.
boost::graph_traits< PrecTrace >::vertex_descriptor AlgoTraceVertex
boost::graph_traits< PRGraph >::vertex_descriptor PRVertexDesc
const std::string & name() const override
The identifying name of the algorithm object.
StatusCode addAlgorithmNode(Gaudi::Algorithm *daughterAlgo, const std::string &parentName, bool inverted, bool allPass)
Add algorithm node.
const DataObjIDColl & outputDataObjs() const override
virtual bool visit(DecisionNode &)
std::vector< ControlFlowNode * > m_children
All direct daughter nodes in the tree.
Gaudi::Algorithm * getAlgorithm() const
get Algorithm representatives
void dumpPrecRules(const boost::filesystem::path &, const EventSlot &slot)
dump to file the precedence rules
constexpr static const auto SUCCESS
std::vector< DecisionNode * > m_parents
Control flow parents of an AlgorithmNode (DecisionNodes)
T duration_cast(T...args)
StatusCode addDataNode(const DataObjID &dataPath)
Add DataNode that represents DataObject.
void rankAlgorithms(IGraphVisitor &ranker) const
Rank Algorithm nodes by the number of data outputs.
bool m_allPass
Whether always passing regardless of daughter results.
const std::vector< DecisionNode * > & getParentDecisionHubs() const
Get all parent decision hubs.
Gaudi::tagged_bool< class ModeOr_tag > ModeOr
virtual bool visitEnter(DecisionNode &) const
bool m_modeOR
Whether acting as "and" (false) or "or" node (true)
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.
void dumpPrecTrace(const boost::filesystem::path &)
dump to file the precedence trace
const float & getRank() const
Get Algorithm rank.
The AlgsExecutionStates encodes the state machine for the execution of algorithms within a single eve...
virtual const std::set< IAlgorithm * > & condAlgs() const =0
get list of all registered condition Algorithms
Gaudi::tagged_bool< class Inverted_tag > Inverted
This class is used for returning status codes from appropriate routines.
const DataObjIDColl & inputDataObjs() const override
std::string dumpDataFlow() const
Print out all data origins and destinations, as reflected in the EF graph.
const std::vector< ControlFlowNode * > & getDaughters() const
Get children nodes.
void registerIODataObjects(const Gaudi::Algorithm *algo)
Register algorithm in the Data Dependency index.
Gaudi::tagged_bool< class Concurrent_tag > Concurrent
bool m_modePromptDecision
Whether to evaluate the hub decision ASA its child decisions allow to do that.
virtual bool getTimelineEvent(TimelineEvent &) const =0
void addEdgeToPrecTrace(const AlgorithmNode *u, const AlgorithmNode *v)
set cause-effect connection between two algorithms in the precedence trace
void addOutputDataNode(DataNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
std::vector< InputHandle_t< In > > m_inputs
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.
Gaudi::tagged_bool< class PromptDecision_tag > PromptDecision
void addParentNode(DecisionNode *node)
Add a parent node.
bool isValid() const
Allow for check if smart pointer is valid.
bool m_modeConcurrent
Whether all daughters will be evaluated concurrently or sequentially.
void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const override
Print a string representing the control flow state.
Base class from which all concrete algorithm classes should be derived.
Gaudi::tagged_bool< class AllPass_tag > AllPass
constexpr static const auto FAILURE
void addParentNode(DecisionNode *node)
Add a parent node.
bool accept(IGraphVisitor &visitor) override
Visitor entry point.
const std::string & getNodeName() const
Get node name.
void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const override
Print a string representing the control flow state.
StatusCode buildDataDependenciesRealm()
Build data dependency realm WITH data object nodes participating.
std::string dumpControlFlow() const
Print out control flow of Algorithms and Sequences.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
void addHeadNode(const std::string &headName, concurrency::Concurrent, concurrency::PromptDecision, concurrency::ModeOr, concurrency::AllPass, concurrency::Inverted)
Add a node, which has no parents.
boost::variant< AlgoProps, DecisionHubProps, DataProps, CondDataProps > VariantVertexProps
void addConsumerNode(AlgorithmNode *node)
Add relationship to consumer AlgorithmNode.