29#ifdef ABC_USE_PTHREADS
32#include "../lib/pthread.h"
47#ifndef ABC_USE_PTHREADS
49int Cec_GiaSplitTest(
Gia_Man_t *
p,
int nProcs,
int nTimeOut,
int nIterMax,
int LookAhead,
int fVerbose,
int fVeryVerbose,
int fSilent ) {
return -1; }
74 DdNode * bBdd, * bBdd0, * bBdd1;
78 vNodes = Vec_PtrStart( Gia_ManObjNum(
p) );
79 dd = Cudd_Init( Gia_ManPiNum(
p), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
81 bBdd = Cudd_ReadLogicZero(dd); Cudd_Ref( bBdd );
82 Vec_PtrWriteEntry( vNodes, 0, bBdd );
85 bBdd = i > nSkip ? Cudd_bddIthVar(dd, i) : Cudd_ReadLogicZero(dd); Cudd_Ref( bBdd );
86 Vec_PtrWriteEntry( vNodes, Gia_ObjId(
p, pObj), bBdd );
90 bBdd0 = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vNodes, Gia_ObjFaninId0(pObj, i)), Gia_ObjFaninC0(pObj) );
91 bBdd1 = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vNodes, Gia_ObjFaninId1(pObj, i)), Gia_ObjFaninC1(pObj) );
92 bBdd = Cudd_bddAnd( dd, bBdd0, bBdd1 ); Cudd_Ref( bBdd );
93 Vec_PtrWriteEntry( vNodes, Gia_ObjId(
p, pObj), bBdd );
102 bBdd = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vNodes, Gia_ObjFaninId0(pObj, Gia_ObjId(
p, pObj))), Gia_ObjFaninC0(pObj) ); Cudd_Ref( bBdd );
103 Vec_PtrWriteEntry( vNodes, Gia_ObjId(
p, pObj), bBdd );
105 if ( bBdd == Cudd_ReadLogicZero(dd) )
106 printf(
"Equivalent!\n" );
108 printf(
"Not tquivalent!\n" );
111 Abc_PrintTime( 1,
"Time", Abc_Clock() - clk );
114void Gia_ManDerefBdd( DdManager * dd,
Vec_Ptr_t * vNodes )
120 Cudd_RecursiveDeref( dd, bBdd );
121 if ( Cudd_CheckZeroRef(dd) > 0 )
122 printf(
"The number of referenced nodes = %d\n", Cudd_CheckZeroRef(dd) );
123 Cudd_PrintInfo( dd, stdout );
129 DdManager * dd = Gia_ManBuildBdd(
p, &vNodes, 50 );
130 Gia_ManDerefBdd( dd, vNodes );
157 if ( Gia_ObjRefNum(
p, Gia_ObjFanin0(pObj)) > 1 &&
158 Gia_ObjRefNum(
p, Gia_ObjFanin1(pObj)) > 1 )
160 printf(
"%5d : ", Counter++ );
161 printf(
"%2d %2d ", Gia_ObjRefNum(
p, Gia_Regular(pFan0)), Gia_ObjRefNum(
p, Gia_Regular(pFan1)) );
162 printf(
"%2d %2d \n", Gia_ObjRefNum(
p, Gia_ObjFanin0(pObj)), Gia_ObjRefNum(
p, Gia_ObjFanin1(pObj)) );
183 vPerm = Vec_IntAlloc( Gia_ManPiNum(
p) );
185 Vec_IntPush( vPerm, Gia_ObjRefNum(
p, pObj) );
187 Vec_IntFree( vPerm );
194 int * pOrder = Gia_PermuteSpecialOrder(
p );
195 vPerm = Vec_IntAllocArray( pOrder, Gia_ManPiNum(
p) );
197 Vec_IntFree( vPerm );
212int Gia_SplitCofVar2(
Gia_Man_t *
p,
int * pnFanouts,
int * pnCost )
215 int i, iBest = -1, CostBest = -1;
216 if (
p->pRefs == NULL )
219 if ( CostBest < Gia_ObjRefNum(
p, pObj) )
220 iBest = i, CostBest = Gia_ObjRefNum(
p, pObj);
222 *pnFanouts = Gia_ObjRefNum(
p, Gia_ManPi(
p, iBest));
226int Gia_SplitCofVar(
Gia_Man_t *
p,
int LookAhead,
int * pnFanouts,
int * pnCost )
230 int * pOrder, i, iBest = -1;
231 if ( LookAhead == 1 )
232 return Gia_SplitCofVar2(
p, pnFanouts, pnCost );
233 pOrder = Gia_PermuteSpecialOrder(
p );
234 LookAhead = Abc_MinInt( LookAhead, Gia_ManPiNum(
p) );
235 for ( i = 0; i < LookAhead; i++ )
238 Cost0 = Gia_ManAndNum(pPart);
242 Cost1 = Gia_ManAndNum(pPart);
245 if ( CostBest > Cost0 + Cost1 )
246 CostBest = Cost0 + Cost1, iBest = pOrder[i];
262 *pnFanouts = Gia_ObjRefNum(
p, Gia_ManPi(
p, iBest));
282 int i, iLit, * pModel;
285 pModel[i] = sat_solver_var_value(pSat, pCnf->
pVarNums[Gia_ObjId(
p, pObj)]);
288 pModel[Abc_Lit2Var(iLit)] = !Abc_LitIsCompl(iLit);
320 for ( i = 0; i < pCnf->
nClauses; i++ )
327 sat_solver_set_runtime_limit( pSat, nTimeOut ? nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
330static inline int Cnf_GiaSolveOne(
Gia_Man_t *
p,
Cnf_Dat_t * pCnf,
int nTimeOut,
int * pnVars,
int * pnConfs )
333 sat_solver * pSat = Cec_GiaDeriveSolver(
p, pCnf, nTimeOut );
340 status =
sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
344 p->pCexComb = Cec_SplitDeriveModel(
p, pCnf, pSat );
352static inline void Cec_GiaSplitClean(
Vec_Ptr_t * vStack )
358 Vec_PtrFree( vStack );
372void Cec_GiaSplitPrint(
int nIter,
int Depth,
int nVars,
int nConfs,
int fStatus,
double Prog,
abctime clk )
374 printf(
"%4d : ", nIter );
375 printf(
"Depth =%3d ", Depth );
376 printf(
"SatVar =%7d ", nVars );
377 printf(
"SatConf =%7d ", nConfs );
378 printf(
"%s ", fStatus ? (fStatus == 1 ?
"UNSAT " :
"UNDECIDED") :
"SAT " );
379 printf(
"Solved %8.4f %% ", 100*Prog );
380 Abc_PrintTime( 1,
"Time", clk );
388 if (
p->pRefs == NULL )
391 printf(
"%d ", Gia_ObjRefNum(
p, pObj) );
406int Cec_GiaSplitTest2(
Gia_Man_t *
p,
int nProcs,
int nTimeOut,
int nIterMax,
int LookAhead,
int fVerbose,
int fVeryVerbose,
int fSilent )
408 abctime clkTotal = Abc_Clock();
411 int nSatVars, nSatConfs;
412 int nIter, status, RetValue = -1;
415 pCnf = Cec_GiaDeriveGiaRemapped(
p );
416 status = Cnf_GiaSolveOne(
p, pCnf, nTimeOut, &nSatVars, &nSatConfs );
419 Cec_GiaSplitPrint( 0, 0, nSatVars, nSatConfs, status, Progress, Abc_Clock() - clkTotal );
423 printf(
"The problem is SAT without cofactoring.\n" );
429 printf(
"The problem is UNSAT without cofactoring.\n" );
434 vStack = Vec_PtrAlloc( 1000 );
437 for ( nIter = 1; Vec_PtrSize(vStack) > 0; nIter++ )
443 int nFanouts, Cost, iVar = Gia_SplitCofVar( pLast, LookAhead, &nFanouts, &Cost );
447 pLast->
vCofVars = Vec_IntAlloc( 100 );
452 printf(
"Var = %5d. Fanouts = %5d. Cost = %8d. AndBefore = %6d. AndAfter = %6d.\n",
453 iVar, nFanouts, Cost, Gia_ManAndNum(pLast), Gia_ManAndNum(pPart) );
459 Vec_IntPush( pPart->
vCofVars, Abc_Var2Lit(iVar, 1) );
461 pCnf = Cec_GiaDeriveGiaRemapped( pPart );
462 status = Cnf_GiaSolveOne( pPart, pCnf, nTimeOut, &nSatVars, &nSatConfs );
465 Progress += 1.0 / pow((
double)2, (
double)Depth);
467 Cec_GiaSplitPrint( nIter, Depth, nSatVars, nSatConfs, status, Progress, Abc_Clock() - clkTotal );
479 Vec_PtrPush( vStack, pPart );
485 Vec_IntPush( pPart->
vCofVars, Abc_Var2Lit(iVar, 0) );
488 pCnf = Cec_GiaDeriveGiaRemapped( pPart );
489 status = Cnf_GiaSolveOne( pPart, pCnf, nTimeOut, &nSatVars, &nSatConfs );
492 Progress += 1.0 / pow((
double)2, (
double)Depth);
494 Cec_GiaSplitPrint( nIter, Depth, nSatVars, nSatConfs, status, Progress, Abc_Clock() - clkTotal );
505 Vec_PtrPush( vStack, pPart );
506 if ( nIterMax && nIter >= nIterMax )
509 if ( Vec_PtrSize(vStack) == 0 )
512 Cec_GiaSplitClean( vStack );
516 printf(
"Problem is SAT " );
517 else if ( RetValue == 1 )
518 printf(
"Problem is UNSAT " );
519 else if ( RetValue == -1 )
520 printf(
"Problem is UNDECIDED " );
522 printf(
"after %d case-splits. ", nIter );
523 Abc_PrintTime( 1,
"Time", Abc_Clock() - clkTotal );
540#define PAR_THR_MAX 100
541typedef struct Par_ThData_t_
552void * Cec_GiaSplitWorkerThread(
void * pArg )
554 Par_ThData_t * pThData = (Par_ThData_t *)pArg;
555 volatile int * pPlace = &pThData->fWorking;
558 while ( *pPlace == 0 );
559 assert( pThData->fWorking );
560 if ( pThData->p == NULL )
562 pthread_exit( NULL );
566 pThData->Result = Cnf_GiaSolveOne( pThData->p, pThData->pCnf, pThData->nTimeOut, &pThData->nVars, &pThData->nConfs );
567 pThData->fWorking = 0;
572int Cec_GiaSplitTestInt(
Gia_Man_t *
p,
int nProcs,
int nTimeOut,
int nIterMax,
int LookAhead,
int fVerbose,
int fVeryVerbose,
int fSilent )
574 abctime clkTotal = Abc_Clock();
580 int i, status, nSatVars, nSatConfs;
581 int nIter = 0, RetValue = -1, fWorkToDo = 1;
584 printf(
"Solving CEC problem by cofactoring with the following parameters:\n" );
586 printf(
"Processes = %d TimeOut = %d sec MaxIter = %d LookAhead = %d Verbose = %d.\n", nProcs, nTimeOut, nIterMax, LookAhead, fVerbose );
589 return Cec_GiaSplitTest2(
p, nProcs, nTimeOut, nIterMax, LookAhead, fVerbose, fVeryVerbose, fSilent );
594 pCnf = Cec_GiaDeriveGiaRemapped(
p );
595 status = Cnf_GiaSolveOne(
p, pCnf, nTimeOut, &nSatVars, &nSatConfs );
597 if ( fVerbose && status != -1 )
598 Cec_GiaSplitPrint( 0, 0, nSatVars, nSatConfs, status, Progress, Abc_Clock() - clkTotal );
602 printf(
"The problem is SAT without cofactoring.\n" );
608 printf(
"The problem is UNSAT without cofactoring.\n" );
613 vStack = Vec_PtrAlloc( 1000 );
616 for ( i = 0; i < nProcs; i++ )
619 ThData[i].pCnf = NULL;
620 ThData[i].iThread = i;
621 ThData[i].nTimeOut = nTimeOut;
622 ThData[i].fWorking = 0;
623 ThData[i].Result = -1;
624 ThData[i].nVars = -1;
625 ThData[i].nConfs = -1;
626 status = pthread_create( WorkerThread + i, NULL,Cec_GiaSplitWorkerThread, (
void *)(ThData + i) );
assert( status == 0 );
631 fWorkToDo = (int)(Vec_PtrSize(vStack) > 0);
632 for ( i = 0; i < nProcs; i++ )
635 if ( ThData[i].fWorking )
641 if ( ThData[i].
p != NULL )
646 pLast->
vCofVars = Vec_IntAlloc( 100 );
648 Cec_GiaSplitPrint( i+1, Depth, ThData[i].nVars, ThData[i].nConfs, ThData[i].Result, Progress, Abc_Clock() - clkTotal );
649 if ( ThData[i].Result == 0 )
655 if ( ThData[i].Result == -1 )
658 int nFanouts, Cost, iVar = Gia_SplitCofVar( pLast, LookAhead, &nFanouts, &Cost );
663 Vec_IntPush( pPart->
vCofVars, Abc_Var2Lit(iVar, 1) );
664 Vec_PtrPush( vStack, pPart );
669 printf(
"Var = %5d. Fanouts = %5d. Cost = %8d. AndBefore = %6d. AndAfter = %6d.\n",
670 iVar, nFanouts, Cost, Gia_ManAndNum(pLast), Gia_ManAndNum(pPart) );
677 Vec_IntPush( pPart->
vCofVars, Abc_Var2Lit(iVar, 1) );
678 Vec_PtrPush( vStack, pPart );
684 Progress += 1.0 / pow((
double)2, (
double)Depth);
686 if ( ThData[i].pCnf == NULL )
689 ThData[i].pCnf = NULL;
691 if ( Vec_PtrSize(vStack) == 0 )
695 ThData[i].p = (
Gia_Man_t*)Vec_PtrPop( vStack );
696 ThData[i].pCnf = Cec_GiaDeriveGiaRemapped( ThData[i].
p );
697 ThData[i].fWorking = 1;
699 if ( nIterMax && nIter >= nIterMax )
706 for ( i = 0; i < nProcs; i++ )
707 if ( ThData[i].fWorking )
710 for ( i = 0; i < nProcs; i++ )
712 assert( !ThData[i].fWorking );
715 if ( ThData[i].pCnf == NULL )
718 ThData[i].pCnf = NULL;
721 ThData[i].fWorking = 1;
724 Cec_GiaSplitClean( vStack );
728 printf(
"Problem is SAT " );
729 else if ( RetValue == 1 )
730 printf(
"Problem is UNSAT " );
731 else if ( RetValue == -1 )
732 printf(
"Problem is UNDECIDED " );
734 printf(
"after %d case-splits. ", nIter );
735 Abc_PrintTime( 1,
"Time", Abc_Clock() - clkTotal );
740int Cec_GiaSplitTest(
Gia_Man_t *
p,
int nProcs,
int nTimeOut,
int nIterMax,
int LookAhead,
int fVerbose,
int fVeryVerbose,
int fSilent )
745 int i, RetValue1, fOneUndef = 0, RetValue = -1;
751 printf(
"\nSolving output %d:\n", i );
752 RetValue1 = Cec_GiaSplitTestInt( pOne, nProcs, nTimeOut, nIterMax, LookAhead, fVerbose, fVeryVerbose, fSilent );
755 if ( RetValue1 == 0 && RetValue == -1 )
761 if ( RetValue1 == -1 )
764 if ( RetValue == -1 )
765 RetValue = fOneUndef ? -1 : 1;
785 Gia_Obj_t * pObj, * pFan0, * pFan1, * pCtrl;
788 vMarks = Vec_IntStart( Gia_ManObjNum(
p) );
796 pCtrl = Gia_Regular(pCtrl);
797 Vec_IntAddToEntry( vMarks, Gia_ObjId(
p, pCtrl), 1 );
799 printf(
"The AIG with %d candidate nodes (PI+AND) has %d unique MUX control drivers:\n",
800 Gia_ManCandNum(
p), Vec_IntCountPositive(vMarks) );
804 if ( !Vec_IntEntry(vMarks, i) )
808 printf(
"%6d : ", Count++ );
809 printf(
"Obj = %6d ", i );
810 printf(
"MUX refs = %5d ", Vec_IntEntry(vMarks, i) );
811 printf(
"Level = %5d ", Gia_ObjLevelId(
p, i) );
812 printf(
"Cof0 = %7d ", Gia_ManAndNum(pCof0) );
813 printf(
"Cof1 = %7d ", Gia_ManAndNum(pCof1) );
818 Vec_IntFree( vMarks );
831 printf(
"PI %5d : ", i );
832 printf(
"Refs = %5d ", Gia_ObjRefNum(
p, pObj) );
833 printf(
"Cof0 = %7d ", Gia_ManAndNum(pCof0) );
834 printf(
"Cof1 = %7d ", Gia_ManAndNum(pCof1) );
int * Abc_QuickSortCost(int *pCosts, int nSize, int fDecrease)
#define ABC_INFINITY
MACRO DEFINITIONS ///.
#define ABC_CALLOC(type, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void Aig_ManStop(Aig_Man_t *p)
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
#define PAR_THR_MAX
DECLARATIONS ///.
#define sat_solver_addclause
ABC_NAMESPACE_IMPL_START int Cec_GiaSplitTest(Gia_Man_t *p, int nProcs, int nTimeOut, int nIterMax, int LookAhead, int fVerbose, int fVeryVerbose, int fSilent)
DECLARATIONS ///.
Cnf_Dat_t * Cnf_Derive(Aig_Man_t *pAig, int nOutputs)
struct Cnf_Dat_t_ Cnf_Dat_t
void Cnf_DataFree(Cnf_Dat_t *p)
Aig_Man_t * Gia_ManToAigSimple(Gia_Man_t *p)
void Gia_ManStop(Gia_Man_t *p)
Gia_Man_t * Gia_ManDup(Gia_Man_t *p)
Gia_Obj_t * Gia_ObjRecognizeMux(Gia_Obj_t *pNode, Gia_Obj_t **ppNodeT, Gia_Obj_t **ppNodeE)
#define Gia_ManForEachPo(p, pObj, i)
#define Gia_ManForEachAnd(p, pObj, i)
Gia_Man_t * Gia_ManDupOutputGroup(Gia_Man_t *p, int iOutStart, int iOutStop)
#define Gia_ManForEachCand(p, pObj, i)
void Gia_ManStopP(Gia_Man_t **p)
Gia_Man_t * Gia_ManDupCofactorVar(Gia_Man_t *p, int iVar, int Value)
#define Gia_ManForEachPi(p, pObj, i)
struct Gia_Obj_t_ Gia_Obj_t
Gia_Man_t * Gia_ManDupPerm(Gia_Man_t *p, Vec_Int_t *vPiPerm)
Gia_Man_t * Gia_ManDupCofactorObj(Gia_Man_t *p, int iObj, int Value)
int Gia_ObjIsMuxType(Gia_Obj_t *pNode)
struct Gia_Man_t_ Gia_Man_t
int Gia_ObjRecognizeExor(Gia_Obj_t *pObj, Gia_Obj_t **ppFan0, Gia_Obj_t **ppFan1)
void Gia_ManCreateRefs(Gia_Man_t *p)
int Gia_ManLevelNum(Gia_Man_t *p)
sat_solver * sat_solver_new(void)
int sat_solver_nconflicts(sat_solver *s)
void sat_solver_setnvars(sat_solver *s, int n)
int sat_solver_nvars(sat_solver *s)
void sat_solver_delete(sat_solver *s)
void Abc_CexFreeP(Abc_Cex_t **p)
Abc_Cex_t * Abc_CexCreate(int nRegs, int nPis, int *pArray, int iFrame, int iPo, int fSkipRegs)
typedefABC_NAMESPACE_HEADER_START struct Abc_Cex_t_ Abc_Cex_t
INCLUDES ///.
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.