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"
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) {}
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;
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  ~DecisionNode() override;
96  void initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map) override;
97  bool accept(IGraphVisitor& visitor) override;
99  bool promoteToControlReadyState(const int& slotNum,
100  AlgsExecutionStates& states,
101  std::vector<int>& node_decisions) const override;
103  void updateDecision(const int& slotNum,
104  AlgsExecutionStates& states,
105  std::vector<int>& node_decisions,
106  const AlgorithmNode* requestor = nullptr) const override;
108  int updateState(AlgsExecutionStates& states,
109  std::vector<int>& node_decisions) const override;
111  void addParentNode(DecisionNode* node);
113  void addDaughterNode(ControlFlowNode* node);
115  const std::vector<ControlFlowNode*>& getDaughters() const {return m_children;}
117  void printState(std::stringstream& output,
118  AlgsExecutionStates& states,
119  const std::vector<int>& node_decisions,
120  const unsigned int& recursionLevel) const override;
121  public:
123  bool m_modeOR;
125  bool m_allPass;
127  bool m_isLazy;
128  private:
133  };
134 
135  class DataNode;
136 
138  public:
140  AlgorithmNode(ExecutionFlowGraph& graph, unsigned int nodeIndex, const std::string& algoName, bool inverted, bool allPass, bool IOBound) :
141  ControlFlowNode(graph, nodeIndex, algoName),
142  m_algoIndex(0),m_algoName(algoName),m_inverted(inverted),m_allPass(allPass),m_rank(-1),m_isIOBound(IOBound)
143  {};
145  ~AlgorithmNode();
147  void initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map) override;
149  bool accept(IGraphVisitor& visitor) override;
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  void setIOBound(bool value) { m_isIOBound = value;}
184  bool isIOBound() const {return m_isIOBound;}
186  bool dataDependenciesSatisfied(const int& slotNum) const;
187  bool dataDependenciesSatisfied(AlgsExecutionStates& states) const;
189  int updateState(AlgsExecutionStates& states,
190  std::vector<int>& node_decisions) const override;
192  bool promoteToControlReadyState(const int& slotNum,
193  AlgsExecutionStates& states,
194  std::vector<int>& node_decisions) const override;
196  bool promoteToDataReadyState(const int& slotNum, const AlgorithmNode* requestor = nullptr) const;
198  void updateDecision(const int& slotNum,
199  AlgsExecutionStates& states,
200  std::vector<int>& node_decisions,
201  const AlgorithmNode* requestor = nullptr) const override;
203  void printState(std::stringstream& output,
204  AlgsExecutionStates& states,
205  const std::vector<int>& node_decisions,
206  const unsigned int& recursionLevel) const override;
207  private:
209  unsigned int m_algoIndex;
215  bool m_allPass;
218 
224 
231  float m_rank;
236  };
237 
238 class DataNode {
239 public:
241  DataNode(ExecutionFlowGraph& /*graph*/, const DataObjID& path): m_data_object_path(path) {}
244  const DataObjID& getPath() {return m_data_object_path;}
247  if (std::find(m_producers.begin(),m_producers.end(),node) == m_producers.end())
248  m_producers.push_back(node);
249  }
252  if (std::find(m_consumers.begin(),m_consumers.end(),node) == m_consumers.end())
253  m_consumers.push_back(node);
254  }
256  const std::vector<AlgorithmNode*>& getProducers() const {return m_producers;}
258  const std::vector<AlgorithmNode*>& getConsumers() const {return m_consumers;}
259 private:
263  };
264 
268 
271 
274  virtual ~IExecutionFlowGraph() = default;
275 };
276 
277 class ExecutionFlowGraph : public CommonMessaging<IExecutionFlowGraph> {
279 public:
282  m_headNode(0), m_nodeCounter(0), m_svcLocator(svc), m_name(name), m_initTime(std::chrono::system_clock::now()),
283  m_eventSlots(nullptr) {}
285  ~ExecutionFlowGraph() override {
286  if (m_headNode != 0) delete m_headNode;
287  }
289  StatusCode initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map);
290  StatusCode initialize(const std::unordered_map<std::string,unsigned int>& algname_index_map,
291  std::vector<EventSlot>& eventSlots);
293  void registerIODataObjects(const Algorithm* algo);
295  StatusCode buildDataDependenciesRealm();
297  StatusCode buildAugmentedDataDependenciesRealm();
299  void addHeadNode(const std::string& headName, bool modeOR, bool allPass, bool isLazy);
301  StatusCode addAlgorithmNode(Algorithm* daughterAlgo, const std::string& parentName, bool inverted, bool allPass);
303  template<class T>
304  void attachAlgorithmsToNodes(const std::string& algo_name, const T& container) {
305  auto node = getAlgorithmNode(algo_name);
306  for (auto ialgoIt = container.unsafe_begin(); ialgoIt != container.unsafe_end(); ++ialgoIt)
307  node->attachAlgorithm(*ialgoIt);
308  }
310  AlgorithmNode* getAlgorithmNode(const std::string& algoName) const;
312  StatusCode addDataNode(const DataObjID& dataPath);
314  DataNode* getDataNode(const DataObjID& dataPath) const;
316  StatusCode addDecisionHubNode(Algorithm* daughterAlgo, const std::string& parentName, bool modeOR, bool allPass, bool isLazy);
318  unsigned int getControlFlowNodeCounter() const {return m_nodeCounter;}
320  void updateEventState(AlgsExecutionStates& states,
321  std::vector<int>& node_decisions) const;
323  void updateDecision(const std::string& algo_name,
324  const int& slotNum,
325  AlgsExecutionStates& states,
326  std::vector<int>& node_decisions) const;
328  void rankAlgorithms(IGraphVisitor& ranker) const;
331  AlgsExecutionStates& states,
332  const std::vector<int>& node_decisions,
333  const unsigned int& recursionLevel) const {m_headNode->printState(output,states,node_decisions,recursionLevel);}
335  const std::vector<AlgorithmNode*> getDataIndependentNodes() const;
337  const std::string& name() const override {return m_name;}
339  SmartIF<ISvcLocator>& serviceLocator() const override {return m_svcLocator;}
341  const std::chrono::system_clock::time_point getInitTime() const {return m_initTime;}
343  AlgsExecutionStates& getAlgoStates(const int& slotNum) const {return m_eventSlots->at(slotNum).algsStates;}
345  std::vector<int>& getNodeDecisions(const int& slotNum) const {return m_eventSlots->at(slotNum).controlFlowState;}
347  std::string dumpDataFlow() const;
349  void dumpExecutionPlan();
351  void addEdgeToExecutionPlan(const AlgorithmNode* u, const AlgorithmNode* v);
352 
353 private:
366  unsigned int m_nodeCounter;
376  };
377 
378 
379 } // namespace concurrency
380 
381 
382 #endif
bool m_isLazy
Whether to evaluate lazily - i.e. whether to stop once result known.
std::vector< DataNode * > m_outputs
Vectors, used in augmented data dependencies realm Outputs of the algorithm, represented as DataNode&#39;...
const unsigned int & getAlgoIndex() const
XXX: CF tests.
std::vector< DecisionNode * > m_parents
XXX: CF tests. All direct parent nodes in the tree.
std::unordered_map< std::string, DataObjIDColl > AlgoOutputsMap
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.
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
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
void setIOBound(bool value)
Set the I/O-boundness flag.
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
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.
std::vector< AlgorithmNode * > m_consumers
AlgorithmNodes that represent algorithms which need the output of the algorithm.
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.
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.
AlgsExecutionStates::State State
std::map< std::string, boost::AlgoVertex > m_exec_plan_map
std::vector< int > & getNodeDecisions(const int &slotNum) const
bool m_inverted
Whether the selection result is negated or not.
bool m_allPass
Whether always passing regardless of daughter results.
ExecutionFlowGraph * m_graph
bool isIOBound() const
Check if algorithm is I/O-bound.
AlgoNodesMap m_algoNameToAlgoNodeMap
Index: map of algorithm&#39;s name to AlgorithmNode.
STL class.
AlgsExecutionStates & getAlgoStates(const int &slotNum) const
virtual ~ControlFlowNode()
Destructor.
bool m_modeOR
Whether acting as "and" (false) or "or" node (true)
T at(T...args)
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 ...
~ExecutionFlowGraph() override
Destructor.
const DataObjID & getPath()
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
std::unordered_map< DataObjID, DataNode *, DataObjID_Hasher > DataNodesMap
const std::vector< ControlFlowNode * > & getDaughters() const
graph_traits< ExecPlan >::vertex_descriptor AlgoVertex
ExecutionFlowGraph(const std::string &name, SmartIF< ISvcLocator > svc)
Constructor.
std::unordered_map< std::string, DataObjIDColl > AlgoInputsMap
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.
void attachAlgorithm(IAlgorithm *ialgo)
Attach Algorithm representative.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:27
DecisionHubsMap m_decisionNameToDecisionHubMap
Index: map of decision&#39;s name to DecisionHub.
float m_rank
Algorithm rank of any kind.
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:78
T find(T...args)
void addSupplierNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data supplier for this one.
const std::vector< AlgorithmNode * > & getSupplierNodes() const
Get all supplier nodes.
std::vector< DataNode * > m_inputs
Inputs of the algorithm, represented as DataNode&#39;s.
ControlFlowNode(ExecutionFlowGraph &graph, unsigned int nodeIndex, const std::string &name)
Constructor.
Clock::time_point time_point
Definition: ITimelineSvc.h:11
std::vector< AlgorithmNode * > m_producers
const std::chrono::system_clock::time_point m_initTime
std::vector< EventSlot > * m_eventSlots
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
AlgoInputsMap m_algoNameToAlgoInputsMap
Indexes: maps of algorithm&#39;s name to algorithm&#39;s inputs/outputs.
DataNode(ExecutionFlowGraph &, const DataObjID &path)
Constructor.
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.
const std::string & name() const override
Retrieve name of the service.
bool m_isIOBound
If an algorithm is I/O-bound (in the broad sense of Von Neumann bottleneck)
const std::chrono::system_clock::time_point getInitTime() const
AlgorithmNode(ExecutionFlowGraph &graph, unsigned int nodeIndex, const std::string &algoName, bool inverted, bool allPass, bool IOBound)
Constructor.
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.
void addConsumerNode(AlgorithmNode *node)
Associate an AlgorithmNode, which is a data consumer of this one.