Skip to content

Commit

Permalink
mpl2: use best valid result for both Hard/Soft
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Koucher <arthurkoucher@precisioninno.com>
  • Loading branch information
AcKoucher committed Nov 5, 2024
1 parent ebd22b8 commit a021691
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
31 changes: 0 additions & 31 deletions src/mpl2/src/SACoreSoftMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,8 @@ void SACoreSoftMacro::run()
graphics_->startSA();
}

best_valid_result_ = std::make_unique<SoftResult>();

fastSA();

if (!isValid() && !best_valid_result_->sequence_pair.pos_sequence.empty()) {
useBestValidResult();
}

if (centralization_on_) {
attemptCentralization(calNormCost());
}
Expand Down Expand Up @@ -1177,29 +1171,4 @@ void SACoreSoftMacro::moveFloorplan(const std::pair<float, float>& 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
2 changes: 0 additions & 2 deletions src/mpl2/src/SACoreSoftMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ class SACoreSoftMacro : public SimulatedAnnealingCore<SoftMacro>
void attemptCentralization(float pre_cost);
void moveFloorplan(const std::pair<float, float>& offset);

void useBestValidResult();

std::vector<Rect> blockages_;

Cluster* root_;
Expand Down
62 changes: 47 additions & 15 deletions src/mpl2/src/SimulatedAnnealingCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,22 +576,20 @@ void SimulatedAnnealingCore<T>::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_) {
for (int i = 0; i < num_perturb_per_step_; i++) {
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;
Expand Down Expand Up @@ -637,20 +635,54 @@ void SimulatedAnnealingCore<T>::fastSA()
graphics_->doNotSkip();
}
calPenalty();

if (!isValid() && !best_valid_result_.sequence_pair.pos_sequence.empty()) {
useBestValidResult();
}
}

template <class T>
void SimulatedAnnealingCore<T>::updateBestValidSoftResult()
void SimulatedAnnealingCore<T>::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<T, SoftMacro>) {
for (const int macro_id : pos_seq_) {
T& macro = macros_[macro_id];
best_valid_result_.macro_id_to_width[macro_id] = macro.getWidth();
}
}
}

template <class T>
void SimulatedAnnealingCore<T>::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<T, SoftMacro>) {
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 <class T>
void SimulatedAnnealingCore<T>::writeCostFile(
const std::string& file_name) const
Expand Down
13 changes: 7 additions & 6 deletions src/mpl2/src/SimulatedAnnealingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, float> 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<int, float> macro_id_to_width;
};

void fastSA();

void initSequencePair();
void updateBestValidSoftResult();
void updateBestValidResult();
void useBestValidResult();

virtual float calNormCost() const = 0;
virtual void calPenalty() = 0;
Expand Down Expand Up @@ -239,7 +240,7 @@ class SimulatedAnnealingCore
utl::Logger* logger_ = nullptr;
Mpl2Observer* graphics_ = nullptr;

std::unique_ptr<SoftResult> best_valid_result_;
Result best_valid_result_;

std::vector<float> cost_list_; // store the cost in the list
std::vector<float> T_list_; // store the temperature
Expand Down

0 comments on commit a021691

Please sign in to comment.