00001
00021 #include "cutInt.h"
00022
00026
00027 struct Cut_OracleStruct_t_
00028 {
00029
00030 Cut_Params_t * pParams;
00031 Vec_Int_t * vFanCounts;
00032 int fSimul;
00033
00034 Vec_Ptr_t * vCutsNew;
00035 Vec_Ptr_t * vCuts0;
00036 Vec_Ptr_t * vCuts1;
00037
00038 Vec_Int_t * vNodeCuts;
00039 Vec_Int_t * vNodeStarts;
00040 Vec_Int_t * vCutPairs;
00041
00042 Extra_MmFixed_t * pMmCuts;
00043 int EntrySize;
00044 int nTruthWords;
00045
00046 int timeTotal;
00047 int nCuts;
00048 int nCutsTriv;
00049 };
00050
00051 static Cut_Cut_t * Cut_CutStart( Cut_Oracle_t * p );
00052 static Cut_Cut_t * Cut_CutTriv( Cut_Oracle_t * p, int Node );
00053 static Cut_Cut_t * Cut_CutMerge( Cut_Oracle_t * p, Cut_Cut_t * pCut0, Cut_Cut_t * pCut1 );
00054
00058
00070 Cut_Oracle_t * Cut_OracleStart( Cut_Man_t * pMan )
00071 {
00072 Cut_Oracle_t * p;
00073 int clk = clock();
00074
00075 assert( pMan->pParams->nVarsMax >= 3 && pMan->pParams->nVarsMax <= CUT_SIZE_MAX );
00076 assert( pMan->pParams->fRecord );
00077
00078 p = ALLOC( Cut_Oracle_t, 1 );
00079 memset( p, 0, sizeof(Cut_Oracle_t) );
00080
00081
00082 p->pParams = pMan->pParams;
00083
00084
00085 p->vNodeCuts = pMan->vNodeCuts; pMan->vNodeCuts = NULL;
00086 p->vNodeStarts = pMan->vNodeStarts; pMan->vNodeStarts = NULL;
00087 p->vCutPairs = pMan->vCutPairs; pMan->vCutPairs = NULL;
00088
00089
00090 p->vCutsNew = Vec_PtrAlloc( p->pParams->nIdsMax );
00091 Vec_PtrFill( p->vCutsNew, p->pParams->nIdsMax, NULL );
00092 p->vCuts0 = Vec_PtrAlloc( 100 );
00093 p->vCuts1 = Vec_PtrAlloc( 100 );
00094
00095
00096 p->EntrySize = sizeof(Cut_Cut_t) + p->pParams->nVarsMax * sizeof(int);
00097 if ( p->pParams->fTruth )
00098 {
00099 if ( p->pParams->nVarsMax > 8 )
00100 {
00101 p->pParams->fTruth = 0;
00102 printf( "Skipping computation of truth table for more than 8 inputs.\n" );
00103 }
00104 else
00105 {
00106 p->nTruthWords = Cut_TruthWords( p->pParams->nVarsMax );
00107 p->EntrySize += p->nTruthWords * sizeof(unsigned);
00108 }
00109 }
00110
00111 p->pMmCuts = Extra_MmFixedStart( p->EntrySize );
00112 return p;
00113 }
00125 void Cut_OracleStop( Cut_Oracle_t * p )
00126 {
00127 Cut_Cut_t * pCut;
00128 int i;
00129
00130
00131 {
00132 printf( "Cut computation statistics with oracle:\n" );
00133 printf( "Current cuts = %8d. (Trivial = %d.)\n", p->nCuts-p->nCutsTriv, p->nCutsTriv );
00134 PRT( "Total time ", p->timeTotal );
00135 }
00136
00137 Vec_PtrForEachEntry( p->vCutsNew, pCut, i )
00138 if ( pCut != NULL )
00139 {
00140 int k = 0;
00141 }
00142 if ( p->vCuts0 ) Vec_PtrFree( p->vCuts0 );
00143 if ( p->vCuts1 ) Vec_PtrFree( p->vCuts1 );
00144 if ( p->vCutsNew ) Vec_PtrFree( p->vCutsNew );
00145 if ( p->vFanCounts ) Vec_IntFree( p->vFanCounts );
00146
00147 if ( p->vNodeCuts ) Vec_IntFree( p->vNodeCuts );
00148 if ( p->vNodeStarts ) Vec_IntFree( p->vNodeStarts );
00149 if ( p->vCutPairs ) Vec_IntFree( p->vCutPairs );
00150
00151 Extra_MmFixedStop( p->pMmCuts );
00152 free( p );
00153 }
00154
00166 void Cut_OracleSetFanoutCounts( Cut_Oracle_t * p, Vec_Int_t * vFanCounts )
00167 {
00168 p->vFanCounts = vFanCounts;
00169 }
00170
00182 int Cut_OracleReadDrop( Cut_Oracle_t * p )
00183 {
00184 return p->pParams->fDrop;
00185 }
00186
00198 void Cut_OracleNodeSetTriv( Cut_Oracle_t * p, int Node )
00199 {
00200 assert( Vec_PtrEntry( p->vCutsNew, Node ) == NULL );
00201 Vec_PtrWriteEntry( p->vCutsNew, Node, Cut_CutTriv(p, Node) );
00202 }
00203
00204
00205
00217 Cut_Cut_t * Cut_CutStart( Cut_Oracle_t * p )
00218 {
00219 Cut_Cut_t * pCut;
00220
00221 pCut = (Cut_Cut_t *)Extra_MmFixedEntryFetch( p->pMmCuts );
00222 memset( pCut, 0, sizeof(Cut_Cut_t) );
00223 pCut->nVarsMax = p->pParams->nVarsMax;
00224 pCut->fSimul = p->fSimul;
00225 p->nCuts++;
00226 return pCut;
00227 }
00228
00240 Cut_Cut_t * Cut_CutTriv( Cut_Oracle_t * p, int Node )
00241 {
00242 Cut_Cut_t * pCut;
00243 pCut = Cut_CutStart( p );
00244 pCut->nLeaves = 1;
00245 pCut->pLeaves[0] = Node;
00246 if ( p->pParams->fTruth )
00247 {
00248 unsigned * pTruth = Cut_CutReadTruth(pCut);
00249 int i;
00250 for ( i = 0; i < p->nTruthWords; i++ )
00251 pTruth[i] = 0xAAAAAAAA;
00252 }
00253 p->nCutsTriv++;
00254 return pCut;
00255 }
00256
00268 Cut_Cut_t * Cut_CutMerge( Cut_Oracle_t * p, Cut_Cut_t * pCut0, Cut_Cut_t * pCut1 )
00269 {
00270 Cut_Cut_t * pCut;
00271 int Limit, i, k, c;
00272
00273 pCut = Cut_CutStart( p );
00274 Limit = p->pParams->nVarsMax;
00275 for ( i = k = c = 0; c < Limit; c++ )
00276 {
00277 if ( k == (int)pCut1->nLeaves )
00278 {
00279 if ( i == (int)pCut0->nLeaves )
00280 {
00281 pCut->nLeaves = c;
00282 return pCut;
00283 }
00284 pCut->pLeaves[c] = pCut0->pLeaves[i++];
00285 continue;
00286 }
00287 if ( i == (int)pCut0->nLeaves )
00288 {
00289 if ( k == (int)pCut1->nLeaves )
00290 {
00291 pCut->nLeaves = c;
00292 return pCut;
00293 }
00294 pCut->pLeaves[c] = pCut1->pLeaves[k++];
00295 continue;
00296 }
00297 if ( pCut0->pLeaves[i] < pCut1->pLeaves[k] )
00298 {
00299 pCut->pLeaves[c] = pCut0->pLeaves[i++];
00300 continue;
00301 }
00302 if ( pCut0->pLeaves[i] > pCut1->pLeaves[k] )
00303 {
00304 pCut->pLeaves[c] = pCut1->pLeaves[k++];
00305 continue;
00306 }
00307 pCut->pLeaves[c] = pCut0->pLeaves[i++];
00308 k++;
00309 }
00310 assert( i == (int)pCut0->nLeaves && k == (int)pCut1->nLeaves );
00311 pCut->nLeaves = c;
00312 return pCut;
00313 }
00314
00326 Cut_Cut_t * Cut_OracleComputeCuts( Cut_Oracle_t * p, int Node, int Node0, int Node1, int fCompl0, int fCompl1 )
00327 {
00328 Cut_Cut_t * pList = NULL, ** ppTail = &pList;
00329 Cut_Cut_t * pCut, * pCut0, * pCut1, * pList0, * pList1;
00330 int iCutStart, nCuts, i, Entry;
00331 int clk = clock();
00332
00333
00334 pList0 = Vec_PtrEntry( p->vCutsNew, Node0 );
00335 pList1 = Vec_PtrEntry( p->vCutsNew, Node1 );
00336 assert( pList0 && pList1 );
00337
00338
00339 p->fSimul = (fCompl0 ^ pList0->fSimul) & (fCompl1 ^ pList1->fSimul);
00340
00341
00342 Vec_PtrClear( p->vCuts0 );
00343 Cut_ListForEachCut( pList0, pCut )
00344 Vec_PtrPush( p->vCuts0, pCut );
00345 Vec_PtrClear( p->vCuts1 );
00346 Cut_ListForEachCut( pList1, pCut )
00347 Vec_PtrPush( p->vCuts1, pCut );
00348
00349
00350 nCuts = Vec_IntEntry(p->vNodeCuts, Node);
00351 iCutStart = Vec_IntEntry(p->vNodeStarts, Node);
00352
00353
00354 assert( Vec_IntEntry(p->vCutPairs, iCutStart) == 0 );
00355 pCut = Cut_CutTriv( p, Node );
00356 *ppTail = pCut;
00357 ppTail = &pCut->pNext;
00358
00359 for ( i = 1; i < nCuts; i++ )
00360 {
00361 Entry = Vec_IntEntry( p->vCutPairs, iCutStart + i );
00362 pCut0 = Vec_PtrEntry( p->vCuts0, Entry & 0xFFFF );
00363 pCut1 = Vec_PtrEntry( p->vCuts1, Entry >> 16 );
00364 pCut = Cut_CutMerge( p, pCut0, pCut1 );
00365 *ppTail = pCut;
00366 ppTail = &pCut->pNext;
00367
00368 if ( p->pParams->fTruth )
00369 Cut_TruthComputeOld( pCut, pCut0, pCut1, fCompl0, fCompl1 );
00370 }
00371 *ppTail = NULL;
00372
00373
00374 assert( Vec_PtrEntry( p->vCutsNew, Node ) == NULL );
00375 Vec_PtrWriteEntry( p->vCutsNew, Node, pList );
00376 p->timeTotal += clock() - clk;
00377 return pList;
00378 }
00379
00391 void Cut_OracleFreeCuts( Cut_Oracle_t * p, int Node )
00392 {
00393 Cut_Cut_t * pList, * pCut, * pCut2;
00394 pList = Vec_PtrEntry( p->vCutsNew, Node );
00395 if ( pList == NULL )
00396 return;
00397 Cut_ListForEachCutSafe( pList, pCut, pCut2 )
00398 Extra_MmFixedEntryRecycle( p->pMmCuts, (char *)pCut );
00399 Vec_PtrWriteEntry( p->vCutsNew, Node, pList );
00400 }
00401
00413 void Cut_OracleTryDroppingCuts( Cut_Oracle_t * p, int Node )
00414 {
00415 int nFanouts;
00416 assert( p->vFanCounts );
00417 nFanouts = Vec_IntEntry( p->vFanCounts, Node );
00418 assert( nFanouts > 0 );
00419 if ( --nFanouts == 0 )
00420 Cut_OracleFreeCuts( p, Node );
00421 Vec_IntWriteEntry( p->vFanCounts, Node, nFanouts );
00422 }
00423
00427
00428