Skip to content

Commit

Permalink
Merge pull request #2676 from verilog-to-routing/temp_net_cost_ref
Browse files Browse the repository at this point in the history
NetCostHandler class
  • Loading branch information
vaughnbetz authored Sep 24, 2024
2 parents 2c2af51 + 5576193 commit 159c2c4
Show file tree
Hide file tree
Showing 39 changed files with 1,435 additions and 1,510 deletions.
2 changes: 1 addition & 1 deletion libs/libvtrutil/src/vtr_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace vtr {
/**
* @brief This function will force the container to be cleared
*
* It release it's held memory.
* It releases its held memory.
* For efficiency, STL containers usually don't
* release their actual heap-allocated memory until
* destruction (even if Container::clear() is called).
Expand Down
95 changes: 95 additions & 0 deletions vpr/src/base/blk_loc_registry.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

#include "blk_loc_registry.h"

#include "move_transactions.h"
#include "globals.h"

BlkLocRegistry::BlkLocRegistry()
: expected_transaction_(e_expected_transaction::APPLY) {}

const vtr::vector_map<ClusterBlockId, t_block_loc>& BlkLocRegistry::block_locs() const {
return block_locs_;
}
Expand Down Expand Up @@ -112,3 +117,93 @@ void BlkLocRegistry::place_sync_external_block_connections(ClusterBlockId iblk)
}
}
}

void BlkLocRegistry::apply_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
auto& device_ctx = g_vpr_ctx.device();

VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::APPLY);

// 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 (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
ClusterBlockId blk = moved_block.block_num;

const t_pl_loc& old_loc = moved_block.old_loc;
const t_pl_loc& new_loc = moved_block.new_loc;

// move the block to its new location
block_locs_[blk].loc = new_loc;

// get physical tile type of the old location
t_physical_tile_type_ptr old_type = device_ctx.grid.get_physical_type({old_loc.x,old_loc.y,old_loc.layer});
// get physical tile type of the new location
t_physical_tile_type_ptr new_type = device_ctx.grid.get_physical_type({new_loc.x,new_loc.y, new_loc.layer});

// if physical tile type of old location does not equal physical tile type of new location, sync the new physical pins
if (old_type != new_type) {
place_sync_external_block_connections(blk);
}
}

expected_transaction_ = e_expected_transaction::COMMIT_REVERT;
}

void BlkLocRegistry::commit_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::COMMIT_REVERT);

// Swap physical location
for (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
ClusterBlockId blk = moved_block.block_num;

const t_pl_loc& to = moved_block.new_loc;
const t_pl_loc& from = moved_block.old_loc;

// Remove from old location only if it hasn't already been updated by a previous block update
if (grid_blocks_.block_at_location(from) == blk) {
grid_blocks_.set_block_at_location(from, ClusterBlockId::INVALID());
grid_blocks_.decrement_usage({from.x, from.y, from.layer});
}

// Add to new location
if (grid_blocks_.block_at_location(to) == ClusterBlockId::INVALID()) {
//Only need to increase usage if previously unused
grid_blocks_.increment_usage({to.x, to.y, to.layer});
}
grid_blocks_.set_block_at_location(to, blk);

} // Finish updating clb for all blocks

expected_transaction_ = e_expected_transaction::APPLY;
}

void BlkLocRegistry::revert_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
auto& device_ctx = g_vpr_ctx.device();

VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::COMMIT_REVERT);

// Swap the blocks back, nets not yet swapped they don't need to be changed
for (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
ClusterBlockId blk = moved_block.block_num;

const t_pl_loc& old_loc = moved_block.old_loc;
const t_pl_loc& new_loc = moved_block.new_loc;

// return the block to where it was before the swap
block_locs_[blk].loc = old_loc;

// get physical tile type of the old location
t_physical_tile_type_ptr old_type = device_ctx.grid.get_physical_type({old_loc.x, old_loc.y, old_loc.layer});
// get physical tile type of the new location
t_physical_tile_type_ptr new_type = device_ctx.grid.get_physical_type({new_loc.x, new_loc.y, new_loc.layer});

// if physical tile type of old location does not equal physical tile type of new location, sync the new physical pins
if (old_type != new_type) {
place_sync_external_block_connections(blk);
}

VTR_ASSERT_SAFE_MSG(grid_blocks_.block_at_location(old_loc) == blk,
"Grid blocks should only have been updated if swap committed (not reverted)");
}

expected_transaction_ = e_expected_transaction::APPLY;
}
37 changes: 36 additions & 1 deletion vpr/src/base/blk_loc_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "grid_block.h"

struct t_block_loc;
struct t_pl_blocks_to_be_moved;

/**
* @class BlkLocRegistry contains information about the placement of clustered blocks.
Expand All @@ -19,7 +20,7 @@ struct t_block_loc;
*/
class BlkLocRegistry {
public:
BlkLocRegistry() = default;
BlkLocRegistry();
~BlkLocRegistry() = default;
BlkLocRegistry(const BlkLocRegistry&) = delete;
BlkLocRegistry& operator=(const BlkLocRegistry&) = default;
Expand Down Expand Up @@ -80,6 +81,40 @@ class BlkLocRegistry {
* It does not check for overuse of locations, therefore it can be used with placements that have resource overuse.
*/
void place_sync_external_block_connections(ClusterBlockId iblk);

/**
* @brief Moves the blocks in blocks_affected to their new locations.
* This method only updates `grid_blocks_` member variable and not `grid_blocks_`.
* After this method is called, either commit_move_blocks() or revert_move_blocks()
* must be called to either revert the new locations or commit them to `grid_block_`
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
*/
void apply_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);

/**
* @brief Commits the blocks in blocks_affected to their new locations (updates inverse lookups in grid_blocks)
* This method can only be called after a call to apply_move_blocks() to commit the block location changes
* to `grid_block_`. If this method is called after apply_move_blocks(), revert_move_blocks() must not be called
* until the next call to apply_move_blocks() is made.
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
*/
void commit_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);

/**
* @brief Moves the blocks in blocks_affected to their old locations by updating `block_locs_` member variable.
* This method can only be called after a call to apply_move_blocks() to revert the block location changes
* applied to `block_locs_`. If this method is called after apply_move_blocks(), commit_move_blocks() must not be called
* until the next call to apply_move_blocks() is made.
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
*/
void revert_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);

enum class e_expected_transaction {
APPLY,
COMMIT_REVERT
};

e_expected_transaction expected_transaction_;
};

#endif //VTR_BLK_LOC_REGISTRY_H
1 change: 1 addition & 0 deletions vpr/src/base/vpr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ struct t_2D_bb {
VTR_ASSERT(ymax_ >= ymin_);
VTR_ASSERT(layer_num_ >= 0);
}

int xmin = OPEN;
int xmax = OPEN;
int ymin = OPEN;
Expand Down
11 changes: 8 additions & 3 deletions vpr/src/place/RL_agent_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
const t_placer_opts& placer_opts,
int move_lim,
double noc_attraction_weight) {

e_reward_function reward_fun = string_to_reward(placer_opts.place_reward_fun);
std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> move_generators;


if (!placer_opts.RL_agent_placement) { // RL agent is disabled
auto move_types = placer_opts.place_static_move_prob;
move_types.resize((int)e_move_type::NUMBER_OF_AUTO_MOVES, 0.0f);
Expand All @@ -20,8 +21,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
move_name.c_str(),
placer_opts.place_static_move_prob[move_type]);
}
move_generators.first = std::make_unique<StaticMoveGenerator>(placer_state, placer_opts.place_static_move_prob);
move_generators.second = std::make_unique<StaticMoveGenerator>(placer_state, placer_opts.place_static_move_prob);
move_generators.first = std::make_unique<StaticMoveGenerator>(placer_state, reward_fun, placer_opts.place_static_move_prob);
move_generators.second = std::make_unique<StaticMoveGenerator>(placer_state, reward_fun, placer_opts.place_static_move_prob);
} else { //RL based placement
/* For the non timing driven placement: the agent has a single state *
* - Available moves are (Uniform / Median / Centroid) *
Expand Down Expand Up @@ -74,6 +75,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
karmed_bandit_agent1,
noc_attraction_weight,
placer_opts.place_high_fanout_net);
Expand All @@ -83,6 +85,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
placer_opts.place_agent_epsilon);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
karmed_bandit_agent2,
noc_attraction_weight,
placer_opts.place_high_fanout_net);
Expand All @@ -100,6 +103,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
karmed_bandit_agent1,
noc_attraction_weight,
placer_opts.place_high_fanout_net);
Expand All @@ -108,6 +112,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
e_agent_space::MOVE_TYPE);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
karmed_bandit_agent2,
noc_attraction_weight,
placer_opts.place_high_fanout_net);
Expand Down
8 changes: 5 additions & 3 deletions vpr/src/place/centroid_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ vtr::vector<ClusterBlockId, NocGroupId> CentroidMoveGenerator::cluster_to_noc_gr
std::map<ClusterBlockId, NocGroupId> CentroidMoveGenerator::noc_router_to_noc_group_;


CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state)
: MoveGenerator(placer_state)
CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function)
: MoveGenerator(placer_state, reward_function)
, noc_attraction_w_(0.0f)
, noc_attraction_enabled_(false) {}

CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function,
float noc_attraction_weight,
size_t high_fanout_net)
: MoveGenerator(placer_state)
: MoveGenerator(placer_state, reward_function)
, noc_attraction_w_(noc_attraction_weight)
, noc_attraction_enabled_(true) {
VTR_ASSERT(noc_attraction_weight > 0.0 && noc_attraction_weight <= 1.0);
Expand Down
6 changes: 5 additions & 1 deletion vpr/src/place/centroid_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class CentroidMoveGenerator : public MoveGenerator {
*
* @param placer_state A mutable reference to the placement state which will
* be stored in this object.
* @param reward_function Specifies the reward function to update q-tables
* of the RL agent.
*/
explicit CentroidMoveGenerator(PlacerState& placer_state);
CentroidMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function);

/**
* The move generator created by calling this constructor considers both
Expand All @@ -45,6 +48,7 @@ class CentroidMoveGenerator : public MoveGenerator {
* ignored when forming NoC groups.
*/
CentroidMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function,
float noc_attraction_weight,
size_t high_fanout_net);

Expand Down
5 changes: 3 additions & 2 deletions vpr/src/place/critical_uniform_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "placer_state.h"
#include "move_utils.h"

CriticalUniformMoveGenerator::CriticalUniformMoveGenerator(PlacerState& placer_state)
: MoveGenerator(placer_state) {}
CriticalUniformMoveGenerator::CriticalUniformMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function)
: MoveGenerator(placer_state, reward_function) {}

e_create_move CriticalUniformMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,
t_propose_action& proposed_action,
Expand Down
3 changes: 2 additions & 1 deletion vpr/src/place/critical_uniform_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
class CriticalUniformMoveGenerator : public MoveGenerator {
public:
CriticalUniformMoveGenerator() = delete;
explicit CriticalUniformMoveGenerator(PlacerState& placer_state);
CriticalUniformMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function);

private:
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,
Expand Down
9 changes: 5 additions & 4 deletions vpr/src/place/directed_moves_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
* @brief enum represents the different reward functions
*/
enum class e_reward_function {
BASIC, ///@ directly uses the change of the annealing cost function
NON_PENALIZING_BASIC, ///@ same as basic reward function but with 0 reward if it's a hill-climbing one
RUNTIME_AWARE, ///@ same as NON_PENALIZING_BASIC but with normalizing with the runtime factor of each move type
WL_BIASED_RUNTIME_AWARE ///@ same as RUNTIME_AWARE but more biased to WL cost (the factor of the bias is REWARD_BB_TIMING_RELATIVE_WEIGHT)
BASIC, ///@ directly uses the change of the annealing cost function
NON_PENALIZING_BASIC, ///@ same as basic reward function but with 0 reward if it's a hill-climbing one
RUNTIME_AWARE, ///@ same as NON_PENALIZING_BASIC but with normalizing with the runtime factor of each move type
WL_BIASED_RUNTIME_AWARE, ///@ same as RUNTIME_AWARE but more biased to WL cost (the factor of the bias is REWARD_BB_TIMING_RELATIVE_WEIGHT)
UNDEFINED_REWARD ///@ Used for manual moves
};

e_reward_function string_to_reward(const std::string& st);
Expand Down
5 changes: 3 additions & 2 deletions vpr/src/place/feasible_region_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <algorithm>
#include <cmath>

FeasibleRegionMoveGenerator::FeasibleRegionMoveGenerator(PlacerState& placer_state)
: MoveGenerator(placer_state) {}
FeasibleRegionMoveGenerator::FeasibleRegionMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function)
: MoveGenerator(placer_state, reward_function) {}

e_create_move FeasibleRegionMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,
t_propose_action& proposed_action,
Expand Down
3 changes: 2 additions & 1 deletion vpr/src/place/feasible_region_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
class FeasibleRegionMoveGenerator : public MoveGenerator {
public:
FeasibleRegionMoveGenerator() = delete;
explicit FeasibleRegionMoveGenerator(PlacerState& placer_state);
FeasibleRegionMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function);

private:
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,
Expand Down
6 changes: 3 additions & 3 deletions vpr/src/place/initial_noc_placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void noc_routers_anneal(const t_noc_opts& noc_opts,
e_create_move create_move_outcome = propose_router_swap(blocks_affected, r_lim_decayed, blk_loc_registry);

if (create_move_outcome != e_create_move::ABORT) {
apply_move_blocks(blocks_affected, blk_loc_registry);
blk_loc_registry.apply_move_blocks(blocks_affected);

NocCostTerms noc_delta_c;
find_affected_noc_routers_and_update_noc_costs(blocks_affected, noc_delta_c, block_locs);
Expand All @@ -278,15 +278,15 @@ static void noc_routers_anneal(const t_noc_opts& noc_opts,

if (move_accepted) {
costs.cost += delta_cost;
commit_move_blocks(blocks_affected, blk_loc_registry.mutable_grid_blocks());
blk_loc_registry.commit_move_blocks(blocks_affected);
commit_noc_costs();
costs += noc_delta_c;
// check if the current placement is better than the stored checkpoint
if (costs.cost < checkpoint.get_cost() || !checkpoint.is_valid()) {
checkpoint.save_checkpoint(costs.cost, block_locs);
}
} else { // The proposed move is rejected
revert_move_blocks(blocks_affected, blk_loc_registry);
blk_loc_registry.revert_move_blocks(blocks_affected);
revert_noc_traffic_flow_routes(blocks_affected, block_locs);
}
}
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/manual_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#endif //NO_GRAPHICS

ManualMoveGenerator::ManualMoveGenerator(PlacerState& placer_state)
: MoveGenerator(placer_state) {}
: MoveGenerator(placer_state, e_reward_function::UNDEFINED_REWARD) {}

//Manual Move Generator function
e_create_move ManualMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/manual_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class ManualMoveGenerator : public MoveGenerator {
public:
ManualMoveGenerator() = delete;
explicit ManualMoveGenerator(PlacerState& placer_state);
ManualMoveGenerator(PlacerState& placer_state);

//Evaluates if move is successful and legal or unable to do.
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,
Expand Down
Loading

0 comments on commit 159c2c4

Please sign in to comment.