All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ExecutionFlowGraph.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_EXECUTIONFLOWGRAPH_H
2 #define GAUDIHIVE_EXECUTIONFLOWGRAPH_H
3 
4 // std includes
5 #include <vector>
6 #include <algorithm>
7 #include <unordered_map>
8 #include <chrono>
9 #include <fstream>
10 #include <sstream>
11 
12 #include <boost/graph/adjacency_list.hpp>
13 #include <boost/graph/graphml.hpp>
14 
15 // fwk includes
16 #include "AlgsExecutionStates.h"
17 #include "EventSlot.h"
18 #include "IGraphVisitor.h"
19 #include "GaudiKernel/Algorithm.h"
20 #include "GaudiKernel/CommonMessaging.h"
21 
22 #include "CPUCruncher.h"
23 
24 namespace boost {
25 
26  struct AlgoNodeStruct {
28  AlgoNodeStruct (const std::string& name, const int index = -1, const int& rank = -1, const double& runtime = -1, const double& eccentricity = -1.0) :
29  m_name(name), m_index(index), m_rank(rank), m_runtime(runtime), m_eccentricity(eccentricity) {}
30  std::string m_name;
31  int m_index;
32  int m_rank;
33  double m_runtime;
35  };
36 
37  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, AlgoNodeStruct> ExecPlan;
38  typedef graph_traits<ExecPlan>::vertex_descriptor AlgoVertex;
39 }
40 
41 namespace concurrency {
42 
44  class ExecutionFlowGraph;
45 
47  public:
49  ControlFlowNode(ExecutionFlowGraph& graph, unsigned int nodeIndex, const std::string& name) :
50  m_graph(&graph), m_nodeIndex(nodeIndex), m_nodeName(name) {};
52  virtual ~ControlFlowNode() {};
54  virtual void initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map) = 0;
56  virtual bool accept(IGraphVisitor& visitor) = 0;
58  virtual bool promoteToControlReadyState(const int& slotNum,
59  AlgsExecutionStates& states,
60  std::vector<int>& node_decisions) const = 0;
62  virtual int updateState(AlgsExecutionStates& states,
63  std::vector<int>& node_decisions) const = 0;
65  virtual void printState(std::stringstream& output,
66  AlgsExecutionStates& states,
67  const std::vector<int>& node_decisions,
68  const unsigned int& recursionLevel) const = 0;
70  const unsigned int& getNodeIndex() const { return m_nodeIndex; }
71  const std::string& getNodeName() const { return m_nodeName; }
72  virtual void updateDecision(const int& slotNum,
73  AlgsExecutionStates& states,
74  std::vector<int>& node_decisions,
75  const AlgorithmNode* requestor = nullptr) const = 0;
76  public:
78  protected:
80  std::string stateToString(const int& stateId) const;
81  unsigned int m_nodeIndex;
82  std::string m_nodeName;
83  };
84 
85 
86  class DecisionNode : public ControlFlowNode {
87  public:
89  DecisionNode(ExecutionFlowGraph& graph, unsigned int nodeIndex, const std::string& name, bool modeOR, bool allPass, bool isLazy) :
90  ControlFlowNode(graph, nodeIndex, name),
91  m_modeOR(modeOR), m_allPass(allPass), m_isLazy(isLazy), m_children()
92  {};
94  virtual ~DecisionNode();
96  virtual void initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map);
97  virtual bool accept(IGraphVisitor& visitor);
99  virtual bool promoteToControlReadyState(const int& slotNum,
100  AlgsExecutionStates& states,
101  std::vector<int>& node_decisions) const;
103  virtual void updateDecision(const int& slotNum,
104  AlgsExecutionStates& states,
105  std::vector<int>& node_decisions,
106  const AlgorithmNode* requestor = nullptr) const;
108  virtual int updateState(AlgsExecutionStates& states,
109  std::vector<int>& node_decisions) const;
111  void addParentNode(DecisionNode* node);
113  void addDaughterNode(ControlFlowNode* node);
115  const std::vector<ControlFlowNode*>& getDaughters() const {return m_children;}
117  virtual void printState(std::stringstream& output,
118  AlgsExecutionStates& states,
119  const std::vector<int>& node_decisions,
120  const unsigned int& recursionLevel) const;
121  public:
123  bool m_modeOR;
125  bool m_allPass;
127  bool m_isLazy;
128  private:
130  std::vector<ControlFlowNode*> m_children;
132  std::vector<DecisionNode*> m_parents;
133  };
134 
135  class DataNode;
136 
138  public:
140  AlgorithmNode(ExecutionFlowGraph& graph, unsigned int nodeIndex, const std::string& algoName, bool inverted, bool allPass) :
141  ControlFlowNode(graph, nodeIndex, algoName),
142  m_algoIndex(0),m_algoName(algoName),m_inverted(inverted),m_allPass(allPass),m_rank(-1)
143  {};
145  ~AlgorithmNode();
147  virtual void initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map);
149  virtual bool accept(IGraphVisitor& visitor);
151  void addParentNode(DecisionNode* node);
152 
154  void addSupplierNode(AlgorithmNode* node) { m_suppliers.push_back(node); }
156  void addConsumerNode(AlgorithmNode* node) { m_consumers.push_back(node); }
158  void attachAlgorithm(IAlgorithm* ialgo) { m_representatives.push_back(ialgo); }
160  const std::vector<IAlgorithm*>& getAlgorithmRepresentatives () const { return m_representatives; }
162  const std::vector<AlgorithmNode*>& getSupplierNodes() const {return m_suppliers;}
164  const std::vector<AlgorithmNode*>& getConsumerNodes() const {return m_consumers;}
165 
167  void addOutputDataNode(DataNode* node);
169  void addInputDataNode(DataNode* node);
171  const std::vector<DataNode*>& getOutputDataNodes() const {return m_outputs;}
173  const std::vector<DataNode*>& getInputDataNodes() const {return m_inputs;}
175  void setRank(float& rank) {m_rank = rank;}
177  const float& getRank() const {return m_rank;}
178 
180  const unsigned int& getAlgoIndex() const { return m_algoIndex; }
182  bool dataDependenciesSatisfied(const int& slotNum) const;
185  virtual int updateState(AlgsExecutionStates& states,
186  std::vector<int>& node_decisions) const;
188  virtual bool promoteToControlReadyState(const int& slotNum,
189  AlgsExecutionStates& states,
190  std::vector<int>& node_decisions) const;
192  bool promoteToDataReadyState(const int& slotNum, const AlgorithmNode* requestor = nullptr) const;
194  virtual void updateDecision(const int& slotNum,
195  AlgsExecutionStates& states,
196  std::vector<int>& node_decisions,
197  const AlgorithmNode* requestor = nullptr) const;
199  virtual void printState(std::stringstream& output,
200  AlgsExecutionStates& states,
201  const std::vector<int>& node_decisions,
202  const unsigned int& recursionLevel) const;
203  private:
205  unsigned int m_algoIndex;
207  std::string m_algoName;
211  bool m_allPass;
213  std::vector<DecisionNode*> m_parents;
214 
217  std::vector<AlgorithmNode*> m_suppliers;
219  std::vector<AlgorithmNode*> m_consumers;
220 
223  std::vector<DataNode*> m_outputs;
225  std::vector<DataNode*> m_inputs;
227  float m_rank;
229  std::vector<IAlgorithm*> m_representatives;
230  };
231 
232 class DataNode {
233 public:
235  DataNode(ExecutionFlowGraph& /*graph*/, const std::string& path): m_data_object_path(path) {}
238  const std::string& getPath() {return m_data_object_path;}
241  if (std::find(m_producers.begin(),m_producers.end(),node) == m_producers.end())
242  m_producers.push_back(node);
243  }
246  if (std::find(m_consumers.begin(),m_consumers.end(),node) == m_consumers.end())
247  m_consumers.push_back(node);
248  }
250  const std::vector<AlgorithmNode*>& getProducers() const {return m_producers;}
252  const std::vector<AlgorithmNode*>& getConsumers() const {return m_consumers;}
253 private:
254  std::string m_data_object_path;
255  std::vector<AlgorithmNode*> m_producers;
256  std::vector<AlgorithmNode*> m_consumers;
257  };
258 
259 typedef std::unordered_map<std::string,AlgorithmNode*> AlgoNodesMap;
260 typedef std::unordered_map<std::string,DecisionNode*> DecisionHubsMap;
261 typedef std::unordered_map<std::string,DataNode*> DataNodesMap;
262 typedef std::unordered_map<std::string,const DataObjectDescriptorCollection*> AlgoInputsMap;
263 typedef std::unordered_map<std::string,const DataObjectDescriptorCollection*> AlgoOutputsMap;
264 
267  virtual ~IExecutionFlowGraph() = default;
268 };
269 
270 class ExecutionFlowGraph : public CommonMessaging<IExecutionFlowGraph> {
272 public:
274  ExecutionFlowGraph(const std::string& name, SmartIF<ISvcLocator> svc) :
275  m_headNode(0), m_nodeCounter(0), m_svcLocator(svc), m_name(name), m_initTime(std::chrono::high_resolution_clock::now()),
276  m_eventSlots(nullptr) {}
278  ~ExecutionFlowGraph() override {
279  if (m_headNode != 0) delete m_headNode;
280  }
282  StatusCode initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map);
283  StatusCode initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map,
284  std::vector<EventSlot>& eventSlots);
286  void registerIODataObjects(const Algorithm* algo);
292  void addHeadNode(const std::string& headName, bool modeOR, bool allPass, bool isLazy);
294  StatusCode addAlgorithmNode(Algorithm* daughterAlgo, const std::string& parentName, bool inverted, bool allPass);
296  template<class T>
297  void attachAlgorithmsToNodes(const std::string& algo_name, const T& container) {
298  auto node = getAlgorithmNode(algo_name);
299  for (auto ialgoIt = container.unsafe_begin(); ialgoIt != container.unsafe_end(); ++ialgoIt)
300  node->attachAlgorithm(*ialgoIt);
301  }
303  AlgorithmNode* getAlgorithmNode(const std::string& algoName) const;
305  StatusCode addDataNode(const std::string& dataPath);
307  DataNode* getDataNode(const std::string& dataPath) const;
309  StatusCode addDecisionHubNode(Algorithm* daughterAlgo, const std::string& parentName, bool modeOR, bool allPass, bool isLazy);
311  unsigned int getControlFlowNodeCounter() const {return m_nodeCounter;}
314  std::vector<int>& node_decisions) const;
316  void updateDecision(const std::string& algo_name,
317  const int& slotNum,
318  AlgsExecutionStates& states,
319  std::vector<int>& node_decisions) const;
321  void rankAlgorithms(IGraphVisitor& ranker) const;
323  void printState(std::stringstream& output,
324  AlgsExecutionStates& states,
325  const std::vector<int>& node_decisions,
326  const unsigned int& recursionLevel) const {m_headNode->printState(output,states,node_decisions,recursionLevel);};
328  const std::vector<AlgorithmNode*> getDataIndependentNodes() const;
330  const std::string& name() const override {return m_name;}
336  AlgsExecutionStates& getAlgoStates(const int& slotNum) const {return m_eventSlots->at(slotNum).algsStates;};
338  std::vector<int>& getNodeDecisions(const int& slotNum) const {return m_eventSlots->at(slotNum).controlFlowState;}
340  void dumpDataFlow() const;
342  void dumpExecutionPlan();
344  void addEdgeToExecutionPlan(const AlgorithmNode* u, const AlgorithmNode* v);
345 
346 private:
359  unsigned int m_nodeCounter;
362  const std::string m_name;
365  std::vector<EventSlot>* m_eventSlots;
368  std::map<std::string,boost::AlgoVertex> m_exec_plan_map;
369  };
370 
371 
372 } // namespace concurrency
373 
374 
375 #endif
void dumpDataFlow() const
Print out all data origins and destinations, as reflected in the EF graph.
bool m_isLazy
Whether to evaluate lazily - i.e. whether to stop once result known.
std::string stateToString(const int &stateId) const
Translation between state id and name.
std::vector< DataNode * > m_outputs
Vectors, used in augmented data dependencies realm Outputs of the algorithm, represented as DataNode'...
const std::vector< AlgorithmNode * > getDataIndependentNodes() const
const unsigned int & getAlgoIndex() const
XXX: CF tests.
virtual bool promoteToControlReadyState(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions) const
XXX: CF tests.
std::vector< DecisionNode * > m_parents
XXX: CF tests. All direct parent nodes in the tree.
StatusCode initialize(const std::unordered_map< std::string, unsigned int > &algname_index_map)
Initialize graph.
unsigned int m_algoIndex
The index of the algorithm.
unsigned int m_nodeCounter
Total number of nodes in the graph.
unsigned int getControlFlowNodeCounter() const
Get total number of graph nodes.
virtual ~IExecutionFlowGraph()=default
AlgoNodeStruct(const std::string &name, const int index=-1, const int &rank=-1, const double &runtime=-1, const double &eccentricity=-1.0)
boost::ExecPlan m_ExecPlan
temporary items to experiment with execution planning
StatusCode buildAugmentedDataDependenciesRealm()
Build data dependency realm WITH data object nodes participating.
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
void addDaughterNode(ControlFlowNode *node)
Add a daughter node.
StatusCode addDecisionHubNode(Algorithm *daughterAlgo, const std::string &parentName, bool modeOR, bool allPass, bool isLazy)
Add a node, which aggregates decisions of direct daughter nodes.
virtual int updateState(AlgsExecutionStates &states, std::vector< int > &node_decisions) const
Method to set algos to CONTROLREADY, if possible.
virtual bool accept(IGraphVisitor &visitor)
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
virtual void initialize(const std::unordered_map< std::string, unsigned int > &algname_index_map)=0
Initialize.
std::string m_algoName
The name of the algorithm.
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, AlgoNodeStruct > ExecPlan
const std::vector< DataNode * > & getInputDataNodes() const
Get all consumer nodes.
bool dataDependenciesSatisfied(const int &slotNum) const
Method to check whether the Algorithm has its all data dependency satisfied.
std::vector< AlgorithmNode * > m_consumers
AlgorithmNodes that represent algorithms which need the output of the algorithm.
DataNode(ExecutionFlowGraph &, const std::string &path)
Constructor.
Clock::time_point time_point
Definition: ITimelineSvc.h:12
AlgorithmNode(ExecutionFlowGraph &graph, unsigned int nodeIndex, const std::string &algoName, bool inverted, bool allPass)
Constructor.
void setRank(float &rank)
Set Algorithm rank.
std::vector< ControlFlowNode * > m_children
All direct daughter nodes in the tree.
DataNodesMap m_dataPathToDataNodeMap
Index: map of data path to DataNode.
list path
Definition: __init__.py:15
std::unordered_map< std::string, DecisionNode * > DecisionHubsMap
const std::vector< IAlgorithm * > & getAlgorithmRepresentatives() const
get Algorithm representatives
std::vector< DecisionNode * > m_parents
XXX: CF tests.
STL namespace.
const std::vector< DataNode * > & getOutputDataNodes() const
Get all supplier nodes.
void updateDecision(const std::string &algo_name, const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions) const
A method to update algorithm node decision, and propagate it upwards.
std::map< std::string, boost::AlgoVertex > m_exec_plan_map
virtual bool accept(IGraphVisitor &visitor)=0
std::vector< int > & getNodeDecisions(const int &slotNum) const
virtual void updateDecision(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions, const AlgorithmNode *requestor=nullptr) const =0
bool m_inverted
Whether the selection result is negated or not.
bool m_allPass
Whether always passing regardless of daughter results.
ExecutionFlowGraph * m_graph
virtual void initialize(const std::unordered_map< std::string, unsigned int > &algname_index_map)
Initialize.
AlgoNodesMap m_algoNameToAlgoNodeMap
Index: map of algorithm's name to AlgorithmNode.
AlgsExecutionStates & getAlgoStates(const int &slotNum) const
void registerIODataObjects(const Algorithm *algo)
Register algorithm in the Data Dependency index.
virtual ~ControlFlowNode()
Destructor.
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.
const float & getRank() const
Get Algorithm rank.
DecisionNode * m_headNode
the head node of the control flow graph; may want to have multiple ones once supporting trigger paths...
The AlgsExecutionStates encodes the state machine for the execution of algorithms within a single eve...
Manage the execution flow using an execution flow graph Once initialized, the graph is const and can ...
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
~ExecutionFlowGraph() override
Destructor.
void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const
Print a string representing the control flow state.
std::vector< AlgorithmNode * > m_consumers
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual bool accept(IGraphVisitor &visitor)
const std::vector< ControlFlowNode * > & getDaughters() const
graph_traits< ExecPlan >::vertex_descriptor AlgoVertex
ExecutionFlowGraph(const std::string &name, SmartIF< ISvcLocator > svc)
Constructor.
virtual void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const
Print a string representing the control flow state.
const std::vector< AlgorithmNode * > & getConsumers() const
Get all data object consumers.
const std::vector< AlgorithmNode * > & getConsumerNodes() const
Get all consumer nodes.
void addConsumerNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data consumer of this one.
virtual ~DecisionNode()
Destructor.
const std::string & name() const override
Retrieve name of the service.
void attachAlgorithm(IAlgorithm *ialgo)
Attach Algorithm representative.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:23
StatusCode buildDataDependenciesRealm()
Build data dependency realm WITHOUT data object nodes: just interconnect algorithm nodes directly...
DecisionHubsMap m_decisionNameToDecisionHubMap
Index: map of decision's name to DecisionHub.
virtual void initialize(const std::unordered_map< std::string, unsigned int > &algname_index_map)
Initialize.
std::unordered_map< std::string, const DataObjectDescriptorCollection * > AlgoOutputsMap
virtual bool promoteToControlReadyState(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions) const =0
XXX: CF tests. Method to set algos to CONTROLREADY, if possible.
float m_rank
Algorithm rank of any kind.
void addOutputDataNode(DataNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
std::vector< IAlgorithm * > m_representatives
Representatives (including clones) of the node.
std::unordered_map< std::string, AlgorithmNode * > AlgoNodesMap
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:77
void addSupplierNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
void rankAlgorithms(IGraphVisitor &ranker) const
Rank Algorithm nodes by the number of data outputs.
virtual void updateDecision(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions, const AlgorithmNode *requestor=nullptr) const
XXX: CF tests.
void addEdgeToExecutionPlan(const AlgorithmNode *u, const AlgorithmNode *v)
set cause-effect connection between two algorithms in the execution plan
StatusCode addAlgorithmNode(Algorithm *daughterAlgo, const std::string &parentName, bool inverted, bool allPass)
Add algorithm node.
const std::vector< AlgorithmNode * > & getSupplierNodes() const
Get all supplier nodes.
std::vector< DataNode * > m_inputs
Inputs of the algorithm, represented as DataNode's.
ControlFlowNode(ExecutionFlowGraph &graph, unsigned int nodeIndex, const std::string &name)
Constructor.
void addParentNode(DecisionNode *node)
XXX: CF tests. Method to add a parent node.
virtual void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const =0
Print a string representing the control flow state.
StatusCode addDataNode(const std::string &dataPath)
Add DataNode that represents DataObject.
DataNode * getDataNode(const std::string &dataPath) const
Get DataNode by DataObject path using graph index.
virtual int updateState(AlgsExecutionStates &states, std::vector< int > &node_decisions) const =0
XXX: CF tests. Method to set algos to CONTROLREADY, if possible.
std::vector< AlgorithmNode * > m_producers
virtual int updateState(AlgsExecutionStates &states, std::vector< int > &node_decisions) const
Method to set algos to CONTROLREADY, if possible.
const std::chrono::system_clock::time_point m_initTime
void dumpExecutionPlan()
dump to file encountered execution plan
std::vector< EventSlot > * m_eventSlots
void addHeadNode(const std::string &headName, bool modeOR, bool allPass, bool isLazy)
Add a node, which has no parents.
void addParentNode(DecisionNode *node)
XXX: CF tests. Method to add a parent node.
std::unordered_map< std::string, const DataObjectDescriptorCollection * > AlgoInputsMap
virtual void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const
Print a string representing the control flow state.
virtual void updateDecision(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions, const AlgorithmNode *requestor=nullptr) const
XXX: CF tests.
AlgoInputsMap m_algoNameToAlgoInputsMap
Indexes: maps of algorithm's name to algorithm's inputs/outputs.
const std::string & getNodeName() const
const unsigned int & getNodeIndex() const
XXX: CF tests.
std::vector< AlgorithmNode * > m_suppliers
Vectors, used in data dependencies realm AlgorithmNodes that represent algorithms producing an input ...
State
Execution states of the algorithms.
void attachAlgorithmsToNodes(const std::string &algo_name, const T &container)
Attach pointers to real Algorithms (and their clones) to Algorithm nodes of the graph.
void updateEventState(AlgsExecutionStates &states, std::vector< int > &node_decisions) const
XXX CF tests. Is needed for older CF implementation.
AlgorithmNode * getAlgorithmNode(const std::string &algoName) const
Get the AlgorithmNode from by algorithm name using graph index.
const std::chrono::system_clock::time_point getInitTime() const
const std::string & getPath()
bool m_allPass
Whether the selection result is relevant or always "pass".
void addProducerNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
const std::vector< AlgorithmNode * > & getProducers() const
Get all data object producers.
DecisionNode(ExecutionFlowGraph &graph, unsigned int nodeIndex, const std::string &name, bool modeOR, bool allPass, bool isLazy)
Constructor.
bool promoteToDataReadyState(const int &slotNum, const AlgorithmNode *requestor=nullptr) const
std::unordered_map< std::string, DataNode * > DataNodesMap
AlgsExecutionStates::State State
void addConsumerNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data consumer of this one.
virtual bool promoteToControlReadyState(const int &slotNum, AlgsExecutionStates &states, std::vector< int > &node_decisions) const
XXX: CF tests. Method to set algos to CONTROLREADY, if possible.