Skip to content

Commit b454511

Browse files
authored
Merge pull request #403 from chenjunhao0315/master
rewire restructured even if the cost is not improved
2 parents dcf9079 + 6c37cc9 commit b454511

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/opt/rar/rewire_miaig.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,27 +1200,33 @@ Miaig randomRead(std::vector<Miaig> &pBests) {
12001200
return pBests[Random_Num(0) % pBests.size()];
12011201
}
12021202

1203+
Miaig randomReadExcept(std::vector<Miaig> &pBests, Miaig &pExcept) {
1204+
int iNum = Random_Num(0) % pBests.size();
1205+
return (pBests[iNum] == pExcept) ? pBests[(iNum + 1) % pBests.size()] : pBests[iNum];
1206+
}
1207+
12031208
Miaig Miaig::rewire(int nIters, float levelGrowRatio, int nExpands, int nGrowth, int nDivs, int nFaninMax, int nTimeOut, int nMode, int nMappedMode, int nDist, int fCheck, int nVerbose) {
12041209
const int nRootSave = 8;
12051210
const int nBestSave = 4;
12061211
int nRestart = 5000;
12071212
std::vector<Miaig> pRoots = {this->dup(0)};
1208-
std::vector<Miaig> pBests = {this->dup(0)};
1213+
std::vector<Miaig> pBests = {this->dup(0)}; Miaig pInit = pBests[0];
12091214
iword clkStart = Time_Clock();
12101215
Miaig pNew;
12111216
Miaig pRoot = pRoots[0];
1212-
Miaig pBest = this->dup(0);
1217+
Miaig pBest = this->dup(0); int improved = 0;
12131218
float (Miaig::*Miaig_ObjectiveFunction)(int, int) = (nMode == 0) ? &Miaig::countAnd2 : &Miaig::countTransistors;
12141219
int maxLevel = levelGrowRatio != 0 ? this->countLevel() * levelGrowRatio : 0;
12151220
int nExpandableLevel = maxLevel ? maxLevel - this->countLevel() : 0;
1221+
int fMapped = nMode > 0;
12161222
word *pExc = _data->pExc;
12171223

12181224
float PrevBest = ((&pBest)->*Miaig_ObjectiveFunction)(1, nMappedMode);
12191225
int iterNotImproveAfterRestart = 0;
12201226
if (nVerbose && maxLevel) printf("Max level : %5d\n", maxLevel);
12211227
if (nVerbose) printf("Initial target : %5g (AND2 = %5g Level = %3d)\n", PrevBest, this->countAnd2(1), this->countLevel());
12221228
for (int i = 0; nIters ? i < nIters : 1; i++) {
1223-
if (nVerbose) printf("\rIteration %7d : %5g -> ", i + 1, ((&pRoot)->*Miaig_ObjectiveFunction)(0, nMappedMode));
1229+
if (nVerbose) printf("\rIteration %7d(%zu) : %5g -> ", i + 1, pBests.size(), ((&pRoot)->*Miaig_ObjectiveFunction)(0, nMappedMode));
12241230
if (nTimeOut && nTimeOut < 1.0 * (Time_Clock() - clkStart) / CLOCKS_PER_SEC) break;
12251231
if (PrevBest == 0) break;
12261232
pNew = pRoot.dupMulti(nFaninMax, nGrowth);
@@ -1239,11 +1245,11 @@ Miaig Miaig::rewire(int nIters, float levelGrowRatio, int nExpands, int nGrowth,
12391245
if (nVerbose) printf("%5g (AND2 = %5g Level = %3d) ", newTarget, pNew.countAnd2(), pNew.countLevel());
12401246
if (nVerbose) Time_PrintEndl("Elapsed time", Time_Clock() - clkStart);
12411247
PrevBest = newTarget;
1242-
pBests = {pNew.dup(0), pNew.dup(0)};
1243-
pBest = pNew.dup(0, 1);
1248+
pBests = {pNew.dup(0, fMapped), pNew.dup(0, fMapped)};
1249+
pBest = pNew.dup(0, fMapped), improved = 1;
12441250
iterNotImproveAfterRestart = 0;
12451251
} else if (PrevBest == newTarget) {
1246-
randomAddBest(pBests, pNew.dup(0), nBestSave);
1252+
randomAddBest(pBests, pNew.dup(0, fMapped), nBestSave);
12471253
}
12481254
// compare
12491255
if (maxLevel ? pNew.countLevel() > maxLevel : 0) {
@@ -1266,7 +1272,7 @@ Miaig Miaig::rewire(int nIters, float levelGrowRatio, int nExpands, int nGrowth,
12661272
pRoot = randomRead(pRoots);
12671273
}
12681274
if (nVerbose) Time_PrintEndl("Total solving time", Time_Clock() - clkStart);
1269-
return pBest;
1275+
return improved ? pBest : randomReadExcept(pBests, pInit);
12701276
}
12711277

12721278
} // namespace Rewire

src/opt/rar/rewire_miaig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class Miaig {
190190
public:
191191
void addref(void);
192192
void release(void);
193+
bool operator==(const Miaig &m) const;
193194

194195
private:
195196
void create(int nIns, int nOuts, int nObjsAlloc);
@@ -394,6 +395,10 @@ inline void Miaig::release(void) {
394395
_refcount = nullptr;
395396
}
396397

398+
inline bool Miaig::operator==(const Miaig &m) const {
399+
return (_data == m._data);
400+
}
401+
397402
inline int &Miaig::nIns(void) {
398403
return _data->nIns;
399404
}

0 commit comments

Comments
 (0)