From 5e5cd01162924c1bc345d5e3581ae275720db86b Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 30 May 2022 10:46:11 -0400 Subject: [PATCH 01/17] Refactoring the API by seperating commit the move in a function --- vpr/src/pack/re_cluster.cpp | 4 +- vpr/src/pack/re_cluster_util.cpp | 81 +++++++++++++++----------------- vpr/src/pack/re_cluster_util.h | 12 ++++- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 26ad1b04f68..166355c5057 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -14,6 +14,7 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, ClusterBlockId old_clb; PartitionRegion temp_cluster_pr; int imacro; + t_lb_router_data* old_router_data = nullptr; t_lb_router_data* router_data = nullptr; //Check that there is a place for a new cluster of the same type @@ -33,7 +34,8 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, } //remove the atom from its current cluster and check its legality - is_removed = remove_atom_from_cluster(atom_id, lb_type_rr_graphs, old_clb, clustering_data, imacro, during_packing); + is_removed = remove_atom_from_cluster(atom_id, lb_type_rr_graphs, old_clb, clustering_data, old_router_data, imacro, during_packing); + if (!is_removed) { VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", atom_id); return (is_removed); diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 102030bedd1..ef3e071d07a 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -48,16 +48,14 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, std::vector* lb_type_rr_graphs, ClusterBlockId& old_clb, t_clustering_data& clustering_data, + t_lb_router_data* router_data, int& imacro, bool during_packing) { - auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - auto& atom_ctx = g_vpr_ctx.mutable_atom(); - //Determine the cluster ID old_clb = atom_to_cluster(atom_id); //re-build router_data structure for this cluster - t_lb_router_data* router_data = lb_load_router_data(lb_type_rr_graphs, old_clb); + router_data = lb_load_router_data(lb_type_rr_graphs, old_clb); //remove atom from router_data remove_atom_from_target(router_data, atom_id); @@ -66,44 +64,8 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, bool is_cluster_legal = check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data); if (is_cluster_legal) { - t_pb* temp = const_cast(atom_ctx.lookup.atom_pb(atom_id)); - t_pb* next = temp->parent_pb; - //char* atom_name = vtr::strdup(temp->name); - bool has_more_children; - - revert_place_atom_block(atom_id, router_data); - //delete atom pb - cleanup_pb(temp); - - has_more_children = count_children_pbs(next); - //keep deleting the parent pbs if they were created only for the removed atom - while (!has_more_children) { - temp = next; - next = next->parent_pb; - cleanup_pb(temp); - has_more_children = count_children_pbs(next); - } - - //if the parents' names are the same as the removed atom names, - //update the name to prevent double the name when creating a new cluster for - // the removed atom - /* - * while(next != nullptr && *(next->name) == *atom_name) { - * next->name = vtr::strdup(child_name); - * if(next->parent_pb == nullptr) - * next = next->parent_pb; - * } - */ - - cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); - 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); - - if (during_packing) { - clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets; - router_data->saved_lb_nets = nullptr; - } - - else + commit_molecule_move(atom_id, old_clb, router_data, clustering_data, during_packing); + if (!during_packing) get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); } else { VTR_LOG("re-cluster: Cluster is illegal after removing an atom\n"); @@ -116,6 +78,41 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, return (is_cluster_legal); } +void commit_molecule_move(const AtomBlockId& atom_id, + const ClusterBlockId& old_clb, + t_lb_router_data* old_router_data, + t_clustering_data& clustering_data, + bool during_packing) { + auto& atom_ctx = g_vpr_ctx.mutable_atom(); + auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + + t_pb* temp = const_cast(atom_ctx.lookup.atom_pb(atom_id)); + t_pb* next = temp->parent_pb; + //char* atom_name = vtr::strdup(temp->name); + bool has_more_children; + + revert_place_atom_block(atom_id, old_router_data); + //delete atom pb + cleanup_pb(temp); + + has_more_children = count_children_pbs(next); + //keep deleting the parent pbs if they were created only for the removed atom + while (!has_more_children) { + temp = next; + next = next->parent_pb; + cleanup_pb(temp); + has_more_children = count_children_pbs(next); + } + + cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); + 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); + + if (during_packing) { + clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets; + old_router_data->saved_lb_nets = nullptr; + } +} + t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index) { //build data structures used by intra-logic block router auto& cluster_ctx = g_vpr_ctx.clustering(); diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index e5de4afdb1f..2a1ede69d1d 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -46,13 +46,14 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, std::vector* lb_type_rr_graphs, ClusterBlockId& old_clb, t_clustering_data& clustering_data, + t_lb_router_data* router_data, int& imacro, bool during_packing); /** * @brief A function that starts a new cluster for one specific molecule * - * It place the molecule in a specific type and mode that should be passed by + * It places the molecule in a specific type and mode that should be passed by * the higher level routine. */ bool start_new_cluster_for_atom(const AtomBlockId atom_id, @@ -77,4 +78,13 @@ void fix_clustered_netlist(const AtomBlockId& atom_id, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb); +/** + * @brief A function that commits the molecule move if it is legal + */ +void commit_molecule_move(const AtomBlockId& atom_id, + const ClusterBlockId& old_clb, + t_lb_router_data* old_router_data, + t_clustering_data& clustering_data, + bool during_packing); + #endif From b4020d1377d14667d5d9f0197ed18661ab05ef97 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Tue, 31 May 2022 17:23:50 -0400 Subject: [PATCH 02/17] Adding option to revert the move if the new location is illegal --- vpr/src/base/vpr_types.cpp | 18 +++++++++++++++ vpr/src/base/vpr_types.h | 10 +++++++++ vpr/src/pack/pack.cpp | 8 ++++++- vpr/src/pack/re_cluster.cpp | 31 +++++++++++++++++++++----- vpr/src/pack/re_cluster_util.cpp | 38 ++++++++++++++------------------ vpr/src/pack/re_cluster_util.h | 11 ++++----- 6 files changed, 81 insertions(+), 35 deletions(-) diff --git a/vpr/src/base/vpr_types.cpp b/vpr/src/base/vpr_types.cpp index 5b74779893e..b8b2feb4524 100644 --- a/vpr/src/base/vpr_types.cpp +++ b/vpr/src/base/vpr_types.cpp @@ -147,6 +147,24 @@ const t_pb* t_pb::find_pb_for_model(const std::string& blif_model) const { return nullptr; //Not found } +/** + * @brief Deep copy function for t_pb class + * + * This funcion deeply copies some data members of + * t_pb class to be used to retrieve pb. + * + * This function is currently used in re-clustering API. + */ +void t_pb::pb_deep_copy(const t_pb* rhs) { + name = rhs->name; + pb_graph_node = rhs->pb_graph_node; + mode = rhs->mode; + parent_pb = rhs->parent_pb; + pb_stats = rhs->pb_stats; + clock_net = rhs->clock_net; + pin_rotations_ = rhs->pin_rotations_; +} + /** * @brief Returns the root pb containing this pb */ diff --git a/vpr/src/base/vpr_types.h b/vpr/src/base/vpr_types.h index 75ce30c031a..c4d66484e2e 100644 --- a/vpr/src/base/vpr_types.h +++ b/vpr/src/base/vpr_types.h @@ -277,6 +277,16 @@ class t_pb { // Member functions + /** + * @brief Deep copy function for t_pb class + * + * This funcion deeply copies some data members of + * t_pb class to be used to retrieve pb. + * + * This function is currently used in re-clustering API. + */ + void t_pb::pb_deep_copy(const t_pb* rhs); + ///@brief Returns true if this block has not parent pb block bool is_root() const { return parent_pb == nullptr; } diff --git a/vpr/src/pack/pack.cpp b/vpr/src/pack/pack.cpp index 935e756b5dd..e54ff10cba8 100644 --- a/vpr/src/pack/pack.cpp +++ b/vpr/src/pack/pack.cpp @@ -265,7 +265,13 @@ bool try_pack(t_packer_opts* packer_opts, /* Packing iterative improvement can be done here */ /* Use the re-cluster API to edit it */ /******************* Start *************************/ - + + enum e_pad_loc_type pad_loc_type; + bool is_removed = move_atom_to_new_cluster(AtomBlockId(4), pad_loc_type, helper_ctx.lb_type_rr_graphs, clustering_data, true); + if (is_removed) { + VTR_LOG("@@@@ Atom is removed\n"); + } + /******************** End **************************/ //check clustering and output it diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 166355c5057..71300c89c2a 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -1,5 +1,6 @@ #include "re_cluster.h" #include "re_cluster_util.h" +#include "initial_placement.h" bool move_atom_to_new_cluster(const AtomBlockId& atom_id, const enum e_pad_loc_type& pad_loc_type, @@ -9,11 +10,16 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, auto& cluster_ctx = g_vpr_ctx.clustering(); auto& helper_ctx = g_vpr_ctx.mutable_helper(); auto& device_ctx = g_vpr_ctx.device(); + auto& atom_ctx = g_vpr_ctx.mutable_atom(); bool is_removed, is_created; ClusterBlockId old_clb; + + //Backup the original pb of the atom before the move + t_pb* atom_pb_packup = new t_pb; + atom_pb_packup->pb_deep_copy(atom_ctx.lookup.atom_pb(atom_id)); + PartitionRegion temp_cluster_pr; - int imacro; t_lb_router_data* old_router_data = nullptr; t_lb_router_data* router_data = nullptr; @@ -34,7 +40,7 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, } //remove the atom from its current cluster and check its legality - is_removed = remove_atom_from_cluster(atom_id, lb_type_rr_graphs, old_clb, clustering_data, old_router_data, imacro, during_packing); + is_removed = remove_atom_from_cluster(atom_id, lb_type_rr_graphs, old_clb, old_router_data); if (!is_removed) { VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", atom_id); @@ -48,7 +54,6 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, block_type, block_mode, helper_ctx.feasible_block_array_size, - imacro, helper_ctx.enable_pin_feasibility_filter, new_clb, &router_data, @@ -58,10 +63,23 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, during_packing); //Print the move result - if (is_created) + if (is_created) { VTR_LOG("Atom:%zu is moved to a new cluster\n", atom_id); - else + + commit_atom_move(atom_id, old_clb, atom_pb_packup, old_router_data, clustering_data, during_packing); + if (!during_packing) { + int imacro; + g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); + get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); + set_imacro_for_iblk(&imacro, new_clb); + place_one_block(new_clb, pad_loc_type); + } + } + else { + atom_ctx.lookup.set_atom_clb(atom_id, old_clb); + atom_ctx.lookup.set_atom_pb(atom_id, atom_pb_packup); VTR_LOG("Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", atom_id); + } //If the move is done after packing not during it, some fixes need to be done on the //clustered netlist @@ -69,5 +87,8 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, fix_clustered_netlist(atom_id, old_clb, new_clb); } + cleanup_pb(atom_pb_packup); + delete atom_pb_packup; + return (is_created); } diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index ef3e071d07a..5fe98d41acd 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -47,10 +47,8 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster) { bool remove_atom_from_cluster(const AtomBlockId& atom_id, std::vector* lb_type_rr_graphs, ClusterBlockId& old_clb, - t_clustering_data& clustering_data, - t_lb_router_data* router_data, - int& imacro, - bool during_packing) { + t_lb_router_data*& router_data) { + //Determine the cluster ID old_clb = atom_to_cluster(atom_id); @@ -63,35 +61,28 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, //check cluster legality bool is_cluster_legal = check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data); - if (is_cluster_legal) { - commit_molecule_move(atom_id, old_clb, router_data, clustering_data, during_packing); - if (!during_packing) - get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); - } else { - VTR_LOG("re-cluster: Cluster is illegal after removing an atom\n"); + if(is_cluster_legal) { + revert_place_atom_block(atom_id, router_data); } - free_router_data(router_data); - router_data = nullptr; - //return true if succeeded return (is_cluster_legal); } -void commit_molecule_move(const AtomBlockId& atom_id, +void commit_atom_move(const AtomBlockId& atom_id, const ClusterBlockId& old_clb, - t_lb_router_data* old_router_data, + t_pb* old_pb, + t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, bool during_packing) { - auto& atom_ctx = g_vpr_ctx.mutable_atom(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - t_pb* temp = const_cast(atom_ctx.lookup.atom_pb(atom_id)); + t_pb* temp = old_pb; t_pb* next = temp->parent_pb; //char* atom_name = vtr::strdup(temp->name); bool has_more_children; - revert_place_atom_block(atom_id, old_router_data); + //revert_place_atom_block(atom_id, old_router_data, false); //delete atom pb cleanup_pb(temp); @@ -108,9 +99,16 @@ void commit_molecule_move(const AtomBlockId& atom_id, 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); if (during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); + //delete clustering_data.intra_lb_routing[old_clb]; clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets; old_router_data->saved_lb_nets = nullptr; } + + free_router_data(old_router_data); + old_router_data = nullptr; + + } t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index) { @@ -131,7 +129,6 @@ bool start_new_cluster_for_atom(const AtomBlockId atom_id, const t_logical_block_type_ptr& type, const int mode, const int feasible_block_array_size, - int& imacro, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, t_lb_router_data** router_data, @@ -200,9 +197,6 @@ bool start_new_cluster_for_atom(const AtomBlockId atom_id, (*router_data)->saved_lb_nets = nullptr; } else { cluster_ctx.clb_nlist.block_pb(clb_index)->pb_route = alloc_and_load_pb_route((*router_data)->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(clb_index)->pb_graph_node); - g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); - set_imacro_for_iblk(&imacro, clb_index); - place_one_block(clb_index, pad_loc_type); } } else { free_pb(pb); diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 2a1ede69d1d..fe3d979471a 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -45,10 +45,7 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr bool remove_atom_from_cluster(const AtomBlockId& atom_id, std::vector* lb_type_rr_graphs, ClusterBlockId& old_clb, - t_clustering_data& clustering_data, - t_lb_router_data* router_data, - int& imacro, - bool during_packing); + t_lb_router_data*& router_data); /** * @brief A function that starts a new cluster for one specific molecule @@ -61,7 +58,6 @@ bool start_new_cluster_for_atom(const AtomBlockId atom_id, const t_logical_block_type_ptr& type, const int mode, const int feasible_block_array_size, - int& imacro, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, t_lb_router_data** router_data, @@ -81,9 +77,10 @@ void fix_clustered_netlist(const AtomBlockId& atom_id, /** * @brief A function that commits the molecule move if it is legal */ -void commit_molecule_move(const AtomBlockId& atom_id, +void commit_atom_move(const AtomBlockId& atom_id, const ClusterBlockId& old_clb, - t_lb_router_data* old_router_data, + t_pb* old_pb, + t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, bool during_packing); From d6dc65c1676b24ce8117ea56e3a1841726b84540 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Tue, 31 May 2022 17:51:26 -0400 Subject: [PATCH 03/17] Remove pad_loc_type argument and refactor the code for more readability --- vpr/src/base/vpr_api.cpp | 2 ++ vpr/src/base/vpr_context.h | 5 +++++ vpr/src/base/vpr_types.h | 16 ++++++++-------- vpr/src/pack/re_cluster.cpp | 17 +++-------------- vpr/src/pack/re_cluster.h | 1 - vpr/src/pack/re_cluster_util.cpp | 25 +++++++++++++++---------- vpr/src/pack/re_cluster_util.h | 13 ++++++------- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/vpr/src/base/vpr_api.cpp b/vpr/src/base/vpr_api.cpp index d2b55121543..c4e79938270 100644 --- a/vpr/src/base/vpr_api.cpp +++ b/vpr/src/base/vpr_api.cpp @@ -350,7 +350,9 @@ void vpr_init_with_options(const t_options* options, t_vpr_setup* vpr_setup, t_a fflush(stdout); auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& device_ctx = g_vpr_ctx.mutable_device(); helper_ctx.lb_type_rr_graphs = vpr_setup->PackerRRGraph; + device_ctx.pad_loc_type = vpr_setup->PlacerOpts.pad_loc_type; } bool vpr_flow(t_vpr_setup& vpr_setup, t_arch& arch) { diff --git a/vpr/src/base/vpr_context.h b/vpr/src/base/vpr_context.h index 750179f5d95..27b8ebb0cf3 100644 --- a/vpr/src/base/vpr_context.h +++ b/vpr/src/base/vpr_context.h @@ -218,6 +218,11 @@ struct DeviceContext : public Context { * Used to determine when reading rrgraph if file is already loaded. */ std::string read_rr_graph_filename; + + /******************************************************************* + * Place Related + *******************************************************************/ + enum e_pad_loc_type pad_loc_type; }; /** diff --git a/vpr/src/base/vpr_types.h b/vpr/src/base/vpr_types.h index c4d66484e2e..b93eba893f4 100644 --- a/vpr/src/base/vpr_types.h +++ b/vpr/src/base/vpr_types.h @@ -278,14 +278,14 @@ class t_pb { // Member functions /** - * @brief Deep copy function for t_pb class - * - * This funcion deeply copies some data members of - * t_pb class to be used to retrieve pb. - * - * This function is currently used in re-clustering API. - */ - void t_pb::pb_deep_copy(const t_pb* rhs); + * @brief Deep copy function for t_pb class + * + * This funcion deeply copies some data members of + * t_pb class to be used to retrieve pb. + * + * This function is currently used in re-clustering API. + */ + void pb_deep_copy(const t_pb* rhs); ///@brief Returns true if this block has not parent pb block bool is_root() const { return parent_pb == nullptr; } diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 71300c89c2a..73b30bf95a7 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -3,7 +3,6 @@ #include "initial_placement.h" bool move_atom_to_new_cluster(const AtomBlockId& atom_id, - const enum e_pad_loc_type& pad_loc_type, std::vector* lb_type_rr_graphs, t_clustering_data& clustering_data, bool during_packing) { @@ -50,7 +49,6 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, //Create new cluster of the same type and mode. ClusterBlockId new_clb(helper_ctx.total_clb_num); is_created = start_new_cluster_for_atom(atom_id, - pad_loc_type, block_type, block_mode, helper_ctx.feasible_block_array_size, @@ -64,18 +62,9 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, //Print the move result if (is_created) { + commit_atom_move(old_clb, new_clb, atom_pb_packup, old_router_data, clustering_data, during_packing); VTR_LOG("Atom:%zu is moved to a new cluster\n", atom_id); - - commit_atom_move(atom_id, old_clb, atom_pb_packup, old_router_data, clustering_data, during_packing); - if (!during_packing) { - int imacro; - g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); - get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); - set_imacro_for_iblk(&imacro, new_clb); - place_one_block(new_clb, pad_loc_type); - } - } - else { + } else { atom_ctx.lookup.set_atom_clb(atom_id, old_clb); atom_ctx.lookup.set_atom_pb(atom_id, atom_pb_packup); VTR_LOG("Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", atom_id); @@ -89,6 +78,6 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, cleanup_pb(atom_pb_packup); delete atom_pb_packup; - + return (is_created); } diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index af6a53703f1..dc958111c70 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -23,7 +23,6 @@ * - Second, during placement (during_packing variable should be false) */ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, - const enum e_pad_loc_type& pad_loc_type, std::vector* lb_type_rr_graphs, t_clustering_data& clustering_data, bool during_packing); diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 5fe98d41acd..6df3f3fd997 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -48,7 +48,6 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, std::vector* lb_type_rr_graphs, ClusterBlockId& old_clb, t_lb_router_data*& router_data) { - //Determine the cluster ID old_clb = atom_to_cluster(atom_id); @@ -61,7 +60,7 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, //check cluster legality bool is_cluster_legal = check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data); - if(is_cluster_legal) { + if (is_cluster_legal) { revert_place_atom_block(atom_id, router_data); } @@ -69,13 +68,14 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, return (is_cluster_legal); } -void commit_atom_move(const AtomBlockId& atom_id, - const ClusterBlockId& old_clb, - t_pb* old_pb, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, - bool during_packing) { +void commit_atom_move(const ClusterBlockId& old_clb, + const ClusterBlockId& new_clb, + t_pb* old_pb, + t_lb_router_data*& old_router_data, + t_clustering_data& clustering_data, + bool during_packing) { auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + auto& device_ctx = g_vpr_ctx.device(); t_pb* temp = old_pb; t_pb* next = temp->parent_pb; @@ -108,7 +108,13 @@ void commit_atom_move(const AtomBlockId& atom_id, free_router_data(old_router_data); old_router_data = nullptr; - + if (!during_packing) { + int imacro; + g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); + get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); + set_imacro_for_iblk(&imacro, new_clb); + place_one_block(new_clb, device_ctx.pad_loc_type); + } } t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index) { @@ -125,7 +131,6 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr } bool start_new_cluster_for_atom(const AtomBlockId atom_id, - const enum e_pad_loc_type& pad_loc_type, const t_logical_block_type_ptr& type, const int mode, const int feasible_block_array_size, diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index fe3d979471a..071cf120d45 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -54,7 +54,6 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, * the higher level routine. */ bool start_new_cluster_for_atom(const AtomBlockId atom_id, - const enum e_pad_loc_type& pad_loc_type, const t_logical_block_type_ptr& type, const int mode, const int feasible_block_array_size, @@ -77,11 +76,11 @@ void fix_clustered_netlist(const AtomBlockId& atom_id, /** * @brief A function that commits the molecule move if it is legal */ -void commit_atom_move(const AtomBlockId& atom_id, - const ClusterBlockId& old_clb, - t_pb* old_pb, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, - bool during_packing); +void commit_atom_move(const ClusterBlockId& old_clb, + const ClusterBlockId& new_clb, + t_pb* old_pb, + t_lb_router_data*& old_router_data, + t_clustering_data& clustering_data, + bool during_packing); #endif From f4af11c917bf956cd1b4c4ca2b2687b17b8dd9d6 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Thu, 2 Jun 2022 14:21:13 -0400 Subject: [PATCH 04/17] upgrade the api to deal with molecules intead of atoms --- vpr/src/pack/re_cluster.cpp | 137 +++++++++++++++++++++++++------ vpr/src/pack/re_cluster.h | 24 ++++-- vpr/src/pack/re_cluster_util.cpp | 109 +++++++++++++----------- vpr/src/pack/re_cluster_util.h | 39 ++++----- 4 files changed, 211 insertions(+), 98 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 73b30bf95a7..418311998df 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -1,11 +1,11 @@ #include "re_cluster.h" #include "re_cluster_util.h" #include "initial_placement.h" +#include "cluster_placement.h" -bool move_atom_to_new_cluster(const AtomBlockId& atom_id, - std::vector* lb_type_rr_graphs, - t_clustering_data& clustering_data, - bool during_packing) { +bool move_mol_to_new_cluster(t_pack_molecule* molecule, + t_clustering_data& clustering_data, + bool during_packing) { auto& cluster_ctx = g_vpr_ctx.clustering(); auto& helper_ctx = g_vpr_ctx.mutable_helper(); auto& device_ctx = g_vpr_ctx.device(); @@ -13,17 +13,24 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, bool is_removed, is_created; ClusterBlockId old_clb; + int molecule_size = get_array_size_of_molecule(molecule); //Backup the original pb of the atom before the move - t_pb* atom_pb_packup = new t_pb; - atom_pb_packup->pb_deep_copy(atom_ctx.lookup.atom_pb(atom_id)); + std::vector mol_pb_backup; + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + t_pb* atom_pb_backup = new t_pb; + atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom])); + mol_pb_backup.push_back(atom_pb_backup); + } + } PartitionRegion temp_cluster_pr; t_lb_router_data* old_router_data = nullptr; t_lb_router_data* router_data = nullptr; //Check that there is a place for a new cluster of the same type - old_clb = atom_to_cluster(atom_id); + old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(old_clb); int block_mode = cluster_ctx.clb_nlist.block_pb(old_clb)->mode; @@ -34,50 +41,126 @@ bool move_atom_to_new_cluster(const AtomBlockId& atom_id, if (helper_ctx.num_used_type_instances[block_type] == num_instances) { VTR_LOG("The utilization of block_type %s is 100%. No space for new clusters\n", block_type->name); - VTR_LOG("Atom %d move aborted\n", atom_id); + VTR_LOG("Atom %d move aborted\n", molecule->atom_block_ids[molecule->root]); return false; } - //remove the atom from its current cluster and check its legality - is_removed = remove_atom_from_cluster(atom_id, lb_type_rr_graphs, old_clb, old_router_data); + //remove the molecule from its current cluster and check its legality + is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); if (!is_removed) { - VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", atom_id); + VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", molecule->atom_block_ids[molecule->root]); return (is_removed); } //Create new cluster of the same type and mode. ClusterBlockId new_clb(helper_ctx.total_clb_num); - is_created = start_new_cluster_for_atom(atom_id, - block_type, - block_mode, - helper_ctx.feasible_block_array_size, - helper_ctx.enable_pin_feasibility_filter, - new_clb, - &router_data, - lb_type_rr_graphs, - temp_cluster_pr, - clustering_data, - during_packing); - - //Print the move result + is_created = start_new_cluster_for_mol(molecule, + block_type, + block_mode, + helper_ctx.feasible_block_array_size, + helper_ctx.enable_pin_feasibility_filter, + new_clb, + &router_data, + temp_cluster_pr, + clustering_data, + during_packing); + + //Commit or revert the move if (is_created) { + commit_atom_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); + VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); + } else { + int atom_idx = 0; + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb); + atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]); + atom_idx++; + } + } + 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]); + } + + //If the move is done after packing not during it, some fixes need to be done on the + //clustered netlist + if (is_created && !during_packing) { + fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb); + } + + for (auto atom_pb_backup : mol_pb_backup) { + cleanup_pb(atom_pb_backup); + delete atom_pb_backup; + } + + return (is_created); +} + +#if 0 +bool move_atom_to_existing_cluster(const AtomBlockId& atom_id, + const ClusterBlockId& new_clb, + bool during_packing, + t_clustering_data& clustering_data) { + //define required contexts + auto& cluster_ctx = g_vpr_ctx.clustering(); + auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& device_ctx = g_vpr_ctx.device(); + auto& atom_ctx = g_vpr_ctx.mutable_atom(); + + //define local variables + bool is_removed, is_added; + + //Check that the old and new clusters are the same type + ClusterBlockId = atom_to_cluster(atom_id); + if(cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { + VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same type", + atom_id); + return false; + } + + //Check that the old and new clusters are the mode + if(cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { + VTR_LOG(tx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { + VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same mode", + atom_id); + return false; + } + + //Backup the original pb of the atom before the move + t_pb* atom_pb_packup = new t_pb; + atom_pb_packup->pb_deep_copy(atom_ctx.lookup.atom_pb(atom_id)); + + //remove the atom from its current cluster and check its legality + is_removed = remove_atom_from_cluster(atom_id, old_clb, old_router_data); + if (!is_removed) { + VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", atom_id); + return false; + } + + + //Add the atom to the new cluster + + + //Commit or revert the move + if (is_added) { commit_atom_move(old_clb, new_clb, atom_pb_packup, old_router_data, clustering_data, during_packing); VTR_LOG("Atom:%zu is moved to a new cluster\n", atom_id); } else { atom_ctx.lookup.set_atom_clb(atom_id, old_clb); atom_ctx.lookup.set_atom_pb(atom_id, atom_pb_packup); - VTR_LOG("Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", atom_id); + VTR_LOG("Atom:%zu move failed. Can't add the atom to the new cluster\n", atom_id); } //If the move is done after packing not during it, some fixes need to be done on the //clustered netlist - if (is_created && !during_packing) { + if (is_added && !during_packing) { fix_clustered_netlist(atom_id, old_clb, new_clb); } + //Free locally allocated memory cleanup_pb(atom_pb_packup); delete atom_pb_packup; - return (is_created); + return (is_added); } +#endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index dc958111c70..b5d05b105b4 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -15,15 +15,29 @@ #include "cluster_util.h" /** - * @brief This function moves an atom out of its cluster and create a new cluster for it + * @brief This function moves a molecule out of its cluster and create a new cluster for it * * This function can be called from 2 spots in the vpr flow. * - First, during packing to optimize the initial clustered netlist * (during_packing variable should be true.) * - Second, during placement (during_packing variable should be false) */ -bool move_atom_to_new_cluster(const AtomBlockId& atom_id, - std::vector* lb_type_rr_graphs, - t_clustering_data& clustering_data, - bool during_packing); +bool move_mol_to_new_cluster(t_pack_molecule* molecule, + t_clustering_data& clustering_data, + bool during_packing); + +#if 0 +/** + * @brief This function moves an atom out of its cluster to another cluster that already exists. + * + * This function can be called from 2 spots in the vpr flow. + * - First, during packing to optimize the initial clustered netlist + * (during_packing variable should be true.) + * - Second, during placement (during_packing variable should be false) + */ +bool move_atom_to_existing_cluster(const AtomBlockId& atom_id, + const ClusterBlockId& new_clb, + bool during_packing, + t_clustering_data& clustering_data); +#endif #endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 6df3f3fd997..16d6c179e0e 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -30,7 +30,8 @@ static void check_net_absorbtion(const AtomNetId atom_net_id, static void fix_cluster_port_after_moving(const ClusterBlockId clb_index); -static void fix_cluster_net_after_moving(const AtomBlockId& atom_id, +static void fix_cluster_net_after_moving(const t_pack_molecule* molecule, + int molecule_size, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb); @@ -44,24 +45,33 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster) { return (cluster_lookup.atoms_in_cluster(cluster)); } -bool remove_atom_from_cluster(const AtomBlockId& atom_id, - std::vector* lb_type_rr_graphs, - ClusterBlockId& old_clb, - t_lb_router_data*& router_data) { +bool remove_mol_from_cluster(const t_pack_molecule* molecule, + int molecule_size, + ClusterBlockId& old_clb, + t_lb_router_data*& router_data) { + auto& helper_ctx = g_vpr_ctx.mutable_helper(); //Determine the cluster ID - old_clb = atom_to_cluster(atom_id); + old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); //re-build router_data structure for this cluster - router_data = lb_load_router_data(lb_type_rr_graphs, old_clb); + router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, old_clb); //remove atom from router_data - remove_atom_from_target(router_data, atom_id); + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + remove_atom_from_target(router_data, molecule->atom_block_ids[i_atom]); + } + } //check cluster legality bool is_cluster_legal = check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data); if (is_cluster_legal) { - revert_place_atom_block(atom_id, router_data); + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + revert_place_atom_block(molecule->atom_block_ids[i_atom], router_data); + } + } } //return true if succeeded @@ -70,29 +80,30 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, void commit_atom_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, - t_pb* old_pb, + std::vector& mol_pbs, t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, bool during_packing) { auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); auto& device_ctx = g_vpr_ctx.device(); - t_pb* temp = old_pb; - t_pb* next = temp->parent_pb; - //char* atom_name = vtr::strdup(temp->name); - bool has_more_children; - - //revert_place_atom_block(atom_id, old_router_data, false); - //delete atom pb - cleanup_pb(temp); + for (auto old_atom_pb : mol_pbs) { + t_pb* temp = old_atom_pb; + t_pb* next = temp->parent_pb; + //char* atom_name = vtr::strdup(temp->name); + bool has_more_children; - has_more_children = count_children_pbs(next); - //keep deleting the parent pbs if they were created only for the removed atom - while (!has_more_children) { - temp = next; - next = next->parent_pb; + //delete atom pb cleanup_pb(temp); + has_more_children = count_children_pbs(next); + //keep deleting the parent pbs if they were created only for the removed atom + while (!has_more_children) { + temp = next; + next = next->parent_pb; + cleanup_pb(temp); + has_more_children = count_children_pbs(next); + } } cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); @@ -130,26 +141,24 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr return (router_data); } -bool start_new_cluster_for_atom(const AtomBlockId atom_id, - const t_logical_block_type_ptr& type, - const int mode, - const int feasible_block_array_size, - bool enable_pin_feasibility_filter, - ClusterBlockId clb_index, - t_lb_router_data** router_data, - std::vector* lb_type_rr_graphs, - PartitionRegion& temp_cluster_pr, - t_clustering_data& clustering_data, - bool during_packing) { +bool start_new_cluster_for_mol(t_pack_molecule* molecule, + const t_logical_block_type_ptr& type, + const int mode, + const int feasible_block_array_size, + bool enable_pin_feasibility_filter, + ClusterBlockId clb_index, + t_lb_router_data** router_data, + PartitionRegion& temp_cluster_pr, + t_clustering_data& clustering_data, + bool during_packing) { auto& atom_ctx = g_vpr_ctx.atom(); auto& floorplanning_ctx = g_vpr_ctx.mutable_floorplanning(); auto& helper_ctx = g_vpr_ctx.mutable_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - t_pack_molecule* molecule = atom_ctx.atom_molecules.find(atom_id)->second; int verbosity = 0; - /*Cluster's PartitionRegion is empty initially, meaning it has no floorplanning constraints*/ + /* Cluster's PartitionRegion is empty initially, meaning it has no floorplanning constraints */ PartitionRegion empty_pr; floorplanning_ctx.cluster_constraints.push_back(empty_pr); @@ -163,7 +172,7 @@ bool start_new_cluster_for_atom(const AtomBlockId atom_id, alloc_and_load_pb_stats(pb, feasible_block_array_size); pb->parent_pb = nullptr; - *router_data = alloc_and_load_router_data(&lb_type_rr_graphs[type->index], type); + *router_data = alloc_and_load_router_data(&(helper_ctx.lb_type_rr_graphs[type->index]), type); e_block_pack_status pack_result = BLK_STATUS_UNDEFINED; pb->mode = mode; @@ -215,18 +224,20 @@ bool start_new_cluster_for_atom(const AtomBlockId atom_id, return (pack_result == BLK_PASSED); } -void fix_clustered_netlist(const AtomBlockId& atom_id, +void fix_clustered_netlist(t_pack_molecule* molecule, + int molecule_size, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb) { fix_cluster_port_after_moving(new_clb); - fix_cluster_net_after_moving(atom_id, old_clb, new_clb); + fix_cluster_net_after_moving(molecule, molecule_size, old_clb, new_clb); } /*******************************************/ /************ static functions *************/ /*******************************************/ -static void fix_cluster_net_after_moving(const AtomBlockId& atom_id, +static void fix_cluster_net_after_moving(const t_pack_molecule* molecule, + int molecule_size, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb) { auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); @@ -244,13 +255,17 @@ static void fix_cluster_net_after_moving(const AtomBlockId& atom_id, } //delete cluster nets that are no longer used - for (auto atom_pin : atom_ctx.nlist.block_pins(atom_id)) { - atom_net_id = atom_ctx.nlist.pin_net(atom_pin); - check_net_absorbtion(atom_net_id, new_clb, old_clb, cluster_pin, previously_absorbed, now_abosrbed); - - if (!previously_absorbed && now_abosrbed) { - cur_clb_net = cluster_ctx.clb_nlist.pin_net(cluster_pin); - cluster_ctx.clb_nlist.remove_net(cur_clb_net); + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + for (auto atom_pin : atom_ctx.nlist.block_pins(molecule->atom_block_ids[i_atom])) { + atom_net_id = atom_ctx.nlist.pin_net(atom_pin); + check_net_absorbtion(atom_net_id, new_clb, old_clb, cluster_pin, previously_absorbed, now_abosrbed); + + if (!previously_absorbed && now_abosrbed) { + cur_clb_net = cluster_ctx.clb_nlist.pin_net(cluster_pin); + cluster_ctx.clb_nlist.remove_net(cur_clb_net); + } + } } } diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 071cf120d45..409c272ecd1 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -17,7 +17,8 @@ */ /** - * @brief A function that returns the cluster ID of an atom ID + * @brief A function that returns the block ID in the clustered netlist + * from its ID in the atom netlist. */ ClusterBlockId atom_to_cluster(const AtomBlockId& atom); @@ -35,17 +36,17 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr const ClusterBlockId& clb_index); /** - * @brief A function that removes an atom from a cluster and check legality of + * @brief A function that removes a molecule from a cluster and check legality of * the old cluster. * * It returns true if the removal is done and the old cluster is legal. * It aborts the removal and returns false if the removal will make the old cluster * illegal */ -bool remove_atom_from_cluster(const AtomBlockId& atom_id, - std::vector* lb_type_rr_graphs, - ClusterBlockId& old_clb, - t_lb_router_data*& router_data); +bool remove_mol_from_cluster(const t_pack_molecule* molecule, + int molecule_size, + ClusterBlockId& old_clb, + t_lb_router_data*& router_data); /** * @brief A function that starts a new cluster for one specific molecule @@ -53,23 +54,23 @@ bool remove_atom_from_cluster(const AtomBlockId& atom_id, * It places the molecule in a specific type and mode that should be passed by * the higher level routine. */ -bool start_new_cluster_for_atom(const AtomBlockId atom_id, - const t_logical_block_type_ptr& type, - const int mode, - const int feasible_block_array_size, - bool enable_pin_feasibility_filter, - ClusterBlockId clb_index, - t_lb_router_data** router_data, - std::vector* lb_type_rr_graphs, - PartitionRegion& temp_cluster_pr, - t_clustering_data& clustering_data, - bool during_packing); +bool start_new_cluster_for_mol(t_pack_molecule* molecule, + const t_logical_block_type_ptr& type, + const int mode, + const int feasible_block_array_size, + bool enable_pin_feasibility_filter, + ClusterBlockId clb_index, + t_lb_router_data** router_data, + PartitionRegion& temp_cluster_pr, + t_clustering_data& clustering_data, + bool during_packing); /** * @brief A function that fix the clustered netlist if the move is performed * after the packing is done and clustered netlist is built */ -void fix_clustered_netlist(const AtomBlockId& atom_id, +void fix_clustered_netlist(t_pack_molecule* molecule, + int molecule_size, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb); @@ -78,7 +79,7 @@ void fix_clustered_netlist(const AtomBlockId& atom_id, */ void commit_atom_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, - t_pb* old_pb, + std::vector& old_mol_pbs, t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, bool during_packing); From 8ded5cf2a3b603d5a485320cdf0716ece2d99788 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 6 Jun 2022 10:18:26 -0400 Subject: [PATCH 05/17] Adding a new function to the re-cluster API that moves a molecule to an existing cluster --- vpr/src/base/vpr_context.h | 3 +- vpr/src/pack/pack.cpp | 20 +++++----- vpr/src/pack/re_cluster.cpp | 67 +++++++++++++++++++------------- vpr/src/pack/re_cluster.h | 7 ++-- vpr/src/pack/re_cluster_util.cpp | 51 +++++++++++++++++++++++- vpr/src/pack/re_cluster_util.h | 10 ++++- 6 files changed, 114 insertions(+), 44 deletions(-) diff --git a/vpr/src/base/vpr_context.h b/vpr/src/base/vpr_context.h index 27b8ebb0cf3..18a1f805f30 100644 --- a/vpr/src/base/vpr_context.h +++ b/vpr/src/base/vpr_context.h @@ -286,7 +286,8 @@ struct ClusteringHelperContext : public Context { int total_clb_num; std::vector* lb_type_rr_graphs; - + t_ext_pin_util_targets target_external_pin_util; + ~ClusteringHelperContext() { free(primitives_list); } diff --git a/vpr/src/pack/pack.cpp b/vpr/src/pack/pack.cpp index e54ff10cba8..1e7bc07d10e 100644 --- a/vpr/src/pack/pack.cpp +++ b/vpr/src/pack/pack.cpp @@ -115,10 +115,10 @@ bool try_pack(t_packer_opts* packer_opts, VTR_LOG("Using inter-cluster delay: %g\n", packer_opts->inter_cluster_net_delay); } - t_ext_pin_util_targets target_external_pin_util = parse_target_external_pin_util(packer_opts->target_external_pin_util); + helper_ctx.target_external_pin_util = parse_target_external_pin_util(packer_opts->target_external_pin_util); t_pack_high_fanout_thresholds high_fanout_thresholds = parse_high_fanout_thresholds(packer_opts->high_fanout_threshold); - VTR_LOG("Packing with pin utilization targets: %s\n", target_external_pin_util_to_string(target_external_pin_util).c_str()); + VTR_LOG("Packing with pin utilization targets: %s\n", target_external_pin_util_to_string(helper_ctx.target_external_pin_util).c_str()); VTR_LOG("Packing with high fanout thresholds: %s\n", high_fanout_thresholds_to_string(high_fanout_thresholds).c_str()); bool allow_unrelated_clustering = false; @@ -151,7 +151,7 @@ bool try_pack(t_packer_opts* packer_opts, allow_unrelated_clustering, balance_block_type_util, lb_type_rr_graphs, - target_external_pin_util, + helper_ctx.target_external_pin_util, high_fanout_thresholds, attraction_groups, floorplan_regions_overfull, @@ -213,7 +213,7 @@ bool try_pack(t_packer_opts* packer_opts, VTR_LOG("Pack iteration is %d\n", pack_iteration); attraction_groups.set_att_group_pulls(4); t_ext_pin_util pin_util(1.0, 1.0); - target_external_pin_util.set_block_pin_util("clb", pin_util); + helper_ctx.target_external_pin_util.set_block_pin_util("clb", pin_util); } } else { @@ -266,11 +266,13 @@ bool try_pack(t_packer_opts* packer_opts, /* Use the re-cluster API to edit it */ /******************* Start *************************/ - enum e_pad_loc_type pad_loc_type; - bool is_removed = move_atom_to_new_cluster(AtomBlockId(4), pad_loc_type, helper_ctx.lb_type_rr_graphs, clustering_data, true); - if (is_removed) { - VTR_LOG("@@@@ Atom is removed\n"); - } + bool is_moved = move_mol_to_new_cluster(atom_ctx.atom_molecules.find(AtomBlockId(5))->second, clustering_data, true); + if (is_moved) + VTR_LOG("Molecule moved ok!\n"); + + is_moved = move_mol_to_existing_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, ClusterBlockId(4), true, clustering_data); + if(is_moved) + VTR_LOG("Molecule moved ok!\n"); /******************** End **************************/ diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 418311998df..897f24885b9 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -15,7 +15,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, ClusterBlockId old_clb; int molecule_size = get_array_size_of_molecule(molecule); - //Backup the original pb of the atom before the move + //Backup the original pb of the molecule before the move std::vector mol_pb_backup; for (int i_atom = 0; i_atom < molecule_size; i_atom++) { if (molecule->atom_block_ids[i_atom]) { @@ -45,7 +45,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, return false; } - //remove the molecule from its current cluster and check its legality + //remove the molecule from its current cluster and check the cluster legality after is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); if (!is_removed) { @@ -68,7 +68,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, //Commit or revert the move if (is_created) { - commit_atom_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); + commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { int atom_idx = 0; @@ -96,71 +96,82 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, return (is_created); } -#if 0 -bool move_atom_to_existing_cluster(const AtomBlockId& atom_id, +bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, bool during_packing, t_clustering_data& clustering_data) { //define required contexts auto& cluster_ctx = g_vpr_ctx.clustering(); - auto& helper_ctx = g_vpr_ctx.mutable_helper(); - auto& device_ctx = g_vpr_ctx.device(); auto& atom_ctx = g_vpr_ctx.mutable_atom(); //define local variables bool is_removed, is_added; + AtomBlockId root_atom_id = molecule->atom_block_ids[molecule->root]; + int molecule_size = get_array_size_of_molecule(molecule); + t_lb_router_data* old_router_data = nullptr; //Check that the old and new clusters are the same type - ClusterBlockId = atom_to_cluster(atom_id); + ClusterBlockId old_clb = atom_to_cluster(root_atom_id); if(cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same type", - atom_id); + root_atom_id); return false; } //Check that the old and new clusters are the mode if(cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { - VTR_LOG(tx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same mode", - atom_id); + root_atom_id); return false; } //Backup the original pb of the atom before the move - t_pb* atom_pb_packup = new t_pb; - atom_pb_packup->pb_deep_copy(atom_ctx.lookup.atom_pb(atom_id)); + std::vector mol_pb_backup; + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + t_pb* atom_pb_backup = new t_pb; + atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom])); + mol_pb_backup.push_back(atom_pb_backup); + } + } - //remove the atom from its current cluster and check its legality - is_removed = remove_atom_from_cluster(atom_id, old_clb, old_router_data); + //remove the molecule from its current cluster and check the cluster legality + is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); if (!is_removed) { - VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", atom_id); + VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", root_atom_id); return false; } //Add the atom to the new cluster - + is_added = pack_mol_in_existing_cluster(molecule, new_clb, during_packing, clustering_data); //Commit or revert the move if (is_added) { - commit_atom_move(old_clb, new_clb, atom_pb_packup, old_router_data, clustering_data, during_packing); - VTR_LOG("Atom:%zu is moved to a new cluster\n", atom_id); + commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); + VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { - atom_ctx.lookup.set_atom_clb(atom_id, old_clb); - atom_ctx.lookup.set_atom_pb(atom_id, atom_pb_packup); - VTR_LOG("Atom:%zu move failed. Can't add the atom to the new cluster\n", atom_id); + int atom_idx = 0; + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb); + atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]); + atom_idx++; + } + } + 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]); } //If the move is done after packing not during it, some fixes need to be done on the //clustered netlist if (is_added && !during_packing) { - fix_clustered_netlist(atom_id, old_clb, new_clb); + fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb); } - //Free locally allocated memory - cleanup_pb(atom_pb_packup); - delete atom_pb_packup; + for (auto atom_pb_backup : mol_pb_backup) { + cleanup_pb(atom_pb_backup); + delete atom_pb_backup; + } return (is_added); -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index b5d05b105b4..db86f19fc88 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -26,18 +26,17 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, bool during_packing); -#if 0 + /** - * @brief This function moves an atom out of its cluster to another cluster that already exists. + * @brief This function moves a molecule out of its cluster to another cluster that already exists. * * This function can be called from 2 spots in the vpr flow. * - First, during packing to optimize the initial clustered netlist * (during_packing variable should be true.) * - Second, during placement (during_packing variable should be false) */ -bool move_atom_to_existing_cluster(const AtomBlockId& atom_id, +bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, bool during_packing, t_clustering_data& clustering_data); -#endif #endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 16d6c179e0e..4a2fdc90070 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -78,7 +78,7 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, return (is_cluster_legal); } -void commit_atom_move(const ClusterBlockId& old_clb, +void commit_mol_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, std::vector& mol_pbs, t_lb_router_data*& old_router_data, @@ -224,6 +224,55 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, return (pack_result == BLK_PASSED); } +bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, + const ClusterBlockId new_clb, + bool during_packing, + t_clustering_data& clustering_data) { + auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + + PartitionRegion temp_cluster_pr; + e_block_pack_status pack_result = BLK_STATUS_UNDEFINED; + 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); + t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb); + + //re-build router_data structure for this cluster + t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb); + + pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]), + molecule, + helper_ctx.primitives_list, + cluster_ctx.clb_nlist.block_pb(new_clb), + helper_ctx.num_models, + helper_ctx.max_cluster_size, + new_clb, + E_DETAILED_ROUTE_FOR_EACH_ATOM, + router_data, + 0, + helper_ctx.enable_pin_feasibility_filter, + helper_ctx.feasible_block_array_size, + target_ext_pin_util, + temp_cluster_pr); + + // If clustering succeeds, add it to the clb netlist + if (pack_result == BLK_PASSED) { + if (during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[new_clb]); + clustering_data.intra_lb_routing[new_clb] = router_data->saved_lb_nets; + router_data->saved_lb_nets = nullptr; + } else { + cluster_ctx.clb_nlist.block_pb(new_clb)->pb_route.clear(); + cluster_ctx.clb_nlist.block_pb(new_clb)->pb_route = alloc_and_load_pb_route(router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(new_clb)->pb_graph_node); + } + } + + //Free failed clustering + free_router_data(router_data); + router_data = nullptr; + + return (pack_result == BLK_PASSED); +} + void fix_clustered_netlist(t_pack_molecule* molecule, int molecule_size, const ClusterBlockId& old_clb, diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 409c272ecd1..2e851db9ce6 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -77,11 +77,19 @@ void fix_clustered_netlist(t_pack_molecule* molecule, /** * @brief A function that commits the molecule move if it is legal */ -void commit_atom_move(const ClusterBlockId& old_clb, +void commit_mol_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, std::vector& old_mol_pbs, t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, bool during_packing); +/** + * @brief A function that packs a molecule into an existing cluster + */ +bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, + const ClusterBlockId clb_index, + bool during_packing, + t_clustering_data& clustering_data); + #endif From db18a4ab3193ed0098a627463ec9a519a1292dc8 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 6 Jun 2022 13:26:51 -0400 Subject: [PATCH 06/17] Address Vaughn's comments on PR#2034 --- vpr/src/base/vpr_api.cpp | 4 +- vpr/src/base/vpr_context.h | 44 +++++++++++++++++--- vpr/src/base/vpr_types.h | 7 ++++ vpr/src/pack/cluster.cpp | 2 +- vpr/src/pack/cluster_util.h | 8 +++- vpr/src/pack/pack.cpp | 17 ++++---- vpr/src/pack/prepack.h | 3 -- vpr/src/pack/re_cluster.cpp | 25 ++++++------ vpr/src/pack/re_cluster.h | 7 ++-- vpr/src/pack/re_cluster_util.cpp | 70 ++++++++++++++++---------------- vpr/src/pack/re_cluster_util.h | 22 ++++++---- 11 files changed, 128 insertions(+), 81 deletions(-) diff --git a/vpr/src/base/vpr_api.cpp b/vpr/src/base/vpr_api.cpp index c4e79938270..3bb7ea83d07 100644 --- a/vpr/src/base/vpr_api.cpp +++ b/vpr/src/base/vpr_api.cpp @@ -349,7 +349,7 @@ void vpr_init_with_options(const t_options* options, t_vpr_setup* vpr_setup, t_a fflush(stdout); - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& device_ctx = g_vpr_ctx.mutable_device(); helper_ctx.lb_type_rr_graphs = vpr_setup->PackerRRGraph; device_ctx.pad_loc_type = vpr_setup->PlacerOpts.pad_loc_type; @@ -390,7 +390,7 @@ bool vpr_flow(t_vpr_setup& vpr_setup, t_arch& arch) { //clean packing-placement data if (vpr_setup.PackerOpts.doPacking == STAGE_DO) { - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); free_cluster_placement_stats(helper_ctx.cluster_placement_stats); } diff --git a/vpr/src/base/vpr_context.h b/vpr/src/base/vpr_context.h index 18a1f805f30..0b5fea3d301 100644 --- a/vpr/src/base/vpr_context.h +++ b/vpr/src/base/vpr_context.h @@ -55,17 +55,33 @@ struct AtomContext : public Context { /******************************************************************** * Atom Netlist ********************************************************************/ + /** + * @brief constructor + * + * In the constructor initialize the list of pack molecules to nullptr and defines a custom deletor for it + */ AtomContext() : list_of_pack_molecules(nullptr, free_pack_molecules) {} + ///@brief Atom netlist AtomNetlist nlist; ///@brief Mappings to/from the Atom Netlist to physically described .blif models AtomLookup lookup; - ///@brief The molecules associated with each atom block + /** + * @brief The molecules associated with each atom block. + * + * This map is loaded in the pre-packing stage and freed at the very end of vpr flow run. + * The pointers in this multimap is shared with list_of_pack_molecules. + */ std::multimap atom_molecules; + /** + * @brief A linked list of all the packing molecules that are loaded in pre-packing stage. + * + * Is is useful in freeing the pack molecules at the destructor of the Atom context using free_pack_molecules. + */ std::unique_ptr list_of_pack_molecules; }; @@ -270,24 +286,40 @@ struct ClusteringContext : public Context { */ std::map> post_routing_clb_pin_nets; std::map> pre_routing_net_pin_mapping; - - std::map num_used_type_instances; }; +/** + * @brief State relating to helper data structure using in clustering + * + * This should contain helper data structures that is useful in clustering/packing. + * They are encapsulated here as they are useful in clustering and reclustering algorithms that may be used + * in packing or placement stages. + */ struct ClusteringHelperContext : public Context { + // A map used to save the number of used instances from each logical block type. std::map num_used_type_instances; + + // Stats keeper for placement information during packing/clustering t_cluster_placement_stats* cluster_placement_stats; + + // total number of models in the architecture int num_models; + int max_cluster_size; t_pb_graph_node** primitives_list; bool enable_pin_feasibility_filter; int feasible_block_array_size; + // total number of CLBs int total_clb_num; + + // A vector of routing resource nodes within each of logic cluster_ctx.blocks types std::vector* lb_type_rr_graphs; + + // the utilization of external input/output pins during packing t_ext_pin_util_targets target_external_pin_util; - + ~ClusteringHelperContext() { free(primitives_list); } @@ -478,8 +510,8 @@ class VprContext : public Context { const ClusteringContext& clustering() const { return clustering_; } ClusteringContext& mutable_clustering() { return clustering_; } - const ClusteringHelperContext& helper() const { return helper_; } - ClusteringHelperContext& mutable_helper() { return helper_; } + const ClusteringHelperContext& cl_helper() const { return helper_; } + ClusteringHelperContext& mutable_cl_helper() { return helper_; } const PlacementContext& placement() const { return placement_; } PlacementContext& mutable_placement() { return placement_; } diff --git a/vpr/src/base/vpr_types.h b/vpr/src/base/vpr_types.h index b93eba893f4..cd645090a3d 100644 --- a/vpr/src/base/vpr_types.h +++ b/vpr/src/base/vpr_types.h @@ -1666,7 +1666,14 @@ typedef vtr::vector>> t_clb_opins_u typedef std::vector> t_arch_switch_fanin; +/** + * @brief Free the linked list that saves all the packing molecules. + */ void free_pack_molecules(t_pack_molecule* list_of_pack_molecules); + +/** + * @brief Free the linked lists to placement locations based on status of primitive inside placement stats data structure. + */ void free_cluster_placement_stats(t_cluster_placement_stats* cluster_placement_stats); #endif diff --git a/vpr/src/pack/cluster.cpp b/vpr/src/pack/cluster.cpp index b7804c6e2bd..8cac315cdab 100644 --- a/vpr/src/pack/cluster.cpp +++ b/vpr/src/pack/cluster.cpp @@ -147,7 +147,7 @@ std::map do_clustering(const t_packer_opts& pa auto& atom_ctx = g_vpr_ctx.atom(); auto& device_ctx = g_vpr_ctx.mutable_device(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); helper_ctx.enable_pin_feasibility_filter = packer_opts.enable_pin_feasibility_filter; helper_ctx.feasible_block_array_size = packer_opts.feasible_block_array_size; diff --git a/vpr/src/pack/cluster_util.h b/vpr/src/pack/cluster_util.h index cdad7f16bf8..b53cc453586 100644 --- a/vpr/src/pack/cluster_util.h +++ b/vpr/src/pack/cluster_util.h @@ -14,6 +14,11 @@ #include "tatum/echo_writer.hpp" #include "tatum/TimingReporter.hpp" +/** + * @file + * @brief This file includes useful structs and functions for building and modifying clustering + */ + #define AAPACK_MAX_HIGH_FANOUT_EXPLORE 10 /* For high-fanout nets that are ignored, consider a maximum of this many sinks, must be less than packer_opts.feasible_block_array_size */ #define AAPACK_MAX_TRANSITIVE_EXPLORE 40 /* When investigating transitive fanout connections in packing, consider a maximum of this many molecules, must be less than packer_opts.feasible_block_array_size */ @@ -74,7 +79,7 @@ struct t_cluster_progress_stats { int num_unrelated_clustering_attempts = 0; }; -/* Useful data structures for packing */ +/* Useful data structures for creating or modifying clusters */ struct t_clustering_data { vtr::vector*> intra_lb_routing; int* hill_climbing_inputs_avail; @@ -85,6 +90,7 @@ struct t_clustering_data { * list of blocks with i inputs to be hooked up via external interconnect. */ t_molecule_link* unclustered_list_head = nullptr; + //Maintaining a linked list of free molecule data for speed t_molecule_link* memory_pool = nullptr; /* Does the atom block that drives the output of this atom net also appear as a * diff --git a/vpr/src/pack/pack.cpp b/vpr/src/pack/pack.cpp index 1e7bc07d10e..9d458e4b96a 100644 --- a/vpr/src/pack/pack.cpp +++ b/vpr/src/pack/pack.cpp @@ -42,15 +42,13 @@ bool try_pack(t_packer_opts* packer_opts, const t_model* library_models, float interc_delay, std::vector* lb_type_rr_graphs) { - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); std::unordered_set is_clock; std::unordered_map expected_lowest_cost_pb_gnode; //The molecules associated with each atom block const t_model* cur_model; t_clustering_data clustering_data; - //int num_models; std::vector list_of_packing_patterns; - //std::unique_ptr list_of_pack_molecules(nullptr, free_pack_molecules); VTR_LOG("Begin packing '%s'.\n", packer_opts->circuit_file_name.c_str()); /* determine number of models in the architecture */ @@ -265,15 +263,20 @@ bool try_pack(t_packer_opts* packer_opts, /* Packing iterative improvement can be done here */ /* Use the re-cluster API to edit it */ /******************* Start *************************/ - + bool is_moved = move_mol_to_new_cluster(atom_ctx.atom_molecules.find(AtomBlockId(5))->second, clustering_data, true); if (is_moved) VTR_LOG("Molecule moved ok!\n"); - is_moved = move_mol_to_existing_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, ClusterBlockId(4), true, clustering_data); - if(is_moved) + is_moved = move_mol_to_new_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, clustering_data, true); + if (is_moved) VTR_LOG("Molecule moved ok!\n"); - + + /* + * is_moved = move_mol_to_existing_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, ClusterBlockId(4), true, clustering_data); + * if(is_moved) + * VTR_LOG("Molecule moved ok!\n"); + */ /******************** End **************************/ //check clustering and output it diff --git a/vpr/src/pack/prepack.h b/vpr/src/pack/prepack.h index 15d676dd68f..de29a40436b 100644 --- a/vpr/src/pack/prepack.h +++ b/vpr/src/pack/prepack.h @@ -18,7 +18,4 @@ void free_pack_pattern(t_pack_patterns* pack_pattern); t_pack_molecule* alloc_and_load_pack_molecules(t_pack_patterns* list_of_pack_patterns, std::unordered_map& expected_lowest_cost_pb_gnode, const int num_packing_patterns); - -//void free_pack_molecules(t_pack_molecule* list_of_pack_molecules); - #endif diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 897f24885b9..8e7562a9734 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -7,7 +7,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, bool during_packing) { auto& cluster_ctx = g_vpr_ctx.clustering(); - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& device_ctx = g_vpr_ctx.device(); auto& atom_ctx = g_vpr_ctx.mutable_atom(); @@ -61,10 +61,10 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, helper_ctx.feasible_block_array_size, helper_ctx.enable_pin_feasibility_filter, new_clb, - &router_data, - temp_cluster_pr, + during_packing, clustering_data, - during_packing); + &router_data, + temp_cluster_pr); //Commit or revert the move if (is_created) { @@ -97,9 +97,9 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, } bool move_mol_to_existing_cluster(t_pack_molecule* molecule, - const ClusterBlockId& new_clb, - bool during_packing, - t_clustering_data& clustering_data) { + const ClusterBlockId& new_clb, + bool during_packing, + t_clustering_data& clustering_data) { //define required contexts auto& cluster_ctx = g_vpr_ctx.clustering(); auto& atom_ctx = g_vpr_ctx.mutable_atom(); @@ -112,16 +112,16 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //Check that the old and new clusters are the same type ClusterBlockId old_clb = atom_to_cluster(root_atom_id); - if(cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { + if (cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same type", - root_atom_id); + root_atom_id); return false; } //Check that the old and new clusters are the mode - if(cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { + if (cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same mode", - root_atom_id); + root_atom_id); return false; } @@ -142,11 +142,10 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, return false; } - //Add the atom to the new cluster is_added = pack_mol_in_existing_cluster(molecule, new_clb, during_packing, clustering_data); - //Commit or revert the move + //Commit or revert the move if (is_added) { commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index db86f19fc88..99d0d59e412 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -26,7 +26,6 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, bool during_packing); - /** * @brief This function moves a molecule out of its cluster to another cluster that already exists. * @@ -36,7 +35,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, * - Second, during placement (during_packing variable should be false) */ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, - const ClusterBlockId& new_clb, - bool during_packing, - t_clustering_data& clustering_data); + const ClusterBlockId& new_clb, + bool during_packing, + t_clustering_data& clustering_data); #endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 4a2fdc90070..af264625946 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -35,6 +35,7 @@ static void fix_cluster_net_after_moving(const t_pack_molecule* molecule, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb); +/***************** API functions ***********************/ ClusterBlockId atom_to_cluster(const AtomBlockId& atom) { auto& atom_ctx = g_vpr_ctx.atom(); return (atom_ctx.lookup.atom_clb(atom)); @@ -49,7 +50,7 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, t_lb_router_data*& router_data) { - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); //Determine the cluster ID old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); @@ -79,11 +80,11 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, } void commit_mol_move(const ClusterBlockId& old_clb, - const ClusterBlockId& new_clb, - std::vector& mol_pbs, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, - bool during_packing) { + const ClusterBlockId& new_clb, + std::vector& mol_pbs, + t_lb_router_data*& old_router_data, + t_clustering_data& clustering_data, + bool during_packing) { auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); auto& device_ctx = g_vpr_ctx.device(); @@ -147,13 +148,13 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, const int feasible_block_array_size, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, - t_lb_router_data** router_data, - PartitionRegion& temp_cluster_pr, + bool during_packing, t_clustering_data& clustering_data, - bool during_packing) { + t_lb_router_data** router_data, + PartitionRegion& temp_cluster_pr) { auto& atom_ctx = g_vpr_ctx.atom(); auto& floorplanning_ctx = g_vpr_ctx.mutable_floorplanning(); - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); int verbosity = 0; @@ -228,7 +229,7 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId new_clb, bool during_packing, t_clustering_data& clustering_data) { - auto& helper_ctx = g_vpr_ctx.mutable_helper(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); PartitionRegion temp_cluster_pr; @@ -373,13 +374,13 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { int num_output_ports = pb->pb_graph_node->num_output_ports; int num_clock_ports = pb->pb_graph_node->num_clock_ports; - int j, k, ipin, rr_node_index; + int iport, ipb_pin, ipin, rr_node_index; ipin = 0; - for (j = 0; j < num_input_ports; j++) { - ClusterPortId input_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[j].name); - for (k = 0; k < pb->pb_graph_node->num_input_pins[j]; k++) { - pb_graph_pin = &pb->pb_graph_node->input_pins[j][k]; + for (iport = 0; iport < num_input_ports; iport++) { + ClusterPortId input_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[iport].name); + for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_input_pins[iport]; ipb_pin++) { + pb_graph_pin = &pb->pb_graph_node->input_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; VTR_ASSERT(pb_graph_pin->pin_count_in_cluster == ipin); @@ -388,9 +389,9 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { if (atom_net_id) { clb_net_id = cluster_ctx.clb_nlist.create_net(atom_ctx.nlist.net_name(atom_net_id)); atom_ctx.lookup.set_atom_clb_net(atom_net_id, clb_net_id); - ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(input_port_id, (BitIndex)k); + ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(input_port_id, (BitIndex)ipb_pin); if (!cur_pin_id) - cluster_ctx.clb_nlist.create_pin(input_port_id, (BitIndex)k, clb_net_id, PinType::SINK, ipin); + cluster_ctx.clb_nlist.create_pin(input_port_id, (BitIndex)ipb_pin, clb_net_id, PinType::SINK, ipin); else cluster_ctx.clb_nlist.set_pin_net(cur_pin_id, PinType::SINK, clb_net_id); } @@ -400,10 +401,10 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { } } - for (j = 0; j < num_output_ports; j++) { - ClusterPortId output_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + j].name); - for (k = 0; k < pb->pb_graph_node->num_output_pins[j]; k++) { - pb_graph_pin = &pb->pb_graph_node->output_pins[j][k]; + for (iport = 0; iport < num_output_ports; iport++) { + ClusterPortId output_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + iport].name); + for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_output_pins[iport]; ipb_pin++) { + pb_graph_pin = &pb->pb_graph_node->output_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; VTR_ASSERT(pb_graph_pin->pin_count_in_cluster == ipin); @@ -412,11 +413,11 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { if (atom_net_id) { clb_net_id = cluster_ctx.clb_nlist.create_net(atom_ctx.nlist.net_name(atom_net_id)); atom_ctx.lookup.set_atom_clb_net(atom_net_id, clb_net_id); - ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(output_port_id, (BitIndex)k); + ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(output_port_id, (BitIndex)ipb_pin); AtomPinId atom_net_driver = atom_ctx.nlist.net_driver(atom_net_id); bool driver_is_constant = atom_ctx.nlist.pin_is_constant(atom_net_driver); if (!cur_pin_id) - cluster_ctx.clb_nlist.create_pin(output_port_id, (BitIndex)k, clb_net_id, PinType::DRIVER, ipin, driver_is_constant); + cluster_ctx.clb_nlist.create_pin(output_port_id, (BitIndex)ipb_pin, clb_net_id, PinType::DRIVER, ipin, driver_is_constant); else { cluster_ctx.clb_nlist.set_pin_net(cur_pin_id, PinType::DRIVER, clb_net_id); cluster_ctx.clb_nlist.set_pin_is_constant(cur_pin_id, driver_is_constant); @@ -429,10 +430,10 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { } } - for (j = 0; j < num_clock_ports; j++) { - ClusterPortId clock_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + num_output_ports + j].name); - for (k = 0; k < pb->pb_graph_node->num_clock_pins[j]; k++) { - pb_graph_pin = &pb->pb_graph_node->clock_pins[j][k]; + for (iport = 0; iport < num_clock_ports; iport++) { + ClusterPortId clock_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + num_output_ports + iport].name); + for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_clock_pins[iport]; ipb_pin++) { + pb_graph_pin = &pb->pb_graph_node->clock_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; VTR_ASSERT(pb_graph_pin->pin_count_in_cluster == ipin); @@ -441,9 +442,9 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { if (atom_net_id) { clb_net_id = cluster_ctx.clb_nlist.create_net(atom_ctx.nlist.net_name(atom_net_id)); atom_ctx.lookup.set_atom_clb_net(atom_net_id, clb_net_id); - ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(clock_port_id, (BitIndex)k); + ClusterPinId cur_pin_id = cluster_ctx.clb_nlist.find_pin(clock_port_id, (BitIndex)ipb_pin); if (!cur_pin_id) - cluster_ctx.clb_nlist.create_pin(clock_port_id, (BitIndex)k, clb_net_id, PinType::SINK, ipin); + cluster_ctx.clb_nlist.create_pin(clock_port_id, (BitIndex)ipb_pin, clb_net_id, PinType::SINK, ipin); else cluster_ctx.clb_nlist.set_pin_net(cur_pin_id, PinType::SINK, clb_net_id); } @@ -550,12 +551,11 @@ static void fix_atom_pin_mapping(const AtomBlockId blk) { static void load_internal_to_block_net_nums(const t_logical_block_type_ptr type, t_pb_routes& pb_route) { int num_pins = type->pb_graph_head->total_pb_pins; - for (int i = 0; i < num_pins; i++) { - if (!pb_route.count(i)) continue; + for (int ipb_pin = 0; ipb_pin < num_pins; ipb_pin++) { + if (!pb_route.count(ipb_pin)) continue; - //if (pb_route[i].driver_pb_pin_id != OPEN && !pb_route[i].atom_net_id) { - if (pb_route[i].driver_pb_pin_id != OPEN) { - load_atom_index_for_pb_pin(pb_route, i); + if (pb_route[ipb_pin].driver_pb_pin_id != OPEN) { + load_atom_index_for_pb_pin(pb_route, ipb_pin); } } } diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 2e851db9ce6..56e0706abe2 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -30,7 +30,7 @@ ClusterBlockId atom_to_cluster(const AtomBlockId& atom); std::vector cluster_to_atoms(const ClusterBlockId& cluster); /** - * @brief A function that loads the router data for a cluster + * @brief A function that loads the intra-cluster router data of one cluster */ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index); @@ -60,14 +60,18 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, const int feasible_block_array_size, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, - t_lb_router_data** router_data, - PartitionRegion& temp_cluster_pr, + bool during_packing, t_clustering_data& clustering_data, - bool during_packing); + t_lb_router_data** router_data, + PartitionRegion& temp_cluster_pr); /** * @brief A function that fix the clustered netlist if the move is performed * after the packing is done and clustered netlist is built + * + * If you are changing clustering after packing is done, you need to update the clustered netlist by + * deleting the newly absorbed nets and creating nets for the atom nets that become unabsorbed. It also + * fixes the cluster ports for both the old and new clusters. */ void fix_clustered_netlist(t_pack_molecule* molecule, int molecule_size, @@ -78,11 +82,11 @@ void fix_clustered_netlist(t_pack_molecule* molecule, * @brief A function that commits the molecule move if it is legal */ void commit_mol_move(const ClusterBlockId& old_clb, - const ClusterBlockId& new_clb, - std::vector& old_mol_pbs, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, - bool during_packing); + const ClusterBlockId& new_clb, + std::vector& old_mol_pbs, + t_lb_router_data*& old_router_data, + t_clustering_data& clustering_data, + bool during_packing); /** * @brief A function that packs a molecule into an existing cluster From d8d55d141a10d9b91888444fb426df57fd13ed3b Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 6 Jun 2022 21:23:55 -0400 Subject: [PATCH 07/17] Fix memory issues --- vpr/src/pack/re_cluster.cpp | 3 ++- vpr/src/pack/re_cluster_util.cpp | 10 ++++++---- vpr/src/pack/re_cluster_util.h | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 8e7562a9734..a47999e1dc0 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -109,6 +109,7 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, AtomBlockId root_atom_id = molecule->atom_block_ids[molecule->root]; int molecule_size = get_array_size_of_molecule(molecule); t_lb_router_data* old_router_data = nullptr; + std::vector new_clb_atoms = cluster_to_atoms(new_clb); //Check that the old and new clusters are the same type ClusterBlockId old_clb = atom_to_cluster(root_atom_id); @@ -143,7 +144,7 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, } //Add the atom to the new cluster - is_added = pack_mol_in_existing_cluster(molecule, new_clb, during_packing, clustering_data); + is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data); //Commit or revert the move if (is_added) { diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index af264625946..5fb46c5b20b 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -53,9 +53,10 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); //Determine the cluster ID old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); + std::vector old_clb_atoms = cluster_to_atoms(old_clb); //re-build router_data structure for this cluster - router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, old_clb); + router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, old_clb, old_clb_atoms); //remove atom from router_data for (int i_atom = 0; i_atom < molecule_size; i_atom++) { @@ -129,14 +130,14 @@ void commit_mol_move(const ClusterBlockId& old_clb, } } -t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index) { +t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, const ClusterBlockId& clb_index, const std::vector& clb_atoms) { //build data structures used by intra-logic block router auto& cluster_ctx = g_vpr_ctx.clustering(); auto block_type = cluster_ctx.clb_nlist.block_type(clb_index); t_lb_router_data* router_data = alloc_and_load_router_data(&lb_type_rr_graphs[block_type->index], block_type); //iterate over atoms of the current cluster and add them to router data - for (auto atom_id : cluster_to_atoms(clb_index)) { + for (auto atom_id : clb_atoms) { add_atom_as_target(router_data, atom_id); } return (router_data); @@ -227,6 +228,7 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId new_clb, + const std::vector& new_clb_atoms, bool during_packing, t_clustering_data& clustering_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); @@ -238,7 +240,7 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb); //re-build router_data structure for this cluster - t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb); + t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]), molecule, diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 56e0706abe2..5d656bb3884 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -33,7 +33,8 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster); * @brief A function that loads the intra-cluster router data of one cluster */ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr_graphs, - const ClusterBlockId& clb_index); + const ClusterBlockId& clb_index, + const std::vector& clb_atoms); /** * @brief A function that removes a molecule from a cluster and check legality of @@ -93,6 +94,7 @@ void commit_mol_move(const ClusterBlockId& old_clb, */ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId clb_index, + const std::vector& clb_atoms, bool during_packing, t_clustering_data& clustering_data); From d75a57d5055b1e28887ea19cc2e13f3c5cfa7a0c Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 6 Jun 2022 21:39:50 -0400 Subject: [PATCH 08/17] Add condition for placing the new cluster only if it's newly created --- vpr/src/pack/re_cluster.cpp | 4 ++-- vpr/src/pack/re_cluster_util.cpp | 5 +++-- vpr/src/pack/re_cluster_util.h | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index a47999e1dc0..dc5dd1f9b33 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -68,7 +68,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, //Commit or revert the move if (is_created) { - commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); + commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, true); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { int atom_idx = 0; @@ -148,7 +148,7 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //Commit or revert the move if (is_added) { - commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing); + commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, false); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { int atom_idx = 0; diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 5fb46c5b20b..cf728e363ba 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -85,7 +85,8 @@ void commit_mol_move(const ClusterBlockId& old_clb, std::vector& mol_pbs, t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, - bool during_packing) { + bool during_packing, + bool new_clb_created) { auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); auto& device_ctx = g_vpr_ctx.device(); @@ -121,7 +122,7 @@ void commit_mol_move(const ClusterBlockId& old_clb, free_router_data(old_router_data); old_router_data = nullptr; - if (!during_packing) { + if (!during_packing && new_clb_created) { int imacro; g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); get_imacro_from_iblk(&imacro, old_clb, g_vpr_ctx.placement().pl_macros); diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 5d656bb3884..0aa5129e05d 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -87,7 +87,8 @@ void commit_mol_move(const ClusterBlockId& old_clb, std::vector& old_mol_pbs, t_lb_router_data*& old_router_data, t_clustering_data& clustering_data, - bool during_packing); + bool during_packing, + bool new_clb_created); /** * @brief A function that packs a molecule into an existing cluster From d9fa5f77659a6bae81930c0e2c54b1a6a31de354 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Tue, 7 Jun 2022 09:59:42 -0400 Subject: [PATCH 09/17] remove test-only hard-coded molecule moves --- vpr/src/pack/pack.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/vpr/src/pack/pack.cpp b/vpr/src/pack/pack.cpp index 9d458e4b96a..d37ea55df25 100644 --- a/vpr/src/pack/pack.cpp +++ b/vpr/src/pack/pack.cpp @@ -264,19 +264,6 @@ bool try_pack(t_packer_opts* packer_opts, /* Use the re-cluster API to edit it */ /******************* Start *************************/ - bool is_moved = move_mol_to_new_cluster(atom_ctx.atom_molecules.find(AtomBlockId(5))->second, clustering_data, true); - if (is_moved) - VTR_LOG("Molecule moved ok!\n"); - - is_moved = move_mol_to_new_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, clustering_data, true); - if (is_moved) - VTR_LOG("Molecule moved ok!\n"); - - /* - * is_moved = move_mol_to_existing_cluster(atom_ctx.atom_molecules.find(AtomBlockId(4))->second, ClusterBlockId(4), true, clustering_data); - * if(is_moved) - * VTR_LOG("Molecule moved ok!\n"); - */ /******************** End **************************/ //check clustering and output it From 5d0f8e888f8c5627b7d36aea6016e406d0309473 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Thu, 9 Jun 2022 11:14:16 -0400 Subject: [PATCH 10/17] Adding a function to swap two molecules (WIP) --- vpr/src/pack/re_cluster.cpp | 183 ++++++++++++++++++++++++++++++++++++ vpr/src/pack/re_cluster.h | 13 +++ 2 files changed, 196 insertions(+) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index dc5dd1f9b33..45348e18cd6 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -2,6 +2,8 @@ #include "re_cluster_util.h" #include "initial_placement.h" #include "cluster_placement.h" +#include "cluster_router.h" + bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, @@ -174,4 +176,185 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, } return (is_added); +} + +bool swap_two_molecules(t_pack_molecule* molecule_1, + t_pack_molecule* molecule_2, + bool during_packing, + t_clustering_data& clustering_data) { + //define required contexts + auto& cluster_ctx = g_vpr_ctx.clustering(); + auto& atom_ctx = g_vpr_ctx.mutable_atom(); + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); + + //define local variables + PartitionRegion temp_cluster_pr_1, temp_cluster_pr_2; + + e_block_pack_status pack_1_result = BLK_STATUS_UNDEFINED; + e_block_pack_status pack_2_result = BLK_STATUS_UNDEFINED; + + AtomBlockId root_1_atom_id = molecule_1->atom_block_ids[molecule_1->root]; + AtomBlockId root_2_atom_id = molecule_2->atom_block_ids[molecule_2->root]; + + int molecule_1_size = get_array_size_of_molecule(molecule_1); + int molecule_2_size = get_array_size_of_molecule(molecule_2); + + //Check that the 2 clusters are the same type + ClusterBlockId clb_1 = atom_to_cluster(root_1_atom_id); + ClusterBlockId clb_2 = atom_to_cluster(root_2_atom_id); + + t_logical_block_type_ptr block_1_type = cluster_ctx.clb_nlist.block_type(clb_1); + t_logical_block_type_ptr block_2_type = cluster_ctx.clb_nlist.block_type(clb_2); + + t_ext_pin_util target_ext_pin_util = helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(clb_1)->name); + + //Check that the old and new clusters are of the same type + if (cluster_ctx.clb_nlist.block_type(clb_1) != cluster_ctx.clb_nlist.block_type(clb_2)) { + VTR_LOG("swapping atoms:(%zu, %zu) is aborted. The 2 cluster blocks are not of the same type", + root_1_atom_id, root_2_atom_id); + return false; + } + + //Check that the old and new clusters are of the same mode + if (cluster_ctx.clb_nlist.block_pb(clb_1)->mode != cluster_ctx.clb_nlist.block_pb(clb_2)->mode) { + VTR_LOG("swapping atoms:(%zu, %zu) is aborted. The 2 cluster blocks are not of the same mode", + root_1_atom_id, root_2_atom_id); + return false; + } + + //Backup the original pb of the molecule before the move + std::vector mol_1_pb_backup, mol_2_pb_backup; + for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { + if (molecule_1->atom_block_ids[i_atom]) { + t_pb* atom_pb_backup = new t_pb; + atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule_1->atom_block_ids[i_atom])); + mol_1_pb_backup.push_back(atom_pb_backup); + } + } + for (int i_atom = 0; i_atom < molecule_2_size; i_atom++) { + if (molecule_2->atom_block_ids[i_atom]) { + t_pb* atom_pb_backup = new t_pb; + atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule_2->atom_block_ids[i_atom])); + mol_2_pb_backup.push_back(atom_pb_backup); + } + } + + //save the atoms of the 2 clusters + std::vector clb_1_atoms = cluster_to_atoms(clb_1); + std::vector clb_2_atoms = cluster_to_atoms(clb_2); + + //re-load the intra-cluster routing + t_lb_router_data* old_1_router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, clb_1, clb_1_atoms); + t_lb_router_data* old_2_router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, clb_2, clb_2_atoms); + + //remove molecules from their old clusters + for(int i_atom = 0; i_atom < molecule_1_size; i_atom++) { + if(molecule_1->atom_block_ids[i_atom]) { + remove_atom_from_target(old_1_router_data, molecule_1->atom_block_ids[i_atom]); + revert_place_atom_block(molecule_1->atom_block_ids[i_atom], old_1_router_data); + } + } + for(int i_atom = 0; i_atom < molecule_2_size; i_atom++) { + if(molecule_2->atom_block_ids[i_atom]) { + remove_atom_from_target(old_2_router_data, molecule_2->atom_block_ids[i_atom]); + revert_place_atom_block(molecule_2->atom_block_ids[i_atom], old_2_router_data); + } + } + + //add the molecules to their new clusters + pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]), + molecule_1, + helper_ctx.primitives_list, + cluster_ctx.clb_nlist.block_pb(clb_1), + helper_ctx.num_models, + helper_ctx.max_cluster_size, + clb_1, + E_DETAILED_ROUTE_FOR_EACH_ATOM, + old_1_router_data, + 0, + helper_ctx.enable_pin_feasibility_filter, + helper_ctx.feasible_block_array_size, + target_ext_pin_util, + temp_cluster_pr_1); + + pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]), + molecule_2, + helper_ctx.primitives_list, + cluster_ctx.clb_nlist.block_pb(clb_2), + helper_ctx.num_models, + helper_ctx.max_cluster_size, + clb_2, + E_DETAILED_ROUTE_FOR_EACH_ATOM, + old_2_router_data, + 0, + helper_ctx.enable_pin_feasibility_filter, + helper_ctx.feasible_block_array_size, + target_ext_pin_util, + temp_cluster_pr_2); + + //commit the move if succeeded or revert if failed + if (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED) { + if(during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[clb_1]); + free_intra_lb_nets(clustering_data.intra_lb_routing[clb_2]); + + clustering_data.intra_lb_routing[clb_1] = old_1_router_data->saved_lb_nets; + clustering_data.intra_lb_routing[clb_2] = old_2_router_data->saved_lb_nets; + } else { + cluster_ctx.clb_nlist.block_pb(clb_1)->pb_route.clear(); + cluster_ctx.clb_nlist.block_pb(clb_1)->pb_route = alloc_and_load_pb_route(old_1_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(clb_1)->pb_graph_node); + cluster_ctx.clb_nlist.block_pb(clb_2)->pb_route.clear(); + cluster_ctx.clb_nlist.block_pb(clb_2)->pb_route = alloc_and_load_pb_route(old_2_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(clb_1)->pb_graph_node); + + } + + commit_mol_move(clb_1, clb_2, mol_1_pb_backup, old_1_router_data, clustering_data, during_packing, false); + commit_mol_move(clb_2, clb_1, mol_2_pb_backup, old_2_router_data, clustering_data, during_packing, false); + + } else { + int atom_idx = 0; + for(int i_atom = 0; i_atom < molecule_1_size; i_atom++) { + if(molecule_1->atom_block_ids[i_atom]) { + atom_ctx.lookup.set_atom_clb(molecule_1->atom_block_ids[i_atom], clb_1); + atom_ctx.lookup.set_atom_pb(molecule_1->atom_block_ids[i_atom], mol_1_pb_backup[atom_idx]); + } + } + + atom_idx = 0; + for(int i_atom = 0; i_atom < molecule_2_size; i_atom++) { + if(molecule_2->atom_block_ids[i_atom]) { + atom_ctx.lookup.set_atom_clb(molecule_2->atom_block_ids[i_atom], clb_2); + atom_ctx.lookup.set_atom_pb(molecule_2->atom_block_ids[i_atom], mol_2_pb_backup[atom_idx]); + } + } + } + + //If the move is done after packing not during it, some fixes need to be done on the clustered netlist + if(pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED && !during_packing) { + fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); + fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); + } + + //free memory + old_1_router_data->saved_lb_nets = nullptr; + old_2_router_data->saved_lb_nets = nullptr; + + free_router_data(old_1_router_data); + free_router_data(old_2_router_data); + + old_1_router_data = nullptr; + old_2_router_data = nullptr; + + //free backup memory + for(auto atom_pb_backup : mol_1_pb_backup) { + cleanup_pb(atom_pb_backup); + delete atom_pb_backup; + } + for(auto atom_pb_backup : mol_2_pb_backup) { + cleanup_pb(atom_pb_backup); + delete atom_pb_backup; + } + + //return the move result + return (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED); } \ No newline at end of file diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index 99d0d59e412..2dec17c0fcf 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -38,4 +38,17 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, bool during_packing, t_clustering_data& clustering_data); + +/** + * @brief This function swap two molecules between two different clusters. + * + * This function can be called from 2 spots in the vpr flow. + * - First, during packing to optimize the initial clustered netlist + * (during_packing variable should be true.) + * - Second, during placement (during_packing variable should be false) + */ +bool swap_two_molecules(t_pack_molecule* molecule_1, + t_pack_molecule* molecule_2, + bool during_packing, + t_clustering_data& clustering_data); #endif \ No newline at end of file From 638701f5a275c9b7bb49d2074a7a0de704428175 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 27 Jun 2022 12:17:14 -0400 Subject: [PATCH 11/17] remove backing up pb to revert move and rebuild cluster placement stats --- vpr/src/pack/re_cluster.cpp | 88 ++++++-------------- vpr/src/pack/re_cluster_util.cpp | 133 +++++++++++++++++++++---------- vpr/src/pack/re_cluster_util.h | 18 ++++- 3 files changed, 132 insertions(+), 107 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 45348e18cd6..91f385873c4 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -4,35 +4,23 @@ #include "cluster_placement.h" #include "cluster_router.h" - bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, bool during_packing) { auto& cluster_ctx = g_vpr_ctx.clustering(); auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& device_ctx = g_vpr_ctx.device(); - auto& atom_ctx = g_vpr_ctx.mutable_atom(); bool is_removed, is_created; - ClusterBlockId old_clb; + ClusterBlockId old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); + ; int molecule_size = get_array_size_of_molecule(molecule); - //Backup the original pb of the molecule before the move - std::vector mol_pb_backup; - for (int i_atom = 0; i_atom < molecule_size; i_atom++) { - if (molecule->atom_block_ids[i_atom]) { - t_pb* atom_pb_backup = new t_pb; - atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom])); - mol_pb_backup.push_back(atom_pb_backup); - } - } - PartitionRegion temp_cluster_pr; t_lb_router_data* old_router_data = nullptr; t_lb_router_data* router_data = nullptr; //Check that there is a place for a new cluster of the same type - old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(old_clb); int block_mode = cluster_ctx.clb_nlist.block_pb(old_clb)->mode; @@ -48,7 +36,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, } //remove the molecule from its current cluster and check the cluster legality after - is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); + is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing); if (!is_removed) { 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, //Commit or revert the move if (is_created) { - commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, true); + commit_mol_move(old_clb, new_clb, during_packing, true); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { - int atom_idx = 0; - for (int i_atom = 0; i_atom < molecule_size; i_atom++) { - if (molecule->atom_block_ids[i_atom]) { - atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb); - atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]); - atom_idx++; - } - } + revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data); 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]); } + free_router_data(old_router_data); + old_router_data = nullptr; + //If the move is done after packing not during it, some fixes need to be done on the //clustered netlist if (is_created && !during_packing) { fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb); } - for (auto atom_pb_backup : mol_pb_backup) { - cleanup_pb(atom_pb_backup); - delete atom_pb_backup; - } - return (is_created); } @@ -104,7 +83,6 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data) { //define required contexts auto& cluster_ctx = g_vpr_ctx.clustering(); - auto& atom_ctx = g_vpr_ctx.mutable_atom(); //define local variables bool is_removed, is_added; @@ -128,56 +106,39 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, return false; } - //Backup the original pb of the atom before the move - std::vector mol_pb_backup; - for (int i_atom = 0; i_atom < molecule_size; i_atom++) { - if (molecule->atom_block_ids[i_atom]) { - t_pb* atom_pb_backup = new t_pb; - atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[i_atom])); - mol_pb_backup.push_back(atom_pb_backup); - } - } - //remove the molecule from its current cluster and check the cluster legality - is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); + is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing); if (!is_removed) { VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", root_atom_id); return false; } + 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); //Add the atom to the new cluster is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data); //Commit or revert the move if (is_added) { - commit_mol_move(old_clb, new_clb, mol_pb_backup, old_router_data, clustering_data, during_packing, false); + commit_mol_move(old_clb, new_clb, during_packing, false); VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { - int atom_idx = 0; - for (int i_atom = 0; i_atom < molecule_size; i_atom++) { - if (molecule->atom_block_ids[i_atom]) { - atom_ctx.lookup.set_atom_clb(molecule->atom_block_ids[i_atom], old_clb); - atom_ctx.lookup.set_atom_pb(molecule->atom_block_ids[i_atom], mol_pb_backup[atom_idx]); - atom_idx++; - } - } + revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data); 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]); } + free_router_data(old_router_data); + old_router_data = nullptr; + //If the move is done after packing not during it, some fixes need to be done on the //clustered netlist if (is_added && !during_packing) { fix_clustered_netlist(molecule, molecule_size, old_clb, new_clb); } - for (auto atom_pb_backup : mol_pb_backup) { - cleanup_pb(atom_pb_backup); - delete atom_pb_backup; - } - return (is_added); } +#if 0 bool swap_two_molecules(t_pack_molecule* molecule_1, t_pack_molecule* molecule_2, bool during_packing, @@ -262,8 +223,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, } //add the molecules to their new clusters - pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]), - molecule_1, + pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]), + molecule_2, helper_ctx.primitives_list, cluster_ctx.clb_nlist.block_pb(clb_1), helper_ctx.num_models, @@ -277,8 +238,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, target_ext_pin_util, temp_cluster_pr_1); - pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]), - molecule_2, + pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]), + molecule_1, helper_ctx.primitives_list, cluster_ctx.clb_nlist.block_pb(clb_2), helper_ctx.num_models, @@ -292,6 +253,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, target_ext_pin_util, temp_cluster_pr_2); + + //commit the move if succeeded or revert if failed if (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED) { if(during_packing) { @@ -308,8 +271,8 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, } - commit_mol_move(clb_1, clb_2, mol_1_pb_backup, old_1_router_data, clustering_data, during_packing, false); - commit_mol_move(clb_2, clb_1, mol_2_pb_backup, old_2_router_data, clustering_data, during_packing, false); + commit_mol_move(clb_1, clb_2, during_packing, false); + commit_mol_move(clb_2, clb_1, during_packing, false); } else { int atom_idx = 0; @@ -357,4 +320,5 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, //return the move result return (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index cf728e363ba..1ff3be08c3f 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -49,8 +49,16 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster) { bool remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, - t_lb_router_data*& router_data) { + t_lb_router_data*& router_data, + t_clustering_data& clustering_data, + bool during_packing) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); + auto& atom_ctx = g_vpr_ctx.atom(); + auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + + t_pb* temp_pb = const_cast(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[0])); + t_pb* next_pb; + //Determine the cluster ID old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); std::vector old_clb_atoms = cluster_to_atoms(old_clb); @@ -74,54 +82,28 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, revert_place_atom_block(molecule->atom_block_ids[i_atom], router_data); } } - } + cleanup_pb(cluster_ctx.clb_nlist.block_pb(old_clb)); + + if (during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); + clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets; + router_data->saved_lb_nets = nullptr; + } else { + cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); + 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); + } + } //return true if succeeded return (is_cluster_legal); } void commit_mol_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, - std::vector& mol_pbs, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, bool during_packing, bool new_clb_created) { - auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); auto& device_ctx = g_vpr_ctx.device(); - for (auto old_atom_pb : mol_pbs) { - t_pb* temp = old_atom_pb; - t_pb* next = temp->parent_pb; - //char* atom_name = vtr::strdup(temp->name); - bool has_more_children; - - //delete atom pb - cleanup_pb(temp); - - has_more_children = count_children_pbs(next); - //keep deleting the parent pbs if they were created only for the removed atom - while (!has_more_children) { - temp = next; - next = next->parent_pb; - cleanup_pb(temp); - has_more_children = count_children_pbs(next); - } - } - - cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); - 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); - - if (during_packing) { - free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); - //delete clustering_data.intra_lb_routing[old_clb]; - clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets; - old_router_data->saved_lb_nets = nullptr; - } - - free_router_data(old_router_data); - old_router_data = nullptr; - if (!during_packing && new_clb_created) { int imacro; g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); @@ -239,6 +221,7 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, e_block_pack_status pack_result = BLK_STATUS_UNDEFINED; 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); t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb); + t_pb* temp_pb = cluster_ctx.clb_nlist.block_pb(new_clb); //re-build router_data structure for this cluster t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); @@ -246,14 +229,15 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]), molecule, helper_ctx.primitives_list, - cluster_ctx.clb_nlist.block_pb(new_clb), + temp_pb, helper_ctx.num_models, helper_ctx.max_cluster_size, new_clb, E_DETAILED_ROUTE_FOR_EACH_ATOM, router_data, 0, - helper_ctx.enable_pin_feasibility_filter, + //helper_ctx.enable_pin_feasibility_filter, + false, helper_ctx.feasible_block_array_size, target_ext_pin_util, temp_cluster_pr); @@ -285,6 +269,40 @@ void fix_clustered_netlist(t_pack_molecule* molecule, fix_cluster_net_after_moving(molecule, molecule_size, old_clb, new_clb); } +void revert_mol_move(const ClusterBlockId& old_clb, + t_pack_molecule* molecule, + t_lb_router_data*& old_router_data, + bool during_packing, + t_clustering_data& clustering_data) { + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); + auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + + PartitionRegion temp_cluster_pr_original; + e_block_pack_status pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[cluster_ctx.clb_nlist.block_type(old_clb)->index]), + molecule, + helper_ctx.primitives_list, + cluster_ctx.clb_nlist.block_pb(old_clb), + helper_ctx.num_models, + helper_ctx.max_cluster_size, + old_clb, + E_DETAILED_ROUTE_FOR_EACH_ATOM, + old_router_data, + 0, + helper_ctx.enable_pin_feasibility_filter, + helper_ctx.feasible_block_array_size, + helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(old_clb)->name), + temp_cluster_pr_original); + + VTR_ASSERT(pack_result == BLK_PASSED); + if (during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); + clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets; + old_router_data->saved_lb_nets = nullptr; + } else { + cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); + 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); + } +} /*******************************************/ /************ static functions *************/ /*******************************************/ @@ -592,4 +610,37 @@ static bool count_children_pbs(const t_pb* pb) { } } return false; -} \ No newline at end of file +} + +#if 1 +void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, + const std::vector& clb_atoms, + int type_idx, + int mode) { + auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); + auto& cluster_ctx = g_vpr_ctx.clustering(); + auto& atom_ctx = g_vpr_ctx.atom(); + + t_cluster_placement_stats* cluster_placement_stats = &(helper_ctx.cluster_placement_stats[type_idx]); + + reset_cluster_placement_stats(cluster_placement_stats); + set_mode_cluster_placement_stats(cluster_ctx.clb_nlist.block_pb(clb_index)->pb_graph_node, mode); + + std::set list_of_pb_atom_nodes; + t_cluster_placement_primitive* cur_cluster_placement_primitive; + + for (auto& atom_blk : clb_atoms) { + list_of_pb_atom_nodes.insert(atom_ctx.lookup.atom_pb(atom_blk)->pb_graph_node); + } + + for (int i = 0; i < cluster_placement_stats->num_pb_types; i++) { + cur_cluster_placement_primitive = cluster_placement_stats->valid_primitives[i]->next_primitive; + while (cur_cluster_placement_primitive != nullptr) { + if (list_of_pb_atom_nodes.find(cur_cluster_placement_primitive->pb_graph_node) != list_of_pb_atom_nodes.end()) { + cur_cluster_placement_primitive->valid = false; + } + cur_cluster_placement_primitive = cur_cluster_placement_primitive->next_primitive; + } + } +} +#endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 7c2c59bd232..4ff5ea4b165 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -50,7 +50,9 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr bool remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, - t_lb_router_data*& router_data); + t_lb_router_data*& router_data, + t_clustering_data& clustering_data, + bool during_packing); /** * @brief A function that starts a new cluster for one specific molecule @@ -87,12 +89,15 @@ void fix_clustered_netlist(t_pack_molecule* molecule, */ void commit_mol_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, - std::vector& old_mol_pbs, - t_lb_router_data*& old_router_data, - t_clustering_data& clustering_data, bool during_packing, bool new_clb_created); +void revert_mol_move(const ClusterBlockId& old_clb, + t_pack_molecule* molecule, + t_lb_router_data*& old_router_data, + bool during_packing, + t_clustering_data& clustering_data); + /** * @brief A function that packs a molecule into an existing cluster */ @@ -102,4 +107,9 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, bool during_packing, t_clustering_data& clustering_data); +void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, + const std::vector& clb_atoms, + int type_idx, + int mode); + #endif From 233dc26e98556a0251f9dd32c6f1652cf0a80299 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 27 Jun 2022 13:38:32 -0400 Subject: [PATCH 12/17] Change the printing statements to print based on verbosity --- vpr/src/pack/re_cluster.cpp | 53 ++++++++++++-------- vpr/src/pack/re_cluster.h | 4 +- vpr/src/pack/re_cluster_util.cpp | 83 +++++++++++++++++--------------- vpr/src/pack/re_cluster_util.h | 19 +++++--- 4 files changed, 92 insertions(+), 67 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 91f385873c4..48847de300a 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -6,7 +6,8 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, - bool during_packing) { + bool during_packing, + int verbosity) { auto& cluster_ctx = g_vpr_ctx.clustering(); auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& device_ctx = g_vpr_ctx.device(); @@ -30,17 +31,23 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, } if (helper_ctx.num_used_type_instances[block_type] == num_instances) { - VTR_LOG("The utilization of block_type %s is 100%. No space for new clusters\n", block_type->name); - VTR_LOG("Atom %d move aborted\n", molecule->atom_block_ids[molecule->root]); + VTR_LOGV(verbosity > 4, "The utilization of block_type %s is 100%. No space for new clusters\n", block_type->name); + VTR_LOGV(verbosity > 4, "Atom %d move aborted\n", molecule->atom_block_ids[molecule->root]); return false; } - //remove the molecule from its current cluster and check the cluster legality after - is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing); + //remove the molecule from its current cluster + remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); - if (!is_removed) { - VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", molecule->atom_block_ids[molecule->root]); - return (is_removed); + //check old cluster legality after removing the molecule + is_removed = is_cluster_legal(old_router_data); + + //if the cluster is legal, commit the molecule removal. Otherwise, abort the move + if (is_removed) { + commit_mol_removal(molecule, molecule_size, old_clb, during_packing, old_router_data, clustering_data); + } else { + VTR_LOGV(verbosity > 4, "Atom: %zu move failed. Can't remove it from the old cluster\n", molecule->atom_block_ids[molecule->root]); + return false; } //Create new cluster of the same type and mode. @@ -52,6 +59,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, helper_ctx.enable_pin_feasibility_filter, new_clb, during_packing, + verbosity, clustering_data, &router_data, temp_cluster_pr); @@ -59,10 +67,10 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, //Commit or revert the move if (is_created) { commit_mol_move(old_clb, new_clb, during_packing, true); - VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); + VTR_LOGV(verbosity > 4, "Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data); - 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]); + VTR_LOGV(verbosity > 4, "Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", molecule->atom_block_ids[molecule->root]); } free_router_data(old_router_data); @@ -80,6 +88,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, bool during_packing, + int verbosity, t_clustering_data& clustering_data) { //define required contexts auto& cluster_ctx = g_vpr_ctx.clustering(); @@ -94,36 +103,42 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //Check that the old and new clusters are the same type ClusterBlockId old_clb = atom_to_cluster(root_atom_id); if (cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { - VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same type", + VTR_LOGV(verbosity > 4, "Atom:%zu move aborted. New and old cluster blocks are not of the same type", root_atom_id); return false; } //Check that the old and new clusters are the mode if (cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { - VTR_LOG("Atom:%zu move aborted. New and old cluster blocks are not of the same mode", + VTR_LOGV(verbosity > 4, "Atom:%zu move aborted. New and old cluster blocks are not of the same mode", root_atom_id); return false; } - //remove the molecule from its current cluster and check the cluster legality - is_removed = remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data, clustering_data, during_packing); - if (!is_removed) { - VTR_LOG("Atom: %zu move failed. Can't remove it from the old cluster\n", root_atom_id); + //remove the molecule from its current cluster + remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); + + //check old cluster legality after removing the molecule + is_removed = is_cluster_legal(old_router_data); + + //if the cluster is legal, commit the molecule removal. Otherwise, abort the move + if (is_removed) { + commit_mol_removal(molecule, molecule_size, old_clb, during_packing, old_router_data, clustering_data); + } else { + VTR_LOGV(verbosity > 4, "Atom: %zu move failed. Can't remove it from the old cluster\n", root_atom_id); return false; } - 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); //Add the atom to the new cluster is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data); //Commit or revert the move if (is_added) { commit_mol_move(old_clb, new_clb, during_packing, false); - VTR_LOG("Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); + VTR_LOGV(verbosity > 4, "Atom:%zu is moved to a new cluster\n", molecule->atom_block_ids[molecule->root]); } else { revert_mol_move(old_clb, molecule, old_router_data, during_packing, clustering_data); - 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]); + VTR_LOGV(verbosity > 4, "Atom:%zu move failed. Can't start a new cluster of the same type and mode\n", molecule->atom_block_ids[molecule->root]); } free_router_data(old_router_data); diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index 2dec17c0fcf..18463e2a82d 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -24,7 +24,8 @@ */ bool move_mol_to_new_cluster(t_pack_molecule* molecule, t_clustering_data& clustering_data, - bool during_packing); + bool during_packing, + int verbosity); /** * @brief This function moves a molecule out of its cluster to another cluster that already exists. @@ -37,6 +38,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, bool during_packing, + int verbosity, t_clustering_data& clustering_data); /** diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 1ff3be08c3f..5ae445bba77 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -35,6 +35,11 @@ static void fix_cluster_net_after_moving(const t_pack_molecule* molecule, const ClusterBlockId& old_clb, const ClusterBlockId& new_clb); +static void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, + const std::vector& clb_atoms, + int type_idx, + int mode); + /***************** API functions ***********************/ ClusterBlockId atom_to_cluster(const AtomBlockId& atom) { auto& atom_ctx = g_vpr_ctx.atom(); @@ -46,18 +51,11 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster) { return (cluster_lookup.atoms_in_cluster(cluster)); } -bool remove_mol_from_cluster(const t_pack_molecule* molecule, +void remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, - t_lb_router_data*& router_data, - t_clustering_data& clustering_data, - bool during_packing) { + t_lb_router_data*& router_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); - auto& atom_ctx = g_vpr_ctx.atom(); - auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - - t_pb* temp_pb = const_cast(atom_ctx.lookup.atom_pb(molecule->atom_block_ids[0])); - t_pb* next_pb; //Determine the cluster ID old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); @@ -72,30 +70,6 @@ bool remove_mol_from_cluster(const t_pack_molecule* molecule, remove_atom_from_target(router_data, molecule->atom_block_ids[i_atom]); } } - - //check cluster legality - bool is_cluster_legal = check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data); - - if (is_cluster_legal) { - for (int i_atom = 0; i_atom < molecule_size; i_atom++) { - if (molecule->atom_block_ids[i_atom]) { - revert_place_atom_block(molecule->atom_block_ids[i_atom], router_data); - } - } - - cleanup_pb(cluster_ctx.clb_nlist.block_pb(old_clb)); - - if (during_packing) { - free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); - clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets; - router_data->saved_lb_nets = nullptr; - } else { - cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); - 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); - } - } - //return true if succeeded - return (is_cluster_legal); } void commit_mol_move(const ClusterBlockId& old_clb, @@ -133,6 +107,7 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, bool during_packing, + int verbosity, t_clustering_data& clustering_data, t_lb_router_data** router_data, PartitionRegion& temp_cluster_pr) { @@ -141,8 +116,6 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); - int verbosity = 0; - /* Cluster's PartitionRegion is empty initially, meaning it has no floorplanning constraints */ PartitionRegion empty_pr; floorplanning_ctx.cluster_constraints.push_back(empty_pr); @@ -181,7 +154,7 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, // If clustering succeeds, add it to the clb netlist if (pack_result == BLK_PASSED) { - VTR_LOGV(verbosity > 2, "\tPASSED_SEED: Block Type %s\n", type->name); + VTR_LOGV(verbosity > 4, "\tPASSED_SEED: Block Type %s\n", type->name); //Once clustering succeeds, add it to the clb netlist if (pb->name != nullptr) { free(pb->name); @@ -223,6 +196,10 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb); t_pb* temp_pb = cluster_ctx.clb_nlist.block_pb(new_clb); + + //re-build cluster placement stats + 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); + //re-build router_data structure for this cluster t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); @@ -612,8 +589,7 @@ static bool count_children_pbs(const t_pb* pb) { return false; } -#if 1 -void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, +static void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, const std::vector& clb_atoms, int type_idx, int mode) { @@ -643,4 +619,33 @@ void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, } } } -#endif \ No newline at end of file + +bool is_cluster_legal(t_lb_router_data*& router_data) { + return (check_cluster_legality(0, E_DETAILED_ROUTE_AT_END_ONLY, router_data)); +} + +void commit_mol_removal(const t_pack_molecule* molecule, + const int& molecule_size, + const ClusterBlockId& old_clb, + bool during_packing, + t_lb_router_data*& router_data, + t_clustering_data& clustering_data) { + auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); + + for (int i_atom = 0; i_atom < molecule_size; i_atom++) { + if (molecule->atom_block_ids[i_atom]) { + revert_place_atom_block(molecule->atom_block_ids[i_atom], router_data); + } + } + + cleanup_pb(cluster_ctx.clb_nlist.block_pb(old_clb)); + + if (during_packing) { + free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); + clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets; + router_data->saved_lb_nets = nullptr; + } else { + cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); + 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); + } +} \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 4ff5ea4b165..5e0b4fd75b3 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -47,12 +47,10 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr * It aborts the removal and returns false if the removal will make the old cluster * illegal */ -bool remove_mol_from_cluster(const t_pack_molecule* molecule, +void remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, - t_lb_router_data*& router_data, - t_clustering_data& clustering_data, - bool during_packing); + t_lb_router_data*& router_data); /** * @brief A function that starts a new cluster for one specific molecule @@ -67,6 +65,7 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, bool enable_pin_feasibility_filter, ClusterBlockId clb_index, bool during_packing, + int verbosity, t_clustering_data& clustering_data, t_lb_router_data** router_data, PartitionRegion& temp_cluster_pr); @@ -107,9 +106,13 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, bool during_packing, t_clustering_data& clustering_data); -void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, - const std::vector& clb_atoms, - int type_idx, - int mode); +bool is_cluster_legal(t_lb_router_data*& router_data); + +void commit_mol_removal(const t_pack_molecule* molecule, + const int& molecule_size, + const ClusterBlockId& old_clb, + bool during_packing, + t_lb_router_data*& router_data, + t_clustering_data& clustering_data); #endif From 0af92da347a22c0c4879e334833561734be41d15 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 27 Jun 2022 14:21:01 -0400 Subject: [PATCH 13/17] re-order parameters and some refactoring --- vpr/src/pack/re_cluster.cpp | 19 +++++----------- vpr/src/pack/re_cluster.h | 4 ++-- vpr/src/pack/re_cluster_util.cpp | 37 +++++++++++++++++++++++++------- vpr/src/pack/re_cluster_util.h | 4 ++++ 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 48847de300a..747cbdcb719 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -5,9 +5,9 @@ #include "cluster_router.h" bool move_mol_to_new_cluster(t_pack_molecule* molecule, - t_clustering_data& clustering_data, bool during_packing, - int verbosity) { + int verbosity, + t_clustering_data& clustering_data) { auto& cluster_ctx = g_vpr_ctx.clustering(); auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& device_ctx = g_vpr_ctx.device(); @@ -99,21 +99,12 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, int molecule_size = get_array_size_of_molecule(molecule); t_lb_router_data* old_router_data = nullptr; std::vector new_clb_atoms = cluster_to_atoms(new_clb); - - //Check that the old and new clusters are the same type ClusterBlockId old_clb = atom_to_cluster(root_atom_id); - if (cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { - VTR_LOGV(verbosity > 4, "Atom:%zu move aborted. New and old cluster blocks are not of the same type", - root_atom_id); - return false; - } - //Check that the old and new clusters are the mode - if (cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { - VTR_LOGV(verbosity > 4, "Atom:%zu move aborted. New and old cluster blocks are not of the same mode", - root_atom_id); + //check old and new clusters compitability + bool is_compitable = check_type_and_mode_compitability(old_clb, new_clb, verbosity); + if (!is_compitable) return false; - } //remove the molecule from its current cluster remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index 18463e2a82d..d2e3e3d6f78 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -23,9 +23,9 @@ * - Second, during placement (during_packing variable should be false) */ bool move_mol_to_new_cluster(t_pack_molecule* molecule, - t_clustering_data& clustering_data, bool during_packing, - int verbosity); + int verbosity, + t_clustering_data& clustering_data); /** * @brief This function moves a molecule out of its cluster to another cluster that already exists. diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index 5ae445bba77..e4f9e54b11e 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -17,7 +17,7 @@ const char* name_suffix = "_m"; //static void set_atom_pin_mapping(const ClusteredNetlist& clb_nlist, const AtomBlockId atom_blk, const AtomPortId atom_port, const t_pb_graph_pin* gpin); static void load_atom_index_for_pb_pin(t_pb_routes& pb_route, int ipin); static void load_internal_to_block_net_nums(const t_logical_block_type_ptr type, t_pb_routes& pb_route); -static bool count_children_pbs(const t_pb* pb); +//static bool count_children_pbs(const t_pb* pb); static void fix_atom_pin_mapping(const AtomBlockId blk); static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index); @@ -36,9 +36,9 @@ static void fix_cluster_net_after_moving(const t_pack_molecule* molecule, const ClusterBlockId& new_clb); static void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, - const std::vector& clb_atoms, - int type_idx, - int mode); + const std::vector& clb_atoms, + int type_idx, + int mode); /***************** API functions ***********************/ ClusterBlockId atom_to_cluster(const AtomBlockId& atom) { @@ -196,7 +196,6 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, t_logical_block_type_ptr block_type = cluster_ctx.clb_nlist.block_type(new_clb); t_pb* temp_pb = cluster_ctx.clb_nlist.block_pb(new_clb); - //re-build cluster placement stats 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); @@ -575,6 +574,7 @@ static void load_atom_index_for_pb_pin(t_pb_routes& pb_route, int ipin) { pb_route[driver].sink_pb_pin_ids.push_back(ipin); } +#if 0 static bool count_children_pbs(const t_pb* pb) { if (pb == nullptr) return 0; @@ -588,11 +588,12 @@ static bool count_children_pbs(const t_pb* pb) { } return false; } +#endif static void rebuild_cluster_placemet_stats(const ClusterBlockId& clb_index, - const std::vector& clb_atoms, - int type_idx, - int mode) { + const std::vector& clb_atoms, + int type_idx, + int mode) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.clustering(); auto& atom_ctx = g_vpr_ctx.atom(); @@ -648,4 +649,24 @@ void commit_mol_removal(const t_pack_molecule* molecule, cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); 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); } +} + +bool check_type_and_mode_compitability(const ClusterBlockId& old_clb, + const ClusterBlockId& new_clb, + int verbosity) { + auto& cluster_ctx = g_vpr_ctx.clustering(); + + //Check that the old and new clusters are the same type + if (cluster_ctx.clb_nlist.block_type(old_clb) != cluster_ctx.clb_nlist.block_type(new_clb)) { + VTR_LOGV(verbosity > 4, "Move aborted. New and old cluster blocks are not of the same type"); + return false; + } + + //Check that the old and new clusters are the mode + if (cluster_ctx.clb_nlist.block_pb(old_clb)->mode != cluster_ctx.clb_nlist.block_pb(new_clb)->mode) { + VTR_LOGV(verbosity > 4, "Move aborted. New and old cluster blocks are not of the same mode"); + return false; + } + + return true; } \ No newline at end of file diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 5e0b4fd75b3..03db396e778 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -115,4 +115,8 @@ void commit_mol_removal(const t_pack_molecule* molecule, t_lb_router_data*& router_data, t_clustering_data& clustering_data); +bool check_type_and_mode_compitability(const ClusterBlockId& old_clb, + const ClusterBlockId& new_clb, + int verbosity); + #endif From 0d44ae5343398da10f2996e6f4ca9c4b80913667 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Wed, 29 Jun 2022 11:39:02 -0400 Subject: [PATCH 14/17] swapping two molecule first implementation (WIP) --- vpr/src/pack/re_cluster.cpp | 160 +++++++++++------------------------- vpr/src/pack/re_cluster.h | 1 + 2 files changed, 49 insertions(+), 112 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 747cbdcb719..efdea33061a 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -144,10 +144,11 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, return (is_added); } -#if 0 +#if 1 bool swap_two_molecules(t_pack_molecule* molecule_1, t_pack_molecule* molecule_2, bool during_packing, + int verbosity, t_clustering_data& clustering_data) { //define required contexts auto& cluster_ctx = g_vpr_ctx.clustering(); @@ -157,8 +158,7 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, //define local variables PartitionRegion temp_cluster_pr_1, temp_cluster_pr_2; - e_block_pack_status pack_1_result = BLK_STATUS_UNDEFINED; - e_block_pack_status pack_2_result = BLK_STATUS_UNDEFINED; + bool mol_1_success, mol_2_success; AtomBlockId root_1_atom_id = molecule_1->atom_block_ids[molecule_1->root]; AtomBlockId root_2_atom_id = molecule_2->atom_block_ids[molecule_2->root]; @@ -170,25 +170,17 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, ClusterBlockId clb_1 = atom_to_cluster(root_1_atom_id); ClusterBlockId clb_2 = atom_to_cluster(root_2_atom_id); - t_logical_block_type_ptr block_1_type = cluster_ctx.clb_nlist.block_type(clb_1); - t_logical_block_type_ptr block_2_type = cluster_ctx.clb_nlist.block_type(clb_2); + //t_logical_block_type_ptr block_1_type = cluster_ctx.clb_nlist.block_type(clb_1); + //t_logical_block_type_ptr block_2_type = cluster_ctx.clb_nlist.block_type(clb_2); - t_ext_pin_util target_ext_pin_util = helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(clb_1)->name); + //t_ext_pin_util target_ext_pin_util = helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(clb_1)->name); //Check that the old and new clusters are of the same type - if (cluster_ctx.clb_nlist.block_type(clb_1) != cluster_ctx.clb_nlist.block_type(clb_2)) { - VTR_LOG("swapping atoms:(%zu, %zu) is aborted. The 2 cluster blocks are not of the same type", - root_1_atom_id, root_2_atom_id); - return false; - } - - //Check that the old and new clusters are of the same mode - if (cluster_ctx.clb_nlist.block_pb(clb_1)->mode != cluster_ctx.clb_nlist.block_pb(clb_2)->mode) { - VTR_LOG("swapping atoms:(%zu, %zu) is aborted. The 2 cluster blocks are not of the same mode", - root_1_atom_id, root_2_atom_id); + bool is_compitable = check_type_and_mode_compitability(clb_1, clb_2, verbosity); + if (!is_compitable) return false; - } +#if 0 //Backup the original pb of the molecule before the move std::vector mol_1_pb_backup, mol_2_pb_backup; for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { @@ -205,126 +197,70 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, mol_2_pb_backup.push_back(atom_pb_backup); } } +#endif - //save the atoms of the 2 clusters - std::vector clb_1_atoms = cluster_to_atoms(clb_1); - std::vector clb_2_atoms = cluster_to_atoms(clb_2); + t_lb_router_data* old_1_router_data = nullptr; + t_lb_router_data* old_2_router_data = nullptr; - //re-load the intra-cluster routing - t_lb_router_data* old_1_router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, clb_1, clb_1_atoms); - t_lb_router_data* old_2_router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, clb_2, clb_2_atoms); + //remove the molecule from its current cluster + remove_mol_from_cluster(molecule_1, molecule_1_size, clb_1, old_1_router_data); + //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); + + //bool is_removed = is_cluster_legal(old_1_router_data); + //bool is_removed_2 = is_cluster_legal(old_2_router_data); - //remove molecules from their old clusters - for(int i_atom = 0; i_atom < molecule_1_size; i_atom++) { - if(molecule_1->atom_block_ids[i_atom]) { - remove_atom_from_target(old_1_router_data, molecule_1->atom_block_ids[i_atom]); + //commit_mol_removal(molecule_1, molecule_1_size, clb_1, during_packing, old_1_router_data, clustering_data); + //commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); + for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { + if (molecule_1->atom_block_ids[i_atom]) { revert_place_atom_block(molecule_1->atom_block_ids[i_atom], old_1_router_data); } } - for(int i_atom = 0; i_atom < molecule_2_size; i_atom++) { - if(molecule_2->atom_block_ids[i_atom]) { - remove_atom_from_target(old_2_router_data, molecule_2->atom_block_ids[i_atom]); - revert_place_atom_block(molecule_2->atom_block_ids[i_atom], old_2_router_data); - } - } - //add the molecules to their new clusters - pack_2_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_1_type->index]), - molecule_2, - helper_ctx.primitives_list, - cluster_ctx.clb_nlist.block_pb(clb_1), - helper_ctx.num_models, - helper_ctx.max_cluster_size, - clb_1, - E_DETAILED_ROUTE_FOR_EACH_ATOM, - old_1_router_data, - 0, - helper_ctx.enable_pin_feasibility_filter, - helper_ctx.feasible_block_array_size, - target_ext_pin_util, - temp_cluster_pr_1); - - pack_1_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_2_type->index]), - molecule_1, - helper_ctx.primitives_list, - cluster_ctx.clb_nlist.block_pb(clb_2), - helper_ctx.num_models, - helper_ctx.max_cluster_size, - clb_2, - E_DETAILED_ROUTE_FOR_EACH_ATOM, - old_2_router_data, - 0, - helper_ctx.enable_pin_feasibility_filter, - helper_ctx.feasible_block_array_size, - target_ext_pin_util, - temp_cluster_pr_2); + cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_1)); + bool is_removed = is_cluster_legal(old_1_router_data); + free_intra_lb_nets(clustering_data.intra_lb_routing[clb_1]); + clustering_data.intra_lb_routing[clb_1] = old_1_router_data->saved_lb_nets; + old_1_router_data->saved_lb_nets = nullptr; + //save the atoms of the 2 clusters + std::vector clb_1_atoms = cluster_to_atoms(clb_1); + std::vector clb_2_atoms = cluster_to_atoms(clb_2); + //Add the atom to the new cluster + mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data); + //mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); + mol_2_success = true; //commit the move if succeeded or revert if failed - if (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED) { - if(during_packing) { - free_intra_lb_nets(clustering_data.intra_lb_routing[clb_1]); - free_intra_lb_nets(clustering_data.intra_lb_routing[clb_2]); - - clustering_data.intra_lb_routing[clb_1] = old_1_router_data->saved_lb_nets; - clustering_data.intra_lb_routing[clb_2] = old_2_router_data->saved_lb_nets; - } else { - cluster_ctx.clb_nlist.block_pb(clb_1)->pb_route.clear(); - cluster_ctx.clb_nlist.block_pb(clb_1)->pb_route = alloc_and_load_pb_route(old_1_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(clb_1)->pb_graph_node); - cluster_ctx.clb_nlist.block_pb(clb_2)->pb_route.clear(); - cluster_ctx.clb_nlist.block_pb(clb_2)->pb_route = alloc_and_load_pb_route(old_2_router_data->saved_lb_nets, cluster_ctx.clb_nlist.block_pb(clb_1)->pb_graph_node); - - } - + if (mol_1_success && mol_2_success) { commit_mol_move(clb_1, clb_2, during_packing, false); - commit_mol_move(clb_2, clb_1, during_packing, false); - + //commit_mol_move(clb_2, clb_1, during_packing, false); + VTR_LOGV(verbosity > 4, "Molecules swap is performed successfully\n"); } else { - int atom_idx = 0; - for(int i_atom = 0; i_atom < molecule_1_size; i_atom++) { - if(molecule_1->atom_block_ids[i_atom]) { - atom_ctx.lookup.set_atom_clb(molecule_1->atom_block_ids[i_atom], clb_1); - atom_ctx.lookup.set_atom_pb(molecule_1->atom_block_ids[i_atom], mol_1_pb_backup[atom_idx]); - } - } - - atom_idx = 0; - for(int i_atom = 0; i_atom < molecule_2_size; i_atom++) { - if(molecule_2->atom_block_ids[i_atom]) { - atom_ctx.lookup.set_atom_clb(molecule_2->atom_block_ids[i_atom], clb_2); - atom_ctx.lookup.set_atom_pb(molecule_2->atom_block_ids[i_atom], mol_2_pb_backup[atom_idx]); - } - } + revert_mol_move(clb_1, molecule_1, old_1_router_data, during_packing, clustering_data); + //revert_mol_move(clb_2, molecule_2, old_2_router_data, during_packing, clustering_data); + VTR_LOGV(verbosity > 4, "Molecules swap failed\n"); } + //If the move is done after packing not during it, some fixes need to be done on the clustered netlist - if(pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED && !during_packing) { + if (mol_1_success && mol_2_success && !during_packing) { fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); - fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); + //fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); } //free memory - old_1_router_data->saved_lb_nets = nullptr; - old_2_router_data->saved_lb_nets = nullptr; + //old_1_router_data->saved_lb_nets = nullptr; + //old_2_router_data->saved_lb_nets = nullptr; free_router_data(old_1_router_data); - free_router_data(old_2_router_data); + //free_router_data(old_2_router_data); old_1_router_data = nullptr; - old_2_router_data = nullptr; - - //free backup memory - for(auto atom_pb_backup : mol_1_pb_backup) { - cleanup_pb(atom_pb_backup); - delete atom_pb_backup; - } - for(auto atom_pb_backup : mol_2_pb_backup) { - cleanup_pb(atom_pb_backup); - delete atom_pb_backup; - } + //old_2_router_data = nullptr; //return the move result - return (pack_1_result == BLK_PASSED && pack_2_result == BLK_PASSED); + return (mol_1_success && mol_2_success); } #endif \ No newline at end of file diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index d2e3e3d6f78..afd7640b9ba 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -52,5 +52,6 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, bool swap_two_molecules(t_pack_molecule* molecule_1, t_pack_molecule* molecule_2, bool during_packing, + int verbosity, t_clustering_data& clustering_data); #endif \ No newline at end of file From 34e56614ee934074a3ee8e96d73baef6a8f666bb Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 4 Jul 2022 13:30:50 -0400 Subject: [PATCH 15/17] Swapping function is now working (need more cleaning) --- vpr/src/pack/re_cluster.cpp | 83 ++++++++++++++++++++++---------- vpr/src/pack/re_cluster_util.cpp | 4 +- vpr/src/pack/re_cluster_util.h | 1 + 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index efdea33061a..7f74fcf4a6f 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -14,7 +14,6 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, bool is_removed, is_created; ClusterBlockId old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); - ; int molecule_size = get_array_size_of_molecule(molecule); PartitionRegion temp_cluster_pr; @@ -37,7 +36,8 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, } //remove the molecule from its current cluster - remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); + std::vector old_clb_atoms = cluster_to_atoms(old_clb); + remove_mol_from_cluster(molecule, molecule_size, old_clb, old_clb_atoms, old_router_data); //check old cluster legality after removing the molecule is_removed = is_cluster_legal(old_router_data); @@ -107,7 +107,8 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, return false; //remove the molecule from its current cluster - remove_mol_from_cluster(molecule, molecule_size, old_clb, old_router_data); + std::vector old_clb_atoms = cluster_to_atoms(old_clb); + remove_mol_from_cluster(molecule, molecule_size, old_clb, old_clb_atoms, old_router_data); //check old cluster legality after removing the molecule is_removed = is_cluster_legal(old_router_data); @@ -202,43 +203,69 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, t_lb_router_data* old_1_router_data = nullptr; t_lb_router_data* old_2_router_data = nullptr; + //save the atoms of the 2 clusters + std::vector clb_1_atoms = cluster_to_atoms(clb_1); + std::vector clb_2_atoms = cluster_to_atoms(clb_2); + //remove the molecule from its current cluster - remove_mol_from_cluster(molecule_1, molecule_1_size, clb_1, old_1_router_data); - //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); + remove_mol_from_cluster(molecule_1, molecule_1_size, clb_1, clb_1_atoms, old_1_router_data); + //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, clb_2_atoms, old_2_router_data); //bool is_removed = is_cluster_legal(old_1_router_data); //bool is_removed_2 = is_cluster_legal(old_2_router_data); - //commit_mol_removal(molecule_1, molecule_1_size, clb_1, during_packing, old_1_router_data, clustering_data); - //commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); + commit_mol_removal(molecule_1, molecule_1_size, clb_1, during_packing, old_1_router_data, clustering_data); + #if 0 for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { if (molecule_1->atom_block_ids[i_atom]) { revert_place_atom_block(molecule_1->atom_block_ids[i_atom], old_1_router_data); } } + cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_1)); + #endif + #if 0 + for (int i_atom = 0; i_atom < molecule_2_size; i_atom++) { + if (molecule_2->atom_block_ids[i_atom]) { + revert_place_atom_block(molecule_2->atom_block_ids[i_atom], old_2_router_data); + } + } + cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_2)); + #endif + + //bool is_removed = is_cluster_legal(old_1_router_data); + //bool is_removed_2 = is_cluster_legal(old_2_router_data); + + remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, clb_2_atoms, old_2_router_data); + commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); + - cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_1)); - bool is_removed = is_cluster_legal(old_1_router_data); + //Add the atom to the new cluster + mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data); + + #if 1 + /* + remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); + bool is_removed_2 = is_cluster_legal(old_2_router_data); + std::vector clb_1_atoms = cluster_to_atoms(clb_1); + commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); + */ + mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); + #endif + mol_2_success = true; + + #if 0 free_intra_lb_nets(clustering_data.intra_lb_routing[clb_1]); clustering_data.intra_lb_routing[clb_1] = old_1_router_data->saved_lb_nets; old_1_router_data->saved_lb_nets = nullptr; - - //save the atoms of the 2 clusters - std::vector clb_1_atoms = cluster_to_atoms(clb_1); - std::vector clb_2_atoms = cluster_to_atoms(clb_2); - - //Add the atom to the new cluster - mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data); - //mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); - mol_2_success = true; + #endif //commit the move if succeeded or revert if failed if (mol_1_success && mol_2_success) { - commit_mol_move(clb_1, clb_2, during_packing, false); + //commit_mol_move(clb_1, clb_2, during_packing, false); //commit_mol_move(clb_2, clb_1, during_packing, false); VTR_LOGV(verbosity > 4, "Molecules swap is performed successfully\n"); } else { - revert_mol_move(clb_1, molecule_1, old_1_router_data, during_packing, clustering_data); + //revert_mol_move(clb_1, molecule_1, old_1_router_data, during_packing, clustering_data); //revert_mol_move(clb_2, molecule_2, old_2_router_data, during_packing, clustering_data); VTR_LOGV(verbosity > 4, "Molecules swap failed\n"); } @@ -246,19 +273,25 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, //If the move is done after packing not during it, some fixes need to be done on the clustered netlist if (mol_1_success && mol_2_success && !during_packing) { - fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); + //fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); //fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); } //free memory - //old_1_router_data->saved_lb_nets = nullptr; - //old_2_router_data->saved_lb_nets = nullptr; + old_1_router_data->saved_lb_nets = nullptr; + old_2_router_data->saved_lb_nets = nullptr; free_router_data(old_1_router_data); - //free_router_data(old_2_router_data); + free_router_data(old_2_router_data); + //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); + //bool is_removed_2 = is_cluster_legal(old_2_router_data); + //commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); + //std::vector clb_1_atoms = cluster_to_atoms(clb_1); + //mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); + old_1_router_data = nullptr; - //old_2_router_data = nullptr; + old_2_router_data = nullptr; //return the move result return (mol_1_success && mol_2_success); diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index e4f9e54b11e..e7a20527acf 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -54,12 +54,13 @@ std::vector cluster_to_atoms(const ClusterBlockId& cluster) { void remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, + std::vector& old_clb_atoms, t_lb_router_data*& router_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); //Determine the cluster ID old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); - std::vector old_clb_atoms = cluster_to_atoms(old_clb); + //std::vector old_clb_atoms = cluster_to_atoms(old_clb); //re-build router_data structure for this cluster router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, old_clb, old_clb_atoms); @@ -68,6 +69,7 @@ void remove_mol_from_cluster(const t_pack_molecule* molecule, for (int i_atom = 0; i_atom < molecule_size; i_atom++) { if (molecule->atom_block_ids[i_atom]) { remove_atom_from_target(router_data, molecule->atom_block_ids[i_atom]); + old_clb_atoms.erase(std::remove(old_clb_atoms.begin(), old_clb_atoms.end(), molecule->atom_block_ids[i_atom])); } } } diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 03db396e778..f5d23dd4186 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -50,6 +50,7 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr void remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, ClusterBlockId& old_clb, + std::vector& old_clb_atoms, t_lb_router_data*& router_data); /** From 6b88b4bdc941d675c9e0d2e78120ed7f4ad61d34 Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 11 Jul 2022 09:53:50 -0400 Subject: [PATCH 16/17] Some fixes and adding check for the last atom in a cluster --- vpr/src/pack/re_cluster.cpp | 117 ++++++------------------------- vpr/src/pack/re_cluster_util.cpp | 20 ++++-- vpr/src/pack/re_cluster_util.h | 4 +- 3 files changed, 38 insertions(+), 103 deletions(-) diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index 7f74fcf4a6f..b3280be880c 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -37,6 +37,10 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, //remove the molecule from its current cluster std::vector old_clb_atoms = cluster_to_atoms(old_clb); + if(old_clb_atoms.size() == 1) { + VTR_LOGV(verbosity > 4, "Atom: %zu move failed. This is the last atom in its cluster.\n"); + return false; + } remove_mol_from_cluster(molecule, molecule_size, old_clb, old_clb_atoms, old_router_data); //check old cluster legality after removing the molecule @@ -90,9 +94,6 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, bool during_packing, int verbosity, t_clustering_data& clustering_data) { - //define required contexts - auto& cluster_ctx = g_vpr_ctx.clustering(); - //define local variables bool is_removed, is_added; AtomBlockId root_atom_id = molecule->atom_block_ids[molecule->root]; @@ -108,6 +109,10 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //remove the molecule from its current cluster std::vector old_clb_atoms = cluster_to_atoms(old_clb); + if(old_clb_atoms.size() == 1) { + VTR_LOGV(verbosity > 4, "Atom: %zu move failed. This is the last atom in its cluster.\n"); + return false; + } remove_mol_from_cluster(molecule, molecule_size, old_clb, old_clb_atoms, old_router_data); //check old cluster legality after removing the molecule @@ -122,7 +127,8 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, } //Add the atom to the new cluster - is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data); + t_lb_router_data* new_router_data = nullptr; + is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data, false, new_router_data); //Commit or revert the move if (is_added) { @@ -151,11 +157,6 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, bool during_packing, int verbosity, t_clustering_data& clustering_data) { - //define required contexts - auto& cluster_ctx = g_vpr_ctx.clustering(); - auto& atom_ctx = g_vpr_ctx.mutable_atom(); - auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); - //define local variables PartitionRegion temp_cluster_pr_1, temp_cluster_pr_2; @@ -171,35 +172,11 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, ClusterBlockId clb_1 = atom_to_cluster(root_1_atom_id); ClusterBlockId clb_2 = atom_to_cluster(root_2_atom_id); - //t_logical_block_type_ptr block_1_type = cluster_ctx.clb_nlist.block_type(clb_1); - //t_logical_block_type_ptr block_2_type = cluster_ctx.clb_nlist.block_type(clb_2); - - //t_ext_pin_util target_ext_pin_util = helper_ctx.target_external_pin_util.get_pin_util(cluster_ctx.clb_nlist.block_type(clb_1)->name); - //Check that the old and new clusters are of the same type bool is_compitable = check_type_and_mode_compitability(clb_1, clb_2, verbosity); if (!is_compitable) return false; -#if 0 - //Backup the original pb of the molecule before the move - std::vector mol_1_pb_backup, mol_2_pb_backup; - for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { - if (molecule_1->atom_block_ids[i_atom]) { - t_pb* atom_pb_backup = new t_pb; - atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule_1->atom_block_ids[i_atom])); - mol_1_pb_backup.push_back(atom_pb_backup); - } - } - for (int i_atom = 0; i_atom < molecule_2_size; i_atom++) { - if (molecule_2->atom_block_ids[i_atom]) { - t_pb* atom_pb_backup = new t_pb; - atom_pb_backup->pb_deep_copy(atom_ctx.lookup.atom_pb(molecule_2->atom_block_ids[i_atom])); - mol_2_pb_backup.push_back(atom_pb_backup); - } - } -#endif - t_lb_router_data* old_1_router_data = nullptr; t_lb_router_data* old_2_router_data = nullptr; @@ -207,92 +184,40 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, std::vector clb_1_atoms = cluster_to_atoms(clb_1); std::vector clb_2_atoms = cluster_to_atoms(clb_2); + if(clb_1_atoms.size() == 1 || clb_2_atoms.size() == 1) { + VTR_LOGV(verbosity > 4, "Atom: %zu, %zu swap failed. This is the last atom in its cluster.\n", molecule_1->atom_block_ids[molecule_1->root], molecule_2->atom_block_ids[molecule_2->root]); + return false; + } + //remove the molecule from its current cluster remove_mol_from_cluster(molecule_1, molecule_1_size, clb_1, clb_1_atoms, old_1_router_data); - //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, clb_2_atoms, old_2_router_data); - - //bool is_removed = is_cluster_legal(old_1_router_data); - //bool is_removed_2 = is_cluster_legal(old_2_router_data); - commit_mol_removal(molecule_1, molecule_1_size, clb_1, during_packing, old_1_router_data, clustering_data); - #if 0 - for (int i_atom = 0; i_atom < molecule_1_size; i_atom++) { - if (molecule_1->atom_block_ids[i_atom]) { - revert_place_atom_block(molecule_1->atom_block_ids[i_atom], old_1_router_data); - } - } - cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_1)); - #endif - #if 0 - for (int i_atom = 0; i_atom < molecule_2_size; i_atom++) { - if (molecule_2->atom_block_ids[i_atom]) { - revert_place_atom_block(molecule_2->atom_block_ids[i_atom], old_2_router_data); - } - } - cleanup_pb(cluster_ctx.clb_nlist.block_pb(clb_2)); - #endif - - //bool is_removed = is_cluster_legal(old_1_router_data); - //bool is_removed_2 = is_cluster_legal(old_2_router_data); remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, clb_2_atoms, old_2_router_data); commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); //Add the atom to the new cluster - mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data); + mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data, true, old_2_router_data); + mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data, true, old_1_router_data); - #if 1 - /* - remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); - bool is_removed_2 = is_cluster_legal(old_2_router_data); - std::vector clb_1_atoms = cluster_to_atoms(clb_1); - commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); - */ - mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); - #endif - mol_2_success = true; - - #if 0 - free_intra_lb_nets(clustering_data.intra_lb_routing[clb_1]); - clustering_data.intra_lb_routing[clb_1] = old_1_router_data->saved_lb_nets; - old_1_router_data->saved_lb_nets = nullptr; - #endif //commit the move if succeeded or revert if failed if (mol_1_success && mol_2_success) { - //commit_mol_move(clb_1, clb_2, during_packing, false); - //commit_mol_move(clb_2, clb_1, during_packing, false); VTR_LOGV(verbosity > 4, "Molecules swap is performed successfully\n"); } else { - //revert_mol_move(clb_1, molecule_1, old_1_router_data, during_packing, clustering_data); - //revert_mol_move(clb_2, molecule_2, old_2_router_data, during_packing, clustering_data); + revert_mol_move(clb_1, molecule_1, old_1_router_data, during_packing, clustering_data); + revert_mol_move(clb_2, molecule_2, old_2_router_data, during_packing, clustering_data); VTR_LOGV(verbosity > 4, "Molecules swap failed\n"); } //If the move is done after packing not during it, some fixes need to be done on the clustered netlist if (mol_1_success && mol_2_success && !during_packing) { - //fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); - //fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); + fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); + fix_clustered_netlist(molecule_2, molecule_2_size, clb_2, clb_1); } - //free memory - old_1_router_data->saved_lb_nets = nullptr; - old_2_router_data->saved_lb_nets = nullptr; - - free_router_data(old_1_router_data); - free_router_data(old_2_router_data); - //remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, old_2_router_data); - //bool is_removed_2 = is_cluster_legal(old_2_router_data); - //commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); - //std::vector clb_1_atoms = cluster_to_atoms(clb_1); - //mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data); - - - old_1_router_data = nullptr; - old_2_router_data = nullptr; - //return the move result return (mol_1_success && mol_2_success); } diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index e7a20527acf..bb6a4a8d77f 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -188,7 +188,9 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId new_clb, const std::vector& new_clb_atoms, bool during_packing, - t_clustering_data& clustering_data) { + t_clustering_data& clustering_data, + bool is_swap, + t_lb_router_data*& router_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); @@ -202,7 +204,8 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, 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); //re-build router_data structure for this cluster - t_lb_router_data* router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); + if(!is_swap) + router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]), molecule, @@ -232,10 +235,12 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, } } - //Free failed clustering - free_router_data(router_data); - router_data = nullptr; - + if(pack_result == BLK_PASSED || !is_swap ) { + //Free clustering router data + free_router_data(router_data); + router_data = nullptr; + } + return (pack_result == BLK_PASSED); } @@ -280,6 +285,9 @@ void revert_mol_move(const ClusterBlockId& old_clb, cluster_ctx.clb_nlist.block_pb(old_clb)->pb_route.clear(); 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); } + + free_router_data(old_router_data); + old_router_data = nullptr; } /*******************************************/ /************ static functions *************/ diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index f5d23dd4186..26d1b59cfb7 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -105,7 +105,9 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId clb_index, const std::vector& clb_atoms, bool during_packing, - t_clustering_data& clustering_data); + t_clustering_data& clustering_data, + bool is_swap, + t_lb_router_data*& router_data); bool is_cluster_legal(t_lb_router_data*& router_data); From 4cb2cd9aa63da1fc6ffa0073c7bd0532c601195e Mon Sep 17 00:00:00 2001 From: MohamedElgammal Date: Mon, 11 Jul 2022 13:38:48 -0400 Subject: [PATCH 17/17] More commenting --- vpr/src/base/vpr_context.h | 8 ++--- vpr/src/base/vpr_types.cpp | 20 +----------- vpr/src/base/vpr_types.h | 10 ------ vpr/src/pack/re_cluster.cpp | 21 ++++++------- vpr/src/pack/re_cluster.h | 13 +++++--- vpr/src/pack/re_cluster_util.cpp | 25 +++++++++------ vpr/src/pack/re_cluster_util.h | 54 ++++++++++++++++++++++++-------- 7 files changed, 79 insertions(+), 72 deletions(-) diff --git a/vpr/src/base/vpr_context.h b/vpr/src/base/vpr_context.h index 0b5fea3d301..21f04b23409 100644 --- a/vpr/src/base/vpr_context.h +++ b/vpr/src/base/vpr_context.h @@ -289,9 +289,9 @@ struct ClusteringContext : public Context { }; /** - * @brief State relating to helper data structure using in clustering + * @brief State relating to helper data structure using in the clustering stage * - * This should contain helper data structures that is useful in clustering/packing. + * This should contain helper data structures that are useful in the clustering/packing stage. * They are encapsulated here as they are useful in clustering and reclustering algorithms that may be used * in packing or placement stages. */ @@ -314,10 +314,10 @@ struct ClusteringHelperContext : public Context { // total number of CLBs int total_clb_num; - // A vector of routing resource nodes within each of logic cluster_ctx.blocks types + // A vector of routing resource nodes within each of logic cluster_ctx.blocks types [0 .. num_logical_block_type-1] std::vector* lb_type_rr_graphs; - // the utilization of external input/output pins during packing + // the utilization of external input/output pins during packing (between 0 and 1) t_ext_pin_util_targets target_external_pin_util; ~ClusteringHelperContext() { diff --git a/vpr/src/base/vpr_types.cpp b/vpr/src/base/vpr_types.cpp index b8b2feb4524..c92e8966de8 100644 --- a/vpr/src/base/vpr_types.cpp +++ b/vpr/src/base/vpr_types.cpp @@ -147,24 +147,6 @@ const t_pb* t_pb::find_pb_for_model(const std::string& blif_model) const { return nullptr; //Not found } -/** - * @brief Deep copy function for t_pb class - * - * This funcion deeply copies some data members of - * t_pb class to be used to retrieve pb. - * - * This function is currently used in re-clustering API. - */ -void t_pb::pb_deep_copy(const t_pb* rhs) { - name = rhs->name; - pb_graph_node = rhs->pb_graph_node; - mode = rhs->mode; - parent_pb = rhs->parent_pb; - pb_stats = rhs->pb_stats; - clock_net = rhs->clock_net; - pin_rotations_ = rhs->pin_rotations_; -} - /** * @brief Returns the root pb containing this pb */ @@ -281,4 +263,4 @@ void free_cluster_placement_stats(t_cluster_placement_stats* cluster_placement_s free(cluster_placement_stats_list[index].valid_primitives); } free(cluster_placement_stats_list); -} \ No newline at end of file +} diff --git a/vpr/src/base/vpr_types.h b/vpr/src/base/vpr_types.h index cd645090a3d..7f6427f616d 100644 --- a/vpr/src/base/vpr_types.h +++ b/vpr/src/base/vpr_types.h @@ -277,16 +277,6 @@ class t_pb { // Member functions - /** - * @brief Deep copy function for t_pb class - * - * This funcion deeply copies some data members of - * t_pb class to be used to retrieve pb. - * - * This function is currently used in re-clustering API. - */ - void pb_deep_copy(const t_pb* rhs); - ///@brief Returns true if this block has not parent pb block bool is_root() const { return parent_pb == nullptr; } diff --git a/vpr/src/pack/re_cluster.cpp b/vpr/src/pack/re_cluster.cpp index b3280be880c..1bf123bcc52 100644 --- a/vpr/src/pack/re_cluster.cpp +++ b/vpr/src/pack/re_cluster.cpp @@ -37,7 +37,7 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, //remove the molecule from its current cluster std::vector old_clb_atoms = cluster_to_atoms(old_clb); - if(old_clb_atoms.size() == 1) { + if (old_clb_atoms.size() == 1) { VTR_LOGV(verbosity > 4, "Atom: %zu move failed. This is the last atom in its cluster.\n"); return false; } @@ -109,7 +109,7 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //remove the molecule from its current cluster std::vector old_clb_atoms = cluster_to_atoms(old_clb); - if(old_clb_atoms.size() == 1) { + if (old_clb_atoms.size() == 1) { VTR_LOGV(verbosity > 4, "Atom: %zu move failed. This is the last atom in its cluster.\n"); return false; } @@ -128,7 +128,7 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, //Add the atom to the new cluster t_lb_router_data* new_router_data = nullptr; - is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, clustering_data, false, new_router_data); + is_added = pack_mol_in_existing_cluster(molecule, new_clb, new_clb_atoms, during_packing, false, clustering_data, new_router_data); //Commit or revert the move if (is_added) { @@ -184,7 +184,7 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, std::vector clb_1_atoms = cluster_to_atoms(clb_1); std::vector clb_2_atoms = cluster_to_atoms(clb_2); - if(clb_1_atoms.size() == 1 || clb_2_atoms.size() == 1) { + if (clb_1_atoms.size() == 1 || clb_2_atoms.size() == 1) { VTR_LOGV(verbosity > 4, "Atom: %zu, %zu swap failed. This is the last atom in its cluster.\n", molecule_1->atom_block_ids[molecule_1->root], molecule_2->atom_block_ids[molecule_2->root]); return false; } @@ -192,15 +192,13 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, //remove the molecule from its current cluster remove_mol_from_cluster(molecule_1, molecule_1_size, clb_1, clb_1_atoms, old_1_router_data); commit_mol_removal(molecule_1, molecule_1_size, clb_1, during_packing, old_1_router_data, clustering_data); - + remove_mol_from_cluster(molecule_2, molecule_2_size, clb_2, clb_2_atoms, old_2_router_data); - commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); - + commit_mol_removal(molecule_2, molecule_2_size, clb_2, during_packing, old_2_router_data, clustering_data); //Add the atom to the new cluster - mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, clustering_data, true, old_2_router_data); - mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, clustering_data, true, old_1_router_data); - + mol_1_success = pack_mol_in_existing_cluster(molecule_1, clb_2, clb_2_atoms, during_packing, true, clustering_data, old_2_router_data); + mol_2_success = pack_mol_in_existing_cluster(molecule_2, clb_1, clb_1_atoms, during_packing, true, clustering_data, old_1_router_data); //commit the move if succeeded or revert if failed if (mol_1_success && mol_2_success) { @@ -211,7 +209,6 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, VTR_LOGV(verbosity > 4, "Molecules swap failed\n"); } - //If the move is done after packing not during it, some fixes need to be done on the clustered netlist if (mol_1_success && mol_2_success && !during_packing) { fix_clustered_netlist(molecule_1, molecule_1_size, clb_1, clb_2); @@ -221,4 +218,4 @@ bool swap_two_molecules(t_pack_molecule* molecule_1, //return the move result return (mol_1_success && mol_2_success); } -#endif \ No newline at end of file +#endif diff --git a/vpr/src/pack/re_cluster.h b/vpr/src/pack/re_cluster.h index afd7640b9ba..5ca2489aac4 100644 --- a/vpr/src/pack/re_cluster.h +++ b/vpr/src/pack/re_cluster.h @@ -15,12 +15,13 @@ #include "cluster_util.h" /** - * @brief This function moves a molecule out of its cluster and create a new cluster for it + * @brief This function moves a molecule out of its cluster and creates a new cluster for it * * This function can be called from 2 spots in the vpr flow. * - First, during packing to optimize the initial clustered netlist * (during_packing variable should be true.) - * - Second, during placement (during_packing variable should be false) + * - Second, during placement (during_packing variable should be false). In this case, the clustered + * netlist is updated. */ bool move_mol_to_new_cluster(t_pack_molecule* molecule, bool during_packing, @@ -33,7 +34,8 @@ bool move_mol_to_new_cluster(t_pack_molecule* molecule, * This function can be called from 2 spots in the vpr flow. * - First, during packing to optimize the initial clustered netlist * (during_packing variable should be true.) - * - Second, during placement (during_packing variable should be false) + * - Second, during placement (during_packing variable should be false). In this case, the clustered + * netlist is updated. */ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId& new_clb, @@ -47,11 +49,12 @@ bool move_mol_to_existing_cluster(t_pack_molecule* molecule, * This function can be called from 2 spots in the vpr flow. * - First, during packing to optimize the initial clustered netlist * (during_packing variable should be true.) - * - Second, during placement (during_packing variable should be false) + * - Second, during placement (during_packing variable should be false). In this case, the clustered + * netlist is updated. */ bool swap_two_molecules(t_pack_molecule* molecule_1, t_pack_molecule* molecule_2, bool during_packing, int verbosity, t_clustering_data& clustering_data); -#endif \ No newline at end of file +#endif diff --git a/vpr/src/pack/re_cluster_util.cpp b/vpr/src/pack/re_cluster_util.cpp index bb6a4a8d77f..a71779dc433 100644 --- a/vpr/src/pack/re_cluster_util.cpp +++ b/vpr/src/pack/re_cluster_util.cpp @@ -58,10 +58,6 @@ void remove_mol_from_cluster(const t_pack_molecule* molecule, t_lb_router_data*& router_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); - //Determine the cluster ID - old_clb = atom_to_cluster(molecule->atom_block_ids[molecule->root]); - //std::vector old_clb_atoms = cluster_to_atoms(old_clb); - //re-build router_data structure for this cluster router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, old_clb, old_clb_atoms); @@ -80,6 +76,7 @@ void commit_mol_move(const ClusterBlockId& old_clb, bool new_clb_created) { auto& device_ctx = g_vpr_ctx.device(); + //Place the new cluster if this function called during placement (after the initial placement is done) if (!during_packing && new_clb_created) { int imacro; g_vpr_ctx.mutable_placement().block_locs.resize(g_vpr_ctx.placement().block_locs.size() + 1); @@ -166,6 +163,7 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, clb_index = cluster_ctx.clb_nlist.create_block(new_name.c_str(), pb, type); helper_ctx.total_clb_num++; + //If you are still in packing, update the clustering data. Otherwise, update the clustered netlist. if (during_packing) { clustering_data.intra_lb_routing.push_back((*router_data)->saved_lb_nets); (*router_data)->saved_lb_nets = nullptr; @@ -188,8 +186,8 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, const ClusterBlockId new_clb, const std::vector& new_clb_atoms, bool during_packing, - t_clustering_data& clustering_data, bool is_swap, + t_clustering_data& clustering_data, t_lb_router_data*& router_data) { auto& helper_ctx = g_vpr_ctx.mutable_cl_helper(); auto& cluster_ctx = g_vpr_ctx.mutable_clustering(); @@ -204,7 +202,7 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, 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); //re-build router_data structure for this cluster - if(!is_swap) + if (!is_swap) router_data = lb_load_router_data(helper_ctx.lb_type_rr_graphs, new_clb, new_clb_atoms); pack_result = try_pack_molecule(&(helper_ctx.cluster_placement_stats[block_type->index]), @@ -225,6 +223,7 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, // If clustering succeeds, add it to the clb netlist if (pack_result == BLK_PASSED) { + //If you are still in packing, update the clustering data. Otherwise, update the clustered netlist. if (during_packing) { free_intra_lb_nets(clustering_data.intra_lb_routing[new_clb]); clustering_data.intra_lb_routing[new_clb] = router_data->saved_lb_nets; @@ -235,12 +234,12 @@ bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, } } - if(pack_result == BLK_PASSED || !is_swap ) { + if (pack_result == BLK_PASSED || !is_swap) { //Free clustering router data free_router_data(router_data); router_data = nullptr; } - + return (pack_result == BLK_PASSED); } @@ -277,6 +276,7 @@ void revert_mol_move(const ClusterBlockId& old_clb, temp_cluster_pr_original); VTR_ASSERT(pack_result == BLK_PASSED); + //If you are still in packing, update the clustering data. Otherwise, update the clustered netlist. if (during_packing) { free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); clustering_data.intra_lb_routing[old_clb] = old_router_data->saved_lb_nets; @@ -384,8 +384,10 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { int iport, ipb_pin, ipin, rr_node_index; ipin = 0; + // iterating over input ports for (iport = 0; iport < num_input_ports; iport++) { ClusterPortId input_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[iport].name); + // iterating over physical block pins of each input port for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_input_pins[iport]; ipb_pin++) { pb_graph_pin = &pb->pb_graph_node->input_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; @@ -408,8 +410,10 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { } } + // iterating over output ports for (iport = 0; iport < num_output_ports; iport++) { ClusterPortId output_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + iport].name); + // iterating over physical block pins of each output port for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_output_pins[iport]; ipb_pin++) { pb_graph_pin = &pb->pb_graph_node->output_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; @@ -437,8 +441,10 @@ static void fix_cluster_pins_after_moving(const ClusterBlockId clb_index) { } } + // iterating over clock ports for (iport = 0; iport < num_clock_ports; iport++) { ClusterPortId clock_port_id = cluster_ctx.clb_nlist.find_port(clb_index, block_type->pb_type->ports[num_input_ports + num_output_ports + iport].name); + // iterating over physical block pins of each clock port for (ipb_pin = 0; ipb_pin < pb->pb_graph_node->num_clock_pins[iport]; ipb_pin++) { pb_graph_pin = &pb->pb_graph_node->clock_pins[iport][ipb_pin]; rr_node_index = pb_graph_pin->pin_count_in_cluster; @@ -651,6 +657,7 @@ void commit_mol_removal(const t_pack_molecule* molecule, cleanup_pb(cluster_ctx.clb_nlist.block_pb(old_clb)); + //If you are still in packing, update the clustering data. Otherwise, update the clustered netlist. if (during_packing) { free_intra_lb_nets(clustering_data.intra_lb_routing[old_clb]); clustering_data.intra_lb_routing[old_clb] = router_data->saved_lb_nets; @@ -679,4 +686,4 @@ bool check_type_and_mode_compitability(const ClusterBlockId& old_clb, } return true; -} \ No newline at end of file +} diff --git a/vpr/src/pack/re_cluster_util.h b/vpr/src/pack/re_cluster_util.h index 26d1b59cfb7..89f12d960ab 100644 --- a/vpr/src/pack/re_cluster_util.h +++ b/vpr/src/pack/re_cluster_util.h @@ -40,12 +40,20 @@ t_lb_router_data* lb_load_router_data(std::vector* lb_type_rr const std::vector& clb_atoms); /** - * @brief A function that removes a molecule from a cluster and check legality of + * @brief A function that removes a molecule from a cluster and checks legality of * the old cluster. * * It returns true if the removal is done and the old cluster is legal. * It aborts the removal and returns false if the removal will make the old cluster - * illegal + * illegal. + * + * This function updates the intra-logic block router data structure (router_data) and + * remove all the atoms of the moecule from old_clb_atoms vector. + * + * @param old_clb: The original cluster of this molecule + * @param old_clb_atoms: A vector containing the list of atoms in the old cluster of the molecule. + * It will be updated in the function to remove the atoms of molecule from it. + * @param router_data: returns the intra logic block router data. */ void remove_mol_from_cluster(const t_pack_molecule* molecule, int molecule_size, @@ -58,6 +66,15 @@ void remove_mol_from_cluster(const t_pack_molecule* molecule, * * It places the molecule in a specific type and mode that should be passed by * the higher level routine. + * + * @param type: the cluster block type needed + * @param mode: the mode of the new cluster + * @param clb_index: the cluster block Id of the newly created cluster block + * @param during_packing: true if this function is called during packing, false if it is called during placement + * @param clustering_data: A data structure containing helper data for the clustering process + * (is updated if this function is called during packing, especially intra_lb_routing data member). + * @param router_data: returns the intra logic block router data. + * @param temp_cluster_pr: returns the partition region of the new cluster. */ bool start_new_cluster_for_mol(t_pack_molecule* molecule, const t_logical_block_type_ptr& type, @@ -71,6 +88,25 @@ bool start_new_cluster_for_mol(t_pack_molecule* molecule, t_lb_router_data** router_data, PartitionRegion& temp_cluster_pr); +/** + * @brief A function that packs a molecule into an existing cluster + * + * @param clb_index: the cluster block Id of the new cluster that we need to pack the molecule in. + * @param: clb_atoms: A vector containing the list of atoms in the new cluster block before adding the molecule. + * @param during_packing: true if this function is called during packing, false if it is called during placement + * @param is_swap: true if this function is called during swapping two molecules. False if the called during a single molecule move + * @param clustering_data: A data structure containing helper data for the clustering process + * (is updated if this function is called during packing, especially intra_lb_routing data member). + * @param router_data: returns the intra logic block router data. + */ +bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, + const ClusterBlockId clb_index, + const std::vector& clb_atoms, + bool during_packing, + bool is_swap, + t_clustering_data& clustering_data, + t_lb_router_data*& router_data); + /** * @brief A function that fix the clustered netlist if the move is performed * after the packing is done and clustered netlist is built @@ -86,6 +122,9 @@ void fix_clustered_netlist(t_pack_molecule* molecule, /** * @brief A function that commits the molecule move if it is legal + * + * @during_packing: true if this function is called during packing, false if it is called during placement + * @new_clb_created: true if the move is creating a new cluster (e.g. move_mol_to_new_cluster) */ void commit_mol_move(const ClusterBlockId& old_clb, const ClusterBlockId& new_clb, @@ -98,17 +137,6 @@ void revert_mol_move(const ClusterBlockId& old_clb, bool during_packing, t_clustering_data& clustering_data); -/** - * @brief A function that packs a molecule into an existing cluster - */ -bool pack_mol_in_existing_cluster(t_pack_molecule* molecule, - const ClusterBlockId clb_index, - const std::vector& clb_atoms, - bool during_packing, - t_clustering_data& clustering_data, - bool is_swap, - t_lb_router_data*& router_data); - bool is_cluster_legal(t_lb_router_data*& router_data); void commit_mol_removal(const t_pack_molecule* molecule,