98static const lit LIT_UNDEF = 0xffffffff;
101static inline lit toLit (
int v) {
return v + v; }
102static inline lit toLitCond(
int v,
int c) {
return v + v + (c != 0); }
103static inline lit lit_neg (
lit l) {
return l ^ 1; }
104static inline int lit_var (
lit l) {
return l >> 1; }
105static inline int lit_sign (
lit l) {
return l & 1; }
106static inline int lit_print(
lit l) {
return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
107static inline lit lit_read (
int s) {
return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
108static inline int lit_check(
lit l,
int n) {
return l >= 0 && lit_var(l) < n; }
111#define Pr_ManForEachClause( p, pCls ) for( pCls = p->pHead; pCls; pCls = pCls->pNext )
112#define Pr_ManForEachClauseRoot( p, pCls ) for( pCls = p->pHead; pCls != p->pLearnt; pCls = pCls->pNext )
113#define Pr_ManForEachClauseLearnt( p, pCls ) for( pCls = p->pLearnt; pCls; pCls = pCls->pNext )
116static char * Pr_ManMemoryFetch(
Pr_Man_t *
p,
int nBytes );
117static void Pr_ManMemoryStop(
Pr_Man_t *
p );
118static void Pr_ManResize(
Pr_Man_t *
p,
int nVarsNew );
148 Pr_ManResize(
p, nVarsAlloc? nVarsAlloc : 256 );
152 p->nChunkSize = (1<<16);
154 p->nResLitsAlloc = (1<<16);
173void Pr_ManResize(
Pr_Man_t *
p,
int nVarsNew )
176 if (
p->nVarsAlloc < nVarsNew )
178 int nVarsAllocOld =
p->nVarsAlloc;
180 if (
p->nVarsAlloc == 0 )
182 while (
p->nVarsAlloc < nVarsNew )
192 memset(
p->pAssigns + nVarsAllocOld, 0xff,
sizeof(
lit) * (
p->nVarsAlloc - nVarsAllocOld) );
193 memset(
p->pSeens + nVarsAllocOld, 0,
sizeof(
char) * (
p->nVarsAlloc - nVarsAllocOld) );
194 memset(
p->pVarTypes + nVarsAllocOld, 0,
sizeof(
char) * (
p->nVarsAlloc - nVarsAllocOld) );
195 memset(
p->pReasons + nVarsAllocOld, 0,
sizeof(
Pr_Cls_t *) * (
p->nVarsAlloc - nVarsAllocOld) );
196 memset(
p->pWatches + nVarsAllocOld, 0,
sizeof(
Pr_Cls_t *) * (
p->nVarsAlloc - nVarsAllocOld)*2 );
199 if (
p->nVars < nVarsNew )
216 printf(
"Runtime stats:\n" );
222 Pr_ManMemoryStop(
p );
246 assert( lit_check(Lit,
p->nVars) );
247 if ( pClause->
pLits[0] == Lit )
248 pClause->
pNext0 =
p->pWatches[lit_neg(Lit)];
252 pClause->
pNext1 =
p->pWatches[lit_neg(Lit)];
254 p->pWatches[lit_neg(Lit)] = pClause;
278 VarMax = lit_var( *pBeg );
279 for ( i = pBeg + 1; i < pEnd; i++ )
282 VarMax = lit_var(Lit) > VarMax ? lit_var(Lit) : VarMax;
283 for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
288 for ( i = pBeg + 1; i < pEnd; i++ )
289 assert( lit_var(*(i-1)) != lit_var(*i) );
291 Pr_ManResize(
p, VarMax+1 );
295 nSize =
sizeof(
Pr_Cls_t) +
sizeof(
lit) * (pEnd - pBeg);
296 pClause = (
Pr_Cls_t *)Pr_ManMemoryFetch(
p, nSize );
300 assert( !fClauseA || fRoot );
302 p->nClausesA += fClauseA;
303 pClause->
Id =
p->nClauses++;
304 pClause->
fA = fClauseA;
305 pClause->
fRoot = fRoot;
306 pClause->
nLits = pEnd - pBeg;
310 if (
p->pHead == NULL )
312 if (
p->pTail == NULL )
316 p->pTail->pNext = pClause;
321 if (
p->pLearnt == NULL && !pClause->
fRoot )
322 p->pLearnt = pClause;
325 if ( pClause->
nLits == 0 )
328 printf(
"More than one empty clause!\n" );
345char * Pr_ManMemoryFetch(
Pr_Man_t *
p,
int nBytes )
348 if (
p->pChunkLast == NULL || nBytes >
p->nChunkSize -
p->nChunkUsed )
350 pMem = (
char *)
ABC_ALLOC(
char,
p->nChunkSize );
351 *(
char **)pMem =
p->pChunkLast;
352 p->pChunkLast = pMem;
353 p->nChunkUsed =
sizeof(
char *);
355 pMem =
p->pChunkLast +
p->nChunkUsed;
356 p->nChunkUsed += nBytes;
373 char * pMem, * pNext;
374 if (
p->pChunkLast == NULL )
376 for ( pMem =
p->pChunkLast; pNext = *(
char **)pMem; pMem = pNext )
395 char * pMem, * pNext;
396 if (
p->pChunkLast == NULL )
398 Total =
p->nChunkUsed;
399 for ( pMem =
p->pChunkLast; pNext = *(
char **)pMem; pMem = pNext )
400 Total +=
p->nChunkSize;
420 Remainder = (nBits%(
sizeof(unsigned)*8));
421 nWords = (nBits/(
sizeof(unsigned)*8)) + (Remainder>0);
423 for ( w =
nWords-1; w >= 0; w-- )
424 for ( i = ((w ==
nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
425 fprintf( pFile,
"%c",
'0' + (
int)((Sign[w] & (1<<i)) > 0) );
441 printf(
"Clause %2d : ", pClause->
Id );
461 int Var = lit_var(Lit);
462 if (
p->pAssigns[
Var] != LIT_UNDEF )
463 return p->pAssigns[
Var] == Lit;
464 p->pAssigns[
Var] = Lit;
465 p->pReasons[
Var] = pReason;
466 p->pTrail[
p->nTrailSize++] = Lit;
482static inline void Pr_ManCancelUntil(
Pr_Man_t *
p,
int Level )
486 for ( i =
p->nTrailSize - 1; i >= Level; i-- )
489 Var = lit_var( Lit );
490 p->pReasons[
Var] = NULL;
491 p->pAssigns[
Var] = LIT_UNDEF;
494 p->nTrailSize = Level;
510 Pr_Cls_t ** ppPrev, * pCur, * pTemp;
511 lit LitF = lit_neg(Lit);
514 ppPrev =
p->pWatches + Lit;
515 for ( pCur =
p->pWatches[Lit]; pCur; pCur = *ppPrev )
518 if ( pCur->
pLits[0] == LitF )
521 pCur->
pLits[1] = LitF;
529 if ( pCur->
pLits[0] ==
p->pAssigns[lit_var(pCur->
pLits[0])] )
536 for ( i = 2; i < (int)pCur->
nLits; i++ )
539 if ( lit_neg(pCur->
pLits[i]) ==
p->pAssigns[lit_var(pCur->
pLits[i])] )
543 pCur->
pLits[i] = LitF;
547 Pr_ManWatchClause(
p, pCur, pCur->
pLits[1] );
550 if ( i < (
int)pCur->
nLits )
554 if ( Pr_ManEnqueue(
p, pCur->
pLits[0], pCur) )
582 for ( i = Start; i <
p->nTrailSize; i++ )
584 pClause = Pr_ManPropagateOne(
p,
p->pTrail[i] );
587p->timeBcp += Abc_Clock() - clk;
591p->timeBcp += Abc_Clock() - clk;
610 printf(
"Clause ID = %d. Proof = %d. {", pClause->
Id, (
int)pClause->
pProof );
611 for ( i = 0; i < (int)pClause->
nLits; i++ )
612 printf(
" %d", pClause->
pLits[i] );
630 printf(
"Resolvent: {" );
631 for ( i = 0; i < nResLits; i++ )
632 printf(
" %d", pResLits[i] );
649 pClause->
pProof = (
void *)++
p->Counter;
651 if (
p->fProofWrite )
654 fprintf( (FILE *)
p->pManProof,
"%d", (
int)pClause->
pProof );
655 for ( v = 0; v < (int)pClause->
nLits; v++ )
656 fprintf( (FILE *)
p->pManProof,
" %d", lit_print(pClause->
pLits[v]) );
657 fprintf( (FILE *)
p->pManProof,
" 0 0\n" );
675 int i, v,
Var, PrevId;
680 if (
p->fProofVerif )
684 p->nResLits = pConflict->
nLits;
688 for ( v = 0; v < (int)pConflict->
nLits; v++ )
689 p->pSeens[lit_var(pConflict->
pLits[v])] = 1;
699 PrevId = (int)pConflict->
pProof;
700 for ( i =
p->nTrailSize - 1; i >= 0; i-- )
703 Var = lit_var(
p->pTrail[i]);
704 if ( !
p->pSeens[
Var] )
709 pReason =
p->pReasons[
Var];
710 if ( pReason == NULL )
715 for ( v = 1; v < (int)pReason->
nLits; v++ )
716 p->pSeens[lit_var(pReason->
pLits[v])] = 1;
722 if (
p->fProofWrite )
723 fprintf( (FILE *)
p->pManProof,
"%d * %d %d 0\n",
p->Counter, PrevId, (
int)pReason->
pProof );
728 if (
p->pVarTypes[
Var] == 1 )
735 if (
p->fProofVerif )
741 for ( v1 = 0; v1 <
p->nResLits; v1++ )
742 if ( lit_var(
p->pResLits[v1]) ==
Var )
744 if ( v1 ==
p->nResLits )
745 printf(
"Recording clause %d: Cannot find variable %d in the temporary resolvent.\n", pFinal->
Id,
Var );
746 if (
p->pResLits[v1] != lit_neg(pReason->
pLits[0]) )
747 printf(
"Recording clause %d: The resolved variable %d is in the wrong polarity.\n", pFinal->
Id,
Var );
751 for ( ; v1 <
p->nResLits; v1++ )
752 p->pResLits[v1] =
p->pResLits[v1+1];
754 for ( v2 = 1; v2 < (int)pReason->
nLits; v2++ )
756 for ( v1 = 0; v1 <
p->nResLits; v1++ )
757 if ( lit_var(
p->pResLits[v1]) == lit_var(pReason->
pLits[v2]) )
760 if ( v1 ==
p->nResLits )
762 if (
p->nResLits ==
p->nResLitsAlloc )
763 printf(
"Recording clause %d: Ran out of space for intermediate resolvent.\n, pFinal->Id" );
764 p->pResLits[
p->nResLits++ ] = pReason->
pLits[v2];
768 if (
p->pResLits[v1] == pReason->
pLits[v2] )
771 printf(
"Recording clause %d: Trying to resolve the clause with more than one opposite literal.\n", pFinal->
Id );
786 if (
p->fProofVerif )
791 for ( v1 = 0; v1 <
p->nResLits; v1++ )
793 for ( v2 = 0; v2 < (int)pFinal->
nLits; v2++ )
794 if ( pFinal->
pLits[v2] ==
p->pResLits[v1] )
796 if ( v2 < (
int)pFinal->
nLits )
800 if ( v1 < p->nResLits )
802 printf(
"Recording clause %d: The final resolvent is wrong.\n", pFinal->
Id );
808p->timeTrace += Abc_Clock() - clk;
836 if ( pClause->
nLits == 0 )
837 printf(
"Error: Empty clause is attempted.\n" );
841 assert(
p->nTrailSize ==
p->nRootSize );
842 for ( i = 0; i < (int)pClause->
nLits; i++ )
843 if ( !Pr_ManEnqueue(
p, lit_neg(pClause->
pLits[i]), NULL ) )
851 if ( pConflict == NULL )
861 Pr_ManCancelUntil(
p,
p->nRootSize );
864 if ( pClause->
nLits > 1 )
866 Pr_ManWatchClause(
p, pClause, pClause->
pLits[0] );
867 Pr_ManWatchClause(
p, pClause, pClause->
pLits[1] );
873 if ( !Pr_ManEnqueue(
p, pClause->
pLits[0], pClause ) )
885 printf(
"Found last conflict after adding unit clause number %d!\n", pClause->
Id );
890 p->nRootSize =
p->nTrailSize;
914 assert( (
int)pClause->
fA == (Counter < (
int)
p->nClausesA) );
915 assert( (
int)pClause->
fRoot == (Counter < (
int)
p->nRoots) );
918 assert(
p->nClauses == Counter );
921 assert(
p->pTail->nLits == 0 );
928 if ( pClause->
nLits > 1 )
930 Pr_ManWatchClause(
p, pClause, pClause->
pLits[0] );
931 Pr_ManWatchClause(
p, pClause, pClause->
pLits[1] );
934 if ( pClause->
nLits != 1 )
938 if ( !Pr_ManEnqueue(
p, pClause->
pLits[0], pClause ) )
941 printf(
"Pr_ManProcessRoots(): Detected a root-level conflict\n" );
952 printf(
"Pr_ManProcessRoots(): Detected a root-level conflict\n" );
958 p->nRootSize =
p->nTrailSize;
975 unsigned uTruths[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
984 for ( v = 0; v < (int)pClause->
nLits; v++ )
985 p->pVarTypes[lit_var(pClause->
pLits[v])] = 1;
994 for ( v = 0; v < (int)pClause->
nLits; v++ )
997 if (
p->pVarTypes[
Var] == 1 )
1001 p->pVarTypes[
Var] = -1;
1008 for ( v = 0; v <
p->nVars; v++ )
1009 if (
p->pVarTypes[v] == -1 )
1010 p->pVarTypes[v] -=
p->nVarsAB++;
1011printf(
"There are %d global variables.\n",
p->nVarsAB );
1024 for ( v = 0; v < (int)pClause->
nLits; v++ )
1027 if (
p->pVarTypes[
Var] < 0 )
1029 if ( lit_sign(pClause->
pLits[v]) )
1030 pClause->
uTruth |= ~uTruths[ -
p->pVarTypes[
Var]-1 ];
1032 pClause->
uTruth |= uTruths[ -
p->pVarTypes[
Var]-1 ];
1064 if (
p->fProofWrite )
1065 p->pManProof = fopen(
"proof.cnf_",
"w" );
1084 printf(
"Interpolant: " );
1089 if (
p->fProofWrite )
1091 fclose( (FILE *)
p->pManProof );
1092 p->pManProof = NULL;
1111 char * pCur, * pBuffer = NULL;
1112 int * pArray = NULL;
1114 int RetValue, Counter, nNumbers, Temp;
1115 int nClauses, nClausesA, nRoots, nVars;
1118 pFile = fopen( pFileName,
"r" );
1119 if ( pFile == NULL )
1121 printf(
"Count not open input file \"%s\".\n", pFileName );
1126 pBuffer = (
char *)
ABC_ALLOC(
char, (1<<16) );
1127 for ( Counter = 0; fgets( pBuffer, (1<<16), pFile ); )
1129 if ( pBuffer[0] ==
'c' )
1131 if ( pBuffer[0] ==
'p' )
1135 RetValue = sscanf( pBuffer + 1,
"%d %d %d %d", &nVars, &nClauses, &nRoots, &nClausesA );
1136 if ( RetValue != 3 && RetValue != 4 )
1138 printf(
"Wrong input file format.\n" );
1141 pArray = (
int *)
ABC_ALLOC(
char,
sizeof(
int) * (nVars + 10) );
1145 for ( pCur = pBuffer; *pCur; pCur++ )
1146 if ( !(*pCur ==
' ' || *pCur ==
'\t' || *pCur ==
'\r' || *pCur ==
'\n') )
1156 for ( ; *pCur && *pCur ==
' '; pCur++ );
1159 sscanf( pCur,
"%d", &Temp );
1162 pArray[ nNumbers++ ] = lit_read( Temp );
1164 for ( ; *pCur && *pCur !=
' '; pCur++ );
1167 if ( !
Pr_ManAddClause(
p, (
unsigned *)pArray, (
unsigned *)pArray + nNumbers, Counter < nRoots, Counter < nClausesA ) )
1169 printf(
"Bad clause number %d.\n", Counter );
1176 if ( Counter != nClauses )
1177 printf(
"Expected %d clauses but read %d.\n", nClauses, Counter );
1181 if ( pBuffer )
ABC_FREE( pBuffer );
1229 abctime clk, clkTotal = Abc_Clock();
1233p->timeRead = Abc_Clock() - clk;
1247 printf(
"Vars = %d. Roots = %d. Learned = %d. Resol steps = %d. Ave = %.2f. Mem = %.2f MB\n",
1248 p->nVars,
p->nRoots,
p->nClauses-
p->nRoots,
p->Counter,
1249 1.0*(
p->Counter-
p->nRoots)/(
p->nClauses-
p->nRoots),
1252p->timeTotal = Abc_Clock() - clkTotal;
#define ABC_ALLOC(type, num)
#define ABC_REALLOC(type, obj, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
int Pr_ManProofTest(char *pFileName)
Pr_Cls_t * Pr_ManPropagate(Pr_Man_t *p, int Start)
int Pr_ManProcessRoots(Pr_Man_t *p)
void Pr_ManPrintInterOne(Pr_Man_t *p, Pr_Cls_t *pClause)
void Pr_ManPrepareInter(Pr_Man_t *p)
#define Pr_ManForEachClause(p, pCls)
#define Pr_ManForEachClauseLearnt(p, pCls)
int Pr_ManProofRecordOne(Pr_Man_t *p, Pr_Cls_t *pClause)
Pr_Man_t * Pr_ManProofRead(char *pFileName)
void Pr_ManPrintResolvent(lit *pResLits, int nResLits)
void Pr_ManPrintClause(Pr_Cls_t *pClause)
int Pr_ManProofWrite(Pr_Man_t *p)
int Pr_ManProofTraceOne(Pr_Man_t *p, Pr_Cls_t *pConflict, Pr_Cls_t *pFinal)
int Pr_ManMemoryReport(Pr_Man_t *p)
Pr_Man_t * Pr_ManAlloc(int nVarsAlloc)
FUNCTION DEFINITIONS ///.
ABC_NAMESPACE_IMPL_START typedef unsigned lit
DECLARATIONS ///.
void Pr_ManProofWriteOne(Pr_Man_t *p, Pr_Cls_t *pClause)
int Pr_ManAddClause(Pr_Man_t *p, lit *pBeg, lit *pEnd, int fRoot, int fClauseA)
void Pr_ManFree(Pr_Man_t *p)
void Extra_PrintBinary_(FILE *pFile, unsigned Sign[], int nBits)
struct Pr_Cls_t_ Pr_Cls_t
#define Pr_ManForEachClauseRoot(p, pCls)
typedefABC_NAMESPACE_HEADER_START struct Pr_Man_t_ Pr_Man_t
INCLUDES ///.