Skip to content

[APPack] Updated How APPack Adheres to Given Placement #2934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion vpr/src/analytical_place/detailed_placer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

#include "detailed_placer.h"
#include <memory>
#include "FlatPlacementInfo.h"
#include "PlacementDelayModelCreator.h"
#include "ap_flow_enums.h"
#include "atom_netlist.h"
#include "clustered_netlist.h"
#include "clustered_netlist_utils.h"
#include "echo_files.h"
#include "flat_placement_types.h"
#include "globals.h"
#include "physical_types.h"
#include "place_and_route.h"
Expand All @@ -22,6 +22,7 @@
#include "vpr_error.h"
#include "vpr_types.h"
#include "vpr_utils.h"
#include "vtr_time.h"

std::unique_ptr<DetailedPlacer> make_detailed_placer(e_ap_detailed_placer detailed_placer_type,
const BlkLocRegistry& curr_clustered_placement,
Expand Down Expand Up @@ -89,6 +90,9 @@ AnnealerDetailedPlacer::AnnealerDetailedPlacer(const BlkLocRegistry& curr_cluste
}

void AnnealerDetailedPlacer::optimize_placement() {
// Create a scoped timer for the detailed placer.
vtr::ScopedStartFinishTimer full_legalizer_timer("AP Detailed Placer");

// Prevent the annealer from directly modifying the global legal placement.
// It should only modify its own, local placement.
g_vpr_ctx.mutable_placement().lock_loc_vars();
Expand Down
14 changes: 7 additions & 7 deletions vpr/src/analytical_place/full_legalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
#include <unordered_set>
#include <vector>

#include "FlatPlacementInfo.h"
#include "ap_flow_enums.h"
#include "blk_loc_registry.h"
#include "device_grid.h"
#include "load_flat_place.h"
#include "noc_place_utils.h"
#include "partial_placement.h"
#include "ShowSetup.h"
#include "ap_flow_enums.h"
#include "ap_netlist_fwd.h"
#include "blk_loc_registry.h"
#include "check_netlist.h"
#include "cluster_legalizer.h"
#include "cluster_util.h"
#include "clustered_netlist.h"
#include "device_grid.h"
#include "flat_placement_types.h"
#include "globals.h"
#include "initial_placement.h"
#include "load_flat_place.h"
#include "logic_types.h"
#include "noc_place_utils.h"
#include "pack.h"
#include "partial_placement.h"
#include "physical_types.h"
#include "place.h"
#include "place_and_route.h"
Expand Down
6 changes: 6 additions & 0 deletions vpr/src/analytical_place/global_placer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,24 @@ SimPLGlobalPlacer::SimPLGlobalPlacer(e_partial_legalizer partial_legalizer_type,
// This can be a long method. Good to time this to see how long it takes to
// construct the global placer.
vtr::ScopedStartFinishTimer global_placer_building_timer("Constructing Global Placer");

// Build the solver.
VTR_LOGV(log_verbosity_ >= 10, "\tBuilding the solver...\n");
solver_ = make_analytical_solver(e_analytical_solver::QP_HYBRID,
ap_netlist_);

// Build the density manager used by the partial legalizer.
VTR_LOGV(log_verbosity_ >= 10, "\tBuilding the density manager...\n");
density_manager_ = std::make_shared<FlatPlacementDensityManager>(ap_netlist_,
prepacker,
atom_netlist,
device_grid,
logical_block_types,
physical_tile_types,
log_verbosity_);

// Build the partial legalizer
VTR_LOGV(log_verbosity_ >= 10, "\tBuilding the partial legalizer...\n");
partial_legalizer_ = make_partial_legalizer(partial_legalizer_type,
ap_netlist_,
density_manager_,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
/**
* @file
* @author Alex Singer
* @date January 2025
* @brief Declaration of the FlatPlacementInfo object, which is used to store
* flat placement information used by the packer.
* @date March 2025
* @brief Declaration of flat placement types used throughout VPR.
*/

#pragma once

#include "atom_netlist.h"
#include "vtr_assert.h"
#include "vtr_vector.h"

/**
* @brief A structure representing a flat placement location on the device.
*
* This is related to the t_pl_loc type; however this uses floating point
* coordinates, allowing for blocks to be placed in illegal positions.
*/
struct t_flat_pl_loc {
float x; /**< The x-coordinate of the location. */
float y; /**< The y-coordinate of the location. */
float layer; /**< The layer of the location. */

/**
* @brief Adds the coordinates of another t_flat_pl_loc to this one.
*
* @param other The other t_flat_pl_loc whose coordinates are to be added.
* @return A reference to this t_flat_pl_loc after addition.
*/
t_flat_pl_loc& operator+=(const t_flat_pl_loc& other) {
x += other.x;
y += other.y;
layer += other.layer;
return *this;
}

/**
* @brief Divides the coordinates of this t_flat_pl_loc by a divisor.
*
* @param divisor The value by which to divide the coordinates.
* @return A reference to this t_flat_pl_loc after division.
*/
t_flat_pl_loc& operator/=(float divisor) {
x /= divisor;
y /= divisor;
layer /= divisor;
return *this;
}
};

/**
* @brief Flat placement storage class.
*
Expand Down Expand Up @@ -55,6 +93,15 @@ class FlatPlacementInfo {
/// object, false otherwise.
bool valid;

/**
* @brief Get the flat placement location of the given atom block.
*/
inline t_flat_pl_loc get_pos(AtomBlockId blk_id) const {
VTR_ASSERT_SAFE_MSG(blk_id.is_valid(), "Block ID is invalid");
VTR_ASSERT_SAFE_MSG(valid, "FlatPlacementInfo not initialized");
return {blk_x_pos[blk_id], blk_y_pos[blk_id], blk_layer[blk_id]};
}

/**
* @brief Default constructor of this class.
*
Expand Down
20 changes: 20 additions & 0 deletions vpr/src/base/flat_placement_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file
* @author Alex Singer
* @date March 2025
* @brief Utility methods for working with flat placements.
*/

#pragma once

#include <cstdlib>
#include "flat_placement_types.h"

/**
* @brief Returns the manhattan distance (L1 distance) between two flat
* placement locations.
*/
inline float get_manhattan_distance(const t_flat_pl_loc& loc_a,
const t_flat_pl_loc& loc_b) {
return std::abs(loc_a.x - loc_b.x) + std::abs(loc_a.y - loc_b.y) + std::abs(loc_a.layer - loc_b.layer);
}
2 changes: 1 addition & 1 deletion vpr/src/base/load_flat_place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "atom_lookup.h"
#include "atom_netlist.h"
#include "clustered_netlist.h"
#include "FlatPlacementInfo.h"
#include "flat_placement_types.h"
#include "globals.h"
#include "vpr_context.h"
#include "vpr_error.h"
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/base/place_and_route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cmath>
#include <algorithm>

#include "FlatPlacementInfo.h"
#include "flat_placement_types.h"
#include "place_macro.h"
#include "vtr_assert.h"
#include "vtr_log.h"
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/base/vpr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <memory>
#include <vector>

#include "FlatPlacementInfo.h"
#include "flat_placement_types.h"
#include "cluster_util.h"
#include "physical_types.h"
#include "place_macro.h"
Expand Down
2 changes: 1 addition & 1 deletion vpr/src/base/vpr_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>
#include <mutex>

#include "FlatPlacementInfo.h"
#include "flat_placement_types.h"
#include "physical_types.h"
#include "place_macro.h"
#include "user_place_constraints.h"
Expand Down
110 changes: 110 additions & 0 deletions vpr/src/pack/appack_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @file
* @author Alex Siner
* @date March 2025
* @brief Declaration of the APPack Context object which stores all the
* information used to configure APPack in the packer.
*/

#pragma once

#include <algorithm>
#include <limits>
#include "device_grid.h"
#include "flat_placement_types.h"
#include "vpr_context.h"

/**
* @brief Configuration options for APPack.
*
* APPack is an upgrade to the AAPack algorithm which uses an atom-level placement
* to inform the packer into creating better clusters. These options configure
* how APPack interprets the flat placement information.
*/
struct t_appack_options {
// Constructor for the appack options.
t_appack_options(const FlatPlacementInfo& flat_placement_info,
const DeviceGrid& device_grid) {
// If the flat placement info is valid, we want to use APPack.
// TODO: Should probably check that all the information is valid here.
use_appack = flat_placement_info.valid;

// Set the max candidate distance as being some fraction of the longest
// distance on the device (from the bottom corner to the top corner).
// We also use an offset for the minimum this distance can be to prevent
// small devices from finding candidates.
float max_candidate_distance_scale = 0.5f;
float max_candidate_distance_offset = 20.f;
// Longest L1 distance on the device.
float longest_distance = device_grid.width() + device_grid.height();
max_candidate_distance = std::max(max_candidate_distance_scale * longest_distance,
max_candidate_distance_offset);
}

// Whether to use APPack or not.
// This is initialized in the constructor based on if the flat placement
// info is valid or not.
bool use_appack = false;

// =========== Cluster location ======================================== //
// What is the location of the cluster being created relative to the
// molecules being packed into it.
enum class e_cl_loc_ty {
CENTROID, /**< The location of the cluster is the centroid of the molecules which have been packed into it. */
SEED /**< The location of the cluster is the location of the first molecule packed into it. */
};
e_cl_loc_ty cluster_location_ty = e_cl_loc_ty::CENTROID;

// =========== Candidate gain attenuation ============================== //
// These terms are used to update the gain of a given candidate based on
// its distance (d) relative to the location of the cluster being constructed.
// gain_new = attenuation * gain_original
// We use the following gain attenuation function:
// attenuation = { 1 - (quad_fac * d)^2 if d < dist_th
// { 1 / sqrt(d - sqrt_offset) if d >= dist_th
// Distance threshold which decides when to use quadratic decay or inverted
// sqrt decay. If the distance is less than this threshold, quadratic decay
// is used. Inverted sqrt is used otherwise.
float dist_th = 1.0f;
// Horizontal offset to the inverted sqrt decay.
float sqrt_offset = -2.9f;
// Scaling factor for the quadratic decay term.
float quad_fac = 0.7f;

// =========== Candidate selection distance ============================ //
// When selecting candidates, what distance from the cluster will we
// consider? Any candidate beyond this distance will not be proposed.
// This is set in the constructor.
// TODO: It may be a good idea to have max different distances for different
// types of molecules / clusters. For example, CLBs vs DSPs
float max_candidate_distance = std::numeric_limits<float>::max();

// TODO: Investigate adding flat placement info to unrelated clustering.

// TODO: Investigate adding flat placement info to seed selection.
};

/**
* @brief State relating to APPack.
*
* This class is intended to contain information on using flat placement
* information in packing.
*/
struct APPackContext : public Context {
/**
* @brief Constructor for the APPack context.
*/
APPackContext(const FlatPlacementInfo& fplace_info, const DeviceGrid& device_grid)
: appack_options(fplace_info, device_grid)
, flat_placement_info(fplace_info) {}

/**
* @brief Options used to configure APPack.
*/
t_appack_options appack_options;

/**
* @brief The flat placement information passed into APPack.
*/
const FlatPlacementInfo& flat_placement_info;
};
7 changes: 7 additions & 0 deletions vpr/src/pack/cluster_legalizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ class ClusterLegalizer {
return cluster.pr;
}

/// @brief Gets the molecules currently packed within the given cluster.
inline const std::vector<PackMoleculeId>& get_cluster_molecules(LegalizationClusterId cluster_id) const {
VTR_ASSERT_SAFE(cluster_id.is_valid() && (size_t)cluster_id < legalization_clusters_.size());
const LegalizationCluster& cluster = legalization_clusters_[cluster_id];
return cluster.molecules;
}

/// @brief Gets the current number of molecules in the cluster.
inline size_t get_num_molecules_in_cluster(LegalizationClusterId cluster_id) const {
VTR_ASSERT_SAFE(cluster_id.is_valid() && (size_t)cluster_id < legalization_clusters_.size());
Expand Down
Loading