Skip to content

Commit 1e90528

Browse files
litghostacomodi
authored andcommitted
Refactor lookahead computation.
Before on the A200T: > Computing connection box lookahead map took 31769.29 seconds (max_rss 45671.6 MiB, delta_rss +14486.3 MiB) After on the A200T: > Computing connection box lookahead map took 16791.79 seconds (max_rss 40113.6 MiB, delta_rss +8895.9 MiB) Signed-off-by: Keith Rothman <[email protected]>
1 parent ec7f4d2 commit 1e90528

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

vpr/src/route/connection_box_lookahead_map.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ struct SampleRegion {
8686

8787
template<typename Entry>
8888
static std::pair<float, int> run_dijkstra(int start_node_ind,
89+
std::vector<bool>* node_expanded,
90+
std::vector<util::Search_Path>* paths,
8991
RoutingCosts* routing_costs);
9092

9193
static std::vector<SampleRegion> find_sample_regions(int num_segments);
@@ -495,7 +497,7 @@ float ConnectionBoxMapLookahead::get_map_cost(int from_node_ind,
495497
template<typename Entry>
496498
static bool add_paths(int start_node_ind,
497499
Entry current,
498-
std::unordered_map<int, util::Search_Path>* paths,
500+
const std::vector<util::Search_Path>& paths,
499501
RoutingCosts* routing_costs) {
500502
auto& device_ctx = g_vpr_ctx.device();
501503
ConnectionBoxId box_id;
@@ -511,7 +513,8 @@ static bool add_paths(int start_node_ind,
511513

512514
// reconstruct the path
513515
std::vector<int> path;
514-
for (int i = (*paths)[node_ind].parent; i != start_node_ind; i = (*paths)[i].parent) {
516+
for (int i = paths[node_ind].parent; i != start_node_ind; i = paths[i].parent) {
517+
VTR_ASSERT(i != -1);
515518
path.push_back(i);
516519
}
517520
path.push_back(start_node_ind);
@@ -538,7 +541,7 @@ static bool add_paths(int start_node_ind,
538541

539542
if (*it != start_node_ind) {
540543
auto& parent_node = device_ctx.rr_nodes[parent];
541-
start_to_here = Entry(*it, parent_node.edge_switch((*paths)[*it].edge), &start_to_here);
544+
start_to_here = Entry(*it, parent_node.edge_switch(paths[*it].edge), &start_to_here);
542545
parent = *it;
543546
}
544547

@@ -573,6 +576,8 @@ static bool add_paths(int start_node_ind,
573576
* the number of paths from start_node_ind stored. */
574577
template<typename Entry>
575578
static std::pair<float, int> run_dijkstra(int start_node_ind,
579+
std::vector<bool>* node_expanded,
580+
std::vector<util::Search_Path>* paths,
576581
RoutingCosts* routing_costs) {
577582
auto& device_ctx = g_vpr_ctx.device();
578583
int path_count = 0;
@@ -584,12 +589,12 @@ static std::pair<float, int> run_dijkstra(int start_node_ind,
584589

585590
/* a list of boolean flags (one for each rr node) to figure out if a
586591
* certain node has already been expanded */
587-
std::vector<bool> node_expanded(device_ctx.rr_nodes.size(), false);
592+
std::fill(node_expanded->begin(), node_expanded->end(), false);
588593
/* For each node keep a list of the cost with which that node has been
589594
* visited (used to determine whether to push a candidate node onto the
590595
* expansion queue.
591596
* Also store the parent node so we can reconstruct a specific path. */
592-
std::unordered_map<int, util::Search_Path> paths;
597+
std::fill(paths->begin(), paths->end(), util::Search_Path{std::numeric_limits<float>::infinity(), -1, -1});
593598
/* a priority queue for expansion */
594599
std::priority_queue<Entry, std::vector<Entry>, std::greater<Entry>> pq;
595600

@@ -607,7 +612,7 @@ static std::pair<float, int> run_dijkstra(int start_node_ind,
607612
int node_ind = current.rr_node_ind;
608613

609614
/* check that we haven't already expanded from this node */
610-
if (node_expanded[node_ind]) {
615+
if ((*node_expanded)[node_ind]) {
611616
continue;
612617
}
613618

@@ -617,11 +622,11 @@ static std::pair<float, int> run_dijkstra(int start_node_ind,
617622
max_cost = current.cost();
618623

619624
path_count++;
620-
add_paths<Entry>(start_node_ind, current, &paths, routing_costs);
625+
add_paths<Entry>(start_node_ind, current, *paths, routing_costs);
621626
} else {
622627
util::expand_dijkstra_neighbours(device_ctx.rr_nodes,
623-
current, paths, node_expanded, pq);
624-
node_expanded[node_ind] = true;
628+
current, paths, node_expanded, &pq);
629+
(*node_expanded)[node_ind] = true;
625630
}
626631
}
627632
return std::make_pair(max_cost, path_count);
@@ -658,6 +663,8 @@ void ConnectionBoxMapLookahead::compute(const std::vector<t_segment_inf>& segmen
658663
RoutingCosts delay_costs;
659664
RoutingCosts base_costs;
660665
int total_path_count = 0;
666+
std::vector<bool> node_expanded(device_ctx.rr_nodes.size());
667+
std::vector<util::Search_Path> paths(device_ctx.rr_nodes.size());
661668

662669
for (auto& point : region.points) {
663670
// statistics
@@ -667,12 +674,12 @@ void ConnectionBoxMapLookahead::compute(const std::vector<t_segment_inf>& segmen
667674
int path_count = 0;
668675
for (auto node_ind : point.nodes) {
669676
{
670-
auto result = run_dijkstra<util::PQ_Entry_Delay>(node_ind, &delay_costs);
677+
auto result = run_dijkstra<util::PQ_Entry_Delay>(node_ind, &node_expanded, &paths, &delay_costs);
671678
max_delay_cost = std::max(max_delay_cost, result.first);
672679
path_count += result.second;
673680
}
674681
{
675-
auto result = run_dijkstra<util::PQ_Entry_Base_Cost>(node_ind, &base_costs);
682+
auto result = run_dijkstra<util::PQ_Entry_Base_Cost>(node_ind, &node_expanded, &paths, &base_costs);
676683
max_base_cost = std::max(max_base_cost, result.first);
677684
path_count += result.second;
678685
}

vpr/src/route/router_lookahead_map_utils.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ util::Cost_Entry util::Expansion_Cost_Entry::get_median_entry() const {
202202
template<typename Entry>
203203
void expand_dijkstra_neighbours(const t_rr_graph_storage& rr_nodes,
204204
const Entry& parent_entry,
205-
std::unordered_map<int, util::Search_Path>& paths,
206-
std::vector<bool>& node_expanded,
205+
std::vector<util::Search_Path>* paths,
206+
std::vector<bool>* node_expanded,
207207
std::priority_queue<Entry,
208208
std::vector<Entry>,
209-
std::greater<Entry>>& pq) {
209+
std::greater<Entry>>* pq) {
210210
int parent_ind = parent_entry.rr_node_ind;
211211

212212
auto& parent_node = rr_nodes[parent_ind];
@@ -216,41 +216,37 @@ void expand_dijkstra_neighbours(const t_rr_graph_storage& rr_nodes,
216216
int switch_ind = parent_node.edge_switch(iedge);
217217

218218
/* skip this child if it has already been expanded from */
219-
if (node_expanded[child_node_ind]) {
219+
if ((*node_expanded)[child_node_ind]) {
220220
continue;
221221
}
222222

223223
Entry child_entry(child_node_ind, switch_ind, &parent_entry);
224224
VTR_ASSERT(child_entry.cost() >= 0);
225-
pq.push(child_entry);
226225

227226
/* Create (if it doesn't exist) or update (if the new cost is lower)
228227
* to specified node */
229228
Search_Path path_entry = {child_entry.cost(), parent_ind, iedge};
230-
auto result = paths.insert(std::make_pair(
231-
child_node_ind,
232-
path_entry));
233-
if (!result.second) {
234-
if (child_entry.cost() < result.first->second.cost) {
235-
result.first->second = path_entry;
236-
}
229+
auto& path = (*paths)[child_node_ind];
230+
if (path_entry.cost < path.cost) {
231+
pq->push(child_entry);
232+
path = path_entry;
237233
}
238234
}
239235
}
240236

241237
template void expand_dijkstra_neighbours(const t_rr_graph_storage& rr_nodes,
242238
const PQ_Entry_Delay& parent_entry,
243-
std::unordered_map<int, Search_Path>& paths,
244-
std::vector<bool>& node_expanded,
239+
std::vector<Search_Path>* paths,
240+
std::vector<bool>* node_expanded,
245241
std::priority_queue<PQ_Entry_Delay,
246242
std::vector<PQ_Entry_Delay>,
247-
std::greater<PQ_Entry_Delay>>& pq);
243+
std::greater<PQ_Entry_Delay>>* pq);
248244
template void expand_dijkstra_neighbours(const t_rr_graph_storage& rr_nodes,
249245
const PQ_Entry_Base_Cost& parent_entry,
250-
std::unordered_map<int, Search_Path>& paths,
251-
std::vector<bool>& node_expanded,
246+
std::vector<Search_Path>* paths,
247+
std::vector<bool>* node_expanded,
252248
std::priority_queue<PQ_Entry_Base_Cost,
253249
std::vector<PQ_Entry_Base_Cost>,
254-
std::greater<PQ_Entry_Base_Cost>>& pq);
250+
std::greater<PQ_Entry_Base_Cost>>* pq);
255251

256252
} // namespace util

vpr/src/route/router_lookahead_map_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ struct Search_Path {
195195
template<typename Entry>
196196
void expand_dijkstra_neighbours(const t_rr_graph_storage& rr_nodes,
197197
const Entry& parent_entry,
198-
std::unordered_map<int, Search_Path>& paths,
199-
std::vector<bool>& node_expanded,
198+
std::vector<Search_Path>* paths,
199+
std::vector<bool>* node_expanded,
200200
std::priority_queue<Entry,
201201
std::vector<Entry>,
202-
std::greater<Entry>>& pq);
202+
std::greater<Entry>>* pq);
203203

204204
} // namespace util
205205

0 commit comments

Comments
 (0)