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 ) ) 16 const char* stateToString(
const int& stateId )
48 const std::vector<int>& node_decisions,
const unsigned int& recursionLevel )
const 55 for (
auto daughter :
m_children ) daughter->printState( output, states, node_decisions, recursionLevel + 2 );
64 bool result = visitor.
visit( *
this );
73 const std::vector<int>& node_decisions,
const unsigned int& recursionLevel )
const 78 <<
", in state: " << states[m_algoIndex] <<
std::endl;
86 visitor.
visit( *
this );
104 if (
std::find( m_outputs.begin(), m_outputs.end(), node ) == m_outputs.end() ) m_outputs.push_back( node );
117 if ( serviceLocator()->existsService(
"CondSvc" ) ) {
119 if ( condSvc.isValid() ) {
120 info() <<
"CondSvc found. DF precedence rules will be augmented with 'Conditions'" <<
endmsg;
121 m_conditionsRealmEnabled =
true;
126 if ( m_conditionsRealmEnabled ) {
128 auto& condAlgs = condSvc->
condAlgs();
129 for (
const auto algo : condAlgs ) {
130 auto itA = m_algoNameToAlgoNodeMap.find( algo->name() );
131 if ( itA != m_algoNameToAlgoNodeMap.end() ) {
133 debug() <<
"Detaching condition algorithm '" << algo->name() <<
"' from the CF realm.." <<
endmsg;
135 parent->m_children.erase(
std::remove( parent->m_children.begin(), parent->m_children.end(), algoNode ),
136 parent->m_children.end() );
138 if ( m_enableAnalysis )
139 boost::remove_edge( node( algoNode->
getNodeName() ), node( parent->getNodeName() ), m_PRGraph );
144 warning() <<
"Algorithm '" << algo->name() <<
"' is not registered in the graph" <<
endmsg;
151 if ( !sc.
isSuccess() ) error() <<
"Could not build the data dependency realm." <<
endmsg;
169 verbose() <<
" Inputs of " << algoName <<
": ";
173 verbose() <<
" Outputs of " << algoName <<
": ";
186 for (
auto& algo : m_algoNameToAlgoNodeMap ) {
188 auto& outputs = m_algoNameToAlgoOutputsMap[algo.first];
189 for (
auto output : outputs ) {
190 const auto sc = addDataNode(
output );
191 if ( !sc.isSuccess() ) {
192 error() <<
"Extra producer (" << algo.first <<
") for DataObject @ " <<
output 193 <<
" has been detected: this is not allowed." <<
endmsg;
196 auto dataNode = getDataNode(
output );
197 dataNode->addProducerNode( algo.second.get() );
198 algo.second->addOutputDataNode( dataNode );
201 if ( m_enableAnalysis )
202 boost::add_edge( node( algo.second->getNodeName() ), node(
output.fullKey() ), m_PRGraph );
207 for (
auto& algo : m_algoNameToAlgoNodeMap ) {
209 for (
auto input : m_algoNameToAlgoInputsMap[algo.first] ) {
211 auto itP = m_dataPathToDataNodeMap.find( input );
213 DataNode* dataNode = ( itP != m_dataPathToDataNodeMap.end() ? getDataNode( input ) : nullptr );
216 algo.second->addInputDataNode( dataNode );
219 if ( m_enableAnalysis )
220 boost::add_edge( node( input.fullKey() ), node( algo.second->getNodeName() ), m_PRGraph );
237 auto& algoName = algo->
name();
241 auto itA = m_algoNameToAlgoNodeMap.find( algoName );
242 if ( itA != m_algoNameToAlgoNodeMap.end() ) {
243 algoNode = itA->second.get();
245 auto r = m_algoNameToAlgoNodeMap.emplace(
246 algoName, std::make_unique<concurrency::AlgorithmNode>( *
this, algo, m_nodeCounter, m_algoCounter, inverted,
248 algoNode = r.first->second.get();
251 if ( m_enableAnalysis ) {
252 boost::add_vertex(
AlgoProps( algo, m_nodeCounter, m_algoCounter, inverted, allPass ), m_PRGraph );
258 registerIODataObjects( algo );
262 auto itP = m_decisionNameToDecisionHubMap.find( parentName );
263 if ( itP != m_decisionNameToDecisionHubMap.end() ) {
264 auto parentNode = itP->second.get();
266 parentNode->addDaughterNode( algoNode );
270 if ( m_enableAnalysis ) boost::add_edge( node( algo->
name() ), node( parentName ), m_PRGraph );
272 ON_VERBOSE verbose() <<
"Attached AlgorithmNode '" << algo->
name() <<
"' to parent DecisionNode '" << parentName
276 error() <<
"Parent DecisionNode '" << parentName <<
"' was not found" <<
endmsg;
286 auto itD = m_dataPathToDataNodeMap.find( dataPath );
290 if ( !m_conditionsRealmEnabled ) {
291 dataNode = std::make_unique<concurrency::DataNode>( *
this, dataPath );
294 if ( m_enableAnalysis ) boost::add_vertex(
DataProps( dataPath ), m_PRGraph );
297 if ( condSvc->isRegistered( dataPath ) ) {
298 dataNode = std::make_unique<concurrency::ConditionNode>( *
this, dataPath, condSvc );
301 if ( m_enableAnalysis ) boost::add_vertex(
CondDataProps( dataPath ), m_PRGraph );
303 dataNode = std::make_unique<concurrency::DataNode>( *
this, dataPath );
306 if ( m_enableAnalysis ) boost::add_vertex(
DataProps( dataPath ), m_PRGraph );
309 m_dataPathToDataNodeMap.emplace( dataPath,
std::move( dataNode ) );
323 auto& decisionHubName = decisionHubAlgo->
name();
325 auto itA = m_decisionNameToDecisionHubMap.
find( decisionHubName );
327 if ( itA != m_decisionNameToDecisionHubMap.end() ) {
328 decisionHubNode = itA->second.get();
330 auto r = m_decisionNameToDecisionHubMap.emplace(
332 std::make_unique<concurrency::DecisionNode>( *
this, m_nodeCounter, decisionHubName, modeConcurrent,
333 modePromptDecision, modeOR, allPass, isInverted ) );
334 decisionHubNode = r.first->second.get();
336 if ( m_enableAnalysis ) {
337 boost::add_vertex(
DecisionHubProps( decisionHubName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
338 allPass, isInverted ),
348 auto itP = m_decisionNameToDecisionHubMap.find( parentName );
349 if ( itP != m_decisionNameToDecisionHubMap.end() ) {
350 auto parentNode = itP->second.get();
351 parentNode->addDaughterNode( decisionHubNode );
355 if ( m_enableAnalysis ) boost::add_edge( node( decisionHubName ), node( parentName ), m_PRGraph );
357 ON_VERBOSE verbose() <<
"Attached DecisionNode '" << decisionHubName <<
"' to parent DecisionNode '" << parentName
361 error() <<
"Parent DecisionNode '" << parentName <<
"' was not found" <<
endmsg;
373 auto itH = m_decisionNameToDecisionHubMap.find( headName );
374 if ( itH != m_decisionNameToDecisionHubMap.end() ) {
375 m_headNode = itH->second.get();
377 auto r = m_decisionNameToDecisionHubMap.emplace(
378 headName, std::make_unique<concurrency::DecisionNode>( *
this, m_nodeCounter, headName, modeConcurrent,
379 modePromptDecision, modeOR, allPass, isInverted ) );
380 m_headNode = r.first->second.get();
383 if ( m_enableAnalysis ) {
384 boost::add_vertex(
DecisionHubProps( headName, m_nodeCounter, modeConcurrent, modePromptDecision, modeOR,
385 allPass, isInverted ),
396 auto vp = vertices( m_PRGraph );
398 return boost::apply_visitor( precedence::VertexName(), m_PRGraph[v] ) == name;
407 for (
auto& pr : m_algoNameToAlgoNodeMap ) pr.second->accept( visitor );
410 for (
auto& pr : m_decisionNameToDecisionHubMap ) pr.second->accept( visitor );
413 for (
auto& pr : m_dataPathToDataNodeMap ) pr.second->accept( visitor );
420 info() <<
"Starting ranking by data outputs .. " <<
endmsg;
421 for (
auto& pair : m_algoNameToAlgoNodeMap ) {
422 ON_DEBUG debug() <<
" Ranking " << pair.first <<
"... " <<
endmsg;
423 pair.second->accept( ranker );
424 ON_DEBUG debug() <<
" ... rank of " << pair.first <<
": " << pair.second->getRank() <<
endmsg;
431 dumpControlFlow( ost, m_headNode, 0 );
441 if ( node != m_headNode ) {
445 ost << ( ( dn->
m_modeOR ) ?
" [OR] " :
"" );
446 ost << ( ( dn->
m_allPass ) ?
" [PASS] " :
"" );
449 for (
const auto& i : dn->
getDaughters() ) dumpControlFlow( ost, i, indent + 1 );
450 }
else if ( an != 0 ) {
454 ost <<
" [n= " << ar->cardinality() <<
"]";
455 ost << ( ( !ar->isClonable() ) ?
" [unclonable] " :
"" );
465 const char idt[] =
" ";
468 ost <<
"\n" << idt <<
"====================================\n";
469 ost << idt <<
"Data origins and destinations:\n";
470 ost << idt <<
"====================================\n";
472 for (
auto& pair : m_dataPathToDataNodeMap ) {
474 for (
auto algoNode : pair.second->getProducers() ) ost << idt <<
" " << algoNode->getNodeName() <<
"\n";
476 ost << idt <<
" V\n";
477 ost << idt <<
" o " << pair.first <<
"\n";
478 ost << idt <<
" V\n";
480 for (
auto algoNode : pair.second->getConsumers() ) ost << idt <<
" " << algoNode->getNodeName() <<
"\n";
482 ost << idt <<
"====================================\n";
492 boost::filesystem::ofstream myfile;
493 myfile.open( fileName, std::ios::app );
496 boost::dynamic_properties dp;
498 dp.property(
"Entity", boost::make_transform_value_property_map(
500 boost::get( boost::vertex_bundle, m_PRGraph ) ) );
502 auto add_prop = [&](
auto name,
auto&& vis ) {
504 boost::make_transform_value_property_map( [vis =
std::forward<decltype( vis )>( vis )](
506 v ) {
return boost::apply_visitor( vis, v ); },
507 boost::get( boost::vertex_bundle, m_PRGraph ) ) );
523 boost::write_graphml( myfile, m_PRGraph, dp );
531 boost::filesystem::ofstream myfile;
532 myfile.open( fileName, std::ios::app );
536 if ( !timelineSvc.
isValid() ) {
537 warning() <<
"Failed to get the TimelineSvc, timing will not be added to " 538 <<
"the task precedence trace dump" <<
endmsg;
541 for (
auto vp = vertices( m_precTrace ); vp.first != vp.second; ++vp.first ) {
543 te.
algorithm = m_precTrace[*vp.first].m_name;
546 m_precTrace[*vp.first].m_runtime = runtime;
551 boost::dynamic_properties dp;
554 dp.property(
"Name",
get( &AlgoTraceProps::m_name, m_precTrace ) );
555 dp.property(
"Rank",
get( &AlgoTraceProps::m_rank, m_precTrace ) );
556 dp.property(
"Runtime",
get( &AlgoTraceProps::m_runtime, m_precTrace ) );
558 boost::write_graphml( myfile, m_precTrace, dp );
572 auto itT = m_prec_trace_map.
find(
"ENTRY" );
573 if ( itT != m_prec_trace_map.end() ) {
574 source = itT->second;
577 m_prec_trace_map[
"ENTRY"] = source;
580 auto itS = m_prec_trace_map.find( u_name );
581 if ( itS != m_prec_trace_map.end() ) {
582 source = itS->second;
587 m_prec_trace_map[u_name] = source;
593 auto itP = m_prec_trace_map.find( v_name );
594 if ( itP != m_prec_trace_map.end() ) {
595 target = itP->second;
600 m_prec_trace_map[v_name] = target;
603 boost::add_edge( source, target, m_precTrace );
605 ON_DEBUG debug() << u_name <<
"-->" << v_name <<
" precedence trait added" <<
endmsg;
constexpr static const auto FAILURE
PRVertexDesc node(const std::string &) const
const unsigned int & getAlgoIndex() const
Get algorithm index.
std::vector< DecisionNode * > m_parents
Direct parent nodes.
StatusCode addAlgorithmNode(Algorithm *daughterAlgo, const std::string &parentName, bool inverted, bool allPass)
Add algorithm node.
void accept(IGraphVisitor &visitor) const
An entry point to visit all graph nodes.
const std::string & name() const override
The identifying name of the algorithm object.
void addDaughterNode(ControlFlowNode *node)
Add a daughter node.
boost::graph_traits< PrecTrace >::vertex_descriptor AlgoTraceVertex
boost::graph_traits< PRGraph >::vertex_descriptor PRVertexDesc
const DataObjIDColl & outputDataObjs() const override
virtual bool visit(DecisionNode &)
std::vector< ControlFlowNode * > m_children
All direct daughter nodes in the tree.
void dumpPrecRules(const boost::filesystem::path &, const EventSlot &slot)
dump to file the precedence rules
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
Algorithm * getAlgorithm() const
get Algorithm representatives
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.
Gaudi::tagged_bool< class Concurrent_tag > Concurrent
constexpr static const auto SUCCESS
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.
Base class from which all concrete algorithm classes should be derived.
std::vector< InputHandle_t< In > > m_inputs
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.
void registerIODataObjects(const Algorithm *algo)
Register algorithm in the Data Dependency index.
bool m_modeConcurrent
Whether all daughters will be evaluated concurrently or sequentially.
StatusCode addDecisionHubNode(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 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.
Class representing the event slot.
Gaudi::tagged_bool< class AllPass_tag > AllPass
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.