00001
00021 #include "abc.h"
00022 #include "resInt.h"
00023 #include "kit.h"
00024 #include "satStore.h"
00025
00029
00030 typedef struct Res_Man_t_ Res_Man_t;
00031 struct Res_Man_t_
00032 {
00033
00034 Res_Par_t * pPars;
00035
00036 Res_Win_t * pWin;
00037 Abc_Ntk_t * pAig;
00038 Res_Sim_t * pSim;
00039 Sto_Man_t * pCnf;
00040 Int_Man_t * pMan;
00041 Vec_Int_t * vMem;
00042 Vec_Vec_t * vResubs;
00043 Vec_Vec_t * vResubsW;
00044 Vec_Vec_t * vLevels;
00045
00046 int nWins;
00047 int nWinNodes;
00048 int nDivNodes;
00049 int nWinsTriv;
00050 int nWinsUsed;
00051 int nConstsUsed;
00052 int nCandSets;
00053 int nProvedSets;
00054 int nSimEmpty;
00055 int nTotalNets;
00056 int nTotalNodes;
00057 int nTotalNets2;
00058 int nTotalNodes2;
00059
00060 int timeWin;
00061 int timeDiv;
00062 int timeAig;
00063 int timeSim;
00064 int timeCand;
00065 int timeSatTotal;
00066 int timeSatSat;
00067 int timeSatUnsat;
00068 int timeSatSim;
00069 int timeInt;
00070 int timeUpd;
00071 int timeTotal;
00072 };
00073
00074 extern Hop_Obj_t * Kit_GraphToHop( Hop_Man_t * pMan, Kit_Graph_t * pGraph );
00075
00076 extern int s_ResynTime;
00077
00081
00093 Res_Man_t * Res_ManAlloc( Res_Par_t * pPars )
00094 {
00095 Res_Man_t * p;
00096 p = ALLOC( Res_Man_t, 1 );
00097 memset( p, 0, sizeof(Res_Man_t) );
00098 assert( pPars->nWindow > 0 && pPars->nWindow < 100 );
00099 assert( pPars->nCands > 0 && pPars->nCands < 100 );
00100 p->pPars = pPars;
00101 p->pWin = Res_WinAlloc();
00102 p->pSim = Res_SimAlloc( pPars->nSimWords );
00103 p->pMan = Int_ManAlloc( 512 );
00104 p->vMem = Vec_IntAlloc( 0 );
00105 p->vResubs = Vec_VecStart( pPars->nCands );
00106 p->vResubsW = Vec_VecStart( pPars->nCands );
00107 p->vLevels = Vec_VecStart( 32 );
00108 return p;
00109 }
00110
00122 void Res_ManFree( Res_Man_t * p )
00123 {
00124 if ( p->pPars->fVerbose )
00125 {
00126 printf( "Reduction in nodes = %5d. (%.2f %%) ",
00127 p->nTotalNodes-p->nTotalNodes2,
00128 100.0*(p->nTotalNodes-p->nTotalNodes2)/p->nTotalNodes );
00129 printf( "Reduction in edges = %5d. (%.2f %%) ",
00130 p->nTotalNets-p->nTotalNets2,
00131 100.0*(p->nTotalNets-p->nTotalNets2)/p->nTotalNets );
00132 printf( "\n" );
00133
00134 printf( "Winds = %d. ", p->nWins );
00135 printf( "Nodes = %d. (Ave = %5.1f) ", p->nWinNodes, 1.0*p->nWinNodes/p->nWins );
00136 printf( "Divs = %d. (Ave = %5.1f) ", p->nDivNodes, 1.0*p->nDivNodes/p->nWins );
00137 printf( "\n" );
00138 printf( "WinsTriv = %d. ", p->nWinsTriv );
00139 printf( "SimsEmpt = %d. ", p->nSimEmpty );
00140 printf( "Const = %d. ", p->nConstsUsed );
00141 printf( "WindUsed = %d. ", p->nWinsUsed );
00142 printf( "Cands = %d. ", p->nCandSets );
00143 printf( "Proved = %d.", p->nProvedSets );
00144 printf( "\n" );
00145
00146 PRTP( "Windowing ", p->timeWin, p->timeTotal );
00147 PRTP( "Divisors ", p->timeDiv, p->timeTotal );
00148 PRTP( "Strashing ", p->timeAig, p->timeTotal );
00149 PRTP( "Simulation ", p->timeSim, p->timeTotal );
00150 PRTP( "Candidates ", p->timeCand, p->timeTotal );
00151 PRTP( "SAT solver ", p->timeSatTotal, p->timeTotal );
00152 PRTP( " sat ", p->timeSatSat, p->timeTotal );
00153 PRTP( " unsat ", p->timeSatUnsat, p->timeTotal );
00154 PRTP( " simul ", p->timeSatSim, p->timeTotal );
00155 PRTP( "Interpol ", p->timeInt, p->timeTotal );
00156 PRTP( "Undating ", p->timeUpd, p->timeTotal );
00157 PRTP( "TOTAL ", p->timeTotal, p->timeTotal );
00158 }
00159 Res_WinFree( p->pWin );
00160 if ( p->pAig ) Abc_NtkDelete( p->pAig );
00161 Res_SimFree( p->pSim );
00162 if ( p->pCnf ) Sto_ManFree( p->pCnf );
00163 Int_ManFree( p->pMan );
00164 Vec_IntFree( p->vMem );
00165 Vec_VecFree( p->vResubs );
00166 Vec_VecFree( p->vResubsW );
00167 Vec_VecFree( p->vLevels );
00168 free( p );
00169 }
00170
00182 void Res_UpdateNetwork( Abc_Obj_t * pObj, Vec_Ptr_t * vFanins, Hop_Obj_t * pFunc, Vec_Vec_t * vLevels )
00183 {
00184 Abc_Obj_t * pObjNew, * pFanin;
00185 int k;
00186
00187 pObjNew = Abc_NtkCreateNode( pObj->pNtk );
00188 pObjNew->pData = pFunc;
00189 Vec_PtrForEachEntry( vFanins, pFanin, k )
00190 Abc_ObjAddFanin( pObjNew, pFanin );
00191
00192
00193 Abc_NtkUpdate( pObj, pObjNew, vLevels );
00194 }
00195
00207 int Abc_NtkResynthesize( Abc_Ntk_t * pNtk, Res_Par_t * pPars )
00208 {
00209 ProgressBar * pProgress;
00210 Res_Man_t * p;
00211 Abc_Obj_t * pObj;
00212 Hop_Obj_t * pFunc;
00213 Kit_Graph_t * pGraph;
00214 Vec_Ptr_t * vFanins;
00215 unsigned * puTruth;
00216 int i, k, RetValue, nNodesOld, nFanins, nFaninsMax;
00217 int clk, clkTotal = clock();
00218
00219
00220 p = Res_ManAlloc( pPars );
00221 p->nTotalNets = Abc_NtkGetTotalFanins(pNtk);
00222 p->nTotalNodes = Abc_NtkNodeNum(pNtk);
00223 nFaninsMax = Abc_NtkGetFaninMax(pNtk);
00224
00225
00226 Abc_NtkSweep( pNtk, 0 );
00227
00228
00229 if ( !Abc_NtkToAig(pNtk) )
00230 {
00231 fprintf( stdout, "Converting to BDD has failed.\n" );
00232 Res_ManFree( p );
00233 return 0;
00234 }
00235 assert( Abc_NtkHasAig(pNtk) );
00236
00237
00238 Abc_NtkLevel( pNtk );
00239 Abc_NtkStartReverseLevels( pNtk, pPars->nGrowthLevel );
00240
00241
00242 nNodesOld = Abc_NtkObjNumMax(pNtk);
00243 pProgress = Extra_ProgressBarStart( stdout, nNodesOld );
00244 Abc_NtkForEachObj( pNtk, pObj, i )
00245 {
00246 Extra_ProgressBarUpdate( pProgress, i, NULL );
00247 if ( !Abc_ObjIsNode(pObj) )
00248 continue;
00249 if ( pObj->Id > nNodesOld )
00250 break;
00251
00252
00253 clk = clock();
00254 RetValue = Res_WinCompute( pObj, p->pPars->nWindow/10, p->pPars->nWindow%10, p->pWin );
00255 p->timeWin += clock() - clk;
00256 if ( !RetValue )
00257 continue;
00258 p->nWinsTriv += Res_WinIsTrivial( p->pWin );
00259
00260 if ( p->pPars->fVeryVerbose )
00261 {
00262 printf( "%5d (lev=%2d) : ", pObj->Id, pObj->Level );
00263 printf( "Win = %3d/%3d/%4d/%3d ",
00264 Vec_PtrSize(p->pWin->vLeaves),
00265 Vec_PtrSize(p->pWin->vBranches),
00266 Vec_PtrSize(p->pWin->vNodes),
00267 Vec_PtrSize(p->pWin->vRoots) );
00268 }
00269
00270
00271 clk = clock();
00272 Res_WinDivisors( p->pWin, Abc_ObjRequiredLevel(pObj) - 1 );
00273 p->timeDiv += clock() - clk;
00274
00275 p->nWins++;
00276 p->nWinNodes += Vec_PtrSize(p->pWin->vNodes);
00277 p->nDivNodes += Vec_PtrSize( p->pWin->vDivs);
00278
00279 if ( p->pPars->fVeryVerbose )
00280 {
00281 printf( "D = %3d ", Vec_PtrSize(p->pWin->vDivs) );
00282 printf( "D+ = %3d ", p->pWin->nDivsPlus );
00283 }
00284
00285
00286 clk = clock();
00287 if ( p->pAig ) Abc_NtkDelete( p->pAig );
00288 p->pAig = Res_WndStrash( p->pWin );
00289 p->timeAig += clock() - clk;
00290
00291 if ( p->pPars->fVeryVerbose )
00292 {
00293 printf( "AIG = %4d ", Abc_NtkNodeNum(p->pAig) );
00294 printf( "\n" );
00295 }
00296
00297
00298 clk = clock();
00299 RetValue = Res_SimPrepare( p->pSim, p->pAig, Vec_PtrSize(p->pWin->vLeaves), 0 );
00300 p->timeSim += clock() - clk;
00301 if ( !RetValue )
00302 {
00303 p->nSimEmpty++;
00304 continue;
00305 }
00306
00307
00308 if ( p->pSim->fConst0 || p->pSim->fConst1 )
00309 {
00310 p->nConstsUsed++;
00311
00312 pFunc = p->pSim->fConst1? Hop_ManConst1(pNtk->pManFunc) : Hop_ManConst0(pNtk->pManFunc);
00313 vFanins = Vec_VecEntry( p->vResubsW, 0 );
00314 Vec_PtrClear( vFanins );
00315 Res_UpdateNetwork( pObj, vFanins, pFunc, p->vLevels );
00316 continue;
00317 }
00318
00319
00320
00321
00322 clk = clock();
00323 if ( p->pPars->fArea )
00324 RetValue = Res_FilterCandidates( p->pWin, p->pAig, p->pSim, p->vResubs, p->vResubsW, nFaninsMax, 1 );
00325 else
00326 RetValue = Res_FilterCandidates( p->pWin, p->pAig, p->pSim, p->vResubs, p->vResubsW, nFaninsMax, 0 );
00327 p->timeCand += clock() - clk;
00328 p->nCandSets += RetValue;
00329 if ( RetValue == 0 )
00330 continue;
00331
00332
00333
00334 p->nWinsUsed++;
00335
00336
00337 Vec_VecForEachLevel( p->vResubs, vFanins, k )
00338 {
00339 if ( Vec_PtrSize(vFanins) == 0 )
00340 break;
00341
00342
00343 clk = clock();
00344 if ( p->pCnf ) Sto_ManFree( p->pCnf );
00345 p->pCnf = Res_SatProveUnsat( p->pAig, vFanins );
00346 if ( p->pCnf == NULL )
00347 {
00348 p->timeSatSat += clock() - clk;
00349
00350
00351 continue;
00352 }
00353 p->timeSatUnsat += clock() - clk;
00354
00355
00356 p->nProvedSets++;
00357
00358
00359
00360
00361
00362
00363
00364
00365 clk = clock();
00366 nFanins = Int_ManInterpolate( p->pMan, p->pCnf, 0, &puTruth );
00367 p->timeInt += clock() - clk;
00368 if ( nFanins != Vec_PtrSize(vFanins) - 2 )
00369 continue;
00370 assert( puTruth );
00371
00372
00373
00374 pGraph = Kit_TruthToGraph( puTruth, nFanins, p->vMem );
00375
00376
00377 pFunc = Kit_GraphToHop( pNtk->pManFunc, pGraph );
00378 Kit_GraphFree( pGraph );
00379
00380
00381 clk = clock();
00382 Res_UpdateNetwork( pObj, Vec_VecEntry(p->vResubsW, k), pFunc, p->vLevels );
00383 p->timeUpd += clock() - clk;
00384 break;
00385 }
00386
00387 }
00388 Extra_ProgressBarStop( pProgress );
00389 Abc_NtkStopReverseLevels( pNtk );
00390
00391 p->timeSatSim += p->pSim->timeSat;
00392 p->timeSatTotal = p->timeSatSat + p->timeSatUnsat + p->timeSatSim;
00393
00394 p->nTotalNets2 = Abc_NtkGetTotalFanins(pNtk);
00395 p->nTotalNodes2 = Abc_NtkNodeNum(pNtk);
00396
00397
00398 p->timeTotal = clock() - clkTotal;
00399 Res_ManFree( p );
00400
00401 s_ResynTime += clock() - clkTotal;
00402
00403 if ( !Abc_NtkCheck( pNtk ) )
00404 {
00405 fprintf( stdout, "Abc_NtkResynthesize(): Network check has failed.\n" );
00406 return 0;
00407 }
00408 return 1;
00409 }
00410
00414
00415