Skip to content

Commit b4f358a

Browse files
Merge pull request #2872 from verilog-to-routing/temp_some_cleanups
Remove a few remaning global variables from the placement stage
2 parents da66e10 + 5a41a00 commit b4f358a

16 files changed

+177
-132
lines changed

libs/libvtrutil/src/vtr_log.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
*
1414
* Three types of log message types are defined:
1515
* - VTR_LOG : The standard 'info' type log message
16-
* - VTR_LOG_WARN : A warning log message. This represents unusual condition that may indicate an issue but executiom continues
17-
* - VTR_LOG_ERROR : An error log message. This represents a clear issue that should result in stopping the program execution. Please note that using this log message will not actually terminate the program. So a VtrError should be thrown after all the neccessary VTR_LOG_ERROR messages are printed.
16+
* - VTR_LOG_WARN : A warning log message. This represents an unusual condition that may indicate an issue but execution continues
17+
* - VTR_LOG_ERROR : An error log message. This represents a clear issue that should result in stopping the program execution.
18+
* Please note that using this log message will not actually terminate the program. So a VtrError should be thrown
19+
* after all the necessary VTR_LOG_ERROR messages are printed.
1820
*
1921
* For example:
2022
*
@@ -27,7 +29,7 @@
2729
*
2830
* Each of the three message types also have a VTR_LOGV_* variant,
2931
* which will cause the message to be logged if a user-defined condition
30-
* is satisifed.
32+
* is satisfied.
3133
*
3234
* For example:
3335
*

vpr/src/base/blk_loc_registry.h

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef VTR_BLK_LOC_REGISTRY_H
2-
#define VTR_BLK_LOC_REGISTRY_H
1+
2+
#pragma once
33

44
#include "clustered_netlist_fwd.h"
55
#include "vtr_vector_map.h"
@@ -44,7 +44,14 @@ class BlkLocRegistry {
4444
*/
4545
PlaceMacros place_macros_;
4646

47+
/// @brief Stores ClusterBlockId of all movable clustered blocks
48+
/// (blocks that are not locked down to a single location)
49+
std::vector<ClusterBlockId> movable_blocks_;
50+
4751
public:
52+
53+
///@brief Stores ClusterBlockId of all movable clustered blocks of each block type
54+
std::vector<std::vector<ClusterBlockId>> movable_blocks_per_type_;
4855
const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs() const;
4956
vtr::vector_map<ClusterBlockId, t_block_loc>& mutable_block_locs();
5057

@@ -66,6 +73,20 @@ class BlkLocRegistry {
6673
///@brief Returns a mutable reference to placement macros.
6774
PlaceMacros& mutable_place_macros();
6875

76+
/// @brief Returns a constant reference to the vector of ClusterBlockIds of all movable clustered blocks.
77+
const std::vector<ClusterBlockId>& movable_blocks() const { return movable_blocks_; }
78+
79+
/// @brief Returns a mutable reference to the vector of ClusterBlockIds of all movable clustered blocks.
80+
std::vector<ClusterBlockId>& mutable_movable_blocks() { return movable_blocks_; }
81+
82+
/// @brief Returns a constant reference to a vector of vectors, where each inner vector contains ClusterBlockIds
83+
/// of movable clustered blocks for a specific block type
84+
const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type() const { return movable_blocks_per_type_; }
85+
86+
/// @brief Returns a mutable reference to a vector of vectors, where each inner vector contains ClusterBlockIds
87+
/// of movable clustered blocks for a specific block type.
88+
std::vector<std::vector<ClusterBlockId>>& mutable_movable_blocks_per_type() { return movable_blocks_per_type_; }
89+
6990
/**
7091
* @brief Performs error checking to see if location is legal for block type,
7192
* and sets the location and grid usage of the block if it is legal.
@@ -149,5 +170,3 @@ class BlkLocRegistry {
149170

150171
e_expected_transaction expected_transaction_;
151172
};
152-
153-
#endif //VTR_BLK_LOC_REGISTRY_H

vpr/src/base/vpr_context.h

-6
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,6 @@ struct PlacementContext : public Context {
367367
*/
368368
void unlock_loc_vars() { VTR_ASSERT_SAFE(!loc_vars_are_accessible_); loc_vars_are_accessible_ = true; }
369369

370-
///@brief Stores ClusterBlockId of all movable clustered blocks (blocks that are not locked down to a single location)
371-
std::vector<ClusterBlockId> movable_blocks;
372-
373-
///@brief Stores ClusterBlockId of all movable clustered of each block type
374-
std::vector<std::vector<ClusterBlockId>> movable_blocks_per_type;
375-
376370
/**
377371
* @brief Compressed grid space for each block type
378372
*

vpr/src/place/RL_agent_util.cpp

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "RL_agent_util.h"
2+
23
#include "static_move_generator.h"
34
#include "manual_move_generator.h"
5+
#include "placer_state.h"
6+
47

58
std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create_move_generators(PlacerState& placer_state,
69
const t_placer_opts& placer_opts,
@@ -60,6 +63,13 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
6063
second_state_avail_moves.push_back(e_move_type::NOC_ATTRACTION_CENTROID);
6164
}
6265

66+
std::vector<int> num_movable_blocks_per_type;
67+
std::ranges::transform(placer_state.blk_loc_registry().movable_blocks_per_type(),
68+
std::back_inserter(num_movable_blocks_per_type),
69+
[](const auto& innerVec) noexcept {
70+
return innerVec.size();
71+
});
72+
6373
if (placer_opts.place_agent_algorithm == e_agent_algorithm::E_GREEDY) {
6474
std::unique_ptr<EpsilonGreedyAgent> karmed_bandit_agent1, karmed_bandit_agent2;
6575
//agent's 1st state
@@ -68,13 +78,15 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
6878
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
6979
e_agent_space::MOVE_BLOCK_TYPE,
7080
placer_opts.place_agent_epsilon,
71-
rng);
81+
rng,
82+
num_movable_blocks_per_type);
7283
} else {
7384
VTR_LOG("Using simple RL 'Epsilon Greedy agent' for choosing move types\n");
7485
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
7586
e_agent_space::MOVE_TYPE,
7687
placer_opts.place_agent_epsilon,
77-
rng);
88+
rng,
89+
num_movable_blocks_per_type);
7890
}
7991
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
8092
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
@@ -87,7 +99,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
8799
karmed_bandit_agent2 = std::make_unique<EpsilonGreedyAgent>(second_state_avail_moves,
88100
e_agent_space::MOVE_TYPE,
89101
placer_opts.place_agent_epsilon,
90-
rng);
102+
rng,
103+
num_movable_blocks_per_type);
91104
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
92105
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
93106
reward_fun,
@@ -102,12 +115,14 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
102115
VTR_LOG("Using simple RL 'Softmax agent' for choosing move and block types\n");
103116
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
104117
e_agent_space::MOVE_BLOCK_TYPE,
105-
rng);
118+
rng,
119+
num_movable_blocks_per_type);
106120
} else {
107121
VTR_LOG("Using simple RL 'Softmax agent' for choosing move types\n");
108122
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
109123
e_agent_space::MOVE_TYPE,
110-
rng);
124+
rng,
125+
num_movable_blocks_per_type);
111126
}
112127
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
113128
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
@@ -119,7 +134,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
119134
//agent's 2nd state
120135
karmed_bandit_agent2 = std::make_unique<SoftmaxAgent>(second_state_avail_moves,
121136
e_agent_space::MOVE_TYPE,
122-
rng);
137+
rng,
138+
num_movable_blocks_per_type);
123139
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
124140
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
125141
reward_fun,

vpr/src/place/initial_placement.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,17 @@ static void place_all_blocks(const t_placer_opts& placer_opts,
246246
* throws an error indicating that initial placement can not be done with the current device size or
247247
* floorplanning constraints.
248248
*/
249-
static void check_initial_placement_legality(const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs);
249+
static void check_initial_placement_legality(const BlkLocRegistry& blk_loc_registry);
250250

251251
/**
252252
* @brief Fills movable_blocks in global PlacementContext
253253
*/
254-
static void alloc_and_load_movable_blocks(const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs);
254+
static void alloc_and_load_movable_blocks(BlkLocRegistry& blk_loc_registry);
255255

256-
static void check_initial_placement_legality(const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs) {
257-
auto& cluster_ctx = g_vpr_ctx.clustering();
258-
auto& place_ctx = g_vpr_ctx.placement();
259-
auto& device_ctx = g_vpr_ctx.device();
256+
static void check_initial_placement_legality(const BlkLocRegistry& blk_loc_registry) {
257+
const auto& cluster_ctx = g_vpr_ctx.clustering();
258+
const auto& device_ctx = g_vpr_ctx.device();
259+
const auto& block_locs = blk_loc_registry.block_locs();
260260

261261
int unplaced_blocks = 0;
262262

@@ -278,14 +278,14 @@ static void check_initial_placement_legality(const vtr::vector_map<ClusterBlockI
278278
unplaced_blocks);
279279
}
280280

281-
for (auto movable_blk_id : place_ctx.movable_blocks) {
281+
for (auto movable_blk_id : blk_loc_registry.movable_blocks()) {
282282
if (block_locs[movable_blk_id].is_fixed) {
283283
VPR_FATAL_ERROR(VPR_ERROR_PLACE, "Fixed block was mistakenly marked as movable during initial placement.\n");
284284
}
285285
}
286286

287287
for (const auto& logical_block_type : device_ctx.logical_block_types) {
288-
const auto& movable_blocks_of_type = place_ctx.movable_blocks_per_type[logical_block_type.index];
288+
const auto& movable_blocks_of_type = blk_loc_registry.movable_blocks_per_type()[logical_block_type.index];
289289
for (const auto& movable_blk_id : movable_blocks_of_type) {
290290
if (block_locs[movable_blk_id].is_fixed) {
291291
VPR_FATAL_ERROR(VPR_ERROR_PLACE, "Fixed block %d of logical type %s was mistakenly marked as movable during initial placement.\n",
@@ -1213,25 +1213,28 @@ bool place_one_block(const ClusterBlockId blk_id,
12131213
return placed_macro;
12141214
}
12151215

1216-
static void alloc_and_load_movable_blocks(const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs) {
1217-
auto& place_ctx = g_vpr_ctx.mutable_placement();
1216+
static void alloc_and_load_movable_blocks(BlkLocRegistry& blk_loc_registry) {
12181217
const auto& cluster_ctx = g_vpr_ctx.clustering();
12191218
const auto& device_ctx = g_vpr_ctx.device();
12201219

1221-
place_ctx.movable_blocks.clear();
1222-
place_ctx.movable_blocks_per_type.clear();
1220+
const auto& block_locs = blk_loc_registry.block_locs();
1221+
auto& movable_blocks = blk_loc_registry.mutable_movable_blocks();
1222+
auto& movable_blocks_per_type = blk_loc_registry.mutable_movable_blocks_per_type();
1223+
1224+
movable_blocks.clear();
1225+
movable_blocks_per_type.clear();
12231226

12241227
size_t n_logical_blocks = device_ctx.logical_block_types.size();
1225-
place_ctx.movable_blocks_per_type.resize(n_logical_blocks);
1228+
movable_blocks_per_type.resize(n_logical_blocks);
12261229

12271230
// iterate over all clustered blocks and store block ids of movable ones
12281231
for (ClusterBlockId blk_id : cluster_ctx.clb_nlist.blocks()) {
12291232
const auto& loc = block_locs[blk_id];
12301233
if (!loc.is_fixed) {
1231-
place_ctx.movable_blocks.push_back(blk_id);
1234+
movable_blocks.push_back(blk_id);
12321235

12331236
const t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(blk_id);
1234-
place_ctx.movable_blocks_per_type[block_type->index].push_back(blk_id);
1237+
movable_blocks_per_type[block_type->index].push_back(blk_id);
12351238
}
12361239
}
12371240
}
@@ -1244,7 +1247,6 @@ void initial_placement(const t_placer_opts& placer_opts,
12441247
const FlatPlacementInfo& flat_placement_info,
12451248
vtr::RngContainer& rng) {
12461249
vtr::ScopedStartFinishTimer timer("Initial Placement");
1247-
auto& block_locs = blk_loc_registry.mutable_block_locs();
12481250
const auto& place_macros = blk_loc_registry.place_macros();
12491251

12501252
/* Initialize the grid blocks to empty.
@@ -1288,8 +1290,8 @@ void initial_placement(const t_placer_opts& placer_opts,
12881290
flat_placement_info, rng);
12891291
}
12901292

1291-
alloc_and_load_movable_blocks(block_locs);
1293+
alloc_and_load_movable_blocks(blk_loc_registry);
12921294

12931295
// ensure all blocks are placed and that NoC routing has no cycles
1294-
check_initial_placement_legality(block_locs);
1296+
check_initial_placement_legality(blk_loc_registry);
12951297
}

vpr/src/place/move_generators/move_generator.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void MoveGenerator::calculate_reward_and_process_outcome(const MoveOutcomeStats&
5151
}
5252
}
5353

54-
void MoveTypeStat::print_placement_move_types_stats() const {
54+
void MoveTypeStat::print_placement_move_types_stats(const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type) const {
5555
VTR_LOG("\n\nPlacement perturbation distribution by block and move type: \n");
5656

5757
VTR_LOG(
@@ -71,9 +71,9 @@ void MoveTypeStat::print_placement_move_types_stats() const {
7171
int num_of_avail_moves = blk_type_moves.size() / device_ctx.logical_block_types.size();
7272

7373
//Print placement information for each block type
74-
for (const auto& itype : device_ctx.logical_block_types) {
74+
for (const t_logical_block_type& itype : device_ctx.logical_block_types) {
7575
//Skip non-existing block types in the netlist
76-
if (itype.index == 0 || movable_blocks_per_type(itype).empty()) {
76+
if (itype.index == 0 || movable_blocks_per_type[itype.index].empty()) {
7777
continue;
7878
}
7979

vpr/src/place/move_generators/move_generator.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef VPR_MOVE_GENERATOR_H
2-
#define VPR_MOVE_GENERATOR_H
1+
2+
#pragma once
33

44
#include "vpr_types.h"
55
#include "move_utils.h"
@@ -35,9 +35,13 @@ struct MoveTypeStat {
3535
vtr::NdMatrix<int, 2> rejected_moves;
3636

3737
/**
38-
* @brief Prints placement perturbation distribution by block and move type.
38+
* @brief Prints statistics on the distribution of placement perturbations,
39+
* categorized by block type and move type.
40+
* @param movable_blocks_per_type A vector of vectors, where each inner vector contains ClusterBlockIds of
41+
* all movable blocks belonging to a specific logical type. The outer vector
42+
* is indexed by the logical type index.
3943
*/
40-
void print_placement_move_types_stats() const;
44+
void print_placement_move_types_stats(const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type) const;
4145

4246
inline void incr_blk_type_moves(const t_propose_action& proposed_action) {
4347
if (proposed_action.logical_blk_type_index != -1) { //if the agent proposed the block type, then collect the block type stat
@@ -124,7 +128,7 @@ class MoveGenerator {
124128
const PlacerCriticalities* criticalities) = 0;
125129

126130
/**
127-
* @brief Recieves feedback about the outcome of the previously proposed move
131+
* @brief Receives feedback about the outcome of the previously proposed move
128132
*
129133
* This function is very useful for RL agent to get the feedback to the agent
130134
*
@@ -151,5 +155,3 @@ class MoveGenerator {
151155
e_reward_function reward_func_;
152156
vtr::RngContainer& rng_;
153157
};
154-
155-
#endif

0 commit comments

Comments
 (0)