Skip to content
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

Remove a few remaning global variables from the placement stage #2872

Merged
merged 10 commits into from
Jan 28, 2025
8 changes: 5 additions & 3 deletions libs/libvtrutil/src/vtr_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*
* Three types of log message types are defined:
* - VTR_LOG : The standard 'info' type log message
* - VTR_LOG_WARN : A warning log message. This represents unusual condition that may indicate an issue but executiom continues
* - 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.
* - VTR_LOG_WARN : A warning log message. This represents an unusual condition that may indicate an issue but execution continues
* - 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 necessary VTR_LOG_ERROR messages are printed.
*
* For example:
*
Expand All @@ -27,7 +29,7 @@
*
* Each of the three message types also have a VTR_LOGV_* variant,
* which will cause the message to be logged if a user-defined condition
* is satisifed.
* is satisfied.
*
* For example:
*
Expand Down
27 changes: 23 additions & 4 deletions vpr/src/base/blk_loc_registry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef VTR_BLK_LOC_REGISTRY_H
#define VTR_BLK_LOC_REGISTRY_H

#pragma once

#include "clustered_netlist_fwd.h"
#include "vtr_vector_map.h"
Expand Down Expand Up @@ -44,7 +44,14 @@ class BlkLocRegistry {
*/
PlaceMacros place_macros_;

/// @brief Stores ClusterBlockId of all movable clustered blocks
/// (blocks that are not locked down to a single location)
std::vector<ClusterBlockId> movable_blocks_;

public:

///@brief Stores ClusterBlockId of all movable clustered blocks of each block type
std::vector<std::vector<ClusterBlockId>> movable_blocks_per_type_;
const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs() const;
vtr::vector_map<ClusterBlockId, t_block_loc>& mutable_block_locs();

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

/// @brief Returns a constant reference to the vector of ClusterBlockIds of all movable clustered blocks.
const std::vector<ClusterBlockId>& movable_blocks() const { return movable_blocks_; }

/// @brief Returns a mutable reference to the vector of ClusterBlockIds of all movable clustered blocks.
std::vector<ClusterBlockId>& mutable_movable_blocks() { return movable_blocks_; }

/// @brief Returns a constant reference to a vector of vectors, where each inner vector contains ClusterBlockIds
/// of movable clustered blocks for a specific block type
const std::vector<std::vector<ClusterBlockId>>& movable_blocks_per_type() const { return movable_blocks_per_type_; }

/// @brief Returns a mutable reference to a vector of vectors, where each inner vector contains ClusterBlockIds
/// of movable clustered blocks for a specific block type.
std::vector<std::vector<ClusterBlockId>>& mutable_movable_blocks_per_type() { return movable_blocks_per_type_; }

/**
* @brief Performs error checking to see if location is legal for block type,
* and sets the location and grid usage of the block if it is legal.
Expand Down Expand Up @@ -149,5 +170,3 @@ class BlkLocRegistry {

e_expected_transaction expected_transaction_;
};

#endif //VTR_BLK_LOC_REGISTRY_H
6 changes: 0 additions & 6 deletions vpr/src/base/vpr_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,6 @@ struct PlacementContext : public Context {
*/
void unlock_loc_vars() { VTR_ASSERT_SAFE(!loc_vars_are_accessible_); loc_vars_are_accessible_ = true; }

///@brief Stores ClusterBlockId of all movable clustered blocks (blocks that are not locked down to a single location)
std::vector<ClusterBlockId> movable_blocks;

///@brief Stores ClusterBlockId of all movable clustered of each block type
std::vector<std::vector<ClusterBlockId>> movable_blocks_per_type;

/**
* @brief Compressed grid space for each block type
*
Expand Down
28 changes: 22 additions & 6 deletions vpr/src/place/RL_agent_util.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "RL_agent_util.h"

#include "static_move_generator.h"
#include "manual_move_generator.h"
#include "placer_state.h"


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

std::vector<int> num_movable_blocks_per_type;
std::ranges::transform(placer_state.blk_loc_registry().movable_blocks_per_type(),
std::back_inserter(num_movable_blocks_per_type),
[](const auto& innerVec) noexcept {
return innerVec.size();
});

if (placer_opts.place_agent_algorithm == e_agent_algorithm::E_GREEDY) {
std::unique_ptr<EpsilonGreedyAgent> karmed_bandit_agent1, karmed_bandit_agent2;
//agent's 1st state
Expand All @@ -68,13 +78,15 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
e_agent_space::MOVE_BLOCK_TYPE,
placer_opts.place_agent_epsilon,
rng);
rng,
num_movable_blocks_per_type);
} else {
VTR_LOG("Using simple RL 'Epsilon Greedy agent' for choosing move types\n");
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(first_state_avail_moves,
e_agent_space::MOVE_TYPE,
placer_opts.place_agent_epsilon,
rng);
rng,
num_movable_blocks_per_type);
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
Expand All @@ -87,7 +99,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
karmed_bandit_agent2 = std::make_unique<EpsilonGreedyAgent>(second_state_avail_moves,
e_agent_space::MOVE_TYPE,
placer_opts.place_agent_epsilon,
rng);
rng,
num_movable_blocks_per_type);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
Expand All @@ -102,12 +115,14 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
VTR_LOG("Using simple RL 'Softmax agent' for choosing move and block types\n");
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
e_agent_space::MOVE_BLOCK_TYPE,
rng);
rng,
num_movable_blocks_per_type);
} else {
VTR_LOG("Using simple RL 'Softmax agent' for choosing move types\n");
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(first_state_avail_moves,
e_agent_space::MOVE_TYPE,
rng);
rng,
num_movable_blocks_per_type);
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.first = std::make_unique<SimpleRLMoveGenerator>(placer_state,
Expand All @@ -119,7 +134,8 @@ std::pair<std::unique_ptr<MoveGenerator>, std::unique_ptr<MoveGenerator>> create
//agent's 2nd state
karmed_bandit_agent2 = std::make_unique<SoftmaxAgent>(second_state_avail_moves,
e_agent_space::MOVE_TYPE,
rng);
rng,
num_movable_blocks_per_type);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generators.second = std::make_unique<SimpleRLMoveGenerator>(placer_state,
reward_fun,
Expand Down
38 changes: 20 additions & 18 deletions vpr/src/place/initial_placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,17 @@ static void place_all_blocks(const t_placer_opts& placer_opts,
* throws an error indicating that initial placement can not be done with the current device size or
* floorplanning constraints.
*/
static void check_initial_placement_legality(const vtr::vector_map<ClusterBlockId, t_block_loc>& block_locs);
static void check_initial_placement_legality(const BlkLocRegistry& blk_loc_registry);

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

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

int unplaced_blocks = 0;

Expand All @@ -271,14 +271,14 @@ static void check_initial_placement_legality(const vtr::vector_map<ClusterBlockI
unplaced_blocks);
}

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

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

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

place_ctx.movable_blocks.clear();
place_ctx.movable_blocks_per_type.clear();
const auto& block_locs = blk_loc_registry.block_locs();
auto& movable_blocks = blk_loc_registry.mutable_movable_blocks();
auto& movable_blocks_per_type = blk_loc_registry.mutable_movable_blocks_per_type();

movable_blocks.clear();
movable_blocks_per_type.clear();

size_t n_logical_blocks = device_ctx.logical_block_types.size();
place_ctx.movable_blocks_per_type.resize(n_logical_blocks);
movable_blocks_per_type.resize(n_logical_blocks);

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

const t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(blk_id);
place_ctx.movable_blocks_per_type[block_type->index].push_back(blk_id);
movable_blocks_per_type[block_type->index].push_back(blk_id);
}
}
}
Expand All @@ -1162,7 +1165,6 @@ void initial_placement(const t_placer_opts& placer_opts,
std::optional<NocCostHandler>& noc_cost_handler,
vtr::RngContainer& rng) {
vtr::ScopedStartFinishTimer timer("Initial Placement");
auto& block_locs = blk_loc_registry.mutable_block_locs();
const auto& place_macros = blk_loc_registry.place_macros();

/* Initialize the grid blocks to empty.
Expand Down Expand Up @@ -1204,8 +1206,8 @@ void initial_placement(const t_placer_opts& placer_opts,
place_all_blocks(placer_opts, block_scores, placer_opts.pad_loc_type, constraints_file, blk_loc_registry, rng);
}

alloc_and_load_movable_blocks(block_locs);
alloc_and_load_movable_blocks(blk_loc_registry);

// ensure all blocks are placed and that NoC routing has no cycles
check_initial_placement_legality(block_locs);
check_initial_placement_legality(blk_loc_registry);
}
6 changes: 3 additions & 3 deletions vpr/src/place/move_generators/move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void MoveGenerator::calculate_reward_and_process_outcome(const MoveOutcomeStats&
}
}

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

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

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

Expand Down
16 changes: 9 additions & 7 deletions vpr/src/place/move_generators/move_generator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef VPR_MOVE_GENERATOR_H
#define VPR_MOVE_GENERATOR_H

#pragma once

#include "vpr_types.h"
#include "move_utils.h"
Expand Down Expand Up @@ -35,9 +35,13 @@ struct MoveTypeStat {
vtr::NdMatrix<int, 2> rejected_moves;

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

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

/**
* @brief Recieves feedback about the outcome of the previously proposed move
* @brief Receives feedback about the outcome of the previously proposed move
*
* This function is very useful for RL agent to get the feedback to the agent
*
Expand All @@ -151,5 +155,3 @@ class MoveGenerator {
e_reward_function reward_func_;
vtr::RngContainer& rng_;
};

#endif
Loading