Skip to content

Commit 53cd616

Browse files
committed
Changed the random number generator in packer to deterministic vtr::irand
1 parent 9761055 commit 53cd616

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

vpr/src/pack/greedy_candidate_selector.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "vpr_context.h"
1818
#include "vpr_types.h"
1919
#include "vtr_assert.h"
20+
#include "vtr_random.h"
2021

2122
/*
2223
* @brief Get gain of packing molecule into current cluster.
@@ -518,7 +519,8 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster(
518519
LegalizationClusterId cluster_id,
519520
const ClusterLegalizer& cluster_legalizer,
520521
const Prepacker& prepacker,
521-
AttractionInfo& attraction_groups) {
522+
AttractionInfo& attraction_groups,
523+
vtr::RngContainer& rng) {
522524
/* Finds the block with the greatest gain that satisfies the
523525
* input, clock and capacity constraints of a cluster that are
524526
* passed in. If no suitable block is found it returns nullptr.
@@ -597,7 +599,8 @@ t_pack_molecule* GreedyCandidateSelector::get_next_candidate_for_cluster(
597599
cluster_id,
598600
prepacker,
599601
cluster_legalizer,
600-
attraction_groups);
602+
attraction_groups,
603+
rng);
601604
}
602605
/* Grab highest gain molecule */
603606
// 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
726729
LegalizationClusterId legalization_cluster_id,
727730
const Prepacker& prepacker,
728731
const ClusterLegalizer& cluster_legalizer,
729-
AttractionInfo& attraction_groups) {
732+
AttractionInfo& attraction_groups,
733+
vtr::RngContainer& rng) {
730734
auto cluster_type = cluster_legalizer.get_cluster_type(legalization_cluster_id);
731735

732736
/*
@@ -779,17 +783,10 @@ void GreedyCandidateSelector::add_cluster_molecule_candidates_by_attraction_grou
779783
return;
780784
}
781785

782-
int min = 0;
783786
int max = num_available_atoms - 1;
784787

785788
for (int j = 0; j < attraction_group_num_atoms_threshold_; j++) {
786-
// FIXME: This is a non-deterministic random number generator and it is
787-
// overkill to what this needs to be. Should use vtr::irand which
788-
// would be faster.
789-
std::random_device rd;
790-
std::mt19937 gen(rd());
791-
std::uniform_int_distribution<> distr(min, max);
792-
int selected_atom = distr(gen);
789+
int selected_atom = rng.irand(max);
793790

794791
AtomBlockId blk_id = available_atoms[selected_atom];
795792

vpr/src/pack/greedy_candidate_selector.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cluster_legalizer.h"
1717
#include "physical_types.h"
1818
#include "vtr_vector.h"
19+
#include "vtr_random.h"
1920

2021
// Forward declarations
2122
class AtomNetlist;
@@ -301,7 +302,8 @@ class GreedyCandidateSelector {
301302
LegalizationClusterId cluster_id,
302303
const ClusterLegalizer& cluster_legalizer,
303304
const Prepacker& prepacker,
304-
AttractionInfo& attraction_groups);
305+
AttractionInfo& attraction_groups,
306+
vtr::RngContainer& rng);
305307

306308
/**
307309
* @brief Finalize the creation of a cluster.
@@ -454,7 +456,8 @@ class GreedyCandidateSelector {
454456
LegalizationClusterId legalization_cluster_id,
455457
const Prepacker& prepacker,
456458
const ClusterLegalizer& cluster_legalizer,
457-
AttractionInfo& attraction_groups);
459+
AttractionInfo& attraction_groups,
460+
vtr::RngContainer& rng);
458461

459462
/**
460463
* @brief Finds a molecule to propose which is unrelated but may be good to

vpr/src/pack/greedy_clusterer.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "vpr_context.h"
5656
#include "vtr_math.h"
5757
#include "vtr_vector.h"
58+
#include "vtr_random.h"
5859

5960
namespace {
6061

@@ -160,6 +161,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,
160161

161162
print_pack_status_header();
162163

164+
vtr::RngContainer rng(0);
165+
163166
// Continue clustering as long as a valid seed is returned from the seed
164167
// selector.
165168
while (seed_mol != nullptr) {
@@ -183,7 +186,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,
183186
balance_block_type_utilization,
184187
attraction_groups,
185188
num_used_type_instances,
186-
mutable_device_ctx);
189+
mutable_device_ctx,
190+
rng);
187191

188192
if (!new_cluster_id.is_valid()) {
189193
// If the previous strategy failed, try to grow the cluster again,
@@ -197,7 +201,8 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,
197201
balance_block_type_utilization,
198202
attraction_groups,
199203
num_used_type_instances,
200-
mutable_device_ctx);
204+
mutable_device_ctx,
205+
rng);
201206
}
202207

203208
// Ensure that the seed was packed successfully.
@@ -239,7 +244,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster(
239244
bool balance_block_type_utilization,
240245
AttractionInfo& attraction_groups,
241246
std::map<t_logical_block_type_ptr, size_t>& num_used_type_instances,
242-
DeviceContext& mutable_device_ctx) {
247+
DeviceContext& mutable_device_ctx,
248+
vtr::RngContainer& rng) {
243249

244250
// Check to ensure that this molecule is unclustered.
245251
VTR_ASSERT(!cluster_legalizer.is_mol_clustered(seed_mol));
@@ -267,7 +273,8 @@ LegalizationClusterId GreedyClusterer::try_grow_cluster(
267273
legalization_cluster_id,
268274
cluster_legalizer,
269275
prepacker,
270-
attraction_groups);
276+
attraction_groups,
277+
rng);
271278

272279
/*
273280
* 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(
316323
legalization_cluster_id,
317324
cluster_legalizer,
318325
prepacker,
319-
attraction_groups);
326+
attraction_groups,
327+
rng);
320328

321329
// If the next candidate molecule is the same as the previous
322330
// candidate molecule, increment the number of repeated

vpr/src/pack/greedy_clusterer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <vector>
1414
#include "cluster_legalizer.h"
1515
#include "physical_types.h"
16+
#include "vtr_random.h"
1617

1718
// Forward declarations
1819
class AtomNetId;
@@ -156,7 +157,8 @@ class GreedyClusterer {
156157
bool balance_block_type_utilization,
157158
AttractionInfo& attraction_groups,
158159
std::map<t_logical_block_type_ptr, size_t>& num_used_type_instances,
159-
DeviceContext& mutable_device_ctx);
160+
DeviceContext& mutable_device_ctx,
161+
vtr::RngContainer& rng);
160162

161163
/**
162164
* @brief Given a seed molecule, starts a new cluster by trying to find a

0 commit comments

Comments
 (0)