abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bmcBCore.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [bmcBCore.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT-based bounded model checking.]
8 
9  Synopsis [Performs recording of BMC core.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: bmcBCore.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "bmc.h"
22 #include "sat/bsat/satSolver.h"
23 #include "sat/bsat/satStore.h"
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////
29 /// DECLARATIONS ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis [Collect pivot variables.]
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
48 {
49  int Num;
50  Vec_Int_t * vPivots = Vec_IntAlloc( 100 );
51  FILE * pFile = fopen( pName, "r" );
52  while ( fscanf( pFile, "%d", &Num ) == 1 )
53  Vec_IntPush( vPivots, Num );
54  fclose( pFile );
55  return vPivots;
56 }
57 Vec_Int_t * Bmc_ManBCoreCollectPivots( Gia_Man_t * p, char * pName, Vec_Int_t * vVarMap )
58 {
59  Gia_Obj_t * pObj;
60  int i, iVar, iFrame;
61  Vec_Int_t * vPivots = Vec_IntAlloc( 100 );
62  Vec_Int_t * vVars = Bmc_ManBCoreReadPivots( pName );
63  Gia_ManForEachObj( p, pObj, i )
64  pObj->fMark0 = 0;
65  Vec_IntForEachEntry( vVars, iVar, i )
66  if ( iVar > 0 && iVar < Gia_ManObjNum(p) )
67  Gia_ManObj( p, iVar )->fMark0 = 1;
68  else
69  printf( "Cannot find object with Id %d in the given AIG.\n", iVar );
70  Vec_IntForEachEntryDouble( vVarMap, iVar, iFrame, i )
71  if ( Gia_ManObj( p, iVar )->fMark0 )
72  Vec_IntPush( vPivots, Abc_Lit2Var(i) );
73  Gia_ManForEachObj( p, pObj, i )
74  pObj->fMark0 = 0;
75  Vec_IntFree( vVars );
76  return vPivots;
77 }
78 
79 /**Function*************************************************************
80 
81  Synopsis [Collect (Id; Frame) pairs.]
82 
83  Description []
84 
85  SideEffects []
86 
87  SeeAlso []
88 
89 ***********************************************************************/
90 static inline void Bmc_ManBCoreAssignVar( Gia_Man_t * p, Gia_Obj_t * pObj, int f, Vec_Int_t * vNodes )
91 {
92  pObj->Value = Abc_Lit2Var(Vec_IntSize(vNodes));
93  Vec_IntPush( vNodes, Gia_ObjId(p, pObj) );
94  Vec_IntPush( vNodes, f );
95 // printf( "Obj %3d Frame %3d ---> Var %3d ", Gia_ObjId(p, pObj), f, pObj->Value );
96 // Gia_ObjPrint( p, pObj );
97 }
98 void Bmc_ManBCoreCollect_rec( Gia_Man_t * p, int Id, int f, Vec_Int_t * vNodes, Vec_Int_t * vRootsNew )
99 {
100  Gia_Obj_t * pObj;
101  if ( Gia_ObjIsTravIdCurrentId(p, Id) )
102  return;
104  pObj = Gia_ManObj( p, Id );
105  Bmc_ManBCoreAssignVar( p, pObj, f, vNodes );
106  if ( Gia_ObjIsPi(p, pObj) )
107  return;
108  if ( Gia_ObjIsRo(p, pObj) )
109  {
110  Vec_IntPush( vRootsNew, Gia_ObjId(p, Gia_ObjRoToRi(p, pObj)) );
111  return;
112  }
113  assert( Gia_ObjIsAnd(pObj) );
114  Bmc_ManBCoreCollect_rec( p, Gia_ObjFaninId0p(p, pObj), f, vNodes, vRootsNew );
115  Bmc_ManBCoreCollect_rec( p, Gia_ObjFaninId1p(p, pObj), f, vNodes, vRootsNew );
116 }
117 Vec_Int_t * Bmc_ManBCoreCollect( Gia_Man_t * p, int iFrame, int iOut, sat_solver * pSat )
118 {
119  Gia_Obj_t * pObj;
120  int f, i, iObj, nNodesOld;
121  Vec_Int_t * vNodes = Vec_IntAlloc( 100 );
122  Vec_Int_t * vRoots = Vec_IntAlloc( 100 );
123  Vec_Int_t * vRoots2 = Vec_IntAlloc( 100 );
124  assert( iFrame >= 0 && iOut >= 0 );
125  // add first variables
126  Vec_IntPush( vNodes, -1 );
127  Vec_IntPush( vNodes, -1 );
128  Bmc_ManBCoreAssignVar( p, Gia_ManPo(p, iOut), iFrame, vNodes );
129  // start with root node
130  Vec_IntPush( vRoots, Gia_ObjId(p, Gia_ManPo(p, iOut)) );
131  // iterate through time frames
132  for ( f = iFrame; f >= 0; f-- )
133  {
135  // add constant node
137  Bmc_ManBCoreAssignVar( p, Gia_ManConst0(p), f, vNodes );
138  sat_solver_add_const( pSat, Gia_ManConst0(p)->Value, 1 );
139  // collect nodes in this timeframe
140  Vec_IntClear( vRoots2 );
141  nNodesOld = Vec_IntSize(vNodes);
142  Gia_ManForEachObjVec( vRoots, p, pObj, i )
143  Bmc_ManBCoreCollect_rec( p, Gia_ObjFaninId0p(p, pObj), f, vNodes, vRoots2 );
144  if ( f == iFrame )
145  {
146  // add the final clause
147  pObj = Gia_ManPo(p, iOut);
148  assert( pObj->Value == 1 );
149  assert( Gia_ObjFanin0(pObj)->Value == 3 );
150 // sat_solver_add_const( pSat, Gia_ObjFanin0(pObj)->Value, Gia_ObjFaninC0(pObj) );
151  sat_solver_add_constraint( pSat, Gia_ObjFanin0(pObj)->Value, pObj->Value, Gia_ObjFaninC0(pObj) );
152  }
153  else
154  {
155  // connect current RIs to previous ROs
156  Gia_ManForEachObjVec( vRoots, p, pObj, i )
157  sat_solver_add_buffer( pSat, pObj->Value, Gia_ObjFanin0(pObj)->Value, Gia_ObjFaninC0(pObj) );
158  }
159  Gia_ManForEachObjVec( vRoots2, p, pObj, i )
160  pObj->Value = Gia_ObjRiToRo(p, pObj)->Value;
161  // add nodes of this timeframe
162  Vec_IntForEachEntryStart( vNodes, iObj, i, nNodesOld )
163  {
164  pObj = Gia_ManObj(p, iObj); i++;
165  if ( Gia_ObjIsCi(pObj) )
166  continue;
167  assert( Gia_ObjIsAnd(pObj) );
168  sat_solver_add_and( pSat, pObj->Value, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value, Gia_ObjFaninC0(pObj), Gia_ObjFaninC1(pObj), 0 );
169  }
170  // collect constant node
171  ABC_SWAP( Vec_Int_t *, vRoots, vRoots2 );
172  }
173  // add constraint variables for the init state
174  Gia_ManForEachObjVec( vRoots, p, pObj, i )
175  {
176  sat_solver_add_constraint( pSat, pObj->Value, Abc_Lit2Var(Vec_IntSize(vNodes)), 1 );
177  pObj = Gia_ObjRiToRo(p, pObj);
178  Bmc_ManBCoreAssignVar( p, pObj, -1, vNodes );
179  }
180  Vec_IntFree( vRoots );
181  Vec_IntFree( vRoots2 );
182  return vNodes;
183 }
184 
185 /**Function*************************************************************
186 
187  Synopsis []
188 
189  Description []
190 
191  SideEffects []
192 
193  SeeAlso []
194 
195 ***********************************************************************/
197 {
198  clock_t clk = clock();
199  Intp_Man_t * pManProof;
200  Vec_Int_t * vVarMap, * vCore;
201  sat_solver * pSat;
202  FILE * pFile;
203  void * pSatCnf;
204  int RetValue;
205  // create SAT solver
206  pSat = sat_solver_new();
207  sat_solver_store_alloc( pSat );
208  sat_solver_setnvars( pSat, 1000 );
209  sat_solver_set_runtime_limit( pSat, pPars->nTimeOut ? pPars->nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
210  vVarMap = Bmc_ManBCoreCollect( p, pPars->iFrame, pPars->iOutput, pSat );
212  // create pivot variables
213  if ( pPars->pFilePivots )
214  {
215  Vec_Int_t * vPivots = Bmc_ManBCoreCollectPivots(p, pPars->pFilePivots, vVarMap);
216  sat_solver_set_pivot_variables( pSat, Vec_IntArray(vPivots), Vec_IntSize(vPivots) );
217  Vec_IntReleaseArray( vPivots );
218  Vec_IntFree( vPivots );
219  }
220  // solve the problem
221  RetValue = sat_solver_solve( pSat, NULL, NULL, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
222  if ( RetValue == l_Undef )
223  {
224  Vec_IntFree( vVarMap );
225  sat_solver_delete( pSat );
226  printf( "Timeout of conflict limit is reached.\n" );
227  return;
228  }
229  if ( RetValue == l_True )
230  {
231  Vec_IntFree( vVarMap );
232  sat_solver_delete( pSat );
233  printf( "The BMC problem is SAT.\n" );
234  return;
235  }
236  if ( pPars->fVerbose )
237  {
238  printf( "SAT solver returned UNSAT after %7d conflicts. ", (int)pSat->stats.conflicts );
239  Abc_PrintTime( 1, "Time", clock() - clk );
240  }
241  assert( RetValue == l_False );
242  pSatCnf = sat_solver_store_release( pSat );
243 // Sto_ManDumpClauses( (Sto_Man_t *)pSatCnf, "cnf_store.txt" );
244  // derive the UNSAT core
245  clk = clock();
246  pManProof = Intp_ManAlloc();
247  vCore = (Vec_Int_t *)Intp_ManUnsatCore( pManProof, (Sto_Man_t *)pSatCnf, 1, pPars->fVerbose );
248  Intp_ManFree( pManProof );
249  if ( pPars->fVerbose )
250  {
251  printf( "UNSAT core contains %d (out of %d) learned clauses. ", Vec_IntSize(vCore), sat_solver_nconflicts(pSat) );
252  Abc_PrintTime( 1, "Time", clock() - clk );
253  }
254  // write the problem
255  Vec_IntSort( vCore, 0 );
256  pFile = pPars->pFileProof ? fopen( pPars->pFileProof, "wb" ) : stdout;
257  Intp_ManUnsatCorePrintForBmc( pFile, (Sto_Man_t *)pSatCnf, vCore, vVarMap );
258  if ( pFile != stdout )
259  fclose( pFile );
260  // cleanup
261  Sto_ManFree( (Sto_Man_t *)pSatCnf );
262  Vec_IntFree( vVarMap );
263  Vec_IntFree( vCore );
264  sat_solver_delete( pSat );
265 }
266 
267 ////////////////////////////////////////////////////////////////////////
268 /// END OF FILE ///
269 ////////////////////////////////////////////////////////////////////////
270 
271 
273 
static abctime sat_solver_set_runtime_limit(sat_solver *s, abctime Limit)
Definition: satSolver.h:233
static int * Vec_IntArray(Vec_Int_t *p)
Definition: vecInt.h:328
int nTimeOut
Definition: bmc.h:103
void * Intp_ManUnsatCore(Intp_Man_t *p, Sto_Man_t *pCnf, int fLearned, int fVerbose)
Definition: satInterP.c:963
void Bmc_ManBCoreCollect_rec(Gia_Man_t *p, int Id, int f, Vec_Int_t *vNodes, Vec_Int_t *vRootsNew)
Definition: bmcBCore.c:98
static int Gia_ObjFaninC1(Gia_Obj_t *pObj)
Definition: gia.h:452
static Llb_Mgr_t * p
Definition: llb3Image.c:950
char * pFileProof
Definition: bmc.h:105
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
#define l_Undef
Definition: SolverTypes.h:86
static Gia_Obj_t * Gia_ManPo(Gia_Man_t *p, int v)
Definition: gia.h:406
int sat_solver_solve(sat_solver *s, lit *begin, lit *end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal)
Definition: satSolver.c:1700
void sat_solver_delete(sat_solver *s)
Definition: satSolver.c:1141
static void sat_solver_set_pivot_variables(sat_solver *s, int *pPivots, int nPivots)
Definition: satSolver.h:259
Vec_Int_t * Bmc_ManBCoreCollectPivots(Gia_Man_t *p, char *pName, Vec_Int_t *vVarMap)
Definition: bmcBCore.c:57
int sat_solver_nconflicts(sat_solver *s)
Definition: satSolver.c:1908
void * sat_solver_store_release(sat_solver *s)
Definition: satSolver.c:1949
static int Gia_ObjFaninId1p(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:464
#define l_True
Definition: SolverTypes.h:84
int fVerbose
Definition: bmc.h:106
static abctime Abc_Clock()
Definition: abc_global.h:279
stats_t stats
Definition: satSolver.h:156
static void Vec_IntSort(Vec_Int_t *p, int fReverse)
Definition: vecInt.h:1293
static Gia_Obj_t * Gia_ManObj(Gia_Man_t *p, int v)
Definition: gia.h:402
Definition: gia.h:75
#define ABC_SWAP(Type, a, b)
Definition: abc_global.h:218
static int sat_solver_add_buffer(sat_solver *pSat, int iVarA, int iVarB, int fCompl)
Definition: satSolver.h:288
Vec_Int_t * Bmc_ManBCoreCollect(Gia_Man_t *p, int iFrame, int iOut, sat_solver *pSat)
Definition: bmcBCore.c:117
static int sat_solver_add_constraint(sat_solver *pSat, int iVar, int iVar2, int fCompl)
Definition: satSolver.h:526
Intp_Man_t * Intp_ManAlloc()
FUNCTION DEFINITIONS ///.
Definition: satInterP.c:96
static int sat_solver_add_const(sat_solver *pSat, int iVar, int fCompl)
Definition: satSolver.h:277
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
void Intp_ManFree(Intp_Man_t *p)
Definition: satInterP.c:178
static Gia_Obj_t * Gia_ObjFanin0(Gia_Obj_t *pObj)
Definition: gia.h:454
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
int iOutput
Definition: bmc.h:102
static int Gia_ObjIsRo(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:443
void sat_solver_store_alloc(sat_solver *s)
Definition: satSolver.c:1916
static Gia_Obj_t * Gia_ObjRoToRi(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:446
void sat_solver_setnvars(sat_solver *s, int n)
Definition: satSolver.c:1072
static void Gia_ObjSetTravIdCurrent(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:531
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
static int Gia_ObjId(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:410
#define Vec_IntForEachEntryStart(vVec, Entry, i, Start)
Definition: vecInt.h:56
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
Definition: vecInt.h:66
int iFrame
Definition: bmc.h:101
static int Gia_ObjIsTravIdCurrentId(Gia_Man_t *p, int Id)
Definition: gia.h:536
#define Gia_ManForEachObjVec(vVec, p, pObj, i)
Definition: gia.h:988
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void sat_solver_store_mark_roots(sat_solver *s)
Definition: satSolver.c:1939
ABC_INT64_T conflicts
Definition: satVec.h:154
sat_solver * sat_solver_new(void)
Definition: satSolver.c:1001
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
void Sto_ManFree(Sto_Man_t *p)
Definition: satStore.c:143
unsigned fMark0
Definition: gia.h:79
static Gia_Obj_t * Gia_ObjRiToRo(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:447
Definition: gia.h:95
static int Gia_ObjIsAnd(Gia_Obj_t *pObj)
Definition: gia.h:422
#define Gia_ManForEachObj(p, pObj, i)
MACRO DEFINITIONS ///.
Definition: gia.h:984
static Gia_Obj_t * Gia_ManConst0(Gia_Man_t *p)
Definition: gia.h:400
static int Abc_Lit2Var(int Lit)
Definition: abc_global.h:264
#define l_False
Definition: SolverTypes.h:85
static int Gia_ObjFaninId0p(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:463
static Gia_Obj_t * Gia_ObjFanin1(Gia_Obj_t *pObj)
Definition: gia.h:455
void Bmc_ManBCorePerform(Gia_Man_t *p, Bmc_BCorePar_t *pPars)
MACRO DEFINITIONS ///.
Definition: bmcBCore.c:196
ABC_NAMESPACE_IMPL_START Vec_Int_t * Bmc_ManBCoreReadPivots(char *pName)
DECLARATIONS ///.
Definition: bmcBCore.c:47
#define assert(ex)
Definition: util_old.h:213
unsigned Value
Definition: gia.h:87
void Gia_ManIncrementTravId(Gia_Man_t *p)
Definition: giaUtil.c:149
static void Bmc_ManBCoreAssignVar(Gia_Man_t *p, Gia_Obj_t *pObj, int f, Vec_Int_t *vNodes)
Definition: bmcBCore.c:90
static int Gia_ObjFaninC0(Gia_Obj_t *pObj)
Definition: gia.h:451
void Intp_ManUnsatCorePrintForBmc(FILE *pFile, Sto_Man_t *pCnf, void *vCore0, void *vVarMap0)
Definition: satInterP.c:1059
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
static int Gia_ObjIsCi(Gia_Obj_t *pObj)
Definition: gia.h:420
static void Gia_ObjSetTravIdCurrentId(Gia_Man_t *p, int Id)
Definition: gia.h:535
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecInt.h:54
char * pFilePivots
Definition: bmc.h:104
static int * Vec_IntReleaseArray(Vec_Int_t *p)
Definition: extraZddTrunc.c:89
static int Gia_ManObjNum(Gia_Man_t *p)
Definition: gia.h:388
static int Gia_ObjIsPi(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:441
static int sat_solver_add_and(sat_solver *pSat, int iVar, int iVar0, int iVar1, int fCompl0, int fCompl1, int fCompl)
Definition: satSolver.h:324