diff --git a/vpr/src/place/move_transactions.cpp b/vpr/src/place/move_transactions.cpp index f36dd4d5e39..72d2205d815 100644 --- a/vpr/src/place/move_transactions.cpp +++ b/vpr/src/place/move_transactions.cpp @@ -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()); + 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) { @@ -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; } @@ -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) { ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num; const t_pl_loc& old_loc = blocks_affected.moved_blocks[iblk].old_loc; @@ -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) { ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num; const t_pl_loc& to = blocks_affected.moved_blocks[iblk].new_loc; @@ -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) { ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num; const t_pl_loc& old_loc = blocks_affected.moved_blocks[iblk].old_loc; @@ -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(); } diff --git a/vpr/src/place/move_transactions.h b/vpr/src/place/move_transactions.h index 27dd2b1b3c6..05b7486cc35 100644 --- a/vpr/src/place/move_transactions.h +++ b/vpr/src/place/move_transactions.h @@ -25,8 +25,6 @@ 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] * @@ -34,15 +32,14 @@ struct t_pl_moved_block { * 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 moved_blocks; std::unordered_set moved_from; std::unordered_set moved_to; std::vector affected_pins; + size_t get_size_and_increment(); }; enum class e_block_move_result { diff --git a/vpr/src/place/move_utils.cpp b/vpr/src/place/move_utils.cpp index 8c6ae2a6249..a97462062d2 100644 --- a/vpr/src/place/move_utils.cpp +++ b/vpr/src/place/move_utils.cpp @@ -488,7 +488,7 @@ std::set determine_locations_emptied_by_move(t_pl_blocks_to_be_moved& std::set moved_from; std::set 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) { //When a block is moved its old location becomes free moved_from.emplace(blocks_affected.moved_blocks[iblk].old_loc); diff --git a/vpr/src/place/net_cost_handler.cpp b/vpr/src/place/net_cost_handler.cpp index 888f2bea130..9ab2050b07a 100644 --- a/vpr/src/place/net_cost_handler.cpp +++ b/vpr/src/place/net_cost_handler.cpp @@ -103,7 +103,6 @@ static std::vector 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& moved_blocks); /** * @brief Update the bounding box (3D) of the net connected to blk_pin. The old and new locations of the pin are @@ -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. * @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, @@ -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, @@ -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& affected_pins, double& timing_delta_c, - int& num_affected_nets, bool is_src_moving); /** @@ -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& 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++) { if (net_driver_block == moved_blocks[block_num].block_num) { is_driven_by_move_blk = true; break; @@ -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.; @@ -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& 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); @@ -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; @@ -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(); inet_affected++) { ClusterNetId net_id = ts_nets_to_update[inet_affected]; const auto& cube_bb = g_vpr_ctx.placement().cube_bb; @@ -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, @@ -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++) { 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; @@ -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, @@ -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) { @@ -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(); inet_affected++) { ClusterNetId net_id = ts_nets_to_update[inet_affected]; @@ -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(); inet_affected++) { ClusterNetId net_id = ts_nets_to_update[inet_affected]; proposed_net_cost[net_id] = -1; @@ -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() { diff --git a/vpr/src/place/net_cost_handler.h b/vpr/src/place/net_cost_handler.h index 57c64cadca5..aea9a21f9ef 100644 --- a/vpr/src/place/net_cost_handler.h +++ b/vpr/src/place/net_cost_handler.h @@ -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( const t_place_algorithm& place_algorithm, const PlaceDelayModel* delay_model, const PlacerCriticalities* criticalities, @@ -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 diff --git a/vpr/src/place/noc_place_utils.cpp b/vpr/src/place/noc_place_utils.cpp index 0b9e274b85b..b610500def6 100644 --- a/vpr/src/place/noc_place_utils.cpp +++ b/vpr/src/place/noc_place_utils.cpp @@ -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) { ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num; // check if the current moved block is a noc router @@ -291,7 +291,7 @@ void revert_noc_traffic_flow_routes(const t_pl_blocks_to_be_moved& blocks_affect std::unordered_set 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) { ClusterBlockId blk = blocks_affected.moved_blocks[iblk].block_num; // check if the current moved block is a noc router diff --git a/vpr/src/place/place.cpp b/vpr/src/place/place.cpp index 4e7f448c34b..85f9172daad 100644 --- a/vpr/src/place/place.cpp +++ b/vpr/src/place/place.cpp @@ -150,7 +150,7 @@ std::unique_ptr 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) @@ -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); @@ -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); @@ -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); diff --git a/vpr/src/place/place_constraints.h b/vpr/src/place/place_constraints.h index ff5aacbb092..bb35e03a001 100644 --- a/vpr/src/place/place_constraints.h +++ b/vpr/src/place/place_constraints.h @@ -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++) { floorplan_legal = cluster_floorplanning_legal(blocks_affected.moved_blocks[i].block_num, blocks_affected.moved_blocks[i].new_loc); if (!floorplan_legal) { diff --git a/vpr/test/test_noc_place_utils.cpp b/vpr/test/test_noc_place_utils.cpp index 80023cf8b81..8d3ba5c346f 100644 --- a/vpr/test/test_noc_place_utils.cpp +++ b/vpr/test/test_noc_place_utils.cpp @@ -748,7 +748,7 @@ TEST_CASE("test_find_affected_noc_routers_and_update_noc_costs, test_commit_noc_ } while (swap_router_block_one == swap_router_block_two); //set up the moved blocks datastructure for the test function - blocks_affected.num_moved_blocks = 2; + blocks_affected.moved_blocks.resize(2); blocks_affected.moved_blocks[0].block_num = swap_router_block_one; blocks_affected.moved_blocks[0].old_loc = t_pl_loc(noc_ctx.noc_model.get_single_noc_router(router_where_cluster_is_placed[swap_router_block_one]).get_router_grid_position_x(), @@ -903,7 +903,7 @@ TEST_CASE("test_find_affected_noc_routers_and_update_noc_costs, test_commit_noc_ // now perform the swap //set up the moved blocks datastructure for the test function - blocks_affected.num_moved_blocks = 2; + blocks_affected.moved_blocks.resize(2); blocks_affected.moved_blocks[0].block_num = swap_router_block_one; @@ -1039,7 +1039,7 @@ TEST_CASE("test_find_affected_noc_routers_and_update_noc_costs, test_commit_noc_ // now perform the swap //set up the moved blocks datastructure for the test function - blocks_affected.num_moved_blocks = 2; + blocks_affected.moved_blocks.resize(2); blocks_affected.moved_blocks[0].block_num = swap_router_block_one; @@ -1142,7 +1142,7 @@ TEST_CASE("test_find_affected_noc_routers_and_update_noc_costs, test_commit_noc_ // now perform the swap //set up the moved blocks datastructure for the test function - blocks_affected.num_moved_blocks = 2; + blocks_affected.moved_blocks.resize(2); blocks_affected.moved_blocks[0].block_num = swap_router_block_one; @@ -1584,7 +1584,7 @@ TEST_CASE("test_revert_noc_traffic_flow_routes", "[noc_place_utils]") { //set up the moved blocks datastructure for the test function // this is needed for the test function (it needs to know what blocks were swapped, so it can undo it) - blocks_affected.num_moved_blocks = 2; + blocks_affected.moved_blocks.resize(2); blocks_affected.moved_blocks[0].block_num = swap_router_block_one; blocks_affected.moved_blocks[0].old_loc = t_pl_loc(noc_ctx.noc_model.get_single_noc_router(router_where_cluster_is_placed[swap_router_block_one]).get_router_grid_position_x(),