From a021691ed713f91056dbc67e6fe6c9fcd750e62e Mon Sep 17 00:00:00 2001 From: Arthur Koucher Date: Tue, 5 Nov 2024 20:39:48 -0300 Subject: [PATCH] mpl2: use best valid result for both Hard/Soft Signed-off-by: Arthur Koucher --- src/mpl2/src/SACoreSoftMacro.cpp | 31 ------------- src/mpl2/src/SACoreSoftMacro.h | 2 - src/mpl2/src/SimulatedAnnealingCore.cpp | 62 +++++++++++++++++++------ src/mpl2/src/SimulatedAnnealingCore.h | 13 +++--- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/mpl2/src/SACoreSoftMacro.cpp b/src/mpl2/src/SACoreSoftMacro.cpp index eadff9d19e4..9bfb3bc8f9d 100644 --- a/src/mpl2/src/SACoreSoftMacro.cpp +++ b/src/mpl2/src/SACoreSoftMacro.cpp @@ -107,14 +107,8 @@ void SACoreSoftMacro::run() graphics_->startSA(); } - best_valid_result_ = std::make_unique(); - fastSA(); - if (!isValid() && !best_valid_result_->sequence_pair.pos_sequence.empty()) { - useBestValidResult(); - } - if (centralization_on_) { attemptCentralization(calNormCost()); } @@ -1177,29 +1171,4 @@ void SACoreSoftMacro::moveFloorplan(const std::pair& offset) calPenalty(); } -void SACoreSoftMacro::useBestValidResult() -{ - pos_seq_ = best_valid_result_->sequence_pair.pos_sequence; - neg_seq_ = best_valid_result_->sequence_pair.neg_sequence; - - for (const int macro_id : pos_seq_) { - SoftMacro& macro = macros_[macro_id]; - const float valid_result_width - = best_valid_result_->macro_id_to_width.at(macro_id); - - if (macro.isMacroCluster()) { - const float valid_result_height = macro.getArea() / valid_result_width; - macro.setShapeF(valid_result_width, valid_result_height); - } else { - macro.setWidth(valid_result_width); - } - } - - packFloorplan(); - if (graphics_) { - graphics_->doNotSkip(); - } - calPenalty(); -} - } // namespace mpl2 diff --git a/src/mpl2/src/SACoreSoftMacro.h b/src/mpl2/src/SACoreSoftMacro.h index 00123e517e8..2356732cd5f 100644 --- a/src/mpl2/src/SACoreSoftMacro.h +++ b/src/mpl2/src/SACoreSoftMacro.h @@ -134,8 +134,6 @@ class SACoreSoftMacro : public SimulatedAnnealingCore void attemptCentralization(float pre_cost); void moveFloorplan(const std::pair& offset); - void useBestValidResult(); - std::vector blockages_; Cluster* root_; diff --git a/src/mpl2/src/SimulatedAnnealingCore.cpp b/src/mpl2/src/SimulatedAnnealingCore.cpp index f1b2952dc46..0c28cfe9f34 100644 --- a/src/mpl2/src/SimulatedAnnealingCore.cpp +++ b/src/mpl2/src/SimulatedAnnealingCore.cpp @@ -576,8 +576,8 @@ void SimulatedAnnealingCore::fastSA() int num_restart = 1; const int max_num_restart = 2; - if (best_valid_result_ && isValid()) { - updateBestValidSoftResult(); + if (isValid()) { + updateBestValidResult(); } while (step <= max_num_step_) { @@ -585,13 +585,11 @@ void SimulatedAnnealingCore::fastSA() perturb(); cost = calNormCost(); - if (best_valid_result_) { - const bool keep_result - = cost < pre_cost - || best_valid_result_->sequence_pair.pos_sequence.empty(); - if (isValid() && keep_result) { - updateBestValidSoftResult(); - } + const bool keep_result + = cost < pre_cost + || best_valid_result_.sequence_pair.pos_sequence.empty(); + if (isValid() && keep_result) { + updateBestValidResult(); } delta_cost = cost - pre_cost; @@ -637,20 +635,54 @@ void SimulatedAnnealingCore::fastSA() graphics_->doNotSkip(); } calPenalty(); + + if (!isValid() && !best_valid_result_.sequence_pair.pos_sequence.empty()) { + useBestValidResult(); + } } template -void SimulatedAnnealingCore::updateBestValidSoftResult() +void SimulatedAnnealingCore::updateBestValidResult() { - best_valid_result_->sequence_pair.pos_sequence = pos_seq_; - best_valid_result_->sequence_pair.neg_sequence = neg_seq_; + best_valid_result_.sequence_pair.pos_sequence = pos_seq_; + best_valid_result_.sequence_pair.neg_sequence = neg_seq_; - for (const int macro_id : pos_seq_) { - T& macro = macros_[macro_id]; - best_valid_result_->macro_id_to_width[macro_id] = macro.getWidth(); + if constexpr (std::is_same_v) { + for (const int macro_id : pos_seq_) { + T& macro = macros_[macro_id]; + best_valid_result_.macro_id_to_width[macro_id] = macro.getWidth(); + } } } +template +void SimulatedAnnealingCore::useBestValidResult() +{ + pos_seq_ = best_valid_result_.sequence_pair.pos_sequence; + neg_seq_ = best_valid_result_.sequence_pair.neg_sequence; + + if constexpr (std::is_same_v) { + for (const int macro_id : pos_seq_) { + T& macro = macros_[macro_id]; + const float valid_result_width + = best_valid_result_.macro_id_to_width.at(macro_id); + + if (macro.isMacroCluster()) { + const float valid_result_height = macro.getArea() / valid_result_width; + macro.setShapeF(valid_result_width, valid_result_height); + } else { + macro.setWidth(valid_result_width); + } + } + } + + packFloorplan(); + if (graphics_) { + graphics_->doNotSkip(); + } + calPenalty(); +} + template void SimulatedAnnealingCore::writeCostFile( const std::string& file_name) const diff --git a/src/mpl2/src/SimulatedAnnealingCore.h b/src/mpl2/src/SimulatedAnnealingCore.h index 3196f443a39..29e835178a1 100644 --- a/src/mpl2/src/SimulatedAnnealingCore.h +++ b/src/mpl2/src/SimulatedAnnealingCore.h @@ -123,18 +123,19 @@ class SimulatedAnnealingCore virtual void fillDeadSpace() = 0; protected: - // The same sequence pair can represent different floorplan - // arrangements depending on the macros' shapes. - struct SoftResult + struct Result { SequencePair sequence_pair; - std::map macro_id_to_width; // Macros' shapes. + // [Only for SoftMacro] The same sequence pair can represent different + // floorplan arrangements depending on the macros' shapes. + std::map macro_id_to_width; }; void fastSA(); void initSequencePair(); - void updateBestValidSoftResult(); + void updateBestValidResult(); + void useBestValidResult(); virtual float calNormCost() const = 0; virtual void calPenalty() = 0; @@ -239,7 +240,7 @@ class SimulatedAnnealingCore utl::Logger* logger_ = nullptr; Mpl2Observer* graphics_ = nullptr; - std::unique_ptr best_valid_result_; + Result best_valid_result_; std::vector cost_list_; // store the cost in the list std::vector T_list_; // store the temperature