30 std::vector<DesignInfo::DesignCell *> LUTs;
33 LUTs.push_back(tmpLUT);
36 LUTs.push_back(tmpLUTPair.first);
37 LUTs.push_back(tmpLUTPair.second);
44 return a->getCellId() > b->getCellId();
50 std::vector<float> LUTPairCost;
53 MaximalCardinalityMatching::Graph G(LUTs.size());
55 for (
unsigned int i = 0;
i < LUTs.size();
i++)
57 if (LUTs[
i]->getInputPins().size() <= 5 &&
60 for (
unsigned int j =
i + 1; j < LUTs.size(); j++)
62 if (LUTs[j]->getInputPins().size() <= 5 && !LUTs[j]->
isLUT6())
81 MaximalCardinalityMatching M(G);
83 std::list<int> matching;
84 matching = M.SolveMaximumMatching();
86 std::set<int> addedNodes;
88 for (std::list<int>::iterator it = matching.begin(); it != matching.end(); it++)
90 std::pair<int, int> e = G.GetEdge(*it);
91 assert(addedNodes.find(e.first) == addedNodes.end());
92 assert(addedNodes.find(e.second) == addedNodes.end());
93 assert(e.first != e.second);
94 auto LUTA = LUTs[e.first];
95 auto LUTB = LUTs[e.second];
100 addedNodes.insert(e.first);
101 addedNodes.insert(e.second);
102 if (LUTA->getCellId() < LUTB->getCellId())
116 assert(singleLUTs.find(curLUT) == singleLUTs.end());
118 if (singleLUTs.size() + 1 + pairedLUTs.size() + parentPackingCLB->getFixedPairedLUTs().size() <= MaxNum_LUTSite)
120 singleLUTs.insert(curLUT);
128 for (
auto singleLUT : singleLUTs)
132 if (!(parentPackingCLB->conflictLUTsContain(curLUT) &&
133 parentPackingCLB->conflictLUTsContain(singleLUT)))
137 LUTToPair = singleLUT;
139 else if (LUTToPair->
getCellId() > singleLUT->getCellId())
141 LUTToPair = singleLUT;
148 singleLUTs.erase(LUTToPair);
151 pairedLUTs.emplace(LUTToPair, curLUT);
155 pairedLUTs.emplace(curLUT, LUTToPair);
161 singleLUTs.insert(curLUT);
163 if (singleLUTs.size() + pairedLUTs.size() + parentPackingCLB->getFixedPairedLUTs().size() <= MaxNum_LUTSite)
168 maxCardinalityMatching();
170 return singleLUTs.size() + pairedLUTs.size() + parentPackingCLB->getFixedPairedLUTs().size() <= MaxNum_LUTSite;
176 if (FFControlSets[
i].getSize() < MaxNum_FFinControlSet)
180 FFControlSets[
i].addFF(curFF);
183 else if (FFControlSets[
i].getCSId() < 0)
186 int anotherSetId =
i - 1 + ((
i % 2 == 0) ? 2 : 0);
187 if (FFControlSets[anotherSetId].getCSId() < 0)
189 FFControlSets[
i].addFF(curFF);
196 FFControlSets[
i].addFF(curFF);
202 FFControlSets[
i].addFF(curFF);
218 if (FFControlSets[anotherHalfCLB].getCSId() < 0)
222 else if (FFControlSets[anotherHalfCLB].getCLK() == CSPtr->
getCLK() &&
223 FFControlSets[anotherHalfCLB].getSR() == CSPtr->
getSR() &&
234 if (FFControlSets[halfCLB].getCSId() < 0 || FFControlSets[anotherHalfCLB].getCSId() < 0)
238 else if (FFControlSets[anotherHalfCLB].getCLK() == FFControlSets[halfCLB].getCLK() &&
239 FFControlSets[anotherHalfCLB].getSR() == FFControlSets[halfCLB].getSR() &&
252 for (
auto tmpFF : curFFs)
253 succ &= addToFFSet(tmpFF, halfCLB);
255 assert(succ &&
"adding FF group should be checked in advance and should be successful.");
261 bool enforceMainFFSlot)
263 if (enforceHalfCLB < 0)
265 if (enforceMainFFSlot)
267 for (
unsigned int i = 0;
i < FFControlSets.size();
i += 2)
269 if (addToFFSet(curFF,
i))
275 const int orderIds[4] = {1, 3, 0, 2};
276 for (
unsigned int i = 0;
i < FFControlSets.size();
i++)
278 if (addToFFSet(curFF, orderIds[
i]))
285 if (addToFFSet(curFF, enforceHalfCLB))
294 for (
auto tmpFF : FFControlSets[
i].getFFs())
296 if (!tmpFF->isVirtualCell())
299 parentPackingCLB->getPlacementInfo()->getPlacementUnitByCell(tmpFF)))
310 if (res <= 1 && addNum <= 2)
316 int enforceHalfCLB,
bool enforceMainFFSlot,
320 int addNum = curFFs.size();
322 for (
auto FF : curFFs)
324 if (FF->getControlSetInfo())
328 CSId = FF->getControlSetInfo()->
getId();
329 CSPtr = FF->getControlSetInfo();
333 assert(CSId == FF->getControlSetInfo()->getId());
337 if (enforceHalfCLB < 0)
339 if (enforceMainFFSlot)
341 for (
int i = 0;
i < (int)FFControlSets.size();
i += 2)
343 int anotherSetId =
i - 1 + ((
i % 2 == 0) ? 2 : 0);
344 if (FFControlSets[
i].getFFs().size() + curFFs.size() <= 4 && FFControlSets[
i].compatibleWith(CSId))
350 assert(addToFFSet(curFFs,
i));
351 FFControlSets[
i].setMustMainSlots();
360 for (
int i = 0;
i < (int)FFControlSets.size();
i++)
362 int anotherSetId =
i - 1 + ((
i % 2 == 0) ? 2 : 0);
363 if (FFControlSets[
i].getFFs().size() + curFFs.size() <= 4 && FFControlSets[
i].compatibleWith(CSId))
369 assert(addToFFSet(curFFs,
i));
379 int anotherSetId = enforceHalfCLB - enforceHalfCLB + ((enforceHalfCLB % 2 == 0) ? 2 : 0);
380 if (FFControlSets[enforceHalfCLB].getFFs().size() + curFFs.size() <= 4 &&
381 FFControlSets[enforceHalfCLB].compatibleWith(CSId))
387 assert(addToFFSet(curFFs, enforceHalfCLB));
398 std::vector<DesignInfo::DesignCell *> cellsToAdd(0);
401 cellsToAdd.push_back(unpackCell->getCell());
405 for (
auto tmpCell : curMacro->getCells())
406 cellsToAdd.push_back(tmpCell);
409 for (
auto curCell : cellsToAdd)
411 std::cout <<
"checking:\n" << curCell <<
"\n";
413 if (curCell->isLUT())
415 succ = addLUT(curCell);
418 std::cout <<
"failed to add LUT cell:\n" << curCell <<
"\nWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
419 std::cout <<
this <<
"\n";
424 std::cout <<
"succedd to add LUT cell:\n" << curCell <<
"\n";
427 else if (curCell->isFF())
429 succ = addFF(curCell);
432 std::cout <<
"failed to add FF cell:\n" << curCell <<
"\nWWWWWWWWWWWWWWWWWWWWWWWWWWW\n";
433 for (
auto inputPin : curCell->getInputPins())
437 if (!inputPin->isUnconnected())
439 std::cout <<
"CLK: " << inputPin->getNet()->getName() <<
"\n";
444 if (!inputPin->isUnconnected())
446 std::cout <<
"CE: " << inputPin->getNet()->getName() <<
"\n";
451 if (!inputPin->isUnconnected())
453 std::cout <<
"SR: " << inputPin->getNet()->getName() <<
"\n";
462 assert(
false &&
"undefined FF input pin type.");
465 std::cout <<
this <<
"\n";
470 std::cout <<
"succedd to add FF cell:\n" << curCell <<
"\n";
476 assert(curCell->isMux());
480 if (!parentPackingCLB->getPlacementInfo()->checkClockColumnLegalization(tmpPU, parentPackingCLB->getCLBSite()))
482 std::cout << parentPackingCLB->getCLBSite()->getName() <<
"\n";
483 std::cout <<
"Clock Utilization Overflow: #clockNum: "
484 << parentPackingCLB->getPlacementInfo()
485 ->getClockCol2ClockNets()[parentPackingCLB->getCLBSite()->getClockHalfColumn()]
494 if (curNet->
getUnits().size() > 512)
504 if (PUs.find(tmpPU) != PUs.end())
517 std::set<PlacementInfo::PlacementNet *, Packing_Netcompare> nets;
527 for (
auto tmpPU : PUs)
530 for (
auto tmpNet : *tmpPU->getNetsSetPtr())
532 if (tmpNet->getUnits().size() > 64)
535 if (nets.find(tmpNet) == nets.end())
540 totalNetNum += tmpPU->getNetsSetPtr()->size() * 0.5;
541 totalLen += getPlacementUnitMaxPathLen(tmpPU);
543 totalCellNum += tmpPU->getWeight();
548 totalConnectivityScore = 0;
549 for (
auto tmpNet : nets)
551 if (tmpNet->getUnits().size() > 1)
553 float tmpConnectivityVal = (float)(getInternalPinsNum(tmpNet) - 1) / (
float)(tmpNet->getUnits().size() - 1);
555 totalConnectivityScore += tmpConnectivityVal;
560 totalConnectivityScore += 1;
564 scoreInSite = totalNetNum * 0.45 + 0.05 * totalLen + 0.5 * totalConnectivityScore -
HPWLWeight * HPWLChange -
565 0.2 * (singleLUTs.size() + pairedLUTs.size());
571 std::set<PlacementInfo::PlacementNet *, Packing_Netcompare> nets;
579 for (
auto tmpPU : PUs)
582 for (
auto tmpNet : *tmpPU->getNetsSetPtr())
584 if (tmpNet->getUnits().size() > 64)
587 if (nets.find(tmpNet) == nets.end())
592 totalNetNum += tmpPU->getNetsSetPtr()->size() * 0.5;
593 totalLen += getPlacementUnitMaxPathLen(tmpPU);
595 totalCellNum += tmpPU->getWeight();
600 totalConnectivityScore = 0;
601 for (
auto tmpNet : nets)
603 if (tmpNet->getUnits().size() > 1)
605 float tmpConnectivityVal = (float)(getInternalPinsNum(tmpNet) - 1) / (
float)(tmpNet->getUnits().size() - 1);
608 totalConnectivityScore += tmpConnectivityVal;
613 totalConnectivityScore += 1;
617 scoreInSite = totalNetNum * 0.45 + 0.05 * totalLen + 0.5 * totalConnectivityScore -
HPWLWeight * HPWLChange -
618 0.2 * (singleLUTs.size() + pairedLUTs.size());
623 if (parentPackingCLB->checkIsLUTRAMSite())
628 if (PUs.find(tmpPU) != PUs.end())
633 if (PUs.find(tmpPU) != PUs.end())
637 bool enforceMainFF =
false;
638 bool isMUXMacro =
false;
645 enforceMainFF =
true;
647 if (parentPackingCLB->checkIsPrePackedSite())
654 if (parentPackingCLB->getCLBSite()->getSiteType() ==
"SLICEM")
659 if (tmpMacro->getCells()[0]->isLUT6())
661 if (tmpMacro->getCells()[0]->getTimingLength() > 10)
662 enforceMainFF =
true;
667 std::vector<DesignInfo::DesignCell *> cellsToAdd(0);
668 std::vector<DesignInfo::DesignCell *> FFsToAdd(0);
671 cellsToAdd.push_back(unpackCell->getCell());
672 if (isMUXMacro && unpackCell->getCell()->isFF())
673 FFsToAdd.push_back(unpackCell->getCell());
677 for (
auto tmpCell : curMacro->getCells())
679 cellsToAdd.push_back(tmpCell);
680 if (isMUXMacro && tmpCell->isFF())
682 FFsToAdd.push_back(tmpCell);
687 if (isMUXMacro && FFsToAdd.size())
689 if (!addFFGroup(FFsToAdd, -1,
true,
true))
696 for (
auto curCell : cellsToAdd)
699 if (curCell->isLUT())
701 succ = addLUT(curCell);
703 else if (curCell->isFF())
708 succ = addFF(curCell, -1, enforceMainFF);
712 if (parentPackingCLB->checkIsCarrySite())
716 assert(curCell->isMux());
725 if (!(PUs.size() > 0 || parentPackingCLB->checkIsPrePackedSite()))
728 std::cout <<
" curPU=\n" << tmpPU <<
"\n";
729 std::cout <<
" curCell=\n" << curCell <<
"\n";
732 if (!curCell->getControlSetInfo())
734 std::cout <<
" CLK: virtual\n";
735 std::cout <<
" SR: virtual\n";
736 std::cout <<
" CE: virtual\n";
741 if (curCell->getControlSetInfo()->getCLK())
742 std::cout <<
" CLK: " << curCell->getControlSetInfo()->getCLK()->
getName() <<
"\n";
743 if (curCell->getControlSetInfo()->getSR())
744 std::cout <<
" SR: " << curCell->getControlSetInfo()->getSR()->getName() <<
"\n";
745 if (curCell->getControlSetInfo()->getCE())
746 std::cout <<
" CE: " << curCell->getControlSetInfo()->getCE()->getName() <<
"\n";
751 assert(PUs.size() > 0 || parentPackingCLB->checkIsPrePackedSite());
764 if (!parentPackingCLB->getPlacementInfo()->checkClockColumnLegalization(tmpPU, parentPackingCLB->getCLBSite()))
773 for (
auto tmpLUTPair0 : pairedLUTs)
775 auto tmpPUA = parentPackingCLB->getPlacementInfo()->getPlacementUnitByCell(tmpLUTPair0.second);
776 auto tmpPUB = parentPackingCLB->getPlacementInfo()->getPlacementUnitByCell(tmpLUTPair0.first);
777 if (PUs.find(tmpPUA) == PUs.end() || PUs.find(tmpPUB) == PUs.end())
781 if (PUs.find(tmpPUA) == PUs.end())
785 if (tmpMacro->checkHasCARRY())
790 if (tmpPUA == parentPackingCLB->getCarryMacro())
799 if (PUs.find(tmpPUB) == PUs.end())
803 if (tmpMacro->checkHasCARRY())
808 if (tmpPUB == parentPackingCLB->getCarryMacro())
824 std::cout <<
"adding PU=\n" << tmpPU <<
"\n";
826 std::cout <<
"removing PU\n" << tmpPU <<
"\n";
829 std::cout <<
"problem is this pair:\n" << tmpPUA <<
"\n" << tmpPUB <<
"\n";
834 for (
auto tmpLUT : singleLUTs)
836 auto tmpPUA = parentPackingCLB->getPlacementInfo()->getPlacementUnitByCell(tmpLUT);
837 if (PUs.find(tmpPUA) == PUs.end())
840 if (PUs.find(tmpPUA) == PUs.end())
844 if (tmpMacro->checkHasCARRY())
849 if (tmpPUA == parentPackingCLB->getCarryMacro())
866 std::cout <<
"adding PU=\n" << tmpPU <<
"\n";
868 std::cout <<
"removing PU\n" << tmpPU <<
"\n";
871 std::cout <<
"problem is this PU LUT:\n" << tmpPUA <<
"\n";
876 for (
auto &CS : FFControlSets)
878 for (
auto tmpFF : CS.getFFs())
880 auto tmpPUA = parentPackingCLB->getPlacementInfo()->getPlacementUnitByCell(tmpFF);
881 if (PUs.find(tmpPUA) == PUs.end())
884 if (PUs.find(tmpPUA) == PUs.end())
888 if (tmpMacro->checkHasCARRY())
893 if (tmpPUA == parentPackingCLB->getCarryMacro())
910 std::cout <<
"adding PU=\n" << tmpPU <<
"\n";
912 std::cout <<
"removing PU\n" << tmpPU <<
"\n";
915 std::cout <<
"problem is this PU FF:\n" << tmpPUA <<
"\n";
925 os <<
"===============================================\n";
927 for (
auto tmpPU : tmpCluster->
getPUs())
930 tmpSize += tmpPU->getWeight();
934 os <<
"PackingCLBCluster: id=" << tmpCluster->
getId() <<
" #cell=" << tmpSize
946 for (
auto tmpPU : tmpCluster->
getPUs())
949 float HPWLChange = 0;
950 for (
auto tmpNet : *tmpPU->getNetsSetPtr())
952 if (tmpNet->getUnits().size() > 64)
959 os <<
" HPWLChange=" << HPWLChange <<
"\n";
965 os <<
"SingleLUT#" <<
cnt <<
": " << singleLUT <<
"\n";
971 os <<
"LUTPair#" <<
cnt <<
"\n";
973 os <<
" LUT-A: " << tmpPair.first <<
"\n";
974 os <<
" LUT-B: " << tmpPair.second <<
"\n";
978 os <<
"FixedLUTPair#" <<
cnt <<
"\n";
980 os <<
" LUT-A: " << tmpPair.first <<
"\n";
981 os <<
" LUT-B: " << tmpPair.second <<
"\n";
986 os <<
"FG#" <<
cnt <<
"\n";
987 if (FG.getFFs().size() == 0)
989 if (FG.getFFs().size() == 0)
991 if (FG.getCSId() >= 0)
994 os <<
" CLK:" << FG.getCLK()->getName() <<
"\n";
996 os <<
" SR:" << FG.getSR()->getName() <<
"\n";
998 os <<
" CE:" << FG.getCE()->getName() <<
"\n";
1003 os <<
" CLK: virtual.\n";
1004 os <<
" SR: virtual.\n";
1005 os <<
" CE: virtual.\n";
1009 for (
auto curFF : FG.getFFs())
1011 os <<
" FF: " << curFF <<
"\n";
1014 os <<
"===============================================\n";
1020 std::cout <<
"===============================================\n";
1022 for (
auto tmpPU : getPUs())
1025 tmpSize += tmpPU->getWeight();
1029 std::cout <<
"PackingCLBCluster: id=" << getId() <<
" #cell=" << tmpSize <<
" hash=" << getHashConst()
1030 <<
" score=" << getScoreInSite() <<
" \n";
1031 std::cout <<
" targetSite: " << getParentPackingCLB()->getCLBSite()->getName()
1032 <<
" X:" << getParentPackingCLB()->getCLBSite()->X() <<
" Y:" << getParentPackingCLB()->getCLBSite()->Y()
1037 std::cout <<
"CARRY part (offset=" << getParentPackingCLB()->getCarrySiteOffset()
1038 <<
"):" << getParentPackingCLB()->getCarryMacro() <<
"\n";
1041 for (
auto tmpPU : getPUs())
1044 float HPWLChange = 0;
1045 for (
auto tmpNet : *tmpPU->getNetsSetPtr())
1047 if (tmpNet->getUnits().size() > 64)
1049 HPWLChange += tmpNet->getNewHPWLByTrying(tmpPU, getParentPackingCLB()->
getCLBSite()->
X(),
1052 HPWLChange -= tmpNet->getHPWL(getParentPackingCLB()->
getY2xRatio());
1054 std::cout <<
" HPWLChange=" << HPWLChange <<
"\n";
1058 for (
auto singleLUT : getSingleLUTs())
1060 std::cout <<
"SingleLUT#" <<
cnt <<
": " << singleLUT <<
"\n";
1064 for (
auto tmpPair : getPairedLUTs())
1066 std::cout <<
"LUTPair#" <<
cnt <<
"\n";
1068 std::cout <<
" LUT-A: " << tmpPair.first <<
"\n";
1069 std::cout <<
" LUT-B: " << tmpPair.second <<
"\n";
1071 for (
auto tmpPair : parentPackingCLB->getFixedPairedLUTs())
1073 std::cout <<
"FixedLUTPair#" <<
cnt <<
"\n";
1075 std::cout <<
" LUT-A: " << tmpPair.first <<
"\n";
1076 std::cout <<
" LUT-B: " << tmpPair.second <<
"\n";
1079 for (
auto &FG : getFFControlSets())
1081 std::cout <<
"FG#" <<
cnt <<
"\n";
1082 if (FG.getFFs().size() == 0)
1084 if (FG.getCSId() >= 0)
1087 std::cout <<
" CLK:" << FG.getCLK()->getName() <<
"\n";
1089 std::cout <<
" SR:" << FG.getSR()->getName() <<
"\n";
1091 std::cout <<
" CE:" << FG.getCE()->getName() <<
"\n";
1096 std::cout <<
" CLK: virtual.\n";
1097 std::cout <<
" SR: virtual.\n";
1098 std::cout <<
" CE: virtual.\n";
1101 for (
auto curFF : FG.getFFs())
1103 std::cout <<
" FF: " << curFF <<
"\n";
1106 std::cout <<
"===============================================\n";