Graph.h
1 #pragma once
2 
3 #include <momemta/config.h>
4 #include <momemta/Configuration.h>
5 #include <momemta/Module.h>
6 
7 #include <ExecutionPath.h>
8 
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 #include <boost/config.hpp>
14 #include <boost/functional/hash.hpp>
15 #include <boost/graph/adjacency_list.hpp>
16 
17 #ifdef DEBUG_TIMING
18 #include <chrono>
19 #endif
20 
21 namespace momemta {
22 
23 // Graph definitions
24 
26 struct Vertex {
27  uint32_t id;
28  std::string name; // Unique name of the module
29  std::string type; // Module type
30  momemta::ModuleList::value_type def;
32 };
33 
35 struct Edge {
36  std::string description;
37  bool virt;
39 };
40 
41 typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, Vertex, Edge> Graph;
42 typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
43 typedef boost::graph_traits<Graph>::edge_descriptor edge_t;
44 
58 public:
65  void initialize(PoolPtr pool);
66 
67  // Interface to module methods
69  void configure();
71  void beginIntegration();
73  Module::Status execute();
75  void endIntegration();
77  void finish();
78 
79 #ifdef DEBUG_TIMING
80  void logTimings() const;
82 #endif
83 
88  void setNDimensions(size_t n);
89 
91  size_t getNDimensions() const;
92 
94  void addDecl(const boost::uuids::uuid& path, const Configuration::ModuleDecl& decl);
96  const std::vector<boost::uuids::uuid>& getPaths() const;
98  const std::vector<Configuration::ModuleDecl>& getDecls(const boost::uuids::uuid& path) const;
99 
100 private:
101  std::vector<boost::uuids::uuid> sorted_execution_paths;
102  std::unordered_map<
103  boost::uuids::uuid,
104  std::vector<Configuration::ModuleDecl>,
105  boost::hash<boost::uuids::uuid>
106  > module_decls;
107 
108  std::vector<ModulePtr> modules;
109 
110  size_t n_dimensions;
111 
112 #ifdef DEBUG_TIMING
113  std::unordered_map<Module *, std::chrono::high_resolution_clock::duration> module_timings;
114 #endif
115 };
116 
128 public:
129 
143  const momemta::ModuleList& available_modules,
144  const Configuration& configuration
145  );
146 
156  std::shared_ptr<ComputationGraph> build();
157 
165  void exportGraph(const std::string& output) const;
166 
167 private:
168  void prune_graph();
169  void sort_graph();
170  void validate();
171 
172  const momemta::ModuleList& available_modules;
173  const Configuration& configuration;
174 
175  Graph g;
176  std::unordered_map<std::string, vertex_t> vertices;
177 
178  std::list<vertex_t> sorted_vertices; // filled only if sort_graph is called
179 
180  bool graph_exportable = false;
181 };
182 
183 }
A vertex of the graph, symbolizing a module.
Definition: Graph.h:26
An edge of the graph, symbolizing the connection between two modules (an InputTag) ...
Definition: Graph.h:35
bool virt
If true, this edge is virtual and represent an additional constrain to the graph. ...
Definition: Graph.h:37
Definition: Graph.h:21
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
A frozen snapshot of the configuration file.
Definition: Configuration.h:36
A module declaration, defined from the configuration file.
Definition: Configuration.h:43
InputTag tag
InputTag of the connection.
Definition: Graph.h:38