00001
00019 #include "mapperInt.h"
00020
00024
00025 static void Map_TableCreate( Map_Man_t * p );
00026 static void Map_TableResize( Map_Man_t * p );
00027 static Map_Node_t * Map_TableLookup( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 );
00028
00029
00030 static inline unsigned Map_HashKey2( Map_Node_t * p0, Map_Node_t * p1, int TableSize ) { return ((unsigned)(p0) + (unsigned)(p1) * 12582917) % TableSize; }
00031
00035
00047 int Map_ManReadInputNum( Map_Man_t * p ) { return p->nInputs; }
00048 int Map_ManReadOutputNum( Map_Man_t * p ) { return p->nOutputs; }
00049 Map_Node_t ** Map_ManReadInputs ( Map_Man_t * p ) { return p->pInputs; }
00050 Map_Node_t ** Map_ManReadOutputs( Map_Man_t * p ) { return p->pOutputs; }
00051 Map_Node_t * Map_ManReadConst1 ( Map_Man_t * p ) { return p->pConst1; }
00052 Map_Time_t * Map_ManReadInputArrivals( Map_Man_t * p ) { return p->pInputArrivals;}
00053 Mio_Library_t * Map_ManReadGenLib ( Map_Man_t * p ) { return p->pSuperLib->pGenlib; }
00054 bool Map_ManReadVerbose( Map_Man_t * p ) { return p->fVerbose; }
00055 float Map_ManReadAreaFinal( Map_Man_t * p ) { return p->AreaFinal; }
00056 float Map_ManReadRequiredGlo( Map_Man_t * p ) { return p->fRequiredGlo; }
00057 void Map_ManSetTimeToMap( Map_Man_t * p, int Time ) { p->timeToMap = Time; }
00058 void Map_ManSetTimeToNet( Map_Man_t * p, int Time ) { p->timeToNet = Time; }
00059 void Map_ManSetTimeSweep( Map_Man_t * p, int Time ) { p->timeSweep = Time; }
00060 void Map_ManSetTimeTotal( Map_Man_t * p, int Time ) { p->timeTotal = Time; }
00061 void Map_ManSetOutputNames( Map_Man_t * p, char ** ppNames ) { p->ppOutputNames = ppNames; }
00062 void Map_ManSetAreaRecovery( Map_Man_t * p, int fAreaRecovery ) { p->fAreaRecovery = fAreaRecovery;}
00063 void Map_ManSetDelayTarget( Map_Man_t * p, float DelayTarget ) { p->DelayTarget = DelayTarget;}
00064 void Map_ManSetInputArrivals( Map_Man_t * p, Map_Time_t * pArrivals ) { p->pInputArrivals = pArrivals;}
00065 void Map_ManSetObeyFanoutLimits( Map_Man_t * p, bool fObeyFanoutLimits ) { p->fObeyFanoutLimits = fObeyFanoutLimits; }
00066 void Map_ManSetNumIterations( Map_Man_t * p, int nIterations ) { p->nIterations = nIterations; }
00067 int Map_ManReadFanoutViolations( Map_Man_t * p ) { return p->nFanoutViolations; }
00068 void Map_ManSetFanoutViolations( Map_Man_t * p, int nVio ) { p->nFanoutViolations = nVio; }
00069 void Map_ManSetChoiceNodeNum( Map_Man_t * p, int nChoiceNodes ) { p->nChoiceNodes = nChoiceNodes; }
00070 void Map_ManSetChoiceNum( Map_Man_t * p, int nChoices ) { p->nChoices = nChoices; }
00071 void Map_ManSetVerbose( Map_Man_t * p, int fVerbose ) { p->fVerbose = fVerbose; }
00072 void Map_ManSetSwitching( Map_Man_t * p, int fSwitching ) { p->fSwitching = fSwitching; }
00073
00085 Map_Man_t * Map_NodeReadMan( Map_Node_t * p ) { return p->p; }
00086 char * Map_NodeReadData( Map_Node_t * p, int fPhase ) { return fPhase? p->pData1 : p->pData0; }
00087 int Map_NodeReadNum( Map_Node_t * p ) { return p->Num; }
00088 int Map_NodeReadLevel( Map_Node_t * p ) { return Map_Regular(p)->Level; }
00089 Map_Cut_t * Map_NodeReadCuts( Map_Node_t * p ) { return p->pCuts; }
00090 Map_Cut_t * Map_NodeReadCutBest( Map_Node_t * p, int fPhase ) { return p->pCutBest[fPhase]; }
00091 Map_Node_t * Map_NodeReadOne( Map_Node_t * p ) { return p->p1; }
00092 Map_Node_t * Map_NodeReadTwo( Map_Node_t * p ) { return p->p2; }
00093 void Map_NodeSetData( Map_Node_t * p, int fPhase, char * pData ) { if (fPhase) p->pData1 = pData; else p->pData0 = pData; }
00094 void Map_NodeSetNextE( Map_Node_t * p, Map_Node_t * pNextE ) { p->pNextE = pNextE; }
00095 void Map_NodeSetRepr( Map_Node_t * p, Map_Node_t * pRepr ) { p->pRepr = pRepr; }
00096 void Map_NodeSetSwitching( Map_Node_t * p, float Switching ) { p->Switching = Switching; }
00097
00109 int Map_NodeIsConst( Map_Node_t * p ) { return (Map_Regular(p))->Num == -1; }
00110 int Map_NodeIsVar( Map_Node_t * p ) { return (Map_Regular(p))->p1 == NULL && (Map_Regular(p))->Num >= 0; }
00111 int Map_NodeIsAnd( Map_Node_t * p ) { return (Map_Regular(p))->p1 != NULL; }
00112 int Map_NodeComparePhase( Map_Node_t * p1, Map_Node_t * p2 ) { assert( !Map_IsComplement(p1) ); assert( !Map_IsComplement(p2) ); return p1->fInv ^ p2->fInv; }
00113
00125 Map_Super_t * Map_CutReadSuperBest( Map_Cut_t * p, int fPhase ) { return p->M[fPhase].pSuperBest;}
00126 Map_Super_t * Map_CutReadSuper0( Map_Cut_t * p ) { return p->M[0].pSuperBest;}
00127 Map_Super_t * Map_CutReadSuper1( Map_Cut_t * p ) { return p->M[1].pSuperBest;}
00128 int Map_CutReadLeavesNum( Map_Cut_t * p ) { return p->nLeaves; }
00129 Map_Node_t ** Map_CutReadLeaves( Map_Cut_t * p ) { return p->ppLeaves; }
00130 unsigned Map_CutReadPhaseBest( Map_Cut_t * p, int fPhase ) { return p->M[fPhase].uPhaseBest;}
00131 unsigned Map_CutReadPhase0( Map_Cut_t * p ) { return p->M[0].uPhaseBest;}
00132 unsigned Map_CutReadPhase1( Map_Cut_t * p ) { return p->M[1].uPhaseBest;}
00133 Map_Cut_t * Map_CutReadNext( Map_Cut_t * p ) { return p->pNext; }
00134
00146 char * Map_SuperReadFormula( Map_Super_t * p ) { return p->pFormula; }
00147 Mio_Gate_t * Map_SuperReadRoot( Map_Super_t * p ) { return p->pRoot; }
00148 int Map_SuperReadNum( Map_Super_t * p ) { return p->Num; }
00149 Map_Super_t ** Map_SuperReadFanins( Map_Super_t * p ) { return p->pFanins; }
00150 int Map_SuperReadFaninNum( Map_Super_t * p ) { return p->nFanins; }
00151 Map_Super_t * Map_SuperReadNext( Map_Super_t * p ) { return p->pNext; }
00152 int Map_SuperReadNumPhases( Map_Super_t * p ) { return p->nPhases; }
00153 unsigned char * Map_SuperReadPhases( Map_Super_t * p ) { return p->uPhases; }
00154 int Map_SuperReadFanoutLimit( Map_Super_t * p ) { return p->nFanLimit;}
00155
00156 Mio_Library_t * Map_SuperLibReadGenLib( Map_SuperLib_t * p ) { return p->pGenlib; }
00157 float Map_SuperLibReadAreaInv( Map_SuperLib_t * p ) { return p->AreaInv; }
00158 Map_Time_t Map_SuperLibReadDelayInv( Map_SuperLib_t * p ) { return p->tDelayInv;}
00159 int Map_SuperLibReadVarsMax( Map_SuperLib_t * p ) { return p->nVarsMax; }
00160
00161
00180 Map_Man_t * Map_ManCreate( int nInputs, int nOutputs, int fVerbose )
00181 {
00182 Map_Man_t * p;
00183 int i;
00184
00185
00186 if ( Abc_FrameReadLibSuper() == NULL )
00187 {
00188 printf( "The supergate library is not specified. Use \"read_library\" or \"read_super\".\n" );
00189 return NULL;
00190 }
00191
00192
00193 p = ALLOC( Map_Man_t, 1 );
00194 memset( p, 0, sizeof(Map_Man_t) );
00195 p->pSuperLib = Abc_FrameReadLibSuper();
00196 p->nVarsMax = p->pSuperLib->nVarsMax;
00197 p->fVerbose = fVerbose;
00198 p->fEpsilon = (float)0.001;
00199 assert( p->nVarsMax > 0 );
00200
00201 if ( p->nVarsMax == 5 )
00202 Extra_Truth4VarN( &p->uCanons, &p->uPhases, &p->pCounters, 8 );
00203
00204
00205 Map_TableCreate( p );
00206 Map_MappingSetupTruthTables( p->uTruths );
00207 Map_MappingSetupTruthTablesLarge( p->uTruthsLarge );
00208
00209 p->mmNodes = Extra_MmFixedStart( sizeof(Map_Node_t) );
00210 p->mmCuts = Extra_MmFixedStart( sizeof(Map_Cut_t) );
00211
00212
00213 p->nNodes = -1;
00214
00215 p->pConst1 = Map_NodeCreate( p, NULL, NULL );
00216 p->vNodesAll = Map_NodeVecAlloc( 100 );
00217 p->vNodesTemp = Map_NodeVecAlloc( 100 );
00218 p->vMapping = Map_NodeVecAlloc( 100 );
00219 p->vVisited = Map_NodeVecAlloc( 100 );
00220
00221
00222 p->nInputs = nInputs;
00223 p->pInputs = ALLOC( Map_Node_t *, nInputs );
00224 for ( i = 0; i < nInputs; i++ )
00225 p->pInputs[i] = Map_NodeCreate( p, NULL, NULL );
00226
00227
00228 p->nOutputs = nOutputs;
00229 p->pOutputs = ALLOC( Map_Node_t *, nOutputs );
00230 memset( p->pOutputs, 0, sizeof(Map_Node_t *) * nOutputs );
00231 return p;
00232 }
00233
00245 void Map_ManFree( Map_Man_t * p )
00246 {
00247
00248
00249
00250
00251 if ( p->vAnds )
00252 Map_NodeVecFree( p->vAnds );
00253 if ( p->vNodesAll )
00254 Map_NodeVecFree( p->vNodesAll );
00255 if ( p->vNodesTemp )
00256 Map_NodeVecFree( p->vNodesTemp );
00257 if ( p->vMapping )
00258 Map_NodeVecFree( p->vMapping );
00259 if ( p->vVisited )
00260 Map_NodeVecFree( p->vVisited );
00261 if ( p->uCanons ) free( p->uCanons );
00262 if ( p->uPhases ) free( p->uPhases );
00263 if ( p->pCounters ) free( p->pCounters );
00264 Extra_MmFixedStop( p->mmNodes );
00265 Extra_MmFixedStop( p->mmCuts );
00266 FREE( p->pInputArrivals );
00267 FREE( p->pInputs );
00268 FREE( p->pOutputs );
00269 FREE( p->pBins );
00270 FREE( p->ppOutputNames );
00271 FREE( p );
00272 }
00273
00274
00286 void Map_ManPrintTimeStats( Map_Man_t * p )
00287 {
00288 printf( "N-canonical = %d. Matchings = %d. Phases = %d. ", p->nCanons, p->nMatches, p->nPhases );
00289 printf( "Choice nodes = %d. Choices = %d.\n", p->nChoiceNodes, p->nChoices );
00290 PRT( "ToMap", p->timeToMap );
00291 PRT( "Cuts ", p->timeCuts );
00292 PRT( "Truth", p->timeTruth );
00293 PRT( "Match", p->timeMatch );
00294 PRT( "Area ", p->timeArea );
00295 PRT( "Sweep", p->timeSweep );
00296 PRT( "ToNet", p->timeToNet );
00297 PRT( "TOTAL", p->timeTotal );
00298 if ( p->time1 ) { PRT( "time1", p->time1 ); }
00299 if ( p->time2 ) { PRT( "time2", p->time2 ); }
00300 if ( p->time3 ) { PRT( "time3", p->time3 ); }
00301 }
00302
00314 void Map_ManPrintStatsToFile( char * pName, float Area, float Delay, int Time )
00315 {
00316 FILE * pTable;
00317 pTable = fopen( "map_stats.txt", "a+" );
00318 fprintf( pTable, "%s ", pName );
00319 fprintf( pTable, "%4.2f ", Area );
00320 fprintf( pTable, "%4.2f ", Delay );
00321 fprintf( pTable, "%4.2f\n", (float)(Time)/(float)(CLOCKS_PER_SEC) );
00322 fclose( pTable );
00323 }
00324
00337 Map_Node_t * Map_NodeCreate( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 )
00338 {
00339 Map_Node_t * pNode;
00340
00341 pNode = (Map_Node_t *)Extra_MmFixedEntryFetch( p->mmNodes );
00342 memset( pNode, 0, sizeof(Map_Node_t) );
00343 pNode->tRequired[0].Rise = pNode->tRequired[0].Fall = pNode->tRequired[0].Worst = MAP_FLOAT_LARGE;
00344 pNode->tRequired[1].Rise = pNode->tRequired[1].Fall = pNode->tRequired[1].Worst = MAP_FLOAT_LARGE;
00345 pNode->p1 = p1;
00346 pNode->p2 = p2;
00347 pNode->p = p;
00348
00349 pNode->Num = p->nNodes++;
00350
00351
00352
00353 if ( pNode->Num >= 0 )
00354 Map_NodeVecPush( p->vNodesAll, pNode );
00355 else
00356 pNode->fInv = 1;
00357
00358 if ( p1 )
00359 {
00360 #ifdef MAP_ALLOCATE_FANOUT
00361
00362 Map_NodeAddFaninFanout( Map_Regular(p1), pNode );
00363 Map_NodeAddFaninFanout( Map_Regular(p2), pNode );
00364 #endif
00365 pNode->Level = 1 + MAP_MAX(Map_Regular(pNode->p1)->Level, Map_Regular(pNode->p2)->Level);
00366 pNode->fInv = Map_NodeIsSimComplement(p1) & Map_NodeIsSimComplement(p2);
00367 }
00368
00369 if ( p1 ) Map_NodeRef(p1);
00370 if ( p2 ) Map_NodeRef(p2);
00371
00372 pNode->nRefEst[0] = pNode->nRefEst[1] = -1;
00373 return pNode;
00374 }
00375
00387 void Map_TableCreate( Map_Man_t * pMan )
00388 {
00389 assert( pMan->pBins == NULL );
00390 pMan->nBins = Cudd_Prime(5000);
00391 pMan->pBins = ALLOC( Map_Node_t *, pMan->nBins );
00392 memset( pMan->pBins, 0, sizeof(Map_Node_t *) * pMan->nBins );
00393 pMan->nNodes = 0;
00394 }
00395
00410 Map_Node_t * Map_TableLookup( Map_Man_t * pMan, Map_Node_t * p1, Map_Node_t * p2 )
00411 {
00412 Map_Node_t * pEnt;
00413 unsigned Key;
00414
00415 if ( p1 == p2 )
00416 return p1;
00417 if ( p1 == Map_Not(p2) )
00418 return Map_Not(pMan->pConst1);
00419 if ( Map_NodeIsConst(p1) )
00420 {
00421 if ( p1 == pMan->pConst1 )
00422 return p2;
00423 return Map_Not(pMan->pConst1);
00424 }
00425 if ( Map_NodeIsConst(p2) )
00426 {
00427 if ( p2 == pMan->pConst1 )
00428 return p1;
00429 return Map_Not(pMan->pConst1);
00430 }
00431
00432 if ( Map_Regular(p1)->Num > Map_Regular(p2)->Num )
00433 pEnt = p1, p1 = p2, p2 = pEnt;
00434
00435 Key = Map_HashKey2( p1, p2, pMan->nBins );
00436 for ( pEnt = pMan->pBins[Key]; pEnt; pEnt = pEnt->pNext )
00437 if ( pEnt->p1 == p1 && pEnt->p2 == p2 )
00438 return pEnt;
00439
00440 if ( pMan->nNodes >= 2 * pMan->nBins )
00441 {
00442 Map_TableResize( pMan );
00443 Key = Map_HashKey2( p1, p2, pMan->nBins );
00444 }
00445
00446 pEnt = Map_NodeCreate( pMan, p1, p2 );
00447
00448 pEnt->pNext = pMan->pBins[Key];
00449 pMan->pBins[Key] = pEnt;
00450 return pEnt;
00451 }
00452
00453
00465 void Map_TableResize( Map_Man_t * pMan )
00466 {
00467 Map_Node_t ** pBinsNew;
00468 Map_Node_t * pEnt, * pEnt2;
00469 int nBinsNew, Counter, i, clk;
00470 unsigned Key;
00471
00472 clk = clock();
00473
00474 nBinsNew = Cudd_Prime(2 * pMan->nBins);
00475
00476 pBinsNew = ALLOC( Map_Node_t *, nBinsNew );
00477 memset( pBinsNew, 0, sizeof(Map_Node_t *) * nBinsNew );
00478
00479 Counter = 0;
00480 for ( i = 0; i < pMan->nBins; i++ )
00481 for ( pEnt = pMan->pBins[i], pEnt2 = pEnt? pEnt->pNext: NULL; pEnt;
00482 pEnt = pEnt2, pEnt2 = pEnt? pEnt->pNext: NULL )
00483 {
00484 Key = Map_HashKey2( pEnt->p1, pEnt->p2, nBinsNew );
00485 pEnt->pNext = pBinsNew[Key];
00486 pBinsNew[Key] = pEnt;
00487 Counter++;
00488 }
00489 assert( Counter == pMan->nNodes - pMan->nInputs );
00490 if ( pMan->fVerbose )
00491 {
00492
00493
00494 }
00495
00496 free( pMan->pBins );
00497 pMan->pBins = pBinsNew;
00498 pMan->nBins = nBinsNew;
00499 }
00500
00501
00502
00514 Map_Node_t * Map_NodeAnd( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 )
00515 {
00516 Map_Node_t * pNode;
00517 pNode = Map_TableLookup( p, p1, p2 );
00518 return pNode;
00519 }
00520
00532 Map_Node_t * Map_NodeOr( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 )
00533 {
00534 Map_Node_t * pNode;
00535 pNode = Map_Not( Map_TableLookup( p, Map_Not(p1), Map_Not(p2) ) );
00536 return pNode;
00537 }
00538
00550 Map_Node_t * Map_NodeExor( Map_Man_t * p, Map_Node_t * p1, Map_Node_t * p2 )
00551 {
00552 return Map_NodeMux( p, p1, Map_Not(p2), p2 );
00553 }
00554
00566 Map_Node_t * Map_NodeMux( Map_Man_t * p, Map_Node_t * pC, Map_Node_t * pT, Map_Node_t * pE )
00567 {
00568 Map_Node_t * pAnd1, * pAnd2, * pRes;
00569 pAnd1 = Map_TableLookup( p, pC, pT );
00570 pAnd2 = Map_TableLookup( p, Map_Not(pC), pE );
00571 pRes = Map_NodeOr( p, pAnd1, pAnd2 );
00572 return pRes;
00573 }
00574
00575
00588 void Map_NodeSetChoice( Map_Man_t * pMan, Map_Node_t * pNodeOld, Map_Node_t * pNodeNew )
00589 {
00590 pNodeNew->pNextE = pNodeOld->pNextE;
00591 pNodeOld->pNextE = pNodeNew;
00592 pNodeNew->pRepr = pNodeOld;
00593 }
00594
00595
00596
00600