abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bmcMaxi.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [bmcMaxi.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT-based bounded model checking.]
8 
9  Synopsis []
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: bmcMaxi.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "bmc.h"
22 #include "sat/cnf/cnf.h"
23 #include "sat/bsat/satStore.h"
24 #include "aig/gia/giaAig.h"
25 
27 
28 
29 ////////////////////////////////////////////////////////////////////////
30 /// DECLARATIONS ///
31 ////////////////////////////////////////////////////////////////////////
32 
33 ////////////////////////////////////////////////////////////////////////
34 /// FUNCTION DEFINITIONS ///
35 ////////////////////////////////////////////////////////////////////////
36 
37 /**Function*************************************************************
38 
39  Synopsis []
40 
41  Description []
42 
43  SideEffects []
44 
45  SeeAlso []
46 
47 ***********************************************************************/
49 {
50  Cnf_Dat_t * pCnf;
51  Aig_Man_t * pAig = Gia_ManToAigSimple( p );
52  pAig->nRegs = 0;
53  pCnf = Cnf_Derive( pAig, Aig_ManCoNum(pAig) );
54  Aig_ManStop( pAig );
55  return pCnf;
56 }
57 
58 /**Function*************************************************************
59 
60  Synopsis []
61 
62  Description []
63 
64  SideEffects []
65 
66  SeeAlso []
67 
68 ***********************************************************************/
69 Gia_Man_t * Gia_ManMaxiUnfold( Gia_Man_t * p, int nFrames, int fUseVars, Vec_Int_t * vInit )
70 {
71  Gia_Man_t * pNew, * pTemp;
72  Gia_Obj_t * pObj;
73  int i, f;
74  pNew = Gia_ManStart( fUseVars * 2 * Gia_ManRegNum(p) + nFrames * Gia_ManObjNum(p) );
75  pNew->pName = Abc_UtilStrsav( p->pName );
76  Gia_ManHashAlloc( pNew );
77  Gia_ManConst0(p)->Value = 0;
78  // control/data variables
79  Gia_ManForEachRo( p, pObj, i )
80  Gia_ManAppendCi( pNew );
81  Gia_ManForEachRo( p, pObj, i )
82  Gia_ManAppendCi( pNew );
83  // build timeframes
84  assert( !vInit || Vec_IntSize(vInit) == Gia_ManRegNum(p) );
85  Gia_ManForEachRo( p, pObj, i )
86  {
87  int Value = Vec_IntEntry( vInit, i );
88  int iCtrl = Gia_ManCiLit( pNew, i );
89  int iData = Gia_ManCiLit( pNew, Gia_ManRegNum(p)+i );
90  // decide based on Value
91  if ( Value == 0 )
92  pObj->Value = fUseVars ? Gia_ManHashAnd(pNew, iCtrl, iData) : 0;
93  else if ( Value == 1 )
94  pObj->Value = fUseVars ? Gia_ManHashOr(pNew, Abc_LitNot(iCtrl), iData) : 1;
95  else if ( Value == 2 )
96  pObj->Value = Gia_ManHashAnd(pNew, iCtrl, iData);
97  else if ( Value == 3 )
98  pObj->Value = Gia_ManHashOr(pNew, Abc_LitNot(iCtrl), iData);
99  else if ( Value == 4 )
100  pObj->Value = 0;
101  else if ( Value == 5 )
102  pObj->Value = 1;
103  else assert( 0 );
104  }
105  for ( f = 0; f < nFrames; f++ )
106  {
107  Gia_ManForEachPi( p, pObj, i )
108  pObj->Value = Gia_ManAppendCi( pNew );
109  Gia_ManForEachAnd( p, pObj, i )
110  pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
111  Gia_ManForEachRi( p, pObj, i )
112  pObj->Value = Gia_ObjFanin0Copy(pObj);
113  Gia_ManForEachRo( p, pObj, i )
114  pObj->Value = Gia_ObjRoToRi(p, pObj)->Value;
115  }
116  Gia_ManForEachRi( p, pObj, i )
117  pObj->Value = Gia_ManAppendCo( pNew, pObj->Value );
118  pNew = Gia_ManCleanup( pTemp = pNew );
119  Gia_ManStop( pTemp );
120  assert( Gia_ManPiNum(pNew) == 2 * Gia_ManRegNum(p) + nFrames * Gia_ManPiNum(p) );
121  return pNew;
122 }
123 
124 
125 /**Function*************************************************************
126 
127  Synopsis []
128 
129  Description []
130 
131  SideEffects []
132 
133  SeeAlso []
134 
135 ***********************************************************************/
136 Vec_Int_t * Gia_ManMaxiPerform( Gia_Man_t * p, Vec_Int_t * vInit, int nFrames, int nTimeOut, int fVerbose )
137 {
138  int nIterMax = 1000000;
139  int i, iLit, Iter, status;
140  int nLits, * pLits;
141  abctime clkTotal = Abc_Clock();
142  abctime clkSat = 0;
143  Vec_Int_t * vLits, * vMap;
144  sat_solver * pSat;
145  Gia_Obj_t * pObj;
146  Gia_Man_t * p0 = Gia_ManMaxiUnfold( p, nFrames, 0, vInit );
147  Gia_Man_t * p1 = Gia_ManMaxiUnfold( p, nFrames, 1, vInit );
148  Gia_Man_t * pM = Gia_ManMiter( p0, p1, 0, 0, 0, 0, 0 );
149  Cnf_Dat_t * pCnf = Cnf_DeriveGiaRemapped( pM );
150  Gia_ManStop( p0 );
151  Gia_ManStop( p1 );
152  assert( Gia_ManRegNum(p) > 0 );
153  if ( fVerbose )
154  printf( "Running with %d frames and %sgiven init state.\n", nFrames, vInit ? "":"no " );
155 
156  pSat = sat_solver_new();
157  sat_solver_setnvars( pSat, pCnf->nVars );
158  sat_solver_set_runtime_limit( pSat, nTimeOut ? nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0 );
159  for ( i = 0; i < pCnf->nClauses; i++ )
160  if ( !sat_solver_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1] ) )
161  assert( 0 );
162 
163  // add one large OR clause
164  vLits = Vec_IntAlloc( Gia_ManCoNum(p) );
165  Gia_ManForEachCo( pM, pObj, i )
166  Vec_IntPush( vLits, Abc_Var2Lit(pCnf->pVarNums[Gia_ObjId(pM, pObj)], 0) );
167  sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );
168 
169  // create assumptions
170  Vec_IntClear( vLits );
171  Gia_ManForEachPi( pM, pObj, i )
172  if ( i == Gia_ManRegNum(p) )
173  break;
174  else if ( Vec_IntEntry(vInit, i) == 0 || Vec_IntEntry(vInit, i) == 1 )
175  Vec_IntPush( vLits, Abc_Var2Lit(pCnf->pVarNums[Gia_ObjId(pM, pObj)], 1) );
176 
177  if ( fVerbose )
178  {
179  printf( "Iter%6d : ", 0 );
180  printf( "Var =%10d ", sat_solver_nvars(pSat) );
181  printf( "Clause =%10d ", sat_solver_nclauses(pSat) );
182  printf( "Conflict =%10d ", sat_solver_nconflicts(pSat) );
183  printf( "Subset =%6d ", Vec_IntSize(vLits) );
184  Abc_PrintTime( 1, "Time", clkSat );
185 // ABC_PRTr( "Solver time", clkSat );
186  }
187  for ( Iter = 0; Iter < nIterMax; Iter++ )
188  {
189  abctime clk = Abc_Clock();
190  status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits), (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0, (ABC_INT64_T)0 );
191  clkSat += Abc_Clock() - clk;
192  if ( status == l_Undef )
193  {
194 // if ( fVerbose )
195 // printf( "\n" );
196  printf( "Timeout reached after %d seconds and %d iterations. ", nTimeOut, Iter );
197  break;
198  }
199  if ( status == l_True )
200  {
201 // if ( fVerbose )
202 // printf( "\n" );
203  printf( "The problem is SAT after %d iterations. ", Iter );
204  break;
205  }
206  assert( status == l_False );
207  nLits = sat_solver_final( pSat, &pLits );
208  if ( fVerbose )
209  {
210  printf( "Iter%6d : ", Iter+1 );
211  printf( "Var =%10d ", sat_solver_nvars(pSat) );
212  printf( "Clause =%10d ", sat_solver_nclauses(pSat) );
213  printf( "Conflict =%10d ", sat_solver_nconflicts(pSat) );
214  printf( "Subset =%6d ", nLits );
215  Abc_PrintTime( 1, "Time", clkSat );
216 // ABC_PRTr( "Solver time", clkSat );
217  }
218  if ( Vec_IntSize(vLits) == nLits )
219  {
220 // if ( fVerbose )
221 // printf( "\n" );
222  printf( "Reached fixed point with %d entries after %d iterations. ", Vec_IntSize(vLits), Iter+1 );
223  break;
224  }
225  // collect used literals
226  Vec_IntClear( vLits );
227  for ( i = 0; i < nLits; i++ )
228  Vec_IntPush( vLits, Abc_LitNot(pLits[i]) );
229  }
230  // create map
231  vMap = Vec_IntStart( pCnf->nVars );
232  Vec_IntForEachEntry( vLits, iLit, i )
233  Vec_IntWriteEntry( vMap, Abc_Lit2Var(iLit), 1 );
234 
235  // create output
236  Vec_IntFree( vLits );
237  vLits = Vec_IntDup(vInit);
238  Gia_ManForEachPi( pM, pObj, i )
239  if ( i == Gia_ManRegNum(p) )
240  break;
241  else if ( Vec_IntEntry(vLits, i) == 4 || Vec_IntEntry(vLits, i) == 5 )
242  Vec_IntWriteEntry( vLits, i, Vec_IntEntry(vLits, i) );
243  else if ( (Vec_IntEntry(vLits, i) == 0 || Vec_IntEntry(vLits, i) == 1) && !Vec_IntEntry(vMap, pCnf->pVarNums[Gia_ObjId(pM, pObj)]) )
244  Vec_IntWriteEntry( vLits, i, Vec_IntEntry(vLits, i) | 2 );
245  Vec_IntFree( vMap );
246 
247  // cleanup
248  sat_solver_delete( pSat );
249  Cnf_DataFree( pCnf );
250  Gia_ManStop( pM );
251  Abc_PrintTime( 1, "Total runtime", Abc_Clock() - clkTotal );
252  return vLits;
253 }
254 
255 /**Function*************************************************************
256 
257  Synopsis []
258 
259  Description []
260 
261  SideEffects []
262 
263  SeeAlso []
264 
265 ***********************************************************************/
266 Vec_Int_t * Gia_ManMaxiTest( Gia_Man_t * p, Vec_Int_t * vInit0, int nFrames, int nWords, int nTimeOut, int fSim, int fVerbose )
267 {
268  Vec_Int_t * vRes, * vInit;
269  vInit = vInit0 ? vInit0 : Vec_IntStart( Gia_ManRegNum(p) );
270  vRes = Gia_ManMaxiPerform( p, vInit, nFrames, nTimeOut, fVerbose );
271  if ( vInit != vInit0 )
272  Vec_IntFree( vInit );
273  return vRes;
274 }
275 
276 ////////////////////////////////////////////////////////////////////////
277 /// END OF FILE ///
278 ////////////////////////////////////////////////////////////////////////
279 
280 
282 
static ABC_NAMESPACE_IMPL_START Cnf_Dat_t * Cnf_DeriveGiaRemapped(Gia_Man_t *p)
DECLARATIONS ///.
Definition: bmcMaxi.c:48
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
void Gia_ManStop(Gia_Man_t *p)
Definition: giaMan.c:77
Aig_Man_t * Gia_ManToAigSimple(Gia_Man_t *p)
Definition: giaAig.c:367
static Vec_Int_t * Vec_IntDup(Vec_Int_t *pVec)
Definition: vecInt.h:214
#define Gia_ManForEachCo(p, pObj, i)
Definition: gia.h:1022
Cnf_Dat_t * Cnf_Derive(Aig_Man_t *pAig, int nOutputs)
Definition: cnfCore.c:165
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
Definition: aig.h:50
static int Gia_ManAppendCo(Gia_Man_t *p, int iLit0)
Definition: gia.h:703
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int sat_solver_addclause(sat_solver *s, lit *begin, lit *end)
Definition: satSolver.c:1492
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
void Aig_ManStop(Aig_Man_t *p)
Definition: aigMan.c:187
#define l_Undef
Definition: SolverTypes.h:86
static int Gia_ManCiLit(Gia_Man_t *p, int CiId)
Definition: gia.h:435
int nClauses
Definition: cnf.h:61
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
static int Gia_ManAppendCi(Gia_Man_t *p)
Definition: gia.h:583
int nVars
Definition: cnf.h:59
void sat_solver_delete(sat_solver *s)
Definition: satSolver.c:1141
static int Abc_Var2Lit(int Var, int fCompl)
Definition: abc_global.h:263
int sat_solver_nconflicts(sat_solver *s)
Definition: satSolver.c:1908
Vec_Int_t * Gia_ManMaxiTest(Gia_Man_t *p, Vec_Int_t *vInit0, int nFrames, int nWords, int nTimeOut, int fSim, int fVerbose)
Definition: bmcMaxi.c:266
int * pVarNums
Definition: cnf.h:63
#define l_True
Definition: SolverTypes.h:84
static abctime Abc_Clock()
Definition: abc_global.h:279
Definition: cnf.h:56
int nWords
Definition: abcNpn.c:127
Definition: gia.h:75
Vec_Int_t * Gia_ManMaxiPerform(Gia_Man_t *p, Vec_Int_t *vInit, int nFrames, int nTimeOut, int fVerbose)
Definition: bmcMaxi.c:136
static int sat_solver_final(sat_solver *s, int **ppArray)
Definition: satSolver.h:227
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
Definition: bblif.c:285
static int Aig_ManCoNum(Aig_Man_t *p)
Definition: aig.h:252
static Vec_Int_t * Vec_IntStart(int nSize)
Definition: bblif.c:172
int ** pClauses
Definition: cnf.h:62
char * pName
Definition: gia.h:97
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static int Gia_ObjFanin1Copy(Gia_Obj_t *pObj)
Definition: gia.h:482
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 int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
int sat_solver_nvars(sat_solver *s)
Definition: satSolver.c:1896
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
Gia_Man_t * Gia_ManStart(int nObjsMax)
DECLARATIONS ///.
Definition: giaMan.c:52
#define Gia_ManForEachAnd(p, pObj, i)
Definition: gia.h:1002
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 Gia_ManForEachPi(p, pObj, i)
Definition: gia.h:1034
static int Gia_ObjFanin0Copy(Gia_Obj_t *pObj)
Definition: gia.h:481
int sat_solver_nclauses(sat_solver *s)
Definition: satSolver.c:1902
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Gia_Man_t * Gia_ManMaxiUnfold(Gia_Man_t *p, int nFrames, int fUseVars, Vec_Int_t *vInit)
Definition: bmcMaxi.c:69
static int Abc_LitNot(int Lit)
Definition: abc_global.h:266
sat_solver * sat_solver_new(void)
Definition: satSolver.c:1001
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
Definition: gia.h:95
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
void Cnf_DataFree(Cnf_Dat_t *p)
Definition: cnfMan.c:180
#define Gia_ManForEachRo(p, pObj, i)
Definition: gia.h:1038
#define assert(ex)
Definition: util_old.h:213
unsigned Value
Definition: gia.h:87
#define Gia_ManForEachRi(p, pObj, i)
Definition: gia.h:1040
void Gia_ManHashAlloc(Gia_Man_t *p)
Definition: giaHash.c:99
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
static int Gia_ManPiNum(Gia_Man_t *p)
Definition: gia.h:385
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
ABC_INT64_T abctime
Definition: abc_global.h:278
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition: giaScl.c:84
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecInt.h:54
int Gia_ManHashOr(Gia_Man_t *p, int iLit0, int iLit1)
Definition: giaHash.c:611
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
Definition: giaHash.c:572
Gia_Man_t * Gia_ManMiter(Gia_Man_t *pAig0, Gia_Man_t *pAig1, int nInsDup, int fDualOut, int fSeq, int fImplic, int fVerbose)
Definition: giaDup.c:2128
static int Gia_ManObjNum(Gia_Man_t *p)
Definition: gia.h:388
static int Gia_ManCoNum(Gia_Man_t *p)
Definition: gia.h:384
static int Gia_ManRegNum(Gia_Man_t *p)
Definition: gia.h:387