Skip to content

Commit 02fc8ab

Browse files
authored
Merge pull request #9610 from gudeh/gpl-fix-incremental
gpl: fix incremental mode
2 parents 8b25aad + fe1aa0f commit 02fc8ab

8 files changed

Lines changed: 21548 additions & 21371 deletions

File tree

‎src/gpl/src/initialPlace.cpp‎

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ void InitialPlace::createSparseMatrix()
283283
instLocVecY_(idx) = inst->cy();
284284

285285
fixedInstForceVecX_(idx) = fixedInstForceVecY_(idx) = 0;
286+
287+
if (inst->isLocked()) {
288+
listX.emplace_back(idx, idx, 1.0f);
289+
listY.emplace_back(idx, idx, 1.0f);
290+
fixedInstForceVecX_(idx) = inst->cx();
291+
fixedInstForceVecY_(idx) = inst->cy();
292+
}
286293
}
287294

288295
// for each net
@@ -325,24 +332,40 @@ void InitialPlace::createSparseMatrix()
325332

326333
// both pin cames from instance
327334
if (pin1->isPlaceInstConnected() && pin2->isPlaceInstConnected()) {
328-
const int inst1 = pin1->getInstance()->getExtId();
329-
const int inst2 = pin2->getInstance()->getExtId();
330-
331-
listX.emplace_back(inst1, inst1, weightX);
332-
listX.emplace_back(inst2, inst2, weightX);
333-
334-
listX.emplace_back(inst1, inst2, -weightX);
335-
listX.emplace_back(inst2, inst1, -weightX);
336-
337-
fixedInstForceVecX_(inst1)
338-
+= -weightX
339-
* ((pin1->cx() - pin1->getInstance()->cx())
340-
- (pin2->cx() - pin2->getInstance()->cx()));
341-
342-
fixedInstForceVecX_(inst2)
343-
+= -weightX
344-
* ((pin2->cx() - pin2->getInstance()->cx())
345-
- (pin1->cx() - pin1->getInstance()->cx()));
335+
Instance* instance1 = pin1->getInstance();
336+
Instance* instance2 = pin2->getInstance();
337+
const int inst1 = instance1->getExtId();
338+
const int inst2 = instance2->getExtId();
339+
const bool locked1 = instance1->isLocked();
340+
const bool locked2 = instance2->isLocked();
341+
342+
if (!locked1 && !locked2) {
343+
// Both movable: standard B2B matrix terms
344+
listX.emplace_back(inst1, inst1, weightX);
345+
listX.emplace_back(inst2, inst2, weightX);
346+
listX.emplace_back(inst1, inst2, -weightX);
347+
listX.emplace_back(inst2, inst1, -weightX);
348+
349+
fixedInstForceVecX_(inst1)
350+
+= -weightX
351+
* ((pin1->cx() - instance1->cx())
352+
- (pin2->cx() - instance2->cx()));
353+
fixedInstForceVecX_(inst2)
354+
+= -weightX
355+
* ((pin2->cx() - instance2->cx())
356+
- (pin1->cx() - instance1->cx()));
357+
} else if (locked1 && !locked2) {
358+
// inst1 is fixed: treat pin1 as a fixed terminal for inst2
359+
listX.emplace_back(inst2, inst2, weightX);
360+
fixedInstForceVecX_(inst2)
361+
+= weightX * (pin1->cx() - (pin2->cx() - instance2->cx()));
362+
} else if (!locked1 && locked2) {
363+
// inst2 is fixed: treat pin2 as a fixed terminal for inst1
364+
listX.emplace_back(inst1, inst1, weightX);
365+
fixedInstForceVecX_(inst1)
366+
+= weightX * (pin2->cx() - (pin1->cx() - instance1->cx()));
367+
}
368+
// both locked: no matrix or RHS contribution needed
346369
}
347370
// pin1 from IO port / pin2 from Instance
348371
else if (!pin1->isPlaceInstConnected()
@@ -379,24 +402,40 @@ void InitialPlace::createSparseMatrix()
379402

380403
// both pin cames from instance
381404
if (pin1->isPlaceInstConnected() && pin2->isPlaceInstConnected()) {
382-
const int inst1 = pin1->getInstance()->getExtId();
383-
const int inst2 = pin2->getInstance()->getExtId();
384-
385-
listY.emplace_back(inst1, inst1, weightY);
386-
listY.emplace_back(inst2, inst2, weightY);
387-
388-
listY.emplace_back(inst1, inst2, -weightY);
389-
listY.emplace_back(inst2, inst1, -weightY);
390-
391-
fixedInstForceVecY_(inst1)
392-
+= -weightY
393-
* ((pin1->cy() - pin1->getInstance()->cy())
394-
- (pin2->cy() - pin2->getInstance()->cy()));
395-
396-
fixedInstForceVecY_(inst2)
397-
+= -weightY
398-
* ((pin2->cy() - pin2->getInstance()->cy())
399-
- (pin1->cy() - pin1->getInstance()->cy()));
405+
Instance* instance1 = pin1->getInstance();
406+
Instance* instance2 = pin2->getInstance();
407+
const int inst1 = instance1->getExtId();
408+
const int inst2 = instance2->getExtId();
409+
const bool locked1 = instance1->isLocked();
410+
const bool locked2 = instance2->isLocked();
411+
412+
if (!locked1 && !locked2) {
413+
// Both movable: standard B2B matrix terms
414+
listY.emplace_back(inst1, inst1, weightY);
415+
listY.emplace_back(inst2, inst2, weightY);
416+
listY.emplace_back(inst1, inst2, -weightY);
417+
listY.emplace_back(inst2, inst1, -weightY);
418+
419+
fixedInstForceVecY_(inst1)
420+
+= -weightY
421+
* ((pin1->cy() - instance1->cy())
422+
- (pin2->cy() - instance2->cy()));
423+
fixedInstForceVecY_(inst2)
424+
+= -weightY
425+
* ((pin2->cy() - instance2->cy())
426+
- (pin1->cy() - instance1->cy()));
427+
} else if (locked1 && !locked2) {
428+
// inst1 is fixed: treat pin1 as a fixed terminal for inst2
429+
listY.emplace_back(inst2, inst2, weightY);
430+
fixedInstForceVecY_(inst2)
431+
+= weightY * (pin1->cy() - (pin2->cy() - instance2->cy()));
432+
} else if (!locked1 && locked2) {
433+
// inst2 is fixed: treat pin2 as a fixed terminal for inst1
434+
listY.emplace_back(inst1, inst1, weightY);
435+
fixedInstForceVecY_(inst1)
436+
+= weightY * (pin2->cy() - (pin1->cy() - instance1->cy()));
437+
}
438+
// both locked: no matrix or RHS contribution needed
400439
}
401440
// pin1 from IO port / pin2 from Instance
402441
else if (!pin1->isPlaceInstConnected()

‎src/gpl/src/nesterovBase.cpp‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,9 @@ NesterovBase::NesterovBase(
19221922
}
19231923

19241924
for (Instance* inst : gCell->insts()) {
1925-
inst->setLocation(pb_inst->lx() + x_offset, pb_inst->ly() + y_offset);
1925+
if (!inst->isLocked()) {
1926+
inst->setLocation(pb_inst->lx() + x_offset, pb_inst->ly() + y_offset);
1927+
}
19261928
}
19271929
gCell->updateLocations();
19281930
nb_gcells_.emplace_back(nbc_.get(), nbc_->getGCellIndex(gCell));
@@ -2821,6 +2823,12 @@ void NesterovBase::updateSingleGradient(
28212823
}
28222824

28232825
GCell* gCell = nb_gcells_.at(gCellIndex);
2826+
if (gCell->isLocked()) {
2827+
wireLengthGrads[gCellIndex] = FloatPoint(0, 0);
2828+
densityGrads[gCellIndex] = FloatPoint(0, 0);
2829+
sumGrads[gCellIndex] = FloatPoint(0, 0);
2830+
return;
2831+
}
28242832

28252833
wireLengthGrads[gCellIndex]
28262834
= nbc_->getWireLengthGradientWA(gCell, wlCoeffX, wlCoeffY);
@@ -2852,6 +2860,11 @@ void NesterovBase::updateInitialPrevSLPCoordi()
28522860
for (size_t i = 0; i < nb_gcells_.size(); i++) {
28532861
GCell* curGCell = nb_gcells_[i];
28542862

2863+
if (curGCell->isLocked()) {
2864+
prevSLPCoordi_[i] = curSLPCoordi_[i];
2865+
continue;
2866+
}
2867+
28552868
float prevCoordiX
28562869
= curSLPCoordi_[i].x
28572870
- (npVars_->initialPrevCoordiUpdateCoef * curSLPSumGrads_[i].x);
@@ -3061,6 +3074,13 @@ void NesterovBase::nesterovUpdateCoordinates(float coeff)
30613074

30623075
// fill in nextCoordinates with given stepLength_
30633076
for (size_t k = 0; k < nb_gcells_.size(); k++) {
3077+
GCell* curGCell = nb_gcells_[k];
3078+
if (curGCell->isLocked()) {
3079+
nextCoordi_[k] = curCoordi_[k];
3080+
nextSLPCoordi_[k] = curSLPCoordi_[k];
3081+
continue;
3082+
}
3083+
30643084
FloatPoint nextCoordi(
30653085
curSLPCoordi_[k].x + stepLength_ * curSLPSumGrads_[k].x,
30663086
curSLPCoordi_[k].y + stepLength_ * curSLPSumGrads_[k].y);
@@ -3069,8 +3089,6 @@ void NesterovBase::nesterovUpdateCoordinates(float coeff)
30693089
nextCoordi.x + coeff * (nextCoordi.x - curCoordi_[k].x),
30703090
nextCoordi.y + coeff * (nextCoordi.y - curCoordi_[k].y));
30713091

3072-
GCell* curGCell = nb_gcells_[k];
3073-
30743092
nextCoordi_[k]
30753093
= FloatPoint(getDensityCoordiLayoutInsideX(curGCell, nextCoordi.x),
30763094
getDensityCoordiLayoutInsideY(curGCell, nextCoordi.y));

‎src/gpl/src/nesterovBase.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ class NesterovBase
10941094
bool checkConvergence(int gpl_iter_count,
10951095
int routability_gpl_iter_count,
10961096
RouteBase* rb);
1097+
void resetConverged() { isConverged_ = false; }
10971098

10981099
bool checkDivergence();
10991100
void saveSnapshot();

‎src/gpl/src/nesterovPlace.cpp‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,6 @@ void NesterovPlace::updateIterGraphics(
308308
}
309309
}
310310

311-
if (npVars_.debug_generate_images && iter == 0) {
312-
std::string gif_path = fmt::format("{}/placement.gif", reports_dir);
313-
placement_gif_key_ = graphics_->gifStart(gif_path);
314-
}
315-
316311
if (npVars_.debug_generate_images && iter % 10 == 0) {
317312
odb::Rect region;
318313
int width_px = 500;
@@ -831,6 +826,7 @@ bool NesterovPlace::isConverged(int gpl_iter_count,
831826
if (num_region_converge == nbVec_.size()) {
832827
if (graphics_ && graphics_->enabled() && npVars_.debug_generate_images) {
833828
graphics_->gifEnd(placement_gif_key_);
829+
placement_gif_key_ = -1;
834830
}
835831
return true;
836832
}
@@ -1035,6 +1031,7 @@ int NesterovPlace::doNesterovPlace(int start_iter)
10351031
nb->setIter(start_iter);
10361032
nb->setMaxPhiCoefChanged(false);
10371033
nb->resetMinSumOverflow();
1034+
nb->resetConverged();
10381035
original_area += nb->getNesterovInstsArea();
10391036
}
10401037

@@ -1044,6 +1041,16 @@ int NesterovPlace::doNesterovPlace(int start_iter)
10441041
= reports_dir + "/gpl_routability_driven";
10451042

10461043
cleanReportsDirs(timing_driven_dir, routability_driven_dir);
1044+
if (graphics_ && graphics_->enabled() && npVars_.debug
1045+
&& npVars_.debug_generate_images) {
1046+
if (placement_gif_key_ != -1) {
1047+
graphics_->gifEnd(placement_gif_key_);
1048+
}
1049+
const std::string gif_name
1050+
= (start_iter == 0) ? "placement.gif" : "placement_stage2.gif";
1051+
placement_gif_key_
1052+
= graphics_->gifStart(fmt::format("{}/{}", reports_dir, gif_name));
1053+
}
10471054
if (graphics_ && graphics_->enabled() && npVars_.debug_generate_images) {
10481055
updateDb();
10491056
std::string label = fmt::format("init_nesterov");

‎src/gpl/src/replace.cpp‎

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,29 @@ void Replace::doIncrementalPlace(const int threads, const PlaceOptions& options)
8383
{
8484
checkHasCoreRows();
8585
log_->info(GPL, 6, "Execute incremental mode global placement.");
86-
if (pbc_ == nullptr) {
86+
int placed_cnt = 0;
87+
int unplaced_cnt = 0;
88+
bool is_pbc_new = (pbc_ == nullptr);
89+
90+
if (is_pbc_new) {
8791
pbc_ = std::make_shared<PlacerBaseCommon>(db_, options, log_);
92+
}
93+
94+
// Count placed vs unplaced, and conditionally lock them down
95+
auto block = db_->getChip()->getBlock();
96+
for (auto inst : block->getInsts()) {
97+
auto status = inst->getPlacementStatus();
98+
if (status == odb::dbPlacementStatus::PLACED) {
99+
if (is_pbc_new) {
100+
pbc_->dbToPb(inst)->lock();
101+
}
102+
++placed_cnt;
103+
} else if (!status.isPlaced()) {
104+
++unplaced_cnt;
105+
}
106+
}
88107

108+
if (is_pbc_new) {
89109
pbVec_.push_back(std::make_shared<PlacerBase>(db_, pbc_, log_, true));
90110

91111
for (auto pd : db_->getChip()->getBlock()->getRegions()) {
@@ -101,20 +121,6 @@ void Replace::doIncrementalPlace(const int threads, const PlaceOptions& options)
101121
}
102122
}
103123

104-
// Lock down already placed objects
105-
int placed_cnt = 0;
106-
int unplaced_cnt = 0;
107-
auto block = db_->getChip()->getBlock();
108-
for (auto inst : block->getInsts()) {
109-
auto status = inst->getPlacementStatus();
110-
if (status == odb::dbPlacementStatus::PLACED) {
111-
pbc_->dbToPb(inst)->lock();
112-
++placed_cnt;
113-
} else if (!status.isPlaced()) {
114-
++unplaced_cnt;
115-
}
116-
}
117-
118124
log_->info(GPL, 154, "Identified {} placed instances", placed_cnt);
119125
log_->info(GPL, 155, "Identified {} not placed instances", unplaced_cnt);
120126

@@ -130,21 +136,30 @@ void Replace::doIncrementalPlace(const int threads, const PlaceOptions& options)
130136
// Roughly place the unplaced objects (allow more overflow).
131137
// Limit iterations to prevent objects drifting too far or
132138
// non-convergence.
133-
PlaceOptions rough_options = options;
134-
rough_options.overflow = std::max(options.overflow, 0.2f);
135-
rough_options.nesterovPlaceMaxIter = 300;
139+
PlaceOptions locked_options = options;
140+
locked_options.overflow = std::max(options.overflow, 0.2f);
141+
locked_options.nesterovPlaceMaxIter = 300;
136142

137-
doInitialPlace(threads, rough_options);
138-
const int iter = doNesterovPlace(threads, rough_options);
143+
// Use uniform density for incremental runs to fill gaps effectively
144+
if (!options.uniformTargetDensityMode) {
145+
locked_options.uniformTargetDensityMode = true;
146+
}
147+
148+
doInitialPlace(threads, locked_options);
149+
const int iter = doNesterovPlace(threads, locked_options);
139150

140-
// Finish the overflow resolution from the rough placement
151+
// Finish the overflow resolution from the locked placement
141152
log_->info(GPL, 133, "Unlocking all instances");
142153
for (auto& pb : pbVec_) {
143154
pb->unlockAll();
144155
}
145156

146-
if (options.overflow < rough_options.overflow) {
147-
doNesterovPlace(threads, options, iter + 1);
157+
if (options.overflow < locked_options.overflow) {
158+
PlaceOptions final_options = options;
159+
final_options.uniformTargetDensityMode = true;
160+
final_options.initDensityPenaltyFactor = 1;
161+
162+
doNesterovPlace(threads, final_options, iter + 1);
148163
}
149164
}
150165

0 commit comments

Comments
 (0)