Skip to content

Commit e726839

Browse files
Change GreedySeedSelector to work with molecules instead of atoms
1 parent 04183ba commit e726839

File tree

3 files changed

+53
-50
lines changed

3 files changed

+53
-50
lines changed

vpr/src/pack/greedy_clusterer.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,
144144
pre_cluster_timing_manager_);
145145

146146
// Pick the first seed molecule.
147-
PackMoleculeId seed_mol_id = seed_selector.get_next_seed(prepacker,
148-
cluster_legalizer);
147+
PackMoleculeId seed_mol_id = seed_selector.get_next_seed(cluster_legalizer);
149148

150149
/****************************************************************
151150
* Clustering
@@ -213,8 +212,7 @@ GreedyClusterer::do_clustering(ClusterLegalizer& cluster_legalizer,
213212
cluster_legalizer);
214213

215214
// Pick new seed.
216-
seed_mol_id = seed_selector.get_next_seed(prepacker,
217-
cluster_legalizer);
215+
seed_mol_id = seed_selector.get_next_seed(cluster_legalizer);
218216
}
219217

220218
// If this architecture has LE physical block, report its usage.

vpr/src/pack/greedy_seed_selector.cpp

+48-37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <algorithm>
1111
#include <cmath>
12+
#include <limits>
13+
#include <vector>
1214
#include "PreClusterTimingManager.h"
1315
#include "atom_netlist.h"
1416
#include "cluster_legalizer.h"
@@ -129,11 +131,12 @@ static inline float get_seed_gain(AtomBlockId blk_id,
129131
* criticalities.
130132
*/
131133
static inline void print_seed_gains(const char* fname,
132-
const std::vector<AtomBlockId>& seed_atoms,
133-
const vtr::vector<AtomBlockId, float>& atom_gain,
134+
const std::vector<PackMoleculeId>& seed_mols,
135+
const vtr::vector<PackMoleculeId, float>& molecule_gain,
134136
const vtr::vector<AtomBlockId, float>& atom_criticality,
135137
const AtomNetlist& atom_netlist,
136-
const LogicalModels& models) {
138+
const LogicalModels& models,
139+
const Prepacker& prepacker) {
137140
FILE* fp = vtr::fopen(fname, "w");
138141

139142
// For pretty formatting determine the maximum name length
@@ -148,16 +151,18 @@ static inline void print_seed_gains(const char* fname,
148151

149152
fprintf(fp, "%-*s %-*s %8s %8s\n", max_name_len, "atom_block_name", max_type_len, "atom_block_type", "gain", "criticality");
150153
fprintf(fp, "\n");
151-
for (auto blk_id : seed_atoms) {
152-
std::string name = atom_netlist.block_name(blk_id);
153-
fprintf(fp, "%-*s ", max_name_len, name.c_str());
154+
for (auto mol_id : seed_mols) {
155+
for (AtomBlockId blk_id : prepacker.get_molecule(mol_id).atom_block_ids) {
156+
std::string name = atom_netlist.block_name(blk_id);
157+
fprintf(fp, "%-*s ", max_name_len, name.c_str());
154158

155-
std::string model_name = models.model_name(atom_netlist.block_model(blk_id));
156-
fprintf(fp, "%-*s ", max_type_len, model_name.c_str());
159+
std::string model_name = models.model_name(atom_netlist.block_model(blk_id));
160+
fprintf(fp, "%-*s ", max_type_len, model_name.c_str());
157161

158-
fprintf(fp, "%*f ", std::max((int)strlen("gain"), 8), atom_gain[blk_id]);
159-
fprintf(fp, "%*f ", std::max((int)strlen("criticality"), 8), atom_criticality[blk_id]);
160-
fprintf(fp, "\n");
162+
fprintf(fp, "%*f ", std::max((int)strlen("gain"), 8), molecule_gain[mol_id]);
163+
fprintf(fp, "%*f ", std::max((int)strlen("criticality"), 8), atom_criticality[blk_id]);
164+
fprintf(fp, "\n");
165+
}
161166
}
162167

163168
fclose(fp);
@@ -169,7 +174,7 @@ GreedySeedSelector::GreedySeedSelector(const AtomNetlist& atom_netlist,
169174
const t_molecule_stats& max_molecule_stats,
170175
const LogicalModels& models,
171176
const PreClusterTimingManager& pre_cluster_timing_manager)
172-
: seed_atoms_(atom_netlist.blocks().begin(), atom_netlist.blocks().end()) {
177+
: seed_mols_(prepacker.molecules().begin(), prepacker.molecules().end()) {
173178
// Seed atoms list is initialized with all atoms in the atom netlist.
174179

175180
// Pre-compute the criticality of each atom
@@ -186,17 +191,28 @@ GreedySeedSelector::GreedySeedSelector(const AtomNetlist& atom_netlist,
186191
// Maintain a lookup table of the seed gain for each atom. This will be
187192
// used to sort the seed atoms.
188193
// Initially all gains are zero.
189-
vtr::vector<AtomBlockId, float> atom_gains(atom_netlist.blocks().size(), 0.f);
190-
191-
// Get the seed gain of each atom.
192-
for (AtomBlockId blk_id : atom_netlist.blocks()) {
193-
atom_gains[blk_id] = get_seed_gain(blk_id,
194-
atom_netlist,
195-
prepacker,
196-
models,
197-
seed_type,
198-
max_molecule_stats,
199-
atom_criticality);
194+
vtr::vector<PackMoleculeId, float> molecule_gains(seed_mols_.size(), 0.f);
195+
196+
// Get the seed gain of each molecule.
197+
for (PackMoleculeId mol_id : seed_mols_) {
198+
// Gain of each molecule is the maximum gain of its atoms
199+
float mol_gain = std::numeric_limits<float>::lowest();
200+
const std::vector<AtomBlockId>& molecule_atoms = prepacker.get_molecule(mol_id).atom_block_ids;
201+
for (AtomBlockId blk_id : molecule_atoms) {
202+
// If the molecule does not fit the entire pack pattern, it's possible to have invalid block ids in the molecule_atoms vector
203+
if (blk_id == AtomBlockId::INVALID()) {
204+
continue;
205+
}
206+
float atom_gain = get_seed_gain(blk_id,
207+
atom_netlist,
208+
prepacker,
209+
models,
210+
seed_type,
211+
max_molecule_stats,
212+
atom_criticality);
213+
mol_gain = std::max(mol_gain, atom_gain);
214+
}
215+
molecule_gains[mol_id] = mol_gain;
200216
}
201217

202218
// Sort seeds in descending order of seed gain (i.e. highest seed gain first)
@@ -207,15 +223,15 @@ GreedySeedSelector::GreedySeedSelector(const AtomNetlist& atom_netlist,
207223
// std::sort which does not specify how equal values are handled). Using a stable
208224
// sort ensures that regardless of the underlying sorting algorithm the same seed
209225
// order is produced regardless of compiler.
210-
auto by_descending_gain = [&](const AtomBlockId lhs, const AtomBlockId rhs) {
211-
return atom_gains[lhs] > atom_gains[rhs];
226+
auto by_descending_gain = [&](const PackMoleculeId lhs, const PackMoleculeId rhs) {
227+
return molecule_gains[lhs] > molecule_gains[rhs];
212228
};
213-
std::stable_sort(seed_atoms_.begin(), seed_atoms_.end(), by_descending_gain);
229+
std::stable_sort(seed_mols_.begin(), seed_mols_.end(), by_descending_gain);
214230

215231
// Print the seed gains if requested.
216232
if (getEchoEnabled() && isEchoFileEnabled(E_ECHO_CLUSTERING_BLOCK_CRITICALITIES)) {
217233
print_seed_gains(getEchoFileName(E_ECHO_CLUSTERING_BLOCK_CRITICALITIES),
218-
seed_atoms_, atom_gains, atom_criticality, atom_netlist, models);
234+
seed_mols_, molecule_gains, atom_criticality, atom_netlist, models, prepacker);
219235
}
220236

221237
// Set the starting seed index (the index of the first molecule to propose).
@@ -224,25 +240,20 @@ GreedySeedSelector::GreedySeedSelector(const AtomNetlist& atom_netlist,
224240
seed_index_ = 0;
225241
}
226242

227-
PackMoleculeId GreedySeedSelector::get_next_seed(const Prepacker& prepacker,
228-
const ClusterLegalizer& cluster_legalizer) {
229-
while (seed_index_ < seed_atoms_.size()) {
243+
PackMoleculeId GreedySeedSelector::get_next_seed(const ClusterLegalizer& cluster_legalizer) {
244+
while (seed_index_ < seed_mols_.size()) {
230245
// Get the current seed atom at the seed index and increment the
231246
// seed index.
232247
// All previous seed indices have been either proposed already or
233248
// are already clustered. This process assumes that once an atom
234249
// is clustered it will never become unclustered.
235-
AtomBlockId seed_blk_id = seed_atoms_[seed_index_++];
250+
PackMoleculeId seed_molecule_id = seed_mols_[seed_index_++];
236251

237252
// If this atom has been clustered, it cannot be proposed as a seed.
238253
// Skip to the next seed.
239-
if (cluster_legalizer.is_atom_clustered(seed_blk_id))
254+
if (cluster_legalizer.is_mol_clustered(seed_molecule_id)) {
240255
continue;
241-
242-
// Get the molecule that contains this atom and return it as the
243-
// next seed.
244-
PackMoleculeId seed_molecule_id = prepacker.get_atom_molecule(seed_blk_id);
245-
VTR_ASSERT(!cluster_legalizer.is_mol_clustered(seed_molecule_id));
256+
}
246257
return seed_molecule_id;
247258
}
248259

vpr/src/pack/greedy_seed_selector.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ class GreedySeedSelector {
7575
* clusters. This is used to check if a molecule has already
7676
* been clustered or not.
7777
*/
78-
PackMoleculeId get_next_seed(const Prepacker& prepacker,
79-
const ClusterLegalizer& cluster_legalizer);
78+
PackMoleculeId get_next_seed(const ClusterLegalizer& cluster_legalizer);
8079

8180
// TODO: Maybe create an update_seed_gains method to update the seed atoms
8281
// list using current clustering information.
@@ -87,13 +86,8 @@ class GreedySeedSelector {
8786
/// are proposed.
8887
size_t seed_index_;
8988

90-
/// @brief A list of seed atoms, sorted in decreasing order of gain. This
89+
/// @brief A list of seed molecules, sorted in decreasing order of gain. This
9190
/// is computed in the constructor and is traversed when a new seed
9291
/// is being proposed.
93-
// FIXME: This should really be seed molecules. It looks like the only
94-
// reason it isn't is because of the atom criticality. May want to
95-
// create the concept of molecule criticality. Currently, the max
96-
// criticality of any block in the molecule is technically being
97-
// used.
98-
std::vector<AtomBlockId> seed_atoms_;
92+
std::vector<PackMoleculeId> seed_mols_;
9993
};

0 commit comments

Comments
 (0)