Skip to content

Replace all sort functions with stable sort #2525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libs/librrgraph/src/base/rr_graph_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ void RRGraph::set_node_segment(const RRNodeId& node, const RRSegmentId& segment_
*/
void RRGraph::partition_node_in_edges(const RRNodeId& node) {
//Partition the edges so the first set of edges are all configurable, and the later are not
auto first_non_config_edge = std::partition(node_in_edges_[node].begin(), node_in_edges_[node].end(),
auto first_non_config_edge = std::stable_partition(node_in_edges_[node].begin(), node_in_edges_[node].end(),
[&](const RREdgeId edge) { return edge_is_configurable(edge); }); /* Condition to partition edges */

size_t num_conf_edges = std::distance(node_in_edges_[node].begin(), first_non_config_edge);
Expand All @@ -1060,7 +1060,7 @@ void RRGraph::partition_node_in_edges(const RRNodeId& node) {
*/
void RRGraph::partition_node_out_edges(const RRNodeId& node) {
//Partition the edges so the first set of edges are all configurable, and the later are not
auto first_non_config_edge = std::partition(node_out_edges_[node].begin(), node_out_edges_[node].end(),
auto first_non_config_edge = std::stable_partition(node_out_edges_[node].begin(), node_out_edges_[node].end(),
[&](const RREdgeId edge) { return edge_is_configurable(edge); }); /* Condition to partition edges */

size_t num_conf_edges = std::distance(node_out_edges_[node].begin(), first_non_config_edge);
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/draw/draw_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ void draw_congestion(ezgl::renderer* g) {

return lhs_cong_ratio < rhs_cong_ratio;
};
std::sort(congested_rr_nodes.begin(), congested_rr_nodes.end(), cmp_ascending_acc_cost);
std::stable_sort(congested_rr_nodes.begin(), congested_rr_nodes.end(), cmp_ascending_acc_cost);

if (draw_state->show_congestion == DRAW_CONGESTED_WITH_NETS) {
auto rr_node_nets = collect_rr_node_nets();
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/draw/draw_rr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ void draw_rr_costs(ezgl::renderer* g, const vtr::vector<RRNodeId, float>& rr_cos
}
return rr_costs[lhs_node] < rr_costs[rhs_node];
};
std::sort(nodes.begin(), nodes.end(), cmp_ascending_cost);
std::stable_sort(nodes.begin(), nodes.end(), cmp_ascending_cost);

for (RRNodeId inode : nodes) {
float cost = rr_costs[inode];
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/place/compressed_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vect
}

//Uniquify x/y locations
std::sort(layer_x_locs.begin(), layer_x_locs.end());
std::stable_sort(layer_x_locs.begin(), layer_x_locs.end());
layer_x_locs.erase(unique(layer_x_locs.begin(), layer_x_locs.end()), layer_x_locs.end());

std::sort(layer_y_locs.begin(), layer_y_locs.end());
std::stable_sort(layer_y_locs.begin(), layer_y_locs.end());
layer_y_locs.erase(unique(layer_y_locs.begin(), layer_y_locs.end()), layer_y_locs.end());

//The index of an x-position in x_locs corresponds to it's compressed
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/cut_spreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ std::pair<int, int> CutSpreader::cut_region(SpreaderRegion& r, bool dir) {
}

// sort blks based on raw location
std::sort(cut_blks.begin(), cut_blks.end(), [&](const ClusterBlockId a, const ClusterBlockId b) {
std::stable_sort(cut_blks.begin(), cut_blks.end(), [&](const ClusterBlockId a, const ClusterBlockId b) {
return dir ? (ap->blk_locs[a].rawy < ap->blk_locs[b].rawy) : (ap->blk_locs[a].rawx < ap->blk_locs[b].rawx);
});

Expand Down
4 changes: 2 additions & 2 deletions vpr/src/place/median_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ e_create_move MedianMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_
}

//calculate the median region
std::sort(place_move_ctx.X_coord.begin(), place_move_ctx.X_coord.end());
std::sort(place_move_ctx.Y_coord.begin(), place_move_ctx.Y_coord.end());
std::stable_sort(place_move_ctx.X_coord.begin(), place_move_ctx.X_coord.end());
std::stable_sort(place_move_ctx.Y_coord.begin(), place_move_ctx.Y_coord.end());

limit_coords.xmin = place_move_ctx.X_coord[floor((place_move_ctx.X_coord.size() - 1) / 2)];
limit_coords.xmax = place_move_ctx.X_coord[floor((place_move_ctx.X_coord.size() - 1) / 2) + 1];
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/place/noc_place_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,8 @@ bool noc_routing_has_cycle() {
static std::vector<NocLinkId> find_affected_links_by_flow_reroute(std::vector<NocLinkId>& prev_links,
std::vector<NocLinkId>& curr_links) {
// Sort both link containers
std::sort(prev_links.begin(), prev_links.end());
std::sort(curr_links.begin(), curr_links.end());
std::stable_sort(prev_links.begin(), prev_links.end());
std::stable_sort(curr_links.begin(), curr_links.end());

// stores links that appear either in prev_links or curr_links but not both of them
std::vector<NocLinkId> unique_links;
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/place/place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,8 +2393,8 @@ static float analyze_setup_slack_cost(const PlacerSetupSlacks* setup_slacks) {
}

//Sort in ascending order, from the worse slack value to the best
std::sort(original_setup_slacks.begin(), original_setup_slacks.end());
std::sort(proposed_setup_slacks.begin(), proposed_setup_slacks.end());
std::stable_sort(original_setup_slacks.begin(), original_setup_slacks.end());
std::stable_sort(proposed_setup_slacks.begin(), proposed_setup_slacks.end());

//Check the first pair of slack values that are different
//If found, return their difference
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/timing_place_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ float delay_reduce(std::vector<float>& delays, e_reducer reducer) {
auto itr = std::max_element(delays.begin(), delays.end());
delay = *itr;
} else if (reducer == e_reducer::MEDIAN) {
std::sort(delays.begin(), delays.end());
std::stable_sort(delays.begin(), delays.end());
delay = vtr::median(delays.begin(), delays.end());
} else if (reducer == e_reducer::ARITHMEAN) {
delay = vtr::arithmean(delays.begin(), delays.end());
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/place/weighted_median_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ e_create_move WeightedMedianMoveGenerator::propose_move(t_pl_blocks_to_be_moved&
}

//calculate the weighted median region
std::sort(place_move_ctx.X_coord.begin(), place_move_ctx.X_coord.end());
std::sort(place_move_ctx.Y_coord.begin(), place_move_ctx.Y_coord.end());
std::stable_sort(place_move_ctx.X_coord.begin(), place_move_ctx.X_coord.end());
std::stable_sort(place_move_ctx.Y_coord.begin(), place_move_ctx.Y_coord.end());

if (place_move_ctx.X_coord.size() == 1) {
limit_coords.xmin = place_move_ctx.X_coord[0];
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/DecompNetlistRouter.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void DecompNetlistRouter<HeapType>::route_partition_tree_node(tbb::task_group& g
* nets use their own #fanouts. */
std::vector<size_t> order(node.nets.size() + node.vnets.size());
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(), [&](size_t i, size_t j) -> bool {
std::stable_sort(order.begin(), order.end(), [&](size_t i, size_t j) -> bool {
ParentNetId id1 = i < node.nets.size() ? node.nets[i] : node.vnets[i - node.nets.size()].net_id;
ParentNetId id2 = j < node.nets.size() ? node.nets[j] : node.vnets[j - node.nets.size()].net_id;
return _net_list.net_sinks(id1).size() > _net_list.net_sinks(id2).size();
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/ParallelNetlistRouter.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void ParallelNetlistRouter<HeapType>::route_partition_tree_node(tbb::task_group&
auto& route_ctx = g_vpr_ctx.mutable_routing();

/* Sort so net with most sinks is routed first. */
std::sort(node.nets.begin(), node.nets.end(), [&](ParentNetId id1, ParentNetId id2) -> bool {
std::stable_sort(node.nets.begin(), node.nets.end(), [&](ParentNetId id1, ParentNetId id2) -> bool {
return _net_list.net_sinks(id1).size() > _net_list.net_sinks(id2).size();
});

Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/SerialNetlistRouter.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inline RouteIterResults SerialNetlistRouter<HeapType>::route_netlist(int itry, f

/* Sort so net with most sinks is routed first */
auto sorted_nets = std::vector<ParentNetId>(_net_list.nets().begin(), _net_list.nets().end());
std::sort(sorted_nets.begin(), sorted_nets.end(), [&](ParentNetId id1, ParentNetId id2) -> bool {
std::stable_sort(sorted_nets.begin(), sorted_nets.end(), [&](ParentNetId id1, ParentNetId id2) -> bool {
return _net_list.net_sinks(id1).size() > _net_list.net_sinks(id2).size();
});

Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/cb_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ static void get_pin_locations(const t_physical_tile_type_ptr block_type, const e
}
}
/* sort the vector at the current side in increasing order, for good measure */
sort(pin_locations->at(iside).begin(), pin_locations->at(iside).end());
std::stable_sort(pin_locations->at(iside).begin(), pin_locations->at(iside).end());
}
}
/* now we have a vector of vectors [0..3][0..num_pins_on_this_side] specifying which pins are on which side */
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/route_net.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ inline NetResultFlags route_net(ConnectionRouter& router,
}

// compare the criticality of different sink nodes
sort(begin(remaining_targets), end(remaining_targets), [&](int a, int b) {
std::stable_sort(begin(remaining_targets), end(remaining_targets), [&](int a, int b) {
return pin_criticality[a] > pin_criticality[b];
});

Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/router_lookahead_map_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ t_routing_cost_map get_routing_cost_map(int longest_seg_length,

//Uniquify the increments (avoid sampling the same locations repeatedly if they happen to
//overlap)
std::sort(ref_increments.begin(), ref_increments.end());
std::stable_sort(ref_increments.begin(), ref_increments.end());
ref_increments.erase(std::unique(ref_increments.begin(), ref_increments.end()), ref_increments.end());

//Upper right non-corner
Expand Down
4 changes: 2 additions & 2 deletions vpr/src/route/router_lookahead_sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static std::vector<SamplePoint> choose_points(const vtr::Matrix<int>& counts,
vtr::Point<int> center = sample(window, 1, 1, 2);

// sort by distance from center
std::sort(points.begin(), points.end(),
std::stable_sort(points.begin(), points.end(),
[&](const SamplePoint& a, const SamplePoint& b) {
return manhattan_distance(a.location, center) < manhattan_distance(b.location, center);
});
Expand Down Expand Up @@ -232,7 +232,7 @@ std::vector<SampleRegion> find_sample_regions(int num_segments) {
compute_sample_regions(sample_regions, segment_counts, bounding_box_for_segment, num_segments);

// sort regions
std::sort(sample_regions.begin(), sample_regions.end(),
std::stable_sort(sample_regions.begin(), sample_regions.end(),
[](const SampleRegion& a, const SampleRegion& b) {
return a.order < b.order;
});
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/route/rr_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ static void build_rr_chan(RRGraphBuilder& rr_graph_builder,
}

void uniquify_edges(t_rr_edge_info_set& rr_edges_to_create) {
std::sort(rr_edges_to_create.begin(), rr_edges_to_create.end());
std::stable_sort(rr_edges_to_create.begin(), rr_edges_to_create.end());
rr_edges_to_create.erase(std::unique(rr_edges_to_create.begin(), rr_edges_to_create.end()), rr_edges_to_create.end());
}

Expand Down
Loading
Loading