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

NetCostHandler class #2676

Merged
merged 43 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4927620
create NetCostHandler class
soheilshahrouz Aug 5, 2024
5bd6612
add public methods for NetCostHandler
soheilshahrouz Aug 5, 2024
94c2dad
remove place_cost_exp argument from alloc_and_load_placement_structs()
soheilshahrouz Aug 5, 2024
5651cbe
move some static functions from net_cost_handler.cpp to NetCostHandle…
soheilshahrouz Aug 5, 2024
57cf345
remove direct calls to comp_layer_bb_cost
soheilshahrouz Aug 5, 2024
78e7dba
remove static ts_info from net_cost_handler
soheilshahrouz Aug 5, 2024
f30780e
remove NetCostHandler::free_try_swap_net_cost_structs()
soheilshahrouz Aug 5, 2024
22c4ea8
remove static pl_net_cost from net_cost_handler.cpp
soheilshahrouz Aug 5, 2024
45a4af4
remove static chanx_place_cost_fac and chany_place_cost_fac from net_…
soheilshahrouz Aug 5, 2024
72ebdab
remove alloc_and_load_try_swap_structs()
soheilshahrouz Aug 6, 2024
6fa0520
added some comments
soheilshahrouz Aug 6, 2024
cd005be
remove local vectors in get_non_updatable_layer_bb_() and get_layer_b…
soheilshahrouz Aug 6, 2024
3f70ff2
add a private const t_placer_opts& placer_opts_ to NetCostHandler
soheilshahrouz Aug 6, 2024
dc9c018
remove args of type t_place_algorithm and t_placer_opts
soheilshahrouz Aug 6, 2024
1deb8b0
added update_bb_functor_
soheilshahrouz Aug 6, 2024
a37e82a
remove dead code
soheilshahrouz Aug 7, 2024
1758de3
Merge branch 'master' into temp_net_cost_ref
vaughnbetz Aug 15, 2024
59c2c71
change the order of args in t_2D_bb constructor to be consistent with…
soheilshahrouz Aug 21, 2024
0cb04be
Merge remote-tracking branch 'origin/master' into temp_net_cost_ref
soheilshahrouz Aug 21, 2024
97d0813
add reward_function_ to MoveGenerator
soheilshahrouz Aug 21, 2024
6530b8b
moved calculate_reward_and_process_outcome() to MoveGenerator
soheilshahrouz Aug 21, 2024
70f0821
don't use unique_ptr for manual_move_generator
soheilshahrouz Aug 21, 2024
01358fa
fixed failing assertion for UNDEFINED_REWARD
soheilshahrouz Aug 24, 2024
3033773
call std::max and std::min instead of using std::max and std::min
soheilshahrouz Aug 26, 2024
c710d8e
remove free_try_swap_structs() and free_try_swap_arrays()
soheilshahrouz Aug 26, 2024
12093d6
move print_placement_move_types_stats() to move_generator.cpp
soheilshahrouz Aug 26, 2024
0b7bfa7
add some comments
soheilshahrouz Sep 3, 2024
7f476f9
move driven_by_moved_block() from net_cost_handler to t_pl_blocks_to_…
soheilshahrouz Sep 3, 2024
e233dab
renamed some methods to avoid duplicate names
soheilshahrouz Sep 3, 2024
a2e8b4a
remove unused header includes
soheilshahrouz Sep 3, 2024
845c4cb
applied PR comments
soheilshahrouz Sep 4, 2024
d2e9d85
revert the order of args in t_2D_bb's constructor
soheilshahrouz Sep 4, 2024
5f90e53
Merge branch 'master' into temp_net_cost_ref
soheilshahrouz Sep 9, 2024
881cf15
Merge branch 'master' into temp_net_cost_ref
soheilshahrouz Sep 15, 2024
9618d53
fix typos and add comments
soheilshahrouz Sep 16, 2024
4598234
move apply_move_blocks(), commit_move_blocks(), and revert_move_block…
soheilshahrouz Sep 16, 2024
521c9e3
apply PR comments
soheilshahrouz Sep 16, 2024
ef8007f
add get_net_bb_cost_functor_ and get_non_updatable_bb_functor_
soheilshahrouz Sep 16, 2024
8bf5d2d
add comments and address compilation warnings
soheilshahrouz Sep 16, 2024
68e489f
add comments for use_ts arguments
soheilshahrouz Sep 16, 2024
074b4c0
Merge branch 'master' into temp_net_cost_ref
soheilshahrouz Sep 16, 2024
9e97e35
Merge branch 'master' into temp_net_cost_ref
soheilshahrouz Sep 21, 2024
5576193
Merge branch 'master' into temp_net_cost_ref
soheilshahrouz Sep 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 release 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
6 changes: 3 additions & 3 deletions vpr/src/base/vpr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,19 +618,19 @@ struct t_bb {
*/
struct t_2D_bb {
t_2D_bb() = default;
t_2D_bb(int xmin_, int xmax_, int ymin_, int ymax_, int layer_num_)
t_2D_bb(int xmin_, int ymin_, int xmax_, int ymax_, int layer_num_)
soheilshahrouz marked this conversation as resolved.
Show resolved Hide resolved
: xmin(xmin_)
, xmax(xmax_)
, ymin(ymin_)
, xmax(xmax_)
, ymax(ymax_)
, layer_num(layer_num_) {
VTR_ASSERT(xmax_ >= xmin_);
VTR_ASSERT(ymax_ >= ymin_);
VTR_ASSERT(layer_num_ >= 0);
}
int xmin = OPEN;
int xmax = OPEN;
int ymin = OPEN;
int xmax = OPEN;
int ymax = OPEN;
int layer_num = 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
4 changes: 3 additions & 1 deletion vpr/src/place/centroid_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class CentroidMoveGenerator : public MoveGenerator {
* @param placer_state A mutable reference to the placement state which will
* be stored in this object.
*/
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 +46,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
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
5 changes: 3 additions & 2 deletions vpr/src/place/median_move_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

#include <algorithm>

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

e_create_move MedianMoveGenerator::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/median_move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
class MedianMoveGenerator : public MoveGenerator {
public:
MedianMoveGenerator() = delete;
explicit MedianMoveGenerator(PlacerState& placer_state);
MedianMoveGenerator(PlacerState& placer_state,
e_reward_function reward_function);

private:
e_create_move propose_move(t_pl_blocks_to_be_moved& blocks_affected,
Expand Down
85 changes: 85 additions & 0 deletions vpr/src/place/move_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

#include "move_generator.h"

#include "vpr_error.h"

soheilshahrouz marked this conversation as resolved.
Show resolved Hide resolved
void MoveGenerator::calculate_reward_and_process_outcome(const MoveOutcomeStats& move_outcome_stats,
double delta_c,
float timing_bb_factor) {
if (reward_func_ == e_reward_function::BASIC) {
soheilshahrouz marked this conversation as resolved.
Show resolved Hide resolved
process_outcome(-1 * delta_c, reward_func_);
} else if (reward_func_ == e_reward_function::NON_PENALIZING_BASIC || reward_func_ == e_reward_function::RUNTIME_AWARE) {
if (delta_c < 0) {
process_outcome(-1 * delta_c, reward_func_);
} else {
process_outcome(0, reward_func_);
}
} else if (reward_func_ == e_reward_function::WL_BIASED_RUNTIME_AWARE) {
if (delta_c < 0) {
float reward = -1
* (move_outcome_stats.delta_cost_norm
+ (0.5 - timing_bb_factor)
* move_outcome_stats.delta_timing_cost_norm
+ timing_bb_factor
* move_outcome_stats.delta_bb_cost_norm);
process_outcome(reward, reward_func_);
} else {
process_outcome(0, reward_func_);
}
} else {
VTR_ASSERT_SAFE(reward_func_ == e_reward_function::UNDEFINED_REWARD);
VPR_ERROR(VPR_ERROR_PLACE, "Undefined reward function!\n");
}
}

void MoveTypeStat::print_placement_move_types_stats() {
VTR_LOG("\n\nPlacement perturbation distribution by block and move type: \n");

VTR_LOG(
"------------------ ----------------- ---------------- ---------------- --------------- ------------ \n");
VTR_LOG(
" Block Type Move Type (%%) of Total Accepted(%%) Rejected(%%) Aborted(%%)\n");
VTR_LOG(
soheilshahrouz marked this conversation as resolved.
Show resolved Hide resolved
"------------------ ----------------- ---------------- ---------------- --------------- ------------ \n");

int total_moves = 0;
for (size_t i = 0; i < blk_type_moves.size(); ++i) {
total_moves += blk_type_moves.get(i);
}

auto& device_ctx = g_vpr_ctx.device();
int count = 0;
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) {
//Skip non-existing block types in the netlist
if (itype.index == 0 || movable_blocks_per_type(itype).empty()) {
continue;
}

count = 0;
for (int imove = 0; imove < num_of_avail_moves; imove++) {
const auto& move_name = move_type_to_string(e_move_type(imove));
int moves = blk_type_moves[itype.index][imove];
if (moves != 0) {
int accepted = accepted_moves[itype.index][imove];
int rejected = rejected_moves[itype.index][imove];
int aborted = moves - (accepted + rejected);
if (count == 0) {
VTR_LOG("%-18.20s", itype.name);
} else {
VTR_LOG(" ");
}
VTR_LOG(
" %-22.20s %-16.2f %-15.2f %-14.2f %-13.2f\n",
move_name.c_str(), 100.0f * (float)moves / (float)total_moves,
100.0f * (float)accepted / (float)moves, 100.0f * (float)rejected / (float)moves,
100.0f * (float)aborted / (float)moves);
}
count++;
}
VTR_LOG("\n");
}
VTR_LOG("\n");
}
27 changes: 24 additions & 3 deletions vpr/src/place/move_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,27 @@ struct MoveTypeStat {
vtr::NdMatrix<int, 2> blk_type_moves;
vtr::NdMatrix<int, 2> accepted_moves;
vtr::NdMatrix<int, 2> rejected_moves;

/**
* @brief Prints placement perturbation distribution by block and move type.
*/
void print_placement_move_types_stats();
};

/**
* @brief a base class for move generators
*
* This class represents the base class for all move generators.
* All its functions are virtual functions.
*/
class MoveGenerator {
public:
MoveGenerator(PlacerState& placer_state)
: placer_state_(placer_state) {}
MoveGenerator(PlacerState& placer_state, e_reward_function reward_function)
: placer_state_(placer_state)
, reward_func_(reward_function) {}

MoveGenerator() = delete;
MoveGenerator(const MoveGenerator&) = delete;
MoveGenerator& operator=(const MoveGenerator&) = delete;
virtual ~MoveGenerator() = default;

/**
Expand Down Expand Up @@ -81,8 +88,22 @@ class MoveGenerator {
*/
virtual void process_outcome(double /*reward*/, e_reward_function /*reward_fun*/) {}

/**
* @brief Calculates the agent's reward and the total process outcome
*
* @param move_outcome_stats Contains information about how much each cost term
* changes by this move and whether the move is accepted.
* @param delta_c The total change in cost by this move.
* @param timing_bb_factor This factor controls the weight of bb cost
* compared to the timing cost in the agent's reward function.
*/
void calculate_reward_and_process_outcome(const MoveOutcomeStats& move_outcome_stats,
double delta_c,
float timing_bb_factor);

protected:
std::reference_wrapper<PlacerState> placer_state_;
e_reward_function reward_func_;
};

#endif
16 changes: 16 additions & 0 deletions vpr/src/place/move_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,19 @@ void t_pl_blocks_to_be_moved::clear_move_blocks() {

affected_pins.clear();
}

bool t_pl_blocks_to_be_moved::driven_by_moved_block(const ClusterNetId net) const {
auto& clb_nlist = g_vpr_ctx.clustering().clb_nlist;

bool is_driven_by_move_blk = false;
ClusterBlockId net_driver_block = clb_nlist.net_driver_block(net);

for (const t_pl_moved_block& block : moved_blocks) {
if (net_driver_block == block.block_num) {
is_driven_by_move_blk = true;
break;
}
}

return is_driven_by_move_blk;
}
Loading
Loading