ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
bmcEco.c
Go to the documentation of this file.
1
20
21#include "bmc.h"
22#include "sat/cnf/cnf.h"
23#include "sat/bsat/satSolver.h"
24#include "aig/gia/giaAig.h"
25
27
28
32
36
48Gia_Man_t * Bmc_EcoMiter( Gia_Man_t * pGold, Gia_Man_t * pOld, Vec_Int_t * vFans )
49{
50 Gia_Man_t * pNew, * pTemp;
51 Gia_Obj_t * pRoot = Gia_ObjFanin0( Gia_ManPo(pOld, Gia_ManPoNum(pOld)-1) ); // fanin of the last PO
52 Gia_Obj_t * pObj;
53 int i, NewPi, Miter;
54 assert( Gia_ManCiNum(pGold) == Gia_ManCiNum(pOld) );
55 assert( Gia_ManCoNum(pGold) == Gia_ManCoNum(pOld) - 1 );
56 assert( Gia_ObjIsAnd(pRoot) );
57 // create the miter
58 pNew = Gia_ManStart( 3 * Gia_ManObjNum(pGold) );
59 pNew->pName = Abc_UtilStrsav( pGold->pName );
60 Gia_ManHashAlloc( pNew );
61 // copy gold
62 Gia_ManConst0(pGold)->Value = 0;
63 Gia_ManForEachCi( pGold, pObj, i )
64 pObj->Value = Gia_ManAppendCi( pNew );
65 NewPi = Gia_ManAppendCi( pNew );
66 Gia_ManForEachAnd( pGold, pObj, i )
67 pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
68 Gia_ManForEachCo( pGold, pObj, i )
69 pObj->Value = Gia_ObjFanin0Copy( pObj );
70 // create onset
71 Gia_ManConst0(pOld)->Value = 0;
72 Gia_ManForEachCi( pOld, pObj, i )
73 pObj->Value = Gia_ManCi(pGold, i)->Value;
74 Gia_ManForEachAnd( pOld, pObj, i )
75 if ( pObj == pRoot )
76 pObj->Value = NewPi;
77 else
78 pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
79 Gia_ManForEachCo( pOld, pObj, i )
80 pObj->Value = Gia_ObjFanin0Copy( pObj );
81 Gia_ManForEachCo( pGold, pObj, i )
82 Gia_ManAppendCo( pNew, Gia_ManHashXor(pNew, pObj->Value, Gia_ManCo(pOld, i)->Value) );
83 // create offset
84 Gia_ManForEachAnd( pOld, pObj, i )
85 if ( pObj == pRoot )
86 pObj->Value = Abc_LitNot(NewPi);
87 else
88 pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
89 Gia_ManForEachCo( pOld, pObj, i )
90 pObj->Value = Gia_ObjFanin0Copy( pObj );
91 Miter = 0;
92 Gia_ManForEachCo( pGold, pObj, i )
93 Miter = Gia_ManHashOr( pNew, Miter, Gia_ManHashXor(pNew, pObj->Value, Gia_ManCo(pOld, i)->Value) );
94 Gia_ManAppendCo( pNew, Miter );
95 // add outputs for the nodes
96 Gia_ManForEachObjVec( vFans, pOld, pObj, i )
97 Gia_ManAppendCo( pNew, pObj->Value );
98 // cleanup
99 pNew = Gia_ManCleanup( pTemp = pNew );
100 Gia_ManStop( pTemp );
101 assert( Gia_ManPiNum(pNew) == Gia_ManCiNum(pGold) + 1 );
102 assert( Gia_ManPoNum(pNew) == Gia_ManCoNum(pGold) + 1 + Vec_IntSize(vFans) );
103 return pNew;
104}
105
117static inline Cnf_Dat_t * Cnf_DeriveGiaRemapped( Gia_Man_t * p )
118{
119 Cnf_Dat_t * pCnf;
120 Aig_Man_t * pAig = Gia_ManToAigSimple( p );
121 pAig->nRegs = 0;
122 pCnf = Cnf_Derive( pAig, Aig_ManCoNum(pAig) );
123 Aig_ManStop( pAig );
124 return pCnf;
125}
126
138int Bmc_EcoSolve( sat_solver * pSat, int Root, Vec_Int_t * vVars )
139{
140 int nBTLimit = 1000000;
141 Vec_Int_t * vLits = Vec_IntAlloc( Vec_IntSize(vVars) );
142 int status, i, Div, iVar, nFinal, * pFinal, nIter = 0, RetValue = 0;
143 int pLits[2], nVars = sat_solver_nvars( pSat );
144 sat_solver_setnvars( pSat, nVars + 1 );
145 pLits[0] = Abc_Var2Lit( Root, 0 ); // F = 1
146 pLits[1] = Abc_Var2Lit( nVars, 0 ); // iNewLit
147 while ( 1 )
148 {
149 // find onset minterm
150 status = sat_solver_solve( pSat, pLits, pLits + 2, nBTLimit, 0, 0, 0 );
151 if ( status == l_Undef )
152 { RetValue = -1; break; }
153 if ( status == l_False )
154 { RetValue = 1; break; }
155 assert( status == l_True );
156 // collect divisor literals
157 Vec_IntClear( vLits );
158 Vec_IntPush( vLits, Abc_LitNot(pLits[0]) ); // F = 0
159 Vec_IntForEachEntry( vVars, Div, i )
160 Vec_IntPush( vLits, sat_solver_var_literal(pSat, Div) );
161 // check against offset
162 status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits), nBTLimit, 0, 0, 0 );
163 if ( status == l_Undef )
164 { RetValue = -1; break; }
165 if ( status == l_True )
166 break;
167 assert( status == l_False );
168 // compute cube and add clause
169 nFinal = sat_solver_final( pSat, &pFinal );
170 Vec_IntClear( vLits );
171 Vec_IntPush( vLits, Abc_LitNot(pLits[1]) ); // NOT(iNewLit)
172 printf( "Cube %d : ", nIter );
173 for ( i = 0; i < nFinal; i++ )
174 {
175 if ( pFinal[i] == pLits[0] )
176 continue;
177 Vec_IntPush( vLits, pFinal[i] );
178 iVar = Vec_IntFind( vVars, Abc_Lit2Var(pFinal[i]) ); assert( iVar >= 0 );
179 printf( "%s%d ", Abc_LitIsCompl(pFinal[i]) ? "+":"-", iVar );
180 }
181 printf( "\n" );
182 status = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntArray(vLits) + Vec_IntSize(vLits) );
183 assert( status );
184 nIter++;
185 }
186// assert( status == l_True );
187 Vec_IntFree( vLits );
188 return RetValue;
189}
190
202int Bmc_EcoPatch( Gia_Man_t * p, int nIns, int nOuts )
203{
204 int i, Lit, RetValue, Root;
205 Gia_Obj_t * pObj;
206 Vec_Int_t * vVars;
207 // generate CNF and solver
208 Cnf_Dat_t * pCnf = Cnf_DeriveGiaRemapped( p );
209 sat_solver * pSat = sat_solver_new();
210 sat_solver_setnvars( pSat, pCnf->nVars );
211 // add timeframe clauses
212 for ( i = 0; i < pCnf->nClauses; i++ )
213 if ( !sat_solver_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1] ) )
214 assert( 0 );
215 // add equality constraints
216 assert( Gia_ManPoNum(p) == nOuts + 1 + nIns );
217 Gia_ManForEachPo( p, pObj, i )
218 {
219 if ( i == nOuts )
220 break;
221 Lit = Abc_Var2Lit( pCnf->pVarNums[Gia_ObjId(p, pObj)], 1 ); // neg lit
222 RetValue = sat_solver_addclause( pSat, &Lit, &Lit + 1 );
223 assert( RetValue );
224 }
225 // add inequality constraint
226 pObj = Gia_ManPo( p, nOuts );
227 Lit = Abc_Var2Lit( pCnf->pVarNums[Gia_ObjId(p, pObj)], 0 ); // pos lit
228 RetValue = sat_solver_addclause( pSat, &Lit, &Lit + 1 );
229 assert( RetValue );
230 // simplify the SAT solver
231 RetValue = sat_solver_simplify( pSat );
232 assert( RetValue );
233 // collect input variables
234 vVars = Vec_IntAlloc( nIns );
235 Gia_ManForEachPo( p, pObj, i )
236 if ( i >= nOuts + 1 )
237 Vec_IntPush( vVars, pCnf->pVarNums[Gia_ObjId(p, pObj)] );
238 assert( Vec_IntSize(vVars) == nIns );
239 // derive the root variable
240 pObj = Gia_ManPi( p, Gia_ManPiNum(p) - 1 );
241 Root = pCnf->pVarNums[Gia_ObjId(p, pObj)];
242 // solve the problem
243 RetValue = Bmc_EcoSolve( pSat, Root, vVars );
244 Vec_IntFree( vVars );
245 return RetValue;
246}
247
260{
261 char * pFileGold = "eco_gold.aig";
262 char * pFileOld = "eco_old.aig";
263 Vec_Int_t * vFans;
264 FILE * pFile;
265 Gia_Man_t * pMiter;
266 Gia_Obj_t * pObj;
267 Gia_Man_t * pGold;
268 Gia_Man_t * pOld;
269 int i, RetValue;
270 // check that the files exist
271 pFile = fopen( pFileGold, "r" );
272 if ( pFile == NULL )
273 {
274 printf( "File \"%s\" does not exist.\n", pFileGold );
275 return;
276 }
277 fclose( pFile );
278 pFile = fopen( pFileOld, "r" );
279 if ( pFile == NULL )
280 {
281 printf( "File \"%s\" does not exist.\n", pFileOld );
282 return;
283 }
284 fclose( pFile );
285 // read files
286 pGold = Gia_AigerRead( pFileGold, 0, 0, 0 );
287 pOld = Gia_AigerRead( pFileOld, 0, 0, 0 );
288 // create ECO miter
289 vFans = Vec_IntAlloc( Gia_ManCiNum(pOld) );
290 Gia_ManForEachCi( pOld, pObj, i )
291 Vec_IntPush( vFans, Gia_ObjId(pOld, pObj) );
292 pMiter = Bmc_EcoMiter( pGold, pOld, vFans );
293 Vec_IntFree( vFans );
294 Gia_AigerWrite( pMiter, "eco_miter.aig", 0, 0, 0 );
295 // find the patch
296 RetValue = Bmc_EcoPatch( pMiter, Gia_ManCiNum(pGold), Gia_ManCoNum(pGold) );
297 if ( RetValue == 1 )
298 printf( "Patch is computed.\n" );
299 if ( RetValue == 0 )
300 printf( "Cannot be patched.\n" );
301 if ( RetValue == -1 )
302 printf( "Resource limit exceeded.\n" );
303 Gia_ManStop( pMiter );
304}
305
309
310
312
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void Aig_ManStop(Aig_Man_t *p)
Definition aigMan.c:187
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
Definition aig.h:50
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
#define l_True
Definition bmcBmcS.c:35
#define l_Undef
Definition bmcBmcS.c:34
#define l_False
Definition bmcBmcS.c:36
void Bmc_EcoMiterTest()
Definition bmcEco.c:259
int Bmc_EcoSolve(sat_solver *pSat, int Root, Vec_Int_t *vVars)
Definition bmcEco.c:138
ABC_NAMESPACE_IMPL_START Gia_Man_t * Bmc_EcoMiter(Gia_Man_t *pGold, Gia_Man_t *pOld, Vec_Int_t *vFans)
DECLARATIONS ///.
Definition bmcEco.c:48
int Bmc_EcoPatch(Gia_Man_t *p, int nIns, int nOuts)
Definition bmcEco.c:202
#define sat_solver_addclause
Definition cecSatG2.c:37
#define sat_solver
Definition cecSatG2.c:34
#define sat_solver_solve
Definition cecSatG2.c:45
Cnf_Dat_t * Cnf_Derive(Aig_Man_t *pAig, int nOutputs)
Definition cnfCore.c:165
struct Cnf_Dat_t_ Cnf_Dat_t
Definition cnf.h:52
Cube * p
Definition exorList.c:222
Aig_Man_t * Gia_ManToAigSimple(Gia_Man_t *p)
Definition giaAig.c:408
void Gia_ManStop(Gia_Man_t *p)
Definition giaMan.c:82
#define Gia_ManForEachPo(p, pObj, i)
Definition gia.h:1250
#define Gia_ManForEachAnd(p, pObj, i)
Definition gia.h:1214
Gia_Man_t * Gia_AigerRead(char *pFileName, int fGiaSimple, int fSkipStrash, int fCheck)
Definition giaAiger.c:1017
void Gia_ManHashAlloc(Gia_Man_t *p)
Definition giaHash.c:105
Gia_Man_t * Gia_ManStart(int nObjsMax)
FUNCTION DEFINITIONS ///.
Definition giaMan.c:57
int Gia_ManHashOr(Gia_Man_t *p, int iLit0, int iLit1)
Definition giaHash.c:621
struct Gia_Obj_t_ Gia_Obj_t
Definition gia.h:76
int Gia_ManHashXor(Gia_Man_t *p, int iLit0, int iLit1)
Definition giaHash.c:668
struct Gia_Man_t_ Gia_Man_t
Definition gia.h:96
#define Gia_ManForEachObjVec(vVec, p, pObj, i)
Definition gia.h:1194
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition giaScl.c:84
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
Definition giaHash.c:576
#define Gia_ManForEachCo(p, pObj, i)
Definition gia.h:1236
#define Gia_ManForEachCi(p, pObj, i)
Definition gia.h:1228
void Gia_AigerWrite(Gia_Man_t *p, char *pFileName, int fWriteSymbols, int fCompact, int fWriteNewLine)
Definition giaAiger.c:1595
sat_solver * sat_solver_new(void)
Definition satSolver.c:1137
int sat_solver_simplify(sat_solver *s)
Definition satSolver.c:1515
void sat_solver_setnvars(sat_solver *s, int n)
Definition satSolver.c:1272
int sat_solver_nvars(sat_solver *s)
Definition satSolver.c:2369
int nVars
Definition cnf.h:59
int * pVarNums
Definition cnf.h:63
int ** pClauses
Definition cnf.h:62
int nClauses
Definition cnf.h:61
char * pName
Definition gia.h:99
unsigned Value
Definition gia.h:89
#define assert(ex)
Definition util_old.h:213
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54