Skip to content

Commit 159c2c4

Browse files
authored
Merge pull request #2676 from verilog-to-routing/temp_net_cost_ref
NetCostHandler class
2 parents 2c2af51 + 5576193 commit 159c2c4

39 files changed

+1435
-1510
lines changed

libs/libvtrutil/src/vtr_memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace vtr {
1414
/**
1515
* @brief This function will force the container to be cleared
1616
*
17-
* It release it's held memory.
17+
* It releases its held memory.
1818
* For efficiency, STL containers usually don't
1919
* release their actual heap-allocated memory until
2020
* destruction (even if Container::clear() is called).

vpr/src/base/blk_loc_registry.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11

22
#include "blk_loc_registry.h"
3+
4+
#include "move_transactions.h"
35
#include "globals.h"
46

7+
BlkLocRegistry::BlkLocRegistry()
8+
: expected_transaction_(e_expected_transaction::APPLY) {}
9+
510
const vtr::vector_map<ClusterBlockId, t_block_loc>& BlkLocRegistry::block_locs() const {
611
return block_locs_;
712
}
@@ -112,3 +117,93 @@ void BlkLocRegistry::place_sync_external_block_connections(ClusterBlockId iblk)
112117
}
113118
}
114119
}
120+
121+
void BlkLocRegistry::apply_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
122+
auto& device_ctx = g_vpr_ctx.device();
123+
124+
VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::APPLY);
125+
126+
// Swap the blocks, but don't swap the nets or update place_ctx.grid_blocks
127+
// yet since we don't know whether the swap will be accepted
128+
for (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
129+
ClusterBlockId blk = moved_block.block_num;
130+
131+
const t_pl_loc& old_loc = moved_block.old_loc;
132+
const t_pl_loc& new_loc = moved_block.new_loc;
133+
134+
// move the block to its new location
135+
block_locs_[blk].loc = new_loc;
136+
137+
// get physical tile type of the old location
138+
t_physical_tile_type_ptr old_type = device_ctx.grid.get_physical_type({old_loc.x,old_loc.y,old_loc.layer});
139+
// get physical tile type of the new location
140+
t_physical_tile_type_ptr new_type = device_ctx.grid.get_physical_type({new_loc.x,new_loc.y, new_loc.layer});
141+
142+
// if physical tile type of old location does not equal physical tile type of new location, sync the new physical pins
143+
if (old_type != new_type) {
144+
place_sync_external_block_connections(blk);
145+
}
146+
}
147+
148+
expected_transaction_ = e_expected_transaction::COMMIT_REVERT;
149+
}
150+
151+
void BlkLocRegistry::commit_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
152+
VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::COMMIT_REVERT);
153+
154+
// Swap physical location
155+
for (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
156+
ClusterBlockId blk = moved_block.block_num;
157+
158+
const t_pl_loc& to = moved_block.new_loc;
159+
const t_pl_loc& from = moved_block.old_loc;
160+
161+
// Remove from old location only if it hasn't already been updated by a previous block update
162+
if (grid_blocks_.block_at_location(from) == blk) {
163+
grid_blocks_.set_block_at_location(from, ClusterBlockId::INVALID());
164+
grid_blocks_.decrement_usage({from.x, from.y, from.layer});
165+
}
166+
167+
// Add to new location
168+
if (grid_blocks_.block_at_location(to) == ClusterBlockId::INVALID()) {
169+
//Only need to increase usage if previously unused
170+
grid_blocks_.increment_usage({to.x, to.y, to.layer});
171+
}
172+
grid_blocks_.set_block_at_location(to, blk);
173+
174+
} // Finish updating clb for all blocks
175+
176+
expected_transaction_ = e_expected_transaction::APPLY;
177+
}
178+
179+
void BlkLocRegistry::revert_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected) {
180+
auto& device_ctx = g_vpr_ctx.device();
181+
182+
VTR_ASSERT_DEBUG(expected_transaction_ == e_expected_transaction::COMMIT_REVERT);
183+
184+
// Swap the blocks back, nets not yet swapped they don't need to be changed
185+
for (const t_pl_moved_block& moved_block : blocks_affected.moved_blocks) {
186+
ClusterBlockId blk = moved_block.block_num;
187+
188+
const t_pl_loc& old_loc = moved_block.old_loc;
189+
const t_pl_loc& new_loc = moved_block.new_loc;
190+
191+
// return the block to where it was before the swap
192+
block_locs_[blk].loc = old_loc;
193+
194+
// get physical tile type of the old location
195+
t_physical_tile_type_ptr old_type = device_ctx.grid.get_physical_type({old_loc.x, old_loc.y, old_loc.layer});
196+
// get physical tile type of the new location
197+
t_physical_tile_type_ptr new_type = device_ctx.grid.get_physical_type({new_loc.x, new_loc.y, new_loc.layer});
198+
199+
// if physical tile type of old location does not equal physical tile type of new location, sync the new physical pins
200+
if (old_type != new_type) {
201+
place_sync_external_block_connections(blk);
202+
}
203+
204+
VTR_ASSERT_SAFE_MSG(grid_blocks_.block_at_location(old_loc) == blk,
205+
"Grid blocks should only have been updated if swap committed (not reverted)");
206+
}
207+
208+
expected_transaction_ = e_expected_transaction::APPLY;
209+
}

vpr/src/base/blk_loc_registry.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "grid_block.h"
88

99
struct t_block_loc;
10+
struct t_pl_blocks_to_be_moved;
1011

1112
/**
1213
* @class BlkLocRegistry contains information about the placement of clustered blocks.
@@ -19,7 +20,7 @@ struct t_block_loc;
1920
*/
2021
class BlkLocRegistry {
2122
public:
22-
BlkLocRegistry() = default;
23+
BlkLocRegistry();
2324
~BlkLocRegistry() = default;
2425
BlkLocRegistry(const BlkLocRegistry&) = delete;
2526
BlkLocRegistry& operator=(const BlkLocRegistry&) = default;
@@ -80,6 +81,40 @@ class BlkLocRegistry {
8081
* It does not check for overuse of locations, therefore it can be used with placements that have resource overuse.
8182
*/
8283
void place_sync_external_block_connections(ClusterBlockId iblk);
84+
85+
/**
86+
* @brief Moves the blocks in blocks_affected to their new locations.
87+
* This method only updates `grid_blocks_` member variable and not `grid_blocks_`.
88+
* After this method is called, either commit_move_blocks() or revert_move_blocks()
89+
* must be called to either revert the new locations or commit them to `grid_block_`
90+
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
91+
*/
92+
void apply_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);
93+
94+
/**
95+
* @brief Commits the blocks in blocks_affected to their new locations (updates inverse lookups in grid_blocks)
96+
* This method can only be called after a call to apply_move_blocks() to commit the block location changes
97+
* to `grid_block_`. If this method is called after apply_move_blocks(), revert_move_blocks() must not be called
98+
* until the next call to apply_move_blocks() is made.
99+
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
100+
*/
101+
void commit_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);
102+
103+
/**
104+
* @brief Moves the blocks in blocks_affected to their old locations by updating `block_locs_` member variable.
105+
* This method can only be called after a call to apply_move_blocks() to revert the block location changes
106+
* applied to `block_locs_`. If this method is called after apply_move_blocks(), commit_move_blocks() must not be called
107+
* until the next call to apply_move_blocks() is made.
108+
* @param blocks_affected Clustered blocks affected by a swap and their old and new locations.
109+
*/
110+
void revert_move_blocks(const t_pl_blocks_to_be_moved& blocks_affected);
111+
112+
enum class e_expected_transaction {
113+
APPLY,
114+
COMMIT_REVERT
115+
};
116+
117+
e_expected_transaction expected_transaction_;
83118
};
84119

85120
#endif //VTR_BLK_LOC_REGISTRY_H

vpr/src/base/vpr_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ struct t_2D_bb {
535535
VTR_ASSERT(ymax_ >= ymin_);
536536
VTR_ASSERT(layer_num_ >= 0);
537537
}
538+
538539
int xmin = OPEN;
539540
int xmax = OPEN;
540541
int ymin = OPEN;

vpr/src/place/RL_agent_util.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
66
const t_placer_opts& placer_opts,
77
int move_lim,
88
double noc_attraction_weight) {
9-
9+
e_reward_function reward_fun = string_to_reward(placer_opts.place_reward_fun);
1010
std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> move_generators;
1111

12+
1213
if (!placer_opts.RL_agent_placement) { // RL agent is disabled
1314
auto move_types = placer_opts.place_static_move_prob;
1415
move_types.resize((int)e_move_type::NUMBER_OF_AUTO_MOVES, 0.0f);
@@ -20,8 +21,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
2021
move_name.c_str(),
2122
placer_opts.place_static_move_prob[move_type]);
2223
}
23-
move_generators.first = std::make_unique<StaticMoveGenerator>(placer_state, placer_opts.place_static_move_prob);
24-
move_generators.second = std::make_unique<StaticMoveGenerator>(placer_state, placer_opts.place_static_move_prob);
24+
move_generators.first = std::make_unique<StaticMoveGenerator>(placer_state, reward_fun, placer_opts.place_static_move_prob);
25+
move_generators.second = std::make_unique<StaticMoveGenerator>(placer_state, reward_fun, placer_opts.place_static_move_prob);
2526
} else { //RL based placement
2627
/* For the non timing driven placement: the agent has a single state *
2728
* - Available moves are (Uniform / Median / Centroid) *
@@ -74,6 +75,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
7475
}
7576
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
7677
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
78+
reward_fun,
7779
karmed_bandit_agent1,
7880
noc_attraction_weight,
7981
placer_opts.place_high_fanout_net);
@@ -83,6 +85,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
8385
placer_opts.place_agent_epsilon);
8486
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
8587
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
88+
reward_fun,
8689
karmed_bandit_agent2,
8790
noc_attraction_weight,
8891
placer_opts.place_high_fanout_net);
@@ -100,6 +103,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
100103
}
101104
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
102105
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
106+
reward_fun,
103107
karmed_bandit_agent1,
104108
noc_attraction_weight,
105109
placer_opts.place_high_fanout_net);
@@ -108,6 +112,7 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
108112
e_agent_space::MOVE_TYPE);
109113
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
110114
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
115+
reward_fun,
111116
karmed_bandit_agent2,
112117
noc_attraction_weight,
113118
placer_opts.place_high_fanout_net);

vpr/src/place/centroid_move_generator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ vtr::vector<ClusterBlockId, NocGroupId> CentroidMoveGenerator::cluster_to_noc_gr
1616
std::map<ClusterBlockId, NocGroupId> CentroidMoveGenerator::noc_router_to_noc_group_;
1717

1818

19-
CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state)
20-
: MoveGenerator(placer_state)
19+
CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state,
20+
e_reward_function reward_function)
21+
: MoveGenerator(placer_state, reward_function)
2122
, noc_attraction_w_(0.0f)
2223
, noc_attraction_enabled_(false) {}
2324

2425
CentroidMoveGenerator::CentroidMoveGenerator(PlacerState& placer_state,
26+
e_reward_function reward_function,
2527
float noc_attraction_weight,
2628
size_t high_fanout_net)
27-
: MoveGenerator(placer_state)
29+
: MoveGenerator(placer_state, reward_function)
2830
, noc_attraction_w_(noc_attraction_weight)
2931
, noc_attraction_enabled_(true) {
3032
VTR_ASSERT(noc_attraction_weight > 0.0 && noc_attraction_weight <= 1.0);

vpr/src/place/centroid_move_generator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class CentroidMoveGenerator : public MoveGenerator {
2626
*
2727
* @param placer_state A mutable reference to the placement state which will
2828
* be stored in this object.
29+
* @param reward_function Specifies the reward function to update q-tables
30+
* of the RL agent.
2931
*/
30-
explicit CentroidMoveGenerator(PlacerState& placer_state);
32+
CentroidMoveGenerator(PlacerState& placer_state,
33+
e_reward_function reward_function);
3134

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

vpr/src/place/critical_uniform_move_generator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "placer_state.h"
55
#include "move_utils.h"
66

7-
CriticalUniformMoveGenerator::CriticalUniformMoveGenerator(PlacerState& placer_state)
8-
: MoveGenerator(placer_state) {}
7+
CriticalUniformMoveGenerator::CriticalUniformMoveGenerator(PlacerState& placer_state,
8+
e_reward_function reward_function)
9+
: MoveGenerator(placer_state, reward_function) {}
910

1011
e_create_move CriticalUniformMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,
1112
t_propose_action& proposed_action,

vpr/src/place/critical_uniform_move_generator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
class CriticalUniformMoveGenerator : public MoveGenerator {
1818
public:
1919
CriticalUniformMoveGenerator() = delete;
20-
explicit CriticalUniformMoveGenerator(PlacerState& placer_state);
20+
CriticalUniformMoveGenerator(PlacerState& placer_state,
21+
e_reward_function reward_function);
2122

2223
private:
2324
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,

vpr/src/place/directed_moves_util.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
* @brief enum represents the different reward functions
99
*/
1010
enum class e_reward_function {
11-
BASIC, ///@ directly uses the change of the annealing cost function
12-
NON_PENALIZING_BASIC, ///@ same as basic reward function but with 0 reward if it's a hill-climbing one
13-
RUNTIME_AWARE, ///@ same as NON_PENALIZING_BASIC but with normalizing with the runtime factor of each move type
14-
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)
11+
BASIC, ///@ directly uses the change of the annealing cost function
12+
NON_PENALIZING_BASIC, ///@ same as basic reward function but with 0 reward if it's a hill-climbing one
13+
RUNTIME_AWARE, ///@ same as NON_PENALIZING_BASIC but with normalizing with the runtime factor of each move type
14+
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)
15+
UNDEFINED_REWARD ///@ Used for manual moves
1516
};
1617

1718
e_reward_function string_to_reward(const std::string& st);

vpr/src/place/feasible_region_move_generator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <algorithm>
99
#include <cmath>
1010

11-
FeasibleRegionMoveGenerator::FeasibleRegionMoveGenerator(PlacerState& placer_state)
12-
: MoveGenerator(placer_state) {}
11+
FeasibleRegionMoveGenerator::FeasibleRegionMoveGenerator(PlacerState& placer_state,
12+
e_reward_function reward_function)
13+
: MoveGenerator(placer_state, reward_function) {}
1314

1415
e_create_move FeasibleRegionMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,
1516
t_propose_action& proposed_action,

vpr/src/place/feasible_region_move_generator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
class FeasibleRegionMoveGenerator : public MoveGenerator {
2222
public:
2323
FeasibleRegionMoveGenerator() = delete;
24-
explicit FeasibleRegionMoveGenerator(PlacerState& placer_state);
24+
FeasibleRegionMoveGenerator(PlacerState& placer_state,
25+
e_reward_function reward_function);
2526

2627
private:
2728
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,

vpr/src/place/initial_noc_placement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static void noc_routers_anneal(const t_noc_opts& noc_opts,
267267
e_create_move create_move_outcome = propose_router_swap(blocks_affected, r_lim_decayed, blk_loc_registry);
268268

269269
if (create_move_outcome != e_create_move::ABORT) {
270-
apply_move_blocks(blocks_affected, blk_loc_registry);
270+
blk_loc_registry.apply_move_blocks(blocks_affected);
271271

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

279279
if (move_accepted) {
280280
costs.cost += delta_cost;
281-
commit_move_blocks(blocks_affected, blk_loc_registry.mutable_grid_blocks());
281+
blk_loc_registry.commit_move_blocks(blocks_affected);
282282
commit_noc_costs();
283283
costs += noc_delta_c;
284284
// check if the current placement is better than the stored checkpoint
285285
if (costs.cost < checkpoint.get_cost() || !checkpoint.is_valid()) {
286286
checkpoint.save_checkpoint(costs.cost, block_locs);
287287
}
288288
} else { // The proposed move is rejected
289-
revert_move_blocks(blocks_affected, blk_loc_registry);
289+
blk_loc_registry.revert_move_blocks(blocks_affected);
290290
revert_noc_traffic_flow_routes(blocks_affected, block_locs);
291291
}
292292
}

vpr/src/place/manual_move_generator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#endif //NO_GRAPHICS
2020

2121
ManualMoveGenerator::ManualMoveGenerator(PlacerState& placer_state)
22-
: MoveGenerator(placer_state) {}
22+
: MoveGenerator(placer_state, e_reward_function::UNDEFINED_REWARD) {}
2323

2424
//Manual Move Generator function
2525
e_create_move ManualMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected,

vpr/src/place/manual_move_generator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class ManualMoveGenerator : public MoveGenerator {
2828
public:
2929
ManualMoveGenerator() = delete;
30-
explicit ManualMoveGenerator(PlacerState& placer_state);
30+
ManualMoveGenerator(PlacerState& placer_state);
3131

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

0 commit comments

Comments
 (0)