Skip to content

Commit 638701f

Browse files
author
MohamedElgammal
committed
remove backing up pb to revert move and rebuild cluster placement stats
1 parent 5d0f8e8 commit 638701f

File tree

3 files changed

+132
-107
lines changed

3 files changed

+132
-107
lines changed

vpr/src/pack/re_cluster.cpp

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@
44
#include "cluster_placement.h"
55
#include "cluster_router.h"
66

7-
87
bool move_mol_to_new_cluster(t_pack_molecule* molecule,
98
t_clustering_data& clustering_data,
109
bool during_packing) {
1110
auto& cluster_ctx = g_vpr_ctx.clustering();
1211
auto& helper_ctx = g_vpr_ctx.mutable_cl_helper();
1312
auto& device_ctx = g_vpr_ctx.device();
14-
auto& atom_ctx = g_vpr_ctx.mutable_atom();
1513

1614
bool is_removed, is_created;
17-
ClusterBlockId old_clb;
15+
ClusterBlockId old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]);
16+
;
1817
int molecule_size = get_array_size_of_molecule(molecule);
1918

20-
//Backup the original pb of the molecule before the move
21-
std::vector<t_pb*> mol_pb_backup;
22-
for (int i_atom = 0; i_atom < molecule_size; i_atom++) {
23-
if (molecule->atom_block_ids[i_atom]) {
24-
t_pb* atom_pb_backup = new t_pb;
25-
atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom]));
26-
mol_pb_backup.push_back(atom_pb_backup);
27-
}
28-
}
29-
3019
PartitionRegion temp_cluster_pr;
3120
t_lb_router_data* old_router_data = nullptr;
3221
t_lb_router_data* router_data = nullptr;
3322

3423
//Check that there is a place for a new cluster of the same type
35-
old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]);
3624
t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(old_clb);
3725
int block_mode = cluster_ctx.clb_nlist.block_pb(old_clb)->mode;
3826

@@ -48,7 +36,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule,
4836
}
4937

5038
//remove the molecule from its current cluster and check the cluster legality after
51-
is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data);
39+
is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing);
5240

5341
if (!is_removed) {
5442
VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", molecule->atom_block_ids[molecule->root]);
@@ -70,31 +58,22 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule,
7058

7159
//Commit or revert the move
7260
if (is_created) {
73-
commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, true);
61+
commit_mol_move(old_clb, new_clb, during_packing, true);
7462
VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]);
7563
} else {
76-
int atom_idx = 0;
77-
for (int i_atom = 0; i_atom < molecule_size; i_atom++) {
78-
if (molecule->atom_block_ids[i_atom]) {
79-
atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb);
80-
atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]);
81-
atom_idx++;
82-
}
83-
}
64+
revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data);
8465
VTR_LOG("Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", molecule->atom_block_ids[molecule->root]);
8566
}
8667

68+
free_router_data(old_router_data);
69+
old_router_data = nullptr;
70+
8771
//If the move is done after packing not during it, some fixes need to be done on the
8872
//clustered netlist
8973
if (is_created && !during_packing) {
9074
fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb);
9175
}
9276

93-
for (auto atom_pb_backup : mol_pb_backup) {
94-
cleanup_pb(atom_pb_backup);
95-
delete atom_pb_backup;
96-
}
97-
9877
return (is_created);
9978
}
10079

@@ -104,7 +83,6 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule,
10483
t_clustering_data& clustering_data) {
10584
//define required contexts
10685
auto& cluster_ctx = g_vpr_ctx.clustering();
107-
auto& atom_ctx = g_vpr_ctx.mutable_atom();
10886

10987
//define local variables
11088
bool is_removed, is_added;
@@ -128,56 +106,39 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule,
128106
return false;
129107
}
130108

131-
//Backup the original pb of the atom before the move
132-
std::vector<t_pb*> mol_pb_backup;
133-
for (int i_atom = 0; i_atom < molecule_size; i_atom++) {
134-
if (molecule->atom_block_ids[i_atom]) {
135-
t_pb* atom_pb_backup = new t_pb;
136-
atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom]));
137-
mol_pb_backup.push_back(atom_pb_backup);
138-
}
139-
}
140-
141109
//remove the molecule from its current cluster and check the cluster legality
142-
is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data);
110+
is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing);
143111
if (!is_removed) {
144112
VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", root_atom_id);
145113
return false;
146114
}
147115

116+
rebuild_cluster_placemet_stats(new_clb, new_clb_atoms, cluster_ctx.clb_nlist.block_type(new_clb)->index, cluster_ctx.clb_nlist.block_pb(new_clb)->mode);
148117
//Add the atom to the new cluster
149118
is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data);
150119

151120
//Commit or revert the move
152121
if (is_added) {
153-
commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, false);
122+
commit_mol_move(old_clb, new_clb, during_packing, false);
154123
VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]);
155124
} else {
156-
int atom_idx = 0;
157-
for (int i_atom = 0; i_atom < molecule_size; i_atom++) {
158-
if (molecule->atom_block_ids[i_atom]) {
159-
atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb);
160-
atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]);
161-
atom_idx++;
162-
}
163-
}
125+
revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data);
164126
VTR_LOG("Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", molecule->atom_block_ids[molecule->root]);
165127
}
166128

129+
free_router_data(old_router_data);
130+
old_router_data = nullptr;
131+
167132
//If the move is done after packing not during it, some fixes need to be done on the
168133
//clustered netlist
169134
if (is_added && !during_packing) {
170135
fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb);
171136
}
172137

173-
for (auto atom_pb_backup : mol_pb_backup) {
174-
cleanup_pb(atom_pb_backup);
175-
delete atom_pb_backup;
176-
}
177-
178138
return (is_added);
179139
}
180140

141+
#if 0
181142
bool swap_two_molecules(t_pack_molecule* molecule_1,
182143
t_pack_molecule* molecule_2,
183144
bool during_packing,
@@ -262,8 +223,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1,
262223
}
263224

264225
//add the molecules to their new clusters
265-
pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]),
266-
molecule_1,
226+
pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]),
227+
molecule_2,
267228
helper_ctx.primitives_list,
268229
cluster_ctx.clb_nlist.block_pb(clb_1),
269230
helper_ctx.num_models,
@@ -277,8 +238,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1,
277238
target_ext_pin_util,
278239
temp_cluster_pr_1);
279240

280-
pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]),
281-
molecule_2,
241+
pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]),
242+
molecule_1,
282243
helper_ctx.primitives_list,
283244
cluster_ctx.clb_nlist.block_pb(clb_2),
284245
helper_ctx.num_models,
@@ -292,6 +253,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1,
292253
target_ext_pin_util,
293254
temp_cluster_pr_2);
294255

256+
257+
295258
//commit the move if succeeded or revert if failed
296259
if (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED) {
297260
if(during_packing) {
@@ -308,8 +271,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1,
308271

309272
}
310273

311-
commit_mol_move(clb_1, clb_2, mol_1_pb_backup, old_1_router_data, clustering_data, during_packing, false);
312-
commit_mol_move(clb_2, clb_1, mol_2_pb_backup, old_2_router_data, clustering_data, during_packing, false);
274+
commit_mol_move(clb_1, clb_2, during_packing, false);
275+
commit_mol_move(clb_2, clb_1, during_packing, false);
313276

314277
} else {
315278
int atom_idx = 0;
@@ -357,4 +320,5 @@ bool swap_two_molecules(t_pack_molecule* molecule_1,
357320

358321
//return the move result
359322
return (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED);
360-
}
323+
}
324+
#endif

vpr/src/pack/re_cluster_util.cpp

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ std::vector<AtomBlockId> cluster_to_atoms(const ClusterBlockId& cluster) {
4949
bool remove_mol_from_cluster(const t_pack_molecule* molecule,
5050
int molecule_size,
5151
ClusterBlockId& old_clb,
52-
t_lb_router_data*& router_data) {
52+
t_lb_router_data*& router_data,
53+
t_clustering_data& clustering_data,
54+
bool during_packing) {
5355
auto& helper_ctx = g_vpr_ctx.mutable_cl_helper();
56+
auto& atom_ctx = g_vpr_ctx.atom();
57+
auto& cluster_ctx = g_vpr_ctx.mutable_clustering();
58+
59+
t_pb* temp_pb = const_cast<t_pb*>(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[0]));
60+
t_pb* next_pb;
61+
5462
//Determine the cluster ID
5563
old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]);
5664
std::vector<AtomBlockId> old_clb_atoms = cluster_to_atoms(old_clb);
@@ -74,54 +82,28 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule,
7482
revert_place_atom_block(molecule->atom_block_ids[i_atom], router_data);
7583
}
7684
}
77-
}
7885

86+
cleanup_pb(cluster_ctx.clb_nlist.block_pb(old_clb));
87+
88+
if (during_packing) {
89+
free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]);
90+
clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets;
91+
router_data->saved_lb_nets = nullptr;
92+
} else {
93+
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear();
94+
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route = alloc_and_load_pb_route(router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(old_clb)->pb_graph_node);
95+
}
96+
}
7997
//return true if succeeded
8098
return (is_cluster_legal);
8199
}
82100

83101
void commit_mol_move(const ClusterBlockId& old_clb,
84102
const ClusterBlockId& new_clb,
85-
std::vector<t_pb*>& mol_pbs,
86-
t_lb_router_data*& old_router_data,
87-
t_clustering_data& clustering_data,
88103
bool during_packing,
89104
bool new_clb_created) {
90-
auto& cluster_ctx = g_vpr_ctx.mutable_clustering();
91105
auto& device_ctx = g_vpr_ctx.device();
92106

93-
for (auto old_atom_pb : mol_pbs) {
94-
t_pb* temp = old_atom_pb;
95-
t_pb* next = temp->parent_pb;
96-
//char* atom_name = vtr::strdup(temp->name);
97-
bool has_more_children;
98-
99-
//delete atom pb
100-
cleanup_pb(temp);
101-
102-
has_more_children = count_children_pbs(next);
103-
//keep deleting the parent pbs if they were created only for the removed atom
104-
while (!has_more_children) {
105-
temp = next;
106-
next = next->parent_pb;
107-
cleanup_pb(temp);
108-
has_more_children = count_children_pbs(next);
109-
}
110-
}
111-
112-
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear();
113-
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route = alloc_and_load_pb_route(old_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(old_clb)->pb_graph_node);
114-
115-
if (during_packing) {
116-
free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]);
117-
//delete clustering_data.intra_lb_routing[old_clb];
118-
clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets;
119-
old_router_data->saved_lb_nets = nullptr;
120-
}
121-
122-
free_router_data(old_router_data);
123-
old_router_data = nullptr;
124-
125107
if (!during_packing && new_clb_created) {
126108
int imacro;
127109
g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1);
@@ -239,21 +221,23 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule,
239221
e_block_pack_status pack_result = BLK_STATUS_UNDEFINED;
240222
t_ext_pin_util target_ext_pin_util = helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(new_clb)->name);
241223
t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb);
224+
t_pb* temp_pb = cluster_ctx.clb_nlist.block_pb(new_clb);
242225

243226
//re-build router_data structure for this cluster
244227
t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms);
245228

246229
pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]),
247230
molecule,
248231
helper_ctx.primitives_list,
249-
cluster_ctx.clb_nlist.block_pb(new_clb),
232+
temp_pb,
250233
helper_ctx.num_models,
251234
helper_ctx.max_cluster_size,
252235
new_clb,
253236
E_DETAILED_ROUTE_FOR_EACH_ATOM,
254237
router_data,
255238
0,
256-
helper_ctx.enable_pin_feasibility_filter,
239+
//helper_ctx.enable_pin_feasibility_filter,
240+
false,
257241
helper_ctx.feasible_block_array_size,
258242
target_ext_pin_util,
259243
temp_cluster_pr);
@@ -285,6 +269,40 @@ void fix_clustered_netlist(t_pack_molecule* molecule,
285269
fix_cluster_net_after_moving(molecule, molecule_size, old_clb, new_clb);
286270
}
287271

272+
void revert_mol_move(const ClusterBlockId& old_clb,
273+
t_pack_molecule* molecule,
274+
t_lb_router_data*& old_router_data,
275+
bool during_packing,
276+
t_clustering_data& clustering_data) {
277+
auto& helper_ctx = g_vpr_ctx.mutable_cl_helper();
278+
auto& cluster_ctx = g_vpr_ctx.mutable_clustering();
279+
280+
PartitionRegion temp_cluster_pr_original;
281+
e_block_pack_status pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[cluster_ctx.clb_nlist.block_type(old_clb)->index]),
282+
molecule,
283+
helper_ctx.primitives_list,
284+
cluster_ctx.clb_nlist.block_pb(old_clb),
285+
helper_ctx.num_models,
286+
helper_ctx.max_cluster_size,
287+
old_clb,
288+
E_DETAILED_ROUTE_FOR_EACH_ATOM,
289+
old_router_data,
290+
0,
291+
helper_ctx.enable_pin_feasibility_filter,
292+
helper_ctx.feasible_block_array_size,
293+
helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(old_clb)->name),
294+
temp_cluster_pr_original);
295+
296+
VTR_ASSERT(pack_result == BLK_PASSED);
297+
if (during_packing) {
298+
free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]);
299+
clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets;
300+
old_router_data->saved_lb_nets = nullptr;
301+
} else {
302+
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear();
303+
cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route = alloc_and_load_pb_route(old_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(old_clb)->pb_graph_node);
304+
}
305+
}
288306
/*******************************************/
289307
/************ static functions *************/
290308
/*******************************************/
@@ -592,4 +610,37 @@ static bool count_children_pbs(const t_pb* pb) {
592610
}
593611
}
594612
return false;
595-
}
613+
}
614+
615+
#if 1
616+
void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index,
617+
const std::vector<AtomBlockId>& clb_atoms,
618+
int type_idx,
619+
int mode) {
620+
auto& helper_ctx = g_vpr_ctx.mutable_cl_helper();
621+
auto& cluster_ctx = g_vpr_ctx.clustering();
622+
auto& atom_ctx = g_vpr_ctx.atom();
623+
624+
t_cluster_placement_stats* cluster_placement_stats = &(helper_ctx.cluster_placement_stats[type_idx]);
625+
626+
reset_cluster_placement_stats(cluster_placement_stats);
627+
set_mode_cluster_placement_stats(cluster_ctx.clb_nlist.block_pb(clb_index)->pb_graph_node, mode);
628+
629+
std::set<t_pb_graph_node*> list_of_pb_atom_nodes;
630+
t_cluster_placement_primitive* cur_cluster_placement_primitive;
631+
632+
for (auto& atom_blk : clb_atoms) {
633+
list_of_pb_atom_nodes.insert(atom_ctx.lookup.atom_pb(atom_blk)->pb_graph_node);
634+
}
635+
636+
for (int i = 0; i < cluster_placement_stats->num_pb_types; i++) {
637+
cur_cluster_placement_primitive = cluster_placement_stats->valid_primitives[i]->next_primitive;
638+
while (cur_cluster_placement_primitive != nullptr) {
639+
if (list_of_pb_atom_nodes.find(cur_cluster_placement_primitive->pb_graph_node) != list_of_pb_atom_nodes.end()) {
640+
cur_cluster_placement_primitive->valid = false;
641+
}
642+
cur_cluster_placement_primitive = cur_cluster_placement_primitive->next_primitive;
643+
}
644+
}
645+
}
646+
#endif

0 commit comments

Comments
 (0)