From 53cd616c3ef6b1fae5a34bbdb200c230b621aae2 Mon Sep 17 00:00:00 2001 From: hadyar-c Date: Fri, 24 Jan 2025 03:40:58 -0500 Subject: [PATCH 1/4] Changed the random number generator in packer to deterministic vtr::irand --- vpr/src/pack/greedy_candidate_selector.cpp | 19 ++++++++----------- vpr/src/pack/greedy_candidate_selector.h | 7 +++++-- vpr/src/pack/greedy_clusterer.cpp | 18 +++++++++++++----- vpr/src/pack/greedy_clusterer.h | 4 +++- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/vpr/src/pack/greedy_candidate_selector.cpp b/vpr/src/pack/greedy_candidate_selector.cpp index 1b74a44aa66..208ff9bcc3d 100644 --- a/vpr/src/pack/greedy_candidate_selector.cpp +++ b/vpr/src/pack/greedy_candidate_selector.cpp @@ -17,6 +17,7 @@ #include "vpr_context.h" #include "vpr_types.h" #include "vtr_assert.h" +#include "vtr_random.h" /* * @brief Get gain of packing molecule into current cluster. @@ -518,7 +519,8 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster( LegalizationClusterId cluster_id, const ClusterLegalizer& cluster_legalizer, const Prepacker& prepacker, - AttractionInfo& attraction_groups) { + AttractionInfo& attraction_groups, + vtr::RngContainer& rng) { /* Finds the block with the greatest gain that satisfies the * input, clock and capacity constraints of a cluster that are * passed in. If no suitable block is found it returns nullptr. @@ -597,7 +599,8 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster( cluster_id, prepacker, cluster_legalizer, - attraction_groups); + attraction_groups, + rng); } /* Grab highest gain molecule */ // If this was a vector, this would just be a pop_back. @@ -726,7 +729,8 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou LegalizationClusterId legalization_cluster_id, const Prepacker& prepacker, const ClusterLegalizer& cluster_legalizer, - AttractionInfo& attraction_groups) { + AttractionInfo& attraction_groups, + vtr::RngContainer& rng) { auto cluster_type = cluster_legalizer.get_cluster_type(legalization_cluster_id); /* @@ -779,17 +783,10 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou return; } - int min = 0; int max = num_available_atoms - 1; for (int j = 0; j < attraction_group_num_atoms_threshold_; j++) { - // FIXME: This is a non-deterministic random number generator and it is - // overkill to what this needs to be. Should use vtr::irand which - // would be faster. - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distr(min, max); - int selected_atom = distr(gen); + int selected_atom = rng.irand(max); AtomBlockId blk_id = available_atoms[selected_atom]; diff --git a/vpr/src/pack/greedy_candidate_selector.h b/vpr/src/pack/greedy_candidate_selector.h index 881e41a4224..08e7f11cd47 100644 --- a/vpr/src/pack/greedy_candidate_selector.h +++ b/vpr/src/pack/greedy_candidate_selector.h @@ -16,6 +16,7 @@ #include "cluster_legalizer.h" #include "physical_types.h" #include "vtr_vector.h" +#include "vtr_random.h" // Forward declarations class AtomNetlist; @@ -301,7 +302,8 @@ class GreedyCandidateSelector { LegalizationClusterId cluster_id, const ClusterLegalizer& cluster_legalizer, const Prepacker& prepacker, - AttractionInfo& attraction_groups); + AttractionInfo& attraction_groups, + vtr::RngContainer& rng); /** * @brief Finalize the creation of a cluster. @@ -454,7 +456,8 @@ class GreedyCandidateSelector { LegalizationClusterId legalization_cluster_id, const Prepacker& prepacker, const ClusterLegalizer& cluster_legalizer, - AttractionInfo& attraction_groups); + AttractionInfo& attraction_groups, + vtr::RngContainer& rng); /** * @brief Finds a molecule to propose which is unrelated but may be good to diff --git a/vpr/src/pack/greedy_clusterer.cpp b/vpr/src/pack/greedy_clusterer.cpp index dd2c9fb4551..86650b3a156 100644 --- a/vpr/src/pack/greedy_clusterer.cpp +++ b/vpr/src/pack/greedy_clusterer.cpp @@ -55,6 +55,7 @@ #include "vpr_context.h" #include "vtr_math.h" #include "vtr_vector.h" +#include "vtr_random.h" namespace { @@ -160,6 +161,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, print_pack_status_header(); + vtr::RngContainer rng(0); + // Continue clustering as long as a valid seed is returned from the seed // selector. while (seed_mol != nullptr) { @@ -183,7 +186,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, balance_block_type_utilization, attraction_groups, num_used_type_instances, - mutable_device_ctx); + mutable_device_ctx, + rng); if (!new_cluster_id.is_valid()) { // If the previous strategy failed, try to grow the cluster again, @@ -197,7 +201,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, balance_block_type_utilization, attraction_groups, num_used_type_instances, - mutable_device_ctx); + mutable_device_ctx, + rng); } // Ensure that the seed was packed successfully. @@ -239,7 +244,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( bool balance_block_type_utilization, AttractionInfo& attraction_groups, std::map& num_used_type_instances, - DeviceContext& mutable_device_ctx) { + DeviceContext& mutable_device_ctx, + vtr::RngContainer& rng) { // Check to ensure that this molecule is unclustered. VTR_ASSERT(!cluster_legalizer.is_mol_clustered(seed_mol)); @@ -267,7 +273,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( legalization_cluster_id, cluster_legalizer, prepacker, - attraction_groups); + attraction_groups, + rng); /* * When attraction groups are created, the purpose is to pack more densely by adding more molecules @@ -316,7 +323,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( legalization_cluster_id, cluster_legalizer, prepacker, - attraction_groups); + attraction_groups, + rng); // If the next candidate molecule is the same as the previous // candidate molecule, increment the number of repeated diff --git a/vpr/src/pack/greedy_clusterer.h b/vpr/src/pack/greedy_clusterer.h index eb1dff3afdf..cb5de2186e0 100644 --- a/vpr/src/pack/greedy_clusterer.h +++ b/vpr/src/pack/greedy_clusterer.h @@ -13,6 +13,7 @@ #include #include "cluster_legalizer.h" #include "physical_types.h" +#include "vtr_random.h" // Forward declarations class AtomNetId; @@ -156,7 +157,8 @@ class GreedyClusterer { bool balance_block_type_utilization, AttractionInfo& attraction_groups, std::map& num_used_type_instances, - DeviceContext& mutable_device_ctx); + DeviceContext& mutable_device_ctx, + vtr::RngContainer& rng); /** * @brief Given a seed molecule, starts a new cluster by trying to find a From bf028eff91638bb2e02237f2dfc425c533567d04 Mon Sep 17 00:00:00 2001 From: hadyar-c Date: Fri, 24 Jan 2025 17:08:56 -0500 Subject: [PATCH 2/4] Revert "Changed the random number generator in packer to deterministic vtr::irand" This reverts commit 53cd616c3ef6b1fae5a34bbdb200c230b621aae2. --- vpr/src/pack/greedy_candidate_selector.cpp | 19 +++++++++++-------- vpr/src/pack/greedy_candidate_selector.h | 7 ++----- vpr/src/pack/greedy_clusterer.cpp | 18 +++++------------- vpr/src/pack/greedy_clusterer.h | 4 +--- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/vpr/src/pack/greedy_candidate_selector.cpp b/vpr/src/pack/greedy_candidate_selector.cpp index 208ff9bcc3d..1b74a44aa66 100644 --- a/vpr/src/pack/greedy_candidate_selector.cpp +++ b/vpr/src/pack/greedy_candidate_selector.cpp @@ -17,7 +17,6 @@ #include "vpr_context.h" #include "vpr_types.h" #include "vtr_assert.h" -#include "vtr_random.h" /* * @brief Get gain of packing molecule into current cluster. @@ -519,8 +518,7 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster( LegalizationClusterId cluster_id, const ClusterLegalizer& cluster_legalizer, const Prepacker& prepacker, - AttractionInfo& attraction_groups, - vtr::RngContainer& rng) { + AttractionInfo& attraction_groups) { /* Finds the block with the greatest gain that satisfies the * input, clock and capacity constraints of a cluster that are * passed in. If no suitable block is found it returns nullptr. @@ -599,8 +597,7 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster( cluster_id, prepacker, cluster_legalizer, - attraction_groups, - rng); + attraction_groups); } /* Grab highest gain molecule */ // If this was a vector, this would just be a pop_back. @@ -729,8 +726,7 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou LegalizationClusterId legalization_cluster_id, const Prepacker& prepacker, const ClusterLegalizer& cluster_legalizer, - AttractionInfo& attraction_groups, - vtr::RngContainer& rng) { + AttractionInfo& attraction_groups) { auto cluster_type = cluster_legalizer.get_cluster_type(legalization_cluster_id); /* @@ -783,10 +779,17 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou return; } + int min = 0; int max = num_available_atoms - 1; for (int j = 0; j < attraction_group_num_atoms_threshold_; j++) { - int selected_atom = rng.irand(max); + // FIXME: This is a non-deterministic random number generator and it is + // overkill to what this needs to be. Should use vtr::irand which + // would be faster. + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distr(min, max); + int selected_atom = distr(gen); AtomBlockId blk_id = available_atoms[selected_atom]; diff --git a/vpr/src/pack/greedy_candidate_selector.h b/vpr/src/pack/greedy_candidate_selector.h index 08e7f11cd47..881e41a4224 100644 --- a/vpr/src/pack/greedy_candidate_selector.h +++ b/vpr/src/pack/greedy_candidate_selector.h @@ -16,7 +16,6 @@ #include "cluster_legalizer.h" #include "physical_types.h" #include "vtr_vector.h" -#include "vtr_random.h" // Forward declarations class AtomNetlist; @@ -302,8 +301,7 @@ class GreedyCandidateSelector { LegalizationClusterId cluster_id, const ClusterLegalizer& cluster_legalizer, const Prepacker& prepacker, - AttractionInfo& attraction_groups, - vtr::RngContainer& rng); + AttractionInfo& attraction_groups); /** * @brief Finalize the creation of a cluster. @@ -456,8 +454,7 @@ class GreedyCandidateSelector { LegalizationClusterId legalization_cluster_id, const Prepacker& prepacker, const ClusterLegalizer& cluster_legalizer, - AttractionInfo& attraction_groups, - vtr::RngContainer& rng); + AttractionInfo& attraction_groups); /** * @brief Finds a molecule to propose which is unrelated but may be good to diff --git a/vpr/src/pack/greedy_clusterer.cpp b/vpr/src/pack/greedy_clusterer.cpp index 86650b3a156..dd2c9fb4551 100644 --- a/vpr/src/pack/greedy_clusterer.cpp +++ b/vpr/src/pack/greedy_clusterer.cpp @@ -55,7 +55,6 @@ #include "vpr_context.h" #include "vtr_math.h" #include "vtr_vector.h" -#include "vtr_random.h" namespace { @@ -161,8 +160,6 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, print_pack_status_header(); - vtr::RngContainer rng(0); - // Continue clustering as long as a valid seed is returned from the seed // selector. while (seed_mol != nullptr) { @@ -186,8 +183,7 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, balance_block_type_utilization, attraction_groups, num_used_type_instances, - mutable_device_ctx, - rng); + mutable_device_ctx); if (!new_cluster_id.is_valid()) { // If the previous strategy failed, try to grow the cluster again, @@ -201,8 +197,7 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer, balance_block_type_utilization, attraction_groups, num_used_type_instances, - mutable_device_ctx, - rng); + mutable_device_ctx); } // Ensure that the seed was packed successfully. @@ -244,8 +239,7 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( bool balance_block_type_utilization, AttractionInfo& attraction_groups, std::map& num_used_type_instances, - DeviceContext& mutable_device_ctx, - vtr::RngContainer& rng) { + DeviceContext& mutable_device_ctx) { // Check to ensure that this molecule is unclustered. VTR_ASSERT(!cluster_legalizer.is_mol_clustered(seed_mol)); @@ -273,8 +267,7 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( legalization_cluster_id, cluster_legalizer, prepacker, - attraction_groups, - rng); + attraction_groups); /* * When attraction groups are created, the purpose is to pack more densely by adding more molecules @@ -323,8 +316,7 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster( legalization_cluster_id, cluster_legalizer, prepacker, - attraction_groups, - rng); + attraction_groups); // If the next candidate molecule is the same as the previous // candidate molecule, increment the number of repeated diff --git a/vpr/src/pack/greedy_clusterer.h b/vpr/src/pack/greedy_clusterer.h index cb5de2186e0..eb1dff3afdf 100644 --- a/vpr/src/pack/greedy_clusterer.h +++ b/vpr/src/pack/greedy_clusterer.h @@ -13,7 +13,6 @@ #include #include "cluster_legalizer.h" #include "physical_types.h" -#include "vtr_random.h" // Forward declarations class AtomNetId; @@ -157,8 +156,7 @@ class GreedyClusterer { bool balance_block_type_utilization, AttractionInfo& attraction_groups, std::map& num_used_type_instances, - DeviceContext& mutable_device_ctx, - vtr::RngContainer& rng); + DeviceContext& mutable_device_ctx); /** * @brief Given a seed molecule, starts a new cluster by trying to find a From 71a4c20f94e1f8cad530d6053f61b2cf401cf25c Mon Sep 17 00:00:00 2001 From: hadyar-c Date: Sun, 26 Jan 2025 12:28:14 -0500 Subject: [PATCH 3/4] Changed the rng initialization to be in GreedyCandidateSelector based on reviews. --- vpr/src/pack/greedy_candidate_selector.cpp | 15 ++++----------- vpr/src/pack/greedy_candidate_selector.h | 5 +++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/vpr/src/pack/greedy_candidate_selector.cpp b/vpr/src/pack/greedy_candidate_selector.cpp index 1b74a44aa66..e028e061199 100644 --- a/vpr/src/pack/greedy_candidate_selector.cpp +++ b/vpr/src/pack/greedy_candidate_selector.cpp @@ -77,7 +77,8 @@ GreedyCandidateSelector::GreedyCandidateSelector( is_clock_(is_clock), is_global_(is_global), net_output_feeds_driving_block_input_(net_output_feeds_driving_block_input), - timing_info_(timing_info) { + timing_info_(timing_info), + rng_(0) { // Initialize the list of molecules to pack, the clustering data, and the // net info. @@ -779,17 +780,9 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou return; } - int min = 0; - int max = num_available_atoms - 1; - for (int j = 0; j < attraction_group_num_atoms_threshold_; j++) { - // FIXME: This is a non-deterministic random number generator and it is - // overkill to what this needs to be. Should use vtr::irand which - // would be faster. - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distr(min, max); - int selected_atom = distr(gen); + //Get a random atom between 0 and the number of available atoms - 1 + int selected_atom = rng_.irand(num_available_atoms - 1); AtomBlockId blk_id = available_atoms[selected_atom]; diff --git a/vpr/src/pack/greedy_candidate_selector.h b/vpr/src/pack/greedy_candidate_selector.h index 881e41a4224..b9726356295 100644 --- a/vpr/src/pack/greedy_candidate_selector.h +++ b/vpr/src/pack/greedy_candidate_selector.h @@ -16,6 +16,7 @@ #include "cluster_legalizer.h" #include "physical_types.h" #include "vtr_vector.h" +#include "vtr_random.h" // Forward declarations class AtomNetlist; @@ -516,5 +517,9 @@ class GreedyCandidateSelector { /// @brief A count on the number of unrelated clustering attempts which /// have been performed. int num_unrelated_clustering_attempts_ = 0; + + /// @brief Random number generator to get a random atom between 0 and + /// number of available atoms -1 for attraction group. + vtr::RngContainer rng_; }; From 313753c05e8aeb970b1652f0bf884d0f1f9f4a53 Mon Sep 17 00:00:00 2001 From: hadyar-c Date: Mon, 27 Jan 2025 23:48:05 -0500 Subject: [PATCH 4/4] Made commenting in the header more general based on review. --- vpr/src/pack/greedy_candidate_selector.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vpr/src/pack/greedy_candidate_selector.h b/vpr/src/pack/greedy_candidate_selector.h index b9726356295..6aa140f7d66 100644 --- a/vpr/src/pack/greedy_candidate_selector.h +++ b/vpr/src/pack/greedy_candidate_selector.h @@ -518,8 +518,9 @@ class GreedyCandidateSelector { /// have been performed. int num_unrelated_clustering_attempts_ = 0; - /// @brief Random number generator to get a random atom between 0 and - /// number of available atoms -1 for attraction group. + /// @brief Random number generator used by the clusterer. Currently this + /// is used only when selecting atoms from attraction groups, but + /// could be used for other purposes in the future. vtr::RngContainer rng_; };