Skip to content

Commit

Permalink
Merge branch 'master' into tileable_rr_graph_write
Browse files Browse the repository at this point in the history
  • Loading branch information
saaramahmoudi authored Sep 1, 2023
2 parents ed36ab2 + b7a5c77 commit e853ae3
Show file tree
Hide file tree
Showing 35 changed files with 414 additions and 467 deletions.
23 changes: 15 additions & 8 deletions vpr/src/base/clustered_netlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "vtr_assert.h"
#include "vpr_error.h"

#include <utility>

/**
* @file
* @brief ClusteredNetlist Class Implementation
*/

ClusteredNetlist::ClusteredNetlist(std::string name, std::string id)
: Netlist<ClusterBlockId, ClusterPortId, ClusterPinId, ClusterNetId>(name, id) {}
: Netlist<ClusterBlockId, ClusterPortId, ClusterPinId, ClusterNetId>(std::move(name), std::move(id)) {}

/*
*
Expand All @@ -28,11 +30,16 @@ t_logical_block_type_ptr ClusteredNetlist::block_type(const ClusterBlockId id) c
return block_types_[id];
}

std::vector<ClusterBlockId> ClusteredNetlist::blocks_per_type(const t_logical_block_type blk_type) const {
const std::vector<ClusterBlockId>& ClusteredNetlist::blocks_per_type(const t_logical_block_type& blk_type) const {
// empty vector is declared static to avoid re-allocation every time the function is called
static std::vector<ClusterBlockId> empty_vector;
if (blocks_per_type_.count(blk_type.index) == 0) {
std::vector<ClusterBlockId> empty_vector;
return empty_vector;
}

// the vector is returned as const reference to avoid unnecessary copies,
// especially that returned vectors may be very large as they contain
// all clustered blocks with a specific block type
return blocks_per_type_.at(blk_type.index);
}

Expand Down Expand Up @@ -132,7 +139,7 @@ ClusterBlockId ClusteredNetlist::create_block(const char* name, t_pb* pb, t_logi
return blk_id;
}

ClusterPortId ClusteredNetlist::create_port(const ClusterBlockId blk_id, const std::string name, BitIndex width, PortType type) {
ClusterPortId ClusteredNetlist::create_port(const ClusterBlockId blk_id, const std::string& name, BitIndex width, PortType type) {
ClusterPortId port_id = find_port(blk_id, name);
if (!port_id) {
port_id = Netlist::create_port(blk_id, name, width, type);
Expand Down Expand Up @@ -163,7 +170,7 @@ ClusterPinId ClusteredNetlist::create_pin(const ClusterPortId port_id, BitIndex
return pin_id;
}

ClusterNetId ClusteredNetlist::create_net(const std::string name) {
ClusterNetId ClusteredNetlist::create_net(const std::string& name) {
//Check if the net has already been created
StringId name_id = create_string(name);
ClusterNetId net_id = find_net(name_id);
Expand Down Expand Up @@ -292,9 +299,9 @@ ClusterBlockId ClusteredNetlist::find_block_by_name_fragment(const std::string&
ClusterBlockId blk_id = ClusterBlockId::INVALID();
std::regex name_to_match(name_pattern);

for (auto compatible_block_id = cluster_block_candidates.begin(); compatible_block_id != cluster_block_candidates.end(); compatible_block_id++) {
if (std::regex_match(Netlist::block_name(*compatible_block_id), name_to_match)) {
blk_id = *compatible_block_id;
for (auto cluster_block_candidate : cluster_block_candidates) {
if (std::regex_match(Netlist::block_name(cluster_block_candidate), name_to_match)) {
blk_id = cluster_block_candidate;
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions vpr/src/base/clustered_netlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ClusteredNetlist : public Netlist<ClusterBlockId, ClusterPortId, ClusterPi
t_logical_block_type_ptr block_type(const ClusterBlockId id) const;

///@brief Returns the blocks with the specific block types in the netlist
std::vector<ClusterBlockId> blocks_per_type(const t_logical_block_type blk_type) const;
const std::vector<ClusterBlockId>& blocks_per_type(const t_logical_block_type& blk_type) const;

///@brief Returns the net of the block attached to the specific pin index
ClusterNetId block_net(const ClusterBlockId blk_id, const int pin_index) const;
Expand Down Expand Up @@ -194,7 +194,7 @@ class ClusteredNetlist : public Netlist<ClusterBlockId, ClusterPortId, ClusterPi
* @param width The width (number of bits) of the port
* @param type The type of the port (INPUT, OUTPUT, or CLOCK)
*/
ClusterPortId create_port(const ClusterBlockId blk_id, const std::string name, BitIndex width, PortType type);
ClusterPortId create_port(const ClusterBlockId blk_id, const std::string& name, BitIndex width, PortType type);
/**
* @brief Create or return an existing pin in the netlist
*
Expand All @@ -212,7 +212,7 @@ class ClusteredNetlist : public Netlist<ClusterBlockId, ClusterPortId, ClusterPi
*
* @param name The unique name of the net
*/
ClusterNetId create_net(const std::string name);
ClusterNetId create_net(const std::string& name);

/**
* @brief Given a name of a block and vector of possible cluster blocks
Expand Down
8 changes: 4 additions & 4 deletions vpr/src/base/read_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ struct ParsePlaceAgentSpace {
ConvertedValue<e_agent_space> from_str(std::string str) {
ConvertedValue<e_agent_space> conv_value;
if (str == "move_type")
conv_value.set_value(MOVE_TYPE);
conv_value.set_value(e_agent_space::MOVE_TYPE);
else if (str == "move_block_type")
conv_value.set_value(MOVE_BLOCK_TYPE);
conv_value.set_value(e_agent_space::MOVE_BLOCK_TYPE);
else {
std::stringstream msg;
msg << "Invalid conversion from '" << str << "' to e_agent_space (expected one of: " << argparse::join(default_choices(), ", ") << ")";
Expand All @@ -478,10 +478,10 @@ struct ParsePlaceAgentSpace {

ConvertedValue<std::string> to_str(e_agent_space val) {
ConvertedValue<std::string> conv_value;
if (val == MOVE_TYPE)
if (val == e_agent_space::MOVE_TYPE)
conv_value.set_value("move_type");
else {
VTR_ASSERT(val == MOVE_BLOCK_TYPE);
VTR_ASSERT(val == e_agent_space::MOVE_BLOCK_TYPE);
conv_value.set_value("move_block_type");
}
return conv_value;
Expand Down
13 changes: 0 additions & 13 deletions vpr/src/base/vpr_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,6 @@ struct PlacementContext : public Context {
* Used for unique identification and consistency checking
*/
std::string placement_id;

/**
* @brief Map physical block type to RL-agent block type
*
* RL-agent block types are the physical block types that are used in the netlist (at least one logical block in the netlist maps to).
* As an example:
* Having physical block types (EMPTY, LAB, DSP, IO),
* agent block types would be (LAB,IO) if netlist doesn't contain DSP blocks.
* Key : physical (agent) block type index
* Value : agent (physical) block type index
*/
std::unordered_map<int, int> phys_blk_type_to_agent_blk_type_map;
std::unordered_map<int, int> agent_blk_type_to_phys_blk_type_map;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/base/vpr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ enum e_agent_algorithm {
* can be based on (block_type, move_type) pair.
*
*/
enum e_agent_space {
enum class e_agent_space {
MOVE_TYPE,
MOVE_BLOCK_TYPE
};
Expand Down
18 changes: 6 additions & 12 deletions vpr/src/draw/manual_moves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#ifndef NO_GRAPHICS

void draw_manual_moves_window(std::string block_id) {
void draw_manual_moves_window(const std::string& block_id) {
t_draw_state* draw_state = get_draw_state_vars();

if (!draw_state->manual_moves_state.manual_move_window_is_open) {
Expand Down Expand Up @@ -249,7 +249,7 @@ void manual_move_cost_summary_dialog() {
//If the user accepts the manual move
case GTK_RESPONSE_ACCEPT:
draw_state->manual_moves_state.manual_move_info.user_move_outcome = ACCEPTED;
application.update_message(msg.c_str());
application.update_message(msg);
break;
//If the user rejects the manual move
case GTK_RESPONSE_REJECT:
Expand Down Expand Up @@ -282,14 +282,8 @@ void close_manual_moves_window() {
draw_state->manual_moves_state.manual_move_window_is_open = false;
}

bool string_is_a_number(std::string block_id) {
for (size_t i = 0; i < block_id.size(); i++) {
//Returns 0 if the string does not have characters from 0-9
if (isdigit(block_id[i]) == 0) {
return false;
}
}
return true;
bool string_is_a_number(const std::string& block_id) {
return std::all_of(block_id.begin(), block_id.end(), isdigit);
}

//Updates ManualMovesInfo cost and placer move outcome variables. User_move_outcome is also updated.
Expand All @@ -311,8 +305,8 @@ e_create_move manual_move_display_and_propose(ManualMoveGenerator& manual_move_g
draw_manual_moves_window("");
update_screen(ScreenUpdatePriority::MAJOR, " ", PLACEMENT, nullptr);
move_type = e_move_type::MANUAL_MOVE;
t_logical_block_type blk_type; //no need to specify block type in manual move "propose_move" function
return manual_move_generator.propose_move(blocks_affected, move_type, blk_type, rlim, placer_opts, criticalities);
t_propose_action proposed_action{move_type, -1}; //no need to specify block type in manual move "propose_move" function
return manual_move_generator.propose_move(blocks_affected, proposed_action, rlim, placer_opts, criticalities);
}

#endif /*NO_GRAPHICS*/
4 changes: 2 additions & 2 deletions vpr/src/draw/manual_moves.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool manual_move_is_selected();
* Window prompts the user for input: block id/name used as the from block in the move generator, x position, y position, and subtile position.
* @param block_id: The block id is passed in if the user decides to highlight the block in the UI. If the user decides to manually input the block ID in the manual move window, the string will be empty and the block ID will later be assigned to ManualMovesState struct.
*/
void draw_manual_moves_window(std::string block_id);
void draw_manual_moves_window(const std::string& block_id);

/**
* @brief Evaluates if the user input is valid and allowed.
Expand Down Expand Up @@ -139,7 +139,7 @@ void close_manual_moves_window();
*
* @return True if the string only contains numbers, false otherwise.
*/
bool string_is_a_number(std::string block_id);
bool string_is_a_number(const std::string& block_id);

/**
* @brief Updates the ManualMovesState variable members.
Expand Down
37 changes: 10 additions & 27 deletions vpr/src/place/RL_agent_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#include "manual_move_generator.h"

void create_move_generators(std::unique_ptr<MoveGenerator>& move_generator, std::unique_ptr<MoveGenerator>& move_generator2, const t_placer_opts& placer_opts, int move_lim) {
//extract available physical block types in the netlist
determine_agent_block_types();

if (placer_opts.RL_agent_placement == false) {
if (placer_opts.place_algorithm.is_timing_driven()) {
VTR_LOG("Using static probabilities for choosing each move type\n");
Expand Down Expand Up @@ -45,7 +42,6 @@ void create_move_generators(std::unique_ptr<MoveGenerator>& move_generator, std:
* only move type. *
* This state is activated late in the anneal and in the Quench */

auto& place_ctx = g_vpr_ctx.placement();
int num_1st_state_avail_moves = placer_opts.place_algorithm.is_timing_driven() ? NUM_PL_1ST_STATE_MOVE_TYPES : NUM_PL_NONTIMING_MOVE_TYPES;
int num_2nd_state_avail_moves = placer_opts.place_algorithm.is_timing_driven() ? NUM_PL_MOVE_TYPES : NUM_PL_NONTIMING_MOVE_TYPES;

Expand All @@ -55,17 +51,20 @@ void create_move_generators(std::unique_ptr<MoveGenerator>& move_generator, std:
if (placer_opts.place_agent_space == e_agent_space::MOVE_BLOCK_TYPE) {
VTR_LOG("Using simple RL 'Epsilon Greedy agent' for choosing move and block types\n");
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(num_1st_state_avail_moves,
place_ctx.agent_blk_type_to_phys_blk_type_map.size(),
e_agent_space::MOVE_BLOCK_TYPE,
placer_opts.place_agent_epsilon);
} else {
VTR_LOG("Using simple RL 'Epsilon Greedy agent' for choosing move types\n");
karmed_bandit_agent1 = std::make_unique<EpsilonGreedyAgent>(num_1st_state_avail_moves,
e_agent_space::MOVE_TYPE,
placer_opts.place_agent_epsilon);
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generator = std::make_unique<SimpleRLMoveGenerator>(karmed_bandit_agent1);
//agent's 2nd state
karmed_bandit_agent2 = std::make_unique<EpsilonGreedyAgent>(num_2nd_state_avail_moves, placer_opts.place_agent_epsilon);
karmed_bandit_agent2 = std::make_unique<EpsilonGreedyAgent>(num_2nd_state_avail_moves,
e_agent_space::MOVE_TYPE,
placer_opts.place_agent_epsilon);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generator2 = std::make_unique<SimpleRLMoveGenerator>(karmed_bandit_agent2);
} else {
Expand All @@ -74,15 +73,17 @@ void create_move_generators(std::unique_ptr<MoveGenerator>& move_generator, std:
if (placer_opts.place_agent_space == e_agent_space::MOVE_BLOCK_TYPE) {
VTR_LOG("Using simple RL 'Softmax agent' for choosing move and block types\n");
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(num_1st_state_avail_moves,
place_ctx.agent_blk_type_to_phys_blk_type_map.size());
e_agent_space::MOVE_BLOCK_TYPE);
} else {
VTR_LOG("Using simple RL 'Softmax agent' for choosing move types\n");
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(num_1st_state_avail_moves);
karmed_bandit_agent1 = std::make_unique<SoftmaxAgent>(num_1st_state_avail_moves,
e_agent_space::MOVE_TYPE);
}
karmed_bandit_agent1->set_step(placer_opts.place_agent_gamma, move_lim);
move_generator = std::make_unique<SimpleRLMoveGenerator>(karmed_bandit_agent1);
//agent's 2nd state
karmed_bandit_agent2 = std::make_unique<SoftmaxAgent>(num_2nd_state_avail_moves);
karmed_bandit_agent2 = std::make_unique<SoftmaxAgent>(num_2nd_state_avail_moves,
e_agent_space::MOVE_TYPE);
karmed_bandit_agent2->set_step(placer_opts.place_agent_gamma, move_lim);
move_generator2 = std::make_unique<SimpleRLMoveGenerator>(karmed_bandit_agent2);
}
Expand Down Expand Up @@ -115,22 +116,4 @@ void update_move_generator(std::unique_ptr<MoveGenerator>& move_generator, std::
else
move_generator2 = std::move(current_move_generator);
}
}

void determine_agent_block_types() {
//Loop through all available logical block types and store the ones that exist in the netlist
auto& device_ctx = g_vpr_ctx.device();
auto& cluster_ctx = g_vpr_ctx.clustering();
auto& place_ctx = g_vpr_ctx.mutable_placement();
int agent_type_index = 0;
for (auto itype : device_ctx.logical_block_types) {
if (itype.index == 0) //ignore empty type
continue;
auto blk_per_type = cluster_ctx.clb_nlist.blocks_per_type(itype);
if (blk_per_type.size() != 0) {
place_ctx.phys_blk_type_to_agent_blk_type_map.insert(std::pair<int, int>(agent_type_index, itype.index));
place_ctx.agent_blk_type_to_phys_blk_type_map.insert(std::pair<int, int>(itype.index, agent_type_index));
agent_type_index++;
}
}
}
6 changes: 0 additions & 6 deletions vpr/src/place/RL_agent_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@ void assign_current_move_generator(std::unique_ptr<MoveGenerator>& move_generato
* @ brief move the updated current_move_generator to its original move_Generator structure based on he placer_options and the agent state
*/
void update_move_generator(std::unique_ptr<MoveGenerator>& move_generator, std::unique_ptr<MoveGenerator>& move_generator2, e_agent_state agent_state, const t_placer_opts& placer_opts, bool in_quench, std::unique_ptr<MoveGenerator>& current_move_generator);

/**
* @ brief determine which block types used by the netlist and create a map between physical block types and agent block types (the ones that are used in the netlist)
*/
void determine_agent_block_types();

#endif
13 changes: 6 additions & 7 deletions vpr/src/place/centroid_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "place_constraints.h"
#include "move_utils.h"

e_create_move CentroidMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected, e_move_type& /*move_type*/, t_logical_block_type& blk_type, float rlim, const t_placer_opts& placer_opts, const PlacerCriticalities* /*criticalities*/) {
e_create_move CentroidMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected, t_propose_action& proposed_action, float rlim, const t_placer_opts& placer_opts, const PlacerCriticalities* /*criticalities*/) {
//Find a movable block based on blk_type
ClusterBlockId b_from = propose_block_to_move(blk_type, false, NULL, NULL);
ClusterBlockId b_from = propose_block_to_move(proposed_action.logical_blk_type_index, false, nullptr, nullptr);

if (!b_from) { //No movable block found
return e_create_move::ABORT;
Expand All @@ -23,15 +23,14 @@ e_create_move CentroidMoveGenerator::propose_move(t_pl_blocks_to_be_moved& block
auto grid_from_type = device_ctx.grid.get_physical_type({from.x, from.y, from.layer});
VTR_ASSERT(is_tile_compatible(grid_from_type, cluster_from_type));

t_range_limiters range_limiters;
range_limiters.original_rlim = rlim;
range_limiters.dm_rlim = placer_opts.place_dm_rlim;
range_limiters.first_rlim = place_move_ctx.first_rlim;
t_range_limiters range_limiters{rlim,
place_move_ctx.first_rlim,
placer_opts.place_dm_rlim};

t_pl_loc to, centroid;

/* Calculate the centroid location*/
calculate_centroid_loc(b_from, false, centroid, NULL);
calculate_centroid_loc(b_from, false, centroid, nullptr);

/* Find a location near the weighted centroid_loc */
if (!find_to_loc_centroid(cluster_from_type, from, centroid, range_limiters, to, b_from)) {
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/place/centroid_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Returns its choices by filling in affected_blocks.
*/
class CentroidMoveGenerator : public MoveGenerator {
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected, e_move_type& /*move_type*/, t_logical_block_type& blk_type, float rlim, const t_placer_opts& placer_opts, const PlacerCriticalities* /*criticalities*/);
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected, t_propose_action& proposed_action, float rlim, const t_placer_opts& placer_opts, const PlacerCriticalities* /*criticalities*/) override;
};

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

e_create_move CriticalUniformMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected, e_move_type& /*move_type*/, t_logical_block_type& blk_type, float rlim, const t_placer_opts& /*placer_opts*/, const PlacerCriticalities* /*criticalities*/) {
e_create_move CriticalUniformMoveGenerator::propose_move(t_pl_blocks_to_be_moved& blocks_affected, t_propose_action& proposed_action, float rlim, const t_placer_opts& /*placer_opts*/, const PlacerCriticalities* /*criticalities*/) {
ClusterNetId net_from;
int pin_from;
//Find a movable block based on blk_type
ClusterBlockId b_from = propose_block_to_move(blk_type, true, &net_from, &pin_from);
ClusterBlockId b_from = propose_block_to_move(proposed_action.logical_blk_type_index, true, &net_from, &pin_from);

auto& place_ctx = g_vpr_ctx.placement();
auto& cluster_ctx = g_vpr_ctx.clustering();
Expand Down
Loading

0 comments on commit e853ae3

Please sign in to comment.