30#ifdef ABC_USE_PTHREADS
33#include "../lib/pthread.h"
65 int fVerboseSolver = 0;
68 char FileNameIn[100], FileNameOut[100];
69 sprintf(FileNameIn,
"%02d.cnf", Id);
70 sprintf(FileNameOut,
"%02d.txt", Id);
72 char *pKissat =
"kissat.exe";
74 char *pKissat =
"kissat";
76 char Command[1000], *pCommand = (
char *)&Command;
78 sprintf(pCommand,
"%s --seed=%d --time=%d %s %s > %s", pKissat, Rand, TimeOut, fVerboseSolver ?
"" :
"-q", FileNameIn, FileNameOut);
80 sprintf(pCommand,
"%s --seed=%d %s %s > %s", pKissat, Rand, fVerboseSolver ?
"" :
"-q", FileNameIn, FileNameOut);
82 FILE * pFile = fopen(FileNameIn,
"rb");
83 if ( pFile != NULL ) {
88 if (
system(pCommand) == -1) {
90 fprintf(stdout,
"Command \"%s\" did not succeed.\n", pCommand);
96 double SolvingTime = ((double)(Abc_Clock() - clkTotal))/((
double)CLOCKS_PER_SEC);
98 printf(
"Problem %2d has a solution. ", Id);
99 else if (vRes == NULL && (TimeOut == 0 || SolvingTime < (
double)TimeOut))
100 printf(
"Problem %2d has no solution. ", Id);
101 else if (vRes == NULL)
102 printf(
"Problem %2d has no solution or timed out after %d sec. ", Id, TimeOut);
103 Abc_PrintTime(1,
"Solving time", Abc_Clock() - clkTotal );
106 printf(
"Problem %2d has a solution. ", Id);
107 Abc_PrintTime(1,
"Solving time", Abc_Clock() - clkTotal );
108 printf(
"(Currently waiting for %d sec for other threads to finish.)\n", TimeOut);
115 for ( i = 0; i < nProcs; i++ )
132#ifndef ABC_USE_PTHREADS
141#define PAR_THR_MAX 100
142typedef struct Cnf_ThData_t_
152void *Cnf_WorkerThread(
void *pArg)
154 Cnf_ThData_t *pThData = (Cnf_ThData_t *)pArg;
155 volatile int *pPlace = &pThData->fWorking;
160 assert(pThData->fWorking);
161 if (pThData->Index == -1)
167 pThData->vRes =
Cnf_RunSolverOnce(pThData->Index, pThData->Rand, pThData->nTimeOut, pThData->fVerbose);
168 pThData->fWorking = 0;
181 printf(
"Running concurrent solving with %d processes.\n", nProcs);
189 for (i = 0; i < nProcs; i++)
191 ThData[i].vRes = NULL;
192 ThData[i].Index = -1;
194 ThData[i].nTimeOut = TimeOut;
195 ThData[i].fWorking = 0;
196 ThData[i].fVerbose = fVerbose;
197 status = pthread_create(WorkerThread + i, NULL, Cnf_WorkerThread, (
void *)(ThData + i));
201 for (k = 0; k < nProcs;)
203 for (i = 0; i < nProcs; i++)
205 if (ThData[i].fWorking)
212 ThData[i].Index = k++;
213 ThData[i].fWorking = 1;
218 for (i = 0; i < nProcs; i++)
219 if (ThData[i].fWorking)
222 for (i = 0; i < nProcs; i++)
224 assert(!ThData[i].fWorking);
225 if (ThData[i].vRes && vRes == NULL)
227 vRes = ThData[i].vRes;
228 ThData[i].vRes = NULL;
230 Vec_IntFreeP(&ThData[i].vRes);
232 ThData[i].Index = -1;
233 ThData[i].fWorking = 1;
258 if (pObj->
nRefs++ || Aig_ObjIsCi(pObj) || Aig_ObjIsConst1(pObj))
260 assert(Aig_ObjIsAnd(pObj));
263 Vec_PtrPush(vMapped, pObj);
269 aArea = Vec_PtrSize(vSuper) + 1;
278 pCutBest = Dar_ObjBestCut(pObj);
279 aArea = Cnf_CutSopCost(
p, pCutBest);
307 vMapped = Vec_PtrAlloc(1000);
332 if (pObj->
nRefs++ || Aig_ObjIsCi(pObj) || Aig_ObjIsConst1(pObj))
334 assert(Aig_ObjIsAnd(pObj));
337 if (vMapped && fPreorder)
338 Vec_PtrPush(vMapped, pObj);
344 aArea = Vec_PtrSize(vSuper) + 1;
356 aArea = pCutBest->
Cost;
361 if (vMapped && !fPreorder)
362 Vec_PtrPush(vMapped, pObj);
387 vMapped = Vec_PtrAlloc(1000);
412 vCiIds = Vec_IntAlloc(Aig_ManCiNum(
p));
414 Vec_IntPush(vCiIds, pCnf->
pVarNums[pObj->
Id]);
434 vCoIds = Vec_IntAlloc(Aig_ManCoNum(
p));
436 Vec_IntPush(vCoIds, pCnf->
pVarNums[pObj->
Id]);
453 int i, c, iClaBeg, iClaEnd, *pLit;
454 unsigned *pPols0 =
ABC_CALLOC(
unsigned, Aig_ManObjNumMax(
p->pMan));
455 unsigned *pPols1 =
ABC_CALLOC(
unsigned, Aig_ManObjNumMax(
p->pMan));
456 unsigned char *pPres =
ABC_CALLOC(
unsigned char,
p->nClauses);
457 for (i = 0; i < Aig_ManObjNumMax(
p->pMan); i++)
459 if (
p->pObj2Count[i] == 0)
461 iClaBeg =
p->pObj2Clause[i];
462 iClaEnd =
p->pObj2Clause[i] +
p->pObj2Count[i];
464 for (c = iClaBeg; c < iClaEnd; c++)
465 for (pLit =
p->pClauses[c] + 1; pLit < p->pClauses[c + 1]; pLit++)
466 if (Abc_LitIsCompl(
p->pClauses[c][0]))
467 pPols0[Abc_Lit2Var(*pLit)] |= (unsigned)(2 - Abc_LitIsCompl(*pLit));
469 pPols1[Abc_Lit2Var(*pLit)] |= (unsigned)(2 - Abc_LitIsCompl(*pLit));
471 for (c = iClaBeg; c < iClaEnd; c++)
472 for (pLit =
p->pClauses[c] + 1; pLit < p->pClauses[c + 1]; pLit++)
473 if (Abc_LitIsCompl(
p->pClauses[c][0]))
474 pPres[c] = (
unsigned char)((
unsigned)pPres[c] | (pPols0[Abc_Lit2Var(*pLit)] << (2 * (pLit -
p->pClauses[c] - 1))));
476 pPres[c] = (
unsigned char)((
unsigned)pPres[c] | (pPols1[Abc_Lit2Var(*pLit)] << (2 * (pLit -
p->pClauses[c] - 1))));
478 for (c = iClaBeg; c < iClaEnd; c++)
479 for (pLit =
p->pClauses[c] + 1; pLit < p->pClauses[c + 1]; pLit++)
480 pPols0[Abc_Lit2Var(*pLit)] = pPols1[Abc_Lit2Var(*pLit)] = 0;
513 int MaxLine = 1000000;
514 int Var, Lit, nVars = -1, nClas = -1, i, Entry, iLine = 0;
518 char *pBuffer, *pToken;
519 FILE *pFile = fopen(pFileName,
"rb");
522 printf(
"Cannot open file \"%s\" for writing.\n", pFileName);
526 while (fgets(pBuffer, MaxLine, pFile) != NULL)
529 if (pBuffer[0] ==
'c')
531 if (pBuffer[0] ==
'p')
533 pToken =
strtok(pBuffer + 1,
" \t");
534 if (
strcmp(pToken,
"cnf"))
536 printf(
"Incorrect input file.\n");
539 pToken =
strtok(NULL,
" \t");
540 nVars = atoi(pToken);
541 pToken =
strtok(NULL,
" \t");
542 nClas = atoi(pToken);
543 if (nVars <= 0 || nClas <= 0)
545 printf(
"Incorrect parameters.\n");
549 vClas = Vec_IntAlloc(nClas + 1);
550 vLits = Vec_IntAlloc(nClas * 8);
553 pToken =
strtok(pBuffer,
" \t\r\n");
556 Vec_IntPush(vClas, Vec_IntSize(vLits));
562 Lit = (
Var > 0) ? Abc_Var2Lit(
Var - 1, 0) : Abc_Var2Lit(-
Var - 1, 1);
563 if (Lit >= 2 * nVars)
565 printf(
"Literal %d is out-of-bound for %d variables.\n", Lit, nVars);
568 Vec_IntPush(vLits, Lit);
569 pToken =
strtok(NULL,
" \t\r\n");
573 printf(
"There is no zero-terminator in line %d.\n", iLine);
578 if (Vec_IntSize(vClas) != nClas)
579 printf(
"Warning! The number of clauses (%d) is different from declaration (%d).\n", Vec_IntSize(vClas), nClas);
580 Vec_IntPush(vClas, Vec_IntSize(vLits));
584 pCnf->
nClauses = Vec_IntSize(vClas) - 1;
587 pCnf->
pClauses[0] = Vec_IntReleaseArray(vLits);
592 Vec_IntFreeP(&vClas);
593 Vec_IntFreeP(&vLits);
609int Cnf_DataSolveFromFile(
char *pFileName,
int nConfLimit,
int nLearnedStart,
int nLearnedDelta,
int nLearnedPerce,
int fVerbose,
int fShowPattern,
int **ppModel,
int nPis)
614 int i, status, RetValue = -1;
619 printf(
"CNF stats: Vars = %6d. Clauses = %7d. Literals = %8d. ", pCnf->
nVars, pCnf->
nClauses, pCnf->
nLiterals);
620 Abc_PrintTime(1,
"Time", Abc_Clock() - clk);
626 printf(
"The problem is trivially UNSAT.\n");
642 status =
sat_solver_solve(pSat, NULL, NULL, (ABC_INT64_T)nConfLimit, 0, (ABC_INT64_T)0, (ABC_INT64_T)0);
645 else if (status ==
l_True)
654 Abc_Print(1,
"UNDECIDED ");
655 else if (RetValue == 0)
656 Abc_Print(1,
"SATISFIABLE ");
658 Abc_Print(1,
"UNSATISFIABLE ");
660 Abc_PrintTime(1,
"Time", Abc_Clock() - clk);
662 if (RetValue == 0 && nPis > 0)
665 for (i = 0; i < nPis; i++)
666 (*ppModel)[i] = sat_solver_var_value(pSat, pCnf->
nVars - nPis + i);
668 if (RetValue == 0 && fShowPattern)
670 for (i = 0; i < pCnf->
nVars; i++)
671 printf(
"%d", sat_solver_var_value(pSat, i));
693 int i, *pLit, NumMax = -1, iVarMax = -1;
694 for (i = 0; i <
p->nClauses; i++)
695 for (pLit =
p->pClauses[i]; pLit < p->pClauses[i + 1]; pLit++)
696 pNums[Abc_Lit2Var(*pLit)]++;
697 for (i = 0; i <
p->nVars; i++)
698 if ((!pSkip || !pSkip[i]) && NumMax < pNums[i])
699 NumMax = pNums[i], iVarMax = i;
707 for (i = 0; i < 100; i++)
711 sprintf(FileName,
"cnf/%03d.cnf", i);
713 printf(
"Dumped file \"%s\".\n", FileName);
736 assert(iVarBeg < iVarEnd && nLits < iVarEnd - iVarBeg);
737 while (Vec_IntSize(vLits) < nLits)
739 int iVar = iVarBeg + (
Abc_Random(0) ^ Rand) % (iVarEnd - iVarBeg);
740 assert(iVar >= iVarBeg && iVar < iVarEnd);
741 if (Vec_IntFind(vLits, Abc_Var2Lit(iVar, 0)) == -1 && Vec_IntFind(vLits, Abc_Var2Lit(iVar, 1)) == -1)
744 Vec_IntPush(vLits, Abc_Var2Lit(iVar, 1));
746 Vec_IntPush(vLits, Abc_Var2Lit(iVar, 0));
748 Vec_IntPush(vLits, Abc_Var2Lit(iVar,
Abc_Random(0) & 1));
751 Vec_IntSort(vLits, 0);
757void Cnf_SplitCnfFile(
char * pFileName,
int nParts,
int iVarBeg,
int iVarEnd,
int nLits,
int Value,
int Rand,
int fPrepro,
int fVerbose)
762 for (k = 0; k < nParts; k++)
766 char FileName[100];
sprintf(FileName,
"%02d.cnf", k);
769 sprintf(Command,
"satelite --verbosity=0 -pre temp.cnf %s", FileName);
774 if (
system(Command) == -1) {
776 fprintf(stdout,
"Command \"%s\" did not succeed. Preprocessing skipped.\n", Command);
790 char FileName[100];
int k;
791 for (k = 0; k < nParts; k++) {
792 sprintf(FileName,
"%02d.cnf", k);
796void Cnf_SplitSat(
char *pFileName,
int iVarBeg,
int iVarEnd,
int nLits,
int Value,
int TimeOut,
int nProcs,
int nIters,
int Seed,
int fPrepro,
int fVerbose)
804 for ( i = 0; i < Seed; i++ )
807 for (i = 0; i < nIters && !vSol; i++)
810 Cnf_SplitCnfFile(pFileName, nProcs, iVarBeg, iVarEnd, nLits, Value, Rand, fPrepro, fVerbose);
814 printf(
"Finished iteration %d. ", i);
815 Abc_PrintTime(0,
"Time", Abc_Clock() - clk2);
818 printf(
"%solution is found. ", vSol ?
"S" :
"No s");
819 Abc_PrintTime(0,
"Total time", Abc_Clock() - clk);
#define ABC_ALLOC(type, num)
#define ABC_INFINITY
MACRO DEFINITIONS ///.
unsigned Abc_Random(int fReset)
#define ABC_CALLOC(type, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define Aig_ManForEachObj(p, pObj, i)
#define Aig_ManForEachCi(p, pObj, i)
ITERATORS ///.
int Aig_ObjCollectSuper(Aig_Obj_t *pObj, Vec_Ptr_t *vSuper)
struct Aig_Obj_t_ Aig_Obj_t
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
#define Aig_ManForEachCo(p, pObj, i)
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
#define PAR_THR_MAX
DECLARATIONS ///.
Vec_Int_t * Cnf_DataCollectCoSatNums(Cnf_Dat_t *pCnf, Aig_Man_t *p)
void Cnf_SplitSat(char *pFileName, int iVarBeg, int iVarEnd, int nLits, int Value, int TimeOut, int nProcs, int nIters, int Seed, int fPrepro, int fVerbose)
Cnf_Dat_t * Cnf_DataReadFromFile(char *pFileName)
Vec_Int_t * Cnf_RunSolverOnce(int Id, int Rand, int TimeOut, int fVerbose)
FUNCTION DEFINITIONS ///.
int Cnf_DataSolveFromFile(char *pFileName, int nConfLimit, int nLearnedStart, int nLearnedDelta, int nLearnedPerce, int fVerbose, int fShowPattern, int **ppModel, int nPis)
unsigned char * Cnf_DataDeriveLitPolarities(Cnf_Dat_t *p)
Vec_Int_t * Cnf_RunSolverArray(int nProcs, int TimeOut, int fVerbose)
int Cnf_DataBestVar(Cnf_Dat_t *p, int *pSkip)
ABC_NAMESPACE_IMPL_START Vec_Int_t * Exa4_ManParse(char *pFileName)
DECLARATIONS ///.
void Cnf_SplitCnfCleanup(int nParts)
int Aig_ManScanMapping_rec(Cnf_Man_t *p, Aig_Obj_t *pObj, Vec_Ptr_t *vMapped)
void Cnf_SplitCnfFile(char *pFileName, int nParts, int iVarBeg, int iVarEnd, int nLits, int Value, int Rand, int fPrepro, int fVerbose)
Vec_Ptr_t * Cnf_ManScanMapping(Cnf_Man_t *p, int fCollect, int fPreorder)
int Cnf_ManScanMapping_rec(Cnf_Man_t *p, Aig_Obj_t *pObj, Vec_Ptr_t *vMapped, int fPreorder)
Vec_Int_t * Cnf_GenRandLits(int iVarBeg, int iVarEnd, int nLits, int Value, int Rand, int fVerbose)
Vec_Int_t * Cnf_DataCollectCiSatNums(Cnf_Dat_t *pCnf, Aig_Man_t *p)
Vec_Int_t * Cnf_RunSolver(int nProcs, int TimeOut, int fVerbose)
Vec_Ptr_t * Aig_ManScanMapping(Cnf_Man_t *p, int fCollect)
Cnf_Dat_t * Cnf_DataDupCofArray(Cnf_Dat_t *p, Vec_Int_t *vLits)
typedefABC_NAMESPACE_HEADER_START struct Cnf_Man_t_ Cnf_Man_t
INCLUDES ///.
Cnf_Dat_t * Cnf_DataDupCof(Cnf_Dat_t *p, int Lit)
struct Cnf_Dat_t_ Cnf_Dat_t
void Cnf_DataFree(Cnf_Dat_t *p)
struct Cnf_Cut_t_ Cnf_Cut_t
#define Cnf_CutForEachLeaf(p, pCut, pLeaf, i)
void Cnf_DataWriteIntoFile(Cnf_Dat_t *p, char *pFileName, int fReadable, Vec_Int_t *vForAlls, Vec_Int_t *vExists)
struct Dar_Cut_t_ Dar_Cut_t
#define Dar_CutForEachLeaf(p, pCut, pLeaf, i)
void * Cnf_DataWriteIntoSolver(Cnf_Dat_t *p, int nFrames, int fInit)
void sat_solver_delete(sat_solver *s)
void Sat_SolverPrintStats(FILE *pFile, sat_solver *p)
#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 ///.