Skip to content

Changed the random number generator in packer to deterministic vtr::irand #2871

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

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
19 changes: 8 additions & 11 deletions vpr/src/pack/greedy_candidate_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

/*
Expand Down Expand Up @@ -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];

Expand Down
7 changes: 5 additions & 2 deletions vpr/src/pack/greedy_candidate_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "cluster_legalizer.h"
#include "physical_types.h"
#include "vtr_vector.h"
#include "vtr_random.h"

// Forward declarations
class AtomNetlist;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions vpr/src/pack/greedy_clusterer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "vpr_context.h"
#include "vtr_math.h"
#include "vtr_vector.h"
#include "vtr_random.h"

namespace {

Expand Down Expand Up @@ -160,6 +161,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,

print_pack_status_header();

vtr::RngContainer rng(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that the only user of this RngContainer object is GreedyCandidateSelector. If this is the case, I think it makes more sense to add it as a member variable to GreedyCandidateSelector and initilize it in its constructor instead of passing it down a deep function call hierarchy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! That would make things much cleaner! Especially since it is an independent object!


// Continue clustering as long as a valid seed is returned from the seed
// selector.
while (seed_mol != nullptr) {
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -239,7 +244,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster(
bool balance_block_type_utilization,
AttractionInfo& attraction_groups,
std::map<t_logical_block_type_ptr, size_t>& 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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion vpr/src/pack/greedy_clusterer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>
#include "cluster_legalizer.h"
#include "physical_types.h"
#include "vtr_random.h"

// Forward declarations
class AtomNetId;
Expand Down Expand Up @@ -156,7 +157,8 @@ class GreedyClusterer {
bool balance_block_type_utilization,
AttractionInfo& attraction_groups,
std::map<t_logical_block_type_ptr, size_t>& 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
Expand Down
Loading