Skip to content

[vpr][place] moved_block uses size() to indicate valid elements #2657

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 17 additions & 8 deletions vpr/src/place/move_transactions.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#include "move_utils.h"

#include "globals.h"
#include "place_util.h"

t_pl_blocks_to_be_moved::t_pl_blocks_to_be_moved(size_t max_blocks){
moved_blocks.reserve(max_blocks);
moved_blocks.resize(0);
}

size_t t_pl_blocks_to_be_moved::get_size_and_increment() {
VTR_ASSERT(moved_blocks.size() < moved_blocks.capacity());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this a VTR_ASSERT_SAFE as it is in a hot routine (VTR_ASSERT_SAFE is taken out of release build).

moved_blocks.resize(moved_blocks.size() + 1);
return moved_blocks.size() - 1;
}

//Records that block 'blk' should be moved to the specified 'to' location
e_block_move_result record_block_move(t_pl_blocks_to_be_moved& blocks_affected, ClusterBlockId blk, t_pl_loc to) {
Expand All @@ -24,11 +34,10 @@ e_block_move_result record_block_move(t_pl_blocks_to_be_moved& blocks_affected,
VTR_ASSERT_SAFE(to.sub_tile < int(place_ctx.grid_blocks.num_blocks_at_location({to.x, to.y, to.layer})));

// Sets up the blocks moved
int imoved_blk = blocks_affected.num_moved_blocks;
size_t imoved_blk = blocks_affected.get_size_and_increment();
blocks_affected.moved_blocks[imoved_blk].block_num = blk;
blocks_affected.moved_blocks[imoved_blk].old_loc = from;
blocks_affected.moved_blocks[imoved_blk].new_loc = to;
blocks_affected.num_moved_blocks++;

return e_block_move_result::VALID;
}
Expand All @@ -40,7 +49,7 @@ void apply_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {

//Swap the blocks, but don't swap the nets or update place_ctx.grid_blocks
//yet since we don't know whether the swap will be accepted
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe, could do num_moved = blocks_affected.moved_blocks.size() before the loop -- it is slightly safer from a compiler optimization point of view, as otherwise we are counting on the compiler to figure out .size() doesn't need to be called each time.

ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;

const t_pl_loc& old_loc = blocks_affected.moved_blocks[iblk].old_loc;
Expand All @@ -67,7 +76,7 @@ void commit_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
auto& place_ctx = g_vpr_ctx.mutable_placement();

/* Swap physical location */
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same optimization (local variable)

ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;

const t_pl_loc& to = blocks_affected.moved_blocks[iblk].new_loc;
Expand Down Expand Up @@ -97,7 +106,7 @@ void revert_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
auto& device_ctx = g_vpr_ctx.device();

// Swap the blocks back, nets not yet swapped they don't need to be changed
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same local loop count optimization.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or use range-based for (Alex figured this out!)

ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;

const t_pl_loc& old_loc = blocks_affected.moved_blocks[iblk].old_loc;
Expand Down Expand Up @@ -126,10 +135,10 @@ void clear_move_blocks(t_pl_blocks_to_be_moved& blocks_affected) {
blocks_affected.moved_to.clear();
blocks_affected.moved_from.clear();

//For run-time, we just reset num_moved_blocks to zero, but do not free the blocks_affected
//For run-time, we just reset size of blocks_affected.moved_blocks to zero, but do not free the blocks_affected
//array to avoid memory allocation

blocks_affected.num_moved_blocks = 0;
blocks_affected.moved_blocks.resize(0);

blocks_affected.affected_pins.clear();
}
7 changes: 2 additions & 5 deletions vpr/src/place/move_transactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ struct t_pl_moved_block {
* placement, in the form of array of structs instead of struct with *
* arrays for cache effifiency *
*
* num_moved_blocks: total number of blocks moved when *
* swapping two blocks. *
* moved blocks: a list of moved blocks data structure with *
* information on the move. *
* [0...max_blocks-1] *
* affected_pins: pins affected by this move (used to *
* incrementally invalidate parts of the timing *
* graph. */
struct t_pl_blocks_to_be_moved {
explicit t_pl_blocks_to_be_moved(size_t max_blocks)
: moved_blocks(max_blocks) {}
explicit t_pl_blocks_to_be_moved(size_t max_blocks);

int num_moved_blocks = 0;
std::vector<t_pl_moved_block> moved_blocks;
std::unordered_set<t_pl_loc> moved_from;
std::unordered_set<t_pl_loc> moved_to;

std::vector<ClusterPinId> affected_pins;
size_t get_size_and_increment();
};

enum class e_block_move_result {
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/move_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ std::set<t_pl_loc> determine_locations_emptied_by_move(t_pl_blocks_to_be_moved&
std::set<t_pl_loc> moved_from;
std::set<t_pl_loc> moved_to;

for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

//When a block is moved its old location becomes free
moved_from.emplace(blocks_affected.moved_blocks[iblk].old_loc);

Expand Down
54 changes: 22 additions & 32 deletions vpr/src/place/net_cost_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ static std::vector<ClusterNetId> ts_nets_to_update;
* @return True if the driver block of the net is among the moving blocks
*/
static bool driven_by_moved_block(const ClusterNetId net,
const int num_blocks,
const std::vector<t_pl_moved_block>& moved_blocks);
/**
* @brief Update the bounding box (3D) of the net connected to blk_pin. The old and new locations of the pin are
Expand Down Expand Up @@ -140,11 +139,10 @@ static void update_td_delta_costs(const PlaceDelayModel* delay_model,
bool is_src_moving);

/**
* @brief if "net" is not already stored as an affected net, mark it in ts_nets_to_update and increment num_affected_nets
* @brief if "net" is not already stored as an affected net, mark it in ts_nets_to_update and increment the size ofts_nets_to_update.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably just say "add it to ts_nets_to_update" (also a typo)

* @param net ID of a net affected by a move
* @param num_affected_nets Incremented if this is a new net affected, and returned via reference.
*/
static void record_affected_net(const ClusterNetId net, int& num_affected_nets);
static void record_affected_net(const ClusterNetId net);

/**
* @brief Call suitable function based on the bounding box type to update the bounding box of the net connected to pin_id. Also,
Expand All @@ -157,7 +155,6 @@ static void record_affected_net(const ClusterNetId net, int& num_affected_nets);
* @param moving_blk_inf Data structure that holds information, e.g., old location and new locatoin, about all moving blocks
* @param affected_pins Netlist pins which are affected, in terms placement cost, by the proposed move.
* @param timing_delta_c Timing cost change based on the proposed move
* @param num_affected_nets A pointer to the first free element of ts_nets_to_update. If a new net is added, the pointer should be increamented.
* @param is_src_moving Is the moving pin the source of a net.
*/
static void update_net_info_on_pin_move(const t_place_algorithm& place_algorithm,
Expand All @@ -168,7 +165,6 @@ static void update_net_info_on_pin_move(const t_place_algorithm& place_algorithm
const t_pl_moved_block& moving_blk_inf,
std::vector<ClusterPinId>& affected_pins,
double& timing_delta_c,
int& num_affected_nets,
bool is_src_moving);

/**
Expand Down Expand Up @@ -446,23 +442,21 @@ static double wirelength_crossing_count(size_t fanout);
/**
* @brief Calculates and returns the total bb (wirelength) cost change that would result from moving the blocks
* indicated in the blocks_affected data structure.
* @param num_affected_nets Number of valid elements in ts_bb_coord_new
* @param bb_delta_c Cost difference after and before moving the block
*/
static void set_bb_delta_cost(const int num_affected_nets, double& bb_delta_c);
static void set_bb_delta_cost(double& bb_delta_c);

/******************************* End of Function definitions ************************************/

//Returns true if 'net' is driven by one of the blocks in 'blocks_affected'
static bool driven_by_moved_block(const ClusterNetId net,
const int num_blocks,
const std::vector<t_pl_moved_block>& moved_blocks) {
auto& clb_nlist = g_vpr_ctx.clustering().clb_nlist;
bool is_driven_by_move_blk = false;
ClusterBlockId net_driver_block = clb_nlist.net_driver_block(
net);

for (int block_num = 0; block_num < num_blocks; block_num++) {
for (size_t block_num = 0; block_num < moved_blocks.size(); block_num++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range based for

if (net_driver_block == moved_blocks[block_num].block_num) {
is_driven_by_move_blk = true;
break;
Expand Down Expand Up @@ -642,13 +636,14 @@ static void update_td_delta_costs(const PlaceDelayModel* delay_model,
}

///@brief Record effected nets.
static void record_affected_net(const ClusterNetId net,
int& num_affected_nets) {
static void record_affected_net(const ClusterNetId net) {
/* Record effected nets. */
if (proposed_net_cost[net] < 0.) {
/* Net not marked yet. */
ts_nets_to_update[num_affected_nets] = net;
num_affected_nets++;
size_t last_size = ts_nets_to_update.size();
VTR_ASSERT(last_size < ts_nets_to_update.capacity());
ts_nets_to_update.resize(last_size + 1);
ts_nets_to_update[last_size] = net;

/* Flag to say we've marked this net. */
proposed_net_cost[net] = 1.;
Expand All @@ -663,7 +658,6 @@ static void update_net_info_on_pin_move(const t_place_algorithm& place_algorithm
const t_pl_moved_block& moving_blk_inf,
std::vector<ClusterPinId>& affected_pins,
double& timing_delta_c,
int& num_affected_nets,
bool is_src_moving) {
const auto& cluster_ctx = g_vpr_ctx.clustering();
const ClusterNetId net_id = cluster_ctx.clb_nlist.pin_net(pin_id);
Expand All @@ -677,7 +671,7 @@ static void update_net_info_on_pin_move(const t_place_algorithm& place_algorithm
}

/* Record effected nets */
record_affected_net(net_id, num_affected_nets);
record_affected_net(net_id);

const auto& cube_bb = g_vpr_ctx.placement().cube_bb;

Expand Down Expand Up @@ -1852,8 +1846,8 @@ static double wirelength_crossing_count(size_t fanout) {
}
}

static void set_bb_delta_cost(const int num_affected_nets, double& bb_delta_c) {
for (int inet_affected = 0; inet_affected < num_affected_nets;
static void set_bb_delta_cost(double& bb_delta_c) {
for (size_t inet_affected = 0; inet_affected < ts_nets_to_update.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

inet_affected++) {
ClusterNetId net_id = ts_nets_to_update[inet_affected];
const auto& cube_bb = g_vpr_ctx.placement().cube_bb;
Expand All @@ -1871,7 +1865,7 @@ static void set_bb_delta_cost(const int num_affected_nets, double& bb_delta_c) {
}
}

int find_affected_nets_and_update_costs(
void find_affected_nets_and_update_costs(
const t_place_algorithm& place_algorithm,
const PlaceDelayModel* delay_model,
const PlacerCriticalities* criticalities,
Expand All @@ -1882,10 +1876,10 @@ int find_affected_nets_and_update_costs(
VTR_ASSERT_SAFE(timing_delta_c == 0.);
auto& clb_nlist = g_vpr_ctx.clustering().clb_nlist;

int num_affected_nets = 0;
ts_nets_to_update.resize(0);

/* Go through all the blocks moved. */
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; iblk++) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); iblk++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

const auto& moving_block_inf = blocks_affected.moved_blocks[iblk];
auto& affected_pins = blocks_affected.affected_pins;
ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;
Expand All @@ -1896,7 +1890,6 @@ int find_affected_nets_and_update_costs(
if (clb_nlist.pin_type(blk_pin) == PinType::SINK) {
ClusterNetId net_id = clb_nlist.pin_net(blk_pin);
is_src_moving = driven_by_moved_block(net_id,
blocks_affected.num_moved_blocks,
blocks_affected.moved_blocks);
}
update_net_info_on_pin_move(place_algorithm,
Expand All @@ -1907,16 +1900,13 @@ int find_affected_nets_and_update_costs(
moving_block_inf,
affected_pins,
timing_delta_c,
num_affected_nets,
is_src_moving);
}
}

/* Now update the bounding box costs (since the net bounding *
* boxes are up-to-date). The cost is only updated once per net. */
set_bb_delta_cost(num_affected_nets, bb_delta_c);

return num_affected_nets;
set_bb_delta_cost(bb_delta_c);
}

double comp_bb_cost(e_cost_methods method) {
Expand Down Expand Up @@ -1997,13 +1987,12 @@ double comp_layer_bb_cost(e_cost_methods method) {
return cost;
}

void update_move_nets(int num_nets_affected,
const bool cube_bb) {
void update_move_nets(const bool cube_bb) {
/* update net cost functions and reset flags. */
auto& cluster_ctx = g_vpr_ctx.clustering();
auto& place_move_ctx = g_placer_ctx.mutable_move();

for (int inet_affected = 0; inet_affected < num_nets_affected;
for (size_t inet_affected = 0; inet_affected < ts_nets_to_update.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

inet_affected++) {
ClusterNetId net_id = ts_nets_to_update[inet_affected];

Expand Down Expand Up @@ -2033,9 +2022,9 @@ void update_move_nets(int num_nets_affected,
}
}

void reset_move_nets(int num_nets_affected) {
void reset_move_nets() {
/* Reset the net cost function flags first. */
for (int inet_affected = 0; inet_affected < num_nets_affected;
for (size_t inet_affected = 0; inet_affected < ts_nets_to_update.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

inet_affected++) {
ClusterNetId net_id = ts_nets_to_update[inet_affected];
proposed_net_cost[net_id] = -1;
Expand Down Expand Up @@ -2242,7 +2231,8 @@ void init_try_swap_net_cost_structs(size_t num_nets, bool cube_bb) {
/*This initialize the whole matrix to OPEN which is an invalid value*/
ts_layer_sink_pin_count.resize({num_nets, size_t(num_layers)}, OPEN);

ts_nets_to_update.resize(num_nets, ClusterNetId::INVALID());
ts_nets_to_update.reserve(num_nets);
ts_nets_to_update.resize(0);
}

void free_try_swap_net_cost_structs() {
Expand Down
7 changes: 3 additions & 4 deletions vpr/src/place/net_cost_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum e_cost_methods {
* @param timing_delta_c
* @return The number of affected nets.
*/
int find_affected_nets_and_update_costs(
void find_affected_nets_and_update_costs(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if this function modifies (and doesn't clear/reset) the ts_* variables; if it changes their state (and leaves it changed) it should be mentioned briefly here.

const t_place_algorithm& place_algorithm,
const PlaceDelayModel* delay_model,
const PlacerCriticalities* criticalities,
Expand Down Expand Up @@ -85,14 +85,13 @@ double comp_layer_bb_cost(e_cost_methods method);
* @param num_nets_affected The number of nets affected by the move. It is used to determine the index up to which elements in ts_nets_to_update are valid.
* @param cube_bb True if we should use the 3D bounding box (cube_bb), false otherwise.
*/
void update_move_nets(int num_nets_affected,
const bool cube_bb);
void update_move_nets(const bool cube_bb);

/**
* @brief Reset the net cost function flags (proposed_net_cost and bb_updated_before)
* @param num_nets_affected
*/
void reset_move_nets(int num_nets_affected);
void reset_move_nets();

/**
* @brief re-calculates different terms of the cost function (wire-length, timing, NoC) and update "costs" accordingly. It is important to note that
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 @@ -136,7 +136,7 @@ void find_affected_noc_routers_and_update_noc_costs(const t_pl_blocks_to_be_move
affected_noc_links.clear();

// go through the moved blocks and process them only if they are NoC routers
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;

// check if the current moved block is a noc router
Expand Down Expand Up @@ -291,7 +291,7 @@ void revert_noc_traffic_flow_routes(const t_pl_blocks_to_be_moved& blocks_affect
std::unordered_set<NocTrafficFlowId> reverted_traffic_flows;

// go through the moved blocks and process them only if they are NoC routers
for (int iblk = 0; iblk < blocks_affected.num_moved_blocks; ++iblk) {
for (size_t iblk = 0; iblk < blocks_affected.moved_blocks.size(); ++iblk) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num;

// check if the current moved block is a noc router
Expand Down
9 changes: 4 additions & 5 deletions vpr/src/place/place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ std::unique_ptr<FILE, decltype(&vtr::fclose)> f_move_stats_file(nullptr,
t, \
int(size_t(b_from)), int(size_t(b_to)), \
from_type->name, (to_type ? to_type->name : "EMPTY"), \
affected_blocks.num_moved_blocks); \
affected_blocks.moved_blocks.size()); \
} \
} while (false)

Expand Down Expand Up @@ -1378,7 +1378,7 @@ static e_move_result try_swap(const t_annealing_state* state,
//
//Also find all the pins affected by the swap, and calculates new connection
//delays and timing costs and store them in proposed_* data structures.
int num_nets_affected = find_affected_nets_and_update_costs(
find_affected_nets_and_update_costs(
place_algorithm, delay_model, criticalities, blocks_affected,
bb_delta_c, timing_delta_c);

Expand Down Expand Up @@ -1481,8 +1481,7 @@ static e_move_result try_swap(const t_annealing_state* state,
}

/* Update net cost functions and reset flags. */
update_move_nets(num_nets_affected,
g_vpr_ctx.placement().cube_bb);
update_move_nets(g_vpr_ctx.placement().cube_bb);

/* Update clb data structures since we kept the move. */
commit_move_blocks(blocks_affected);
Expand All @@ -1506,7 +1505,7 @@ static e_move_result try_swap(const t_annealing_state* state,
VTR_ASSERT_SAFE(move_outcome == REJECTED);

/* Reset the net cost function flags first. */
reset_move_nets(num_nets_affected);
reset_move_nets();

/* Restore the place_ctx.block_locs data structures to their state before the move. */
revert_move_blocks(blocks_affected);
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/place_constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void print_macro_constraint_error(const t_pl_macro& pl_macro);
inline bool floorplan_legal(const t_pl_blocks_to_be_moved& blocks_affected) {
bool floorplan_legal;

for (int i = 0; i < blocks_affected.num_moved_blocks; i++) {
for (size_t i = 0; i < blocks_affected.moved_blocks.size(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range-based for

floorplan_legal = cluster_floorplanning_legal(blocks_affected.moved_blocks[i].block_num,
blocks_affected.moved_blocks[i].new_loc);
if (!floorplan_legal) {
Expand Down
Loading
Loading