Skip to content

Commit 69ed525

Browse files
committed
Add edge_groups.{cpp,h}, pass context into set_device_context()
1 parent 47ac847 commit 69ed525

File tree

5 files changed

+161
-146
lines changed

5 files changed

+161
-146
lines changed

vpr/src/route/edge_groups.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "edge_groups.h"
2+
3+
#include <stack>
4+
5+
// Adds non-configurable (undirected) edge to be grouped.
6+
//
7+
// Returns true if this is a new edge.
8+
bool EdgeGroups::add_non_config_edge(int from_node, int to_node) {
9+
return graph_[from_node].edges.insert(to_node).second && graph_[to_node].edges.insert(from_node).second;
10+
}
11+
12+
// After add_non_config_edge has been called for all edges, create_sets
13+
// will form groups of nodes that are connected via non-configurable
14+
// edges.
15+
void EdgeGroups::create_sets() {
16+
rr_non_config_node_sets_map_.clear();
17+
18+
// https://en.wikipedia.org/wiki/Component_(graph_theory)#Algorithms
19+
std::vector<size_t> group_size;
20+
for (auto& node : graph_) {
21+
if (node.second.set == OPEN) {
22+
node.second.set = group_size.size();
23+
group_size.push_back(add_connected_group(node.second));
24+
}
25+
}
26+
27+
// Sanity check the node sets.
28+
for (const auto& node : graph_) {
29+
VTR_ASSERT(node.second.set != OPEN);
30+
for (const auto& e : node.second.edges) {
31+
int set = graph_[e].set;
32+
VTR_ASSERT(set == node.second.set);
33+
}
34+
}
35+
36+
// Create compact set of sets.
37+
rr_non_config_node_sets_map_.resize(group_size.size());
38+
for (size_t i = 0; i < group_size.size(); i++) {
39+
rr_non_config_node_sets_map_[i].reserve(group_size[i]);
40+
}
41+
for (const auto& node : graph_) {
42+
rr_non_config_node_sets_map_[node.second.set].push_back(node.first);
43+
}
44+
}
45+
46+
// Create t_non_configurable_rr_sets from set data.
47+
// NOTE: The stored graph is undirected, so this may generate reverse edges that don't exist.
48+
t_non_configurable_rr_sets EdgeGroups::output_sets() {
49+
t_non_configurable_rr_sets sets;
50+
for (const auto& nodes : rr_non_config_node_sets_map_) {
51+
std::set<t_node_edge> edge_set;
52+
std::set<int> node_set(nodes.begin(), nodes.end());
53+
54+
for (const auto& src : node_set) {
55+
for (const auto& dest : graph_[src].edges) {
56+
edge_set.emplace(t_node_edge(src, dest));
57+
}
58+
}
59+
60+
sets.node_sets.emplace(std::move(node_set));
61+
sets.edge_sets.emplace(std::move(edge_set));
62+
}
63+
64+
return sets;
65+
}
66+
67+
// Set device context structures for non-configurable node sets.
68+
void EdgeGroups::set_device_context(DeviceContext& device_ctx) {
69+
std::vector<std::vector<int>> rr_non_config_node_sets;
70+
for (const auto& item : rr_non_config_node_sets_map_) {
71+
rr_non_config_node_sets.emplace_back(std::move(item));
72+
}
73+
74+
std::unordered_map<int, int> rr_node_to_non_config_node_set;
75+
for (size_t set = 0; set < rr_non_config_node_sets.size(); ++set) {
76+
for (const auto inode : rr_non_config_node_sets[set]) {
77+
rr_node_to_non_config_node_set.insert(
78+
std::make_pair(inode, set));
79+
}
80+
}
81+
82+
device_ctx.rr_non_config_node_sets = std::move(rr_non_config_node_sets);
83+
device_ctx.rr_node_to_non_config_node_set = std::move(rr_node_to_non_config_node_set);
84+
}
85+
86+
// Perform a DFS traversal marking everything reachable with the same set id
87+
size_t EdgeGroups::add_connected_group(const node_data& node) {
88+
// stack contains nodes with edges to mark with node.set
89+
// The set for each node must be set before pushing it onto the stack
90+
std::stack<const node_data*> stack;
91+
stack.push(&node);
92+
size_t n = 1;
93+
while (!stack.empty()) {
94+
auto top = stack.top();
95+
stack.pop();
96+
for (auto e : top->edges) {
97+
auto& next = graph_[e];
98+
if (next.set != node.set) {
99+
VTR_ASSERT(next.set == OPEN);
100+
n++;
101+
next.set = node.set;
102+
stack.push(&next);
103+
}
104+
}
105+
}
106+
return n;
107+
}

vpr/src/route/edge_groups.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef EDGE_GROUPS_H
2+
#define EDGE_GROUPS_H
3+
4+
#include <unordered_set>
5+
#include <unordered_map>
6+
#include <vector>
7+
#include <cstddef>
8+
9+
#include "vpr_types.h"
10+
#include "vpr_context.h"
11+
12+
// Class for building a group of connected edges.
13+
class EdgeGroups {
14+
public:
15+
EdgeGroups() {}
16+
17+
// Adds non-configurable (undirected) edge to be grouped.
18+
//
19+
// Returns true if this is a new edge.
20+
bool add_non_config_edge(int from_node, int to_node);
21+
22+
// After add_non_config_edge has been called for all edges, create_sets
23+
// will form groups of nodes that are connected via non-configurable
24+
// edges.
25+
void create_sets();
26+
27+
// Create t_non_configurable_rr_sets from set data.
28+
// NOTE: The stored graph is undirected, so this may generate reverse edges that don't exist.
29+
t_non_configurable_rr_sets output_sets();
30+
31+
// Set device context structures for non-configurable node sets.
32+
void set_device_context(DeviceContext& device_ctx);
33+
34+
private:
35+
struct node_data {
36+
std::unordered_set<int> edges;
37+
int set = OPEN;
38+
};
39+
40+
// Perform a DFS traversal marking everything reachable with the same set id
41+
size_t add_connected_group(const node_data& node);
42+
43+
// Set of non-configurable edges.
44+
std::unordered_map<int, node_data> graph_;
45+
46+
// Compact set of node sets. Map key is arbitrary.
47+
std::vector<std::vector<int>> rr_non_config_node_sets_map_;
48+
};
49+
50+
#endif

vpr/src/route/rr_graph.cpp

Lines changed: 3 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <ctime>
55
#include <algorithm>
66
#include <vector>
7-
#include <stack>
87
#include "vtr_assert.h"
98

109
#include "vtr_util.h"
@@ -34,6 +33,7 @@
3433
#include "rr_graph_reader.h"
3534
#include "router_lookahead_map.h"
3635
#include "rr_graph_clock.h"
36+
#include "edge_groups.h"
3737

3838
#include "rr_types.h"
3939

@@ -2956,111 +2956,6 @@ static int pick_best_direct_connect_target_rr_node(const t_rr_graph_storage& rr_
29562956
return best_rr;
29572957
}
29582958

2959-
// Adds non-configurable (undirected) edge to be grouped.
2960-
//
2961-
// Returns true if this is a new edge.
2962-
bool EdgeGroups::add_non_config_edge(int from_node, int to_node) {
2963-
return graph_[from_node].edges.insert(to_node).second && graph_[to_node].edges.insert(from_node).second;
2964-
}
2965-
2966-
// After add_non_config_edge has been called for all edges, create_sets
2967-
// will form groups of nodes that are connected via non-configurable
2968-
// edges.
2969-
void EdgeGroups::create_sets() {
2970-
rr_non_config_node_sets_map_.clear();
2971-
2972-
// https://en.wikipedia.org/wiki/Component_(graph_theory)#Algorithms
2973-
std::vector<size_t> group_size;
2974-
for (auto& node : graph_) {
2975-
if (node.second.set == OPEN) {
2976-
node.second.set = group_size.size();
2977-
group_size.push_back(add_connected_group(node.second));
2978-
}
2979-
}
2980-
2981-
// Sanity check the node sets.
2982-
for (const auto& node : graph_) {
2983-
VTR_ASSERT(node.second.set != OPEN);
2984-
for (const auto& e : node.second.edges) {
2985-
int set = graph_[e].set;
2986-
VTR_ASSERT(set == node.second.set);
2987-
}
2988-
}
2989-
2990-
// Create compact set of sets.
2991-
rr_non_config_node_sets_map_.resize(group_size.size());
2992-
for (size_t i = 0; i < group_size.size(); i++) {
2993-
rr_non_config_node_sets_map_[i].reserve(group_size[i]);
2994-
}
2995-
for (const auto& node : graph_) {
2996-
rr_non_config_node_sets_map_[node.second.set].push_back(node.first);
2997-
}
2998-
}
2999-
3000-
// Create t_non_configurable_rr_sets from set data.
3001-
// NOTE: The stored graph is undirected, so this may generate reverse edges that don't exist.
3002-
t_non_configurable_rr_sets EdgeGroups::output_sets() {
3003-
t_non_configurable_rr_sets sets;
3004-
for (const auto& nodes : rr_non_config_node_sets_map_) {
3005-
std::set<t_node_edge> edge_set;
3006-
std::set<int> node_set(nodes.begin(), nodes.end());
3007-
3008-
for (const auto& src : node_set) {
3009-
for (const auto& dest : graph_[src].edges) {
3010-
edge_set.emplace(t_node_edge(src, dest));
3011-
}
3012-
}
3013-
3014-
sets.node_sets.emplace(std::move(node_set));
3015-
sets.edge_sets.emplace(std::move(edge_set));
3016-
}
3017-
3018-
return sets;
3019-
}
3020-
3021-
// Set device context structures for non-configurable node sets.
3022-
void EdgeGroups::set_device_context() {
3023-
std::vector<std::vector<int>> rr_non_config_node_sets;
3024-
for (const auto& item : rr_non_config_node_sets_map_) {
3025-
rr_non_config_node_sets.emplace_back(std::move(item));
3026-
}
3027-
3028-
std::unordered_map<int, int> rr_node_to_non_config_node_set;
3029-
for (size_t set = 0; set < rr_non_config_node_sets.size(); ++set) {
3030-
for (const auto inode : rr_non_config_node_sets[set]) {
3031-
rr_node_to_non_config_node_set.insert(
3032-
std::make_pair(inode, set));
3033-
}
3034-
}
3035-
3036-
auto& device_ctx = g_vpr_ctx.mutable_device();
3037-
device_ctx.rr_non_config_node_sets = std::move(rr_non_config_node_sets);
3038-
device_ctx.rr_node_to_non_config_node_set = std::move(rr_node_to_non_config_node_set);
3039-
}
3040-
3041-
// Perform a DFS traversal marking everything reachable with the same set id
3042-
size_t EdgeGroups::add_connected_group(const node_data& node) {
3043-
// stack contains nodes with edges to mark with node.set
3044-
// The set for each node must be set before pushing it onto the stack
3045-
std::stack<const node_data*> stack;
3046-
stack.push(&node);
3047-
size_t n = 1;
3048-
while (!stack.empty()) {
3049-
auto top = stack.top();
3050-
stack.pop();
3051-
for (auto e : top->edges) {
3052-
auto& next = graph_[e];
3053-
if (next.set != node.set) {
3054-
VTR_ASSERT(next.set == OPEN);
3055-
n++;
3056-
next.set = node.set;
3057-
stack.push(&next);
3058-
}
3059-
}
3060-
}
3061-
return n;
3062-
}
3063-
30642959
//Collects the sets of connected non-configurable edges in the RR graph
30652960
static void create_edge_groups(EdgeGroups* groups) {
30662961
auto& device_ctx = g_vpr_ctx.device();
@@ -3084,7 +2979,8 @@ t_non_configurable_rr_sets identify_non_configurable_rr_sets() {
30842979
}
30852980

30862981
static void process_non_config_sets() {
2982+
auto& device_ctx = g_vpr_ctx.mutable_device();
30872983
EdgeGroups groups;
30882984
create_edge_groups(&groups);
3089-
groups.set_device_context();
2985+
groups.set_device_context(device_ctx);
30902986
}

vpr/src/route/rr_graph.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,6 @@ void load_rr_switch_from_arch_switch(int arch_switch_idx,
5555
const float R_minW_nmos,
5656
const float R_minW_pmos);
5757

58-
// Class for building a group of connected edges.
59-
class EdgeGroups {
60-
public:
61-
EdgeGroups() {}
62-
63-
// Adds non-configurable (undirected) edge to be grouped.
64-
//
65-
// Returns true if this is a new edge.
66-
bool add_non_config_edge(int from_node, int to_node);
67-
68-
// After add_non_config_edge has been called for all edges, create_sets
69-
// will form groups of nodes that are connected via non-configurable
70-
// edges.
71-
void create_sets();
72-
73-
// Create t_non_configurable_rr_sets from set data.
74-
// NOTE: The stored graph is undirected, so this may generate reverse edges that don't exist.
75-
t_non_configurable_rr_sets output_sets();
76-
77-
// Set device context structures for non-configurable node sets.
78-
void set_device_context();
79-
80-
private:
81-
struct node_data {
82-
std::unordered_set<int> edges;
83-
int set = OPEN;
84-
};
85-
86-
// Perform a DFS traversal marking everything reachable with the same set id
87-
size_t add_connected_group(const node_data& node);
88-
89-
// Set of non-configurable edges.
90-
std::unordered_map<int, node_data> graph_;
91-
92-
// Compact set of node sets. Map key is arbitrary.
93-
std::vector<std::vector<int>> rr_non_config_node_sets_map_;
94-
};
95-
9658
t_non_configurable_rr_sets identify_non_configurable_rr_sets();
9759

9860
#endif

vpr/test/test_edge_groups.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "catch.hpp"
99

10-
#include "rr_graph.h"
10+
#include "edge_groups.h"
1111

1212
namespace {
1313

0 commit comments

Comments
 (0)