P4C
The P4 Compiler
Loading...
Searching...
No Matches
simple_power_graph.h
1
19#ifndef BF_P4C_MAU_SIMPLE_POWER_GRAPH_H_
20#define BF_P4C_MAU_SIMPLE_POWER_GRAPH_H_
21
22#include <cstring>
23#include <map>
24#include <set>
25#include <stack>
26#include <string>
27#include <vector>
28
29#include "bf-p4c/mau/mau_power.h"
30
31namespace MauPower {
32class Node;
33class Edge;
34
35using ::operator<<;
36
38 std::set<const Node *> nodes;
39 std::set<const Edge *> edges;
40};
41
47class Edge {
48 public:
49 const size_t id_; // edge id (used for dot output)
50 const cstring name_; // debug name of edge
51 cstring to_string() const { return name_; }
52 bool is_equivalent(const Edge *other) const;
53
59 std::string get_edge_color() const;
60
61 explicit Edge(size_t id, cstring name, std::vector<Node *> &child_nodes)
62 : id_(id), name_(name) {
63 for (auto n : child_nodes) {
64 child_nodes_.push_back(n);
65 }
66 }
67 // Encapsulation of what next Node(s) (logical table(s)) this edge connects to.
68 std::vector<Node *> child_nodes_;
69 void dbprint(std::ostream &out, NodeAndEdgeSet *seen) const;
70 void dbprint(std::ostream &out) const {
71 NodeAndEdgeSet seen;
72 dbprint(out, &seen);
73 }
74};
75
81class Node {
82 public:
83 const UniqueId unique_id_;
84 const int id_; // node id (used for dot output)
85 std::vector<Edge *> out_edges_; // collection of outbound edges
86
87 explicit Node(UniqueId uniq_id, int id) : unique_id_(uniq_id), id_(id) {}
88 ~Node() {
89 for (auto e : out_edges_) delete e;
90 }
91 Node(const Node &n) : unique_id_(n.unique_id_), id_(n.id_) { // copy
92 for (auto e : n.out_edges_) add_edge(e);
93 }
94 Node &operator=(const Node &n) = delete; // copy assignment
95 Node(const Node &&n) = delete; // move
96 Node &operator=(const Node &&n) = delete; // move assignment
97
104 void create_and_add_edge(cstring edge_name, std::vector<Node *> &child_nodes);
105 bool is_equivalent(const Node *other) const;
106 cstring to_string() const { return "" + unique_id_.build_name(); }
107 void dbprint(std::ostream &out, NodeAndEdgeSet *seen) const;
108 void dbprint(std::ostream &out) const {
109 NodeAndEdgeSet seen;
110 dbprint(out, &seen);
111 }
112
113 private:
114 void add_edge(Edge *e);
115};
116
127 std::map<UniqueId, Node *> nodes_ = {};
128 std::map<UniqueId, std::set<UniqueId>> pred_ = {};
129
130 public:
131 const cstring name_; // graph name, e.g. ingress or egress
132
133 explicit SimplePowerGraph(cstring gress) : name_(gress), running_id_(1) {
134 UniqueId root_uid;
135 root_uid.name = "$root"_cs;
136 root_ = new Node(root_uid, 0);
137 nodes_.emplace(root_uid, root_);
138 }
139 Node *get_root() const { return root_; }
148 void add_connection(UniqueId parent, ordered_set<UniqueId> activated, cstring edge_name);
152 void to_dot(cstring filename);
157 void get_leaves(UniqueId n, std::vector<UniqueId> &leaves);
161 bool can_reach(UniqueId parent, UniqueId child);
176 bool active_simultaneously(UniqueId parent, UniqueId child);
180 const std::set<UniqueId> &predecessors(UniqueId child) const {
181 if (pred_.count(child)) return pred_.at(child);
182 static std::set<UniqueId> empty;
183 return empty;
184 }
188 std::vector<Node *> topo_sort();
196 const std::map<UniqueId, PowerMemoryAccess> &table_memory_access, std::set<Node *> &rv);
197
198 void dbprint(std::ostream &out, NodeAndEdgeSet *seen) const;
199 void dbprint(std::ostream &out) const {
200 NodeAndEdgeSet seen;
201 dbprint(out, &seen);
202 }
203
204 private:
205 int running_id_; // For producing unique integer values of graph Nodes.
206 Node *root_;
207 // Memoize if a parent-child relationship is reachable.
208 std::map<std::pair<int, int>, bool> found_reachable_ = {};
209 // Memoize if a parent-child pair is active simultaneously.
210 std::map<std::pair<int, int>, bool> found_simultaneous_ = {};
211 // Memoize worst-case power computed for a node.
212 std::map<int, double> computed_power_ = {};
213 // Memoize worst_path computed for a node;
214 std::map<int, std::set<UniqueId>> computed_worst_path_ = {};
215 // Keep track of who parent was on worst-case power path.
216 std::map<Node *, Edge *> worst_path_edges_ = {};
217
222 Node *create_node(UniqueId id);
223 int get_next_id() { return running_id_++; }
224 void topo_visit(Node *node, std::stack<Node *> *stack, std::map<Node *, bool> *visited);
231 double visit_node_power(Node *n, const std::map<UniqueId, PowerMemoryAccess> &tma,
232 std::set<UniqueId> &worst_path);
233};
234
235} // end namespace MauPower
236
237#endif /* BF_P4C_MAU_SIMPLE_POWER_GRAPH_H_ */
Definition simple_power_graph.h:47
std::string get_edge_color() const
Definition simple_power_graph.cpp:52
Definition simple_power_graph.h:81
void create_and_add_edge(cstring edge_name, std::vector< Node * > &child_nodes)
Definition simple_power_graph.cpp:61
Definition simple_power_graph.h:126
bool can_reach(UniqueId parent, UniqueId child)
Definition simple_power_graph.cpp:221
const std::set< UniqueId > & predecessors(UniqueId child) const
Definition simple_power_graph.h:180
void add_connection(UniqueId parent, ordered_set< UniqueId > activated, cstring edge_name)
Definition simple_power_graph.cpp:112
double get_tables_on_worst_path(const std::map< UniqueId, PowerMemoryAccess > &table_memory_access, std::set< Node * > &rv)
Definition simple_power_graph.cpp:296
bool active_simultaneously(UniqueId parent, UniqueId child)
Definition simple_power_graph.cpp:256
void get_leaves(UniqueId n, std::vector< UniqueId > &leaves)
Definition simple_power_graph.cpp:161
std::vector< Node * > topo_sort()
Definition simple_power_graph.cpp:207
void to_dot(cstring filename)
Definition simple_power_graph.cpp:130
Definition stringify.h:33
Definition unique_id.h:168
Definition cstring.h:85
Definition ordered_set.h:32
Definition mau/asm_output.h:39
Definition simple_power_graph.h:37