ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
sfmSat.c
Go to the documentation of this file.
1
20
21#include "sfmInt.h"
22#include "misc/util/utilTruth.h"
23
25
26
30
34
47{
48 // p->vOrder contains all variables in the window in a good order
49 // p->vDivs is a subset of nodes in p->vOrder used as divisor candidates
50 // p->vTfo contains TFO of the node (does not include node)
51 // p->vRoots contains roots of the TFO of the node (may include node)
52 Vec_Int_t * vClause;
53 int RetValue, iNode = -1, iFanin, i, k;
54 abctime clk = Abc_Clock();
55// if ( p->pSat )
56// printf( "%d ", p->pSat->stats.learnts );
57 sat_solver_restart( p->pSat );
58 sat_solver_setnvars( p->pSat, 1 + Vec_IntSize(p->vOrder) + Vec_IntSize(p->vTfo) + Vec_IntSize(p->vRoots) + 10 );
59 // create SAT variables
60 Sfm_NtkCleanVars( p );
61 p->nSatVars = 1;
62 Vec_IntForEachEntry( p->vOrder, iNode, i )
63 Sfm_ObjSetSatVar( p, iNode, p->nSatVars++ );
64 // collect divisor variables
65 Vec_IntClear( p->vDivVars );
66 Vec_IntForEachEntry( p->vDivs, iNode, i )
67 Vec_IntPush( p->vDivVars, Sfm_ObjSatVar(p, iNode) );
68 // add CNF clauses for the TFI
69 Vec_IntForEachEntry( p->vOrder, iNode, i )
70 {
71 if ( Sfm_ObjIsPi(p, iNode) )
72 continue;
73 // collect fanin variables
74 Vec_IntClear( p->vFaninMap );
75 Sfm_ObjForEachFanin( p, iNode, iFanin, k )
76 Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iFanin) );
77 Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iNode) );
78 // generate CNF
79 Sfm_TranslateCnf( p->vClauses, (Vec_Str_t *)Vec_WecEntry(p->vCnfs, iNode), p->vFaninMap, -1 );
80 // add clauses
81 Vec_WecForEachLevel( p->vClauses, vClause, k )
82 {
83 if ( Vec_IntSize(vClause) == 0 )
84 break;
85 RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(vClause), Vec_IntArray(vClause) + Vec_IntSize(vClause) );
86 if ( RetValue == 0 )
87 return 0;
88 }
89 }
90 if ( Vec_IntSize(p->vTfo) > 0 )
91 {
92 assert( p->pPars->nTfoLevMax > 0 );
93 assert( Vec_IntSize(p->vRoots) > 0 );
94 assert( Vec_IntEntry(p->vTfo, 0) != p->iPivotNode );
95 // collect variables of root nodes
96 Vec_IntClear( p->vLits );
97 Vec_IntForEachEntry( p->vRoots, iNode, i )
98 Vec_IntPush( p->vLits, Sfm_ObjSatVar(p, iNode) );
99 // assign new variables to the TFO nodes
100 Vec_IntForEachEntry( p->vTfo, iNode, i )
101 {
102 Sfm_ObjCleanSatVar( p, Sfm_ObjSatVar(p, iNode) );
103 Sfm_ObjSetSatVar( p, iNode, p->nSatVars++ );
104 }
105 // add CNF clauses for the TFO
106 Vec_IntForEachEntry( p->vTfo, iNode, i )
107 {
108 assert( Sfm_ObjIsNode(p, iNode) );
109 // collect fanin variables
110 Vec_IntClear( p->vFaninMap );
111 Sfm_ObjForEachFanin( p, iNode, iFanin, k )
112 Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iFanin) );
113 Vec_IntPush( p->vFaninMap, Sfm_ObjSatVar(p, iNode) );
114 // generate CNF
115 Sfm_TranslateCnf( p->vClauses, (Vec_Str_t *)Vec_WecEntry(p->vCnfs, iNode), p->vFaninMap, Sfm_ObjSatVar(p, p->iPivotNode) );
116 // add clauses
117 Vec_WecForEachLevel( p->vClauses, vClause, k )
118 {
119 if ( Vec_IntSize(vClause) == 0 )
120 break;
121 RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(vClause), Vec_IntArray(vClause) + Vec_IntSize(vClause) );
122 if ( RetValue == 0 )
123 return 0;
124 }
125 }
126 // create XOR clauses for the roots
127 Vec_IntForEachEntry( p->vRoots, iNode, i )
128 {
129 sat_solver_add_xor( p->pSat, Vec_IntEntry(p->vLits, i), Sfm_ObjSatVar(p, iNode), p->nSatVars++, 0 );
130 Vec_IntWriteEntry( p->vLits, i, Abc_Var2Lit(p->nSatVars-1, 0) );
131 }
132 // make OR clause for the last nRoots variables
133 RetValue = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits) );
134 if ( RetValue == 0 )
135 return 0;
136 }
137 // finalize
138 RetValue = sat_solver_simplify( p->pSat );
139 p->timeCnf += Abc_Clock() - clk;
140 return RetValue;
141}
142
155{
156 word * pSign;
157 int status, i, Div, iVar, nFinal, * pFinal, nIter = 0;
158 int pLits[2], nVars = sat_solver_nvars( p->pSat );
159 int nWords = Abc_Truth6WordNum( Vec_IntSize(p->vDivIds) );
160 sat_solver_setnvars( p->pSat, nVars + 1 );
161 pLits[0] = Abc_Var2Lit( Sfm_ObjSatVar(p, p->iPivotNode), 0 ); // F = 1
162 pLits[1] = Abc_Var2Lit( nVars, 0 ); // iNewLit
163 assert( Vec_IntSize(p->vDivIds) <= SFM_FANIN_MAX );
164 Abc_TtClear( p->pTruth, nWords );
165 while ( 1 )
166 {
167 // find onset minterm
168 p->nSatCalls++;
169 status = sat_solver_solve( p->pSat, pLits, pLits + 2, p->pPars->nBTLimit, 0, 0, 0 );
170 if ( status == l_Undef )
171 return SFM_SAT_UNDEC;
172 if ( status == l_False )
173 return p->pTruth[0];
174 assert( status == l_True );
175 // remember variable values
176 Vec_IntClear( p->vValues );
177 Vec_IntForEachEntry( p->vDivVars, iVar, i )
178 Vec_IntPush( p->vValues, sat_solver_var_value(p->pSat, iVar) );
179 // collect divisor literals
180 Vec_IntClear( p->vLits );
181 Vec_IntPush( p->vLits, Abc_LitNot(pLits[0]) ); // F = 0
182 Vec_IntForEachEntry( p->vDivIds, Div, i )
183 Vec_IntPush( p->vLits, sat_solver_var_literal(p->pSat, Div) );
184 // check against offset
185 p->nSatCalls++;
186 status = sat_solver_solve( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits), p->pPars->nBTLimit, 0, 0, 0 );
187 if ( status == l_Undef )
188 return SFM_SAT_UNDEC;
189 if ( status == l_True )
190 break;
191 assert( status == l_False );
192 // compute cube and add clause
193 nFinal = sat_solver_final( p->pSat, &pFinal );
194 Abc_TtFill( p->pCube, nWords );
195 Vec_IntClear( p->vLits );
196 Vec_IntPush( p->vLits, Abc_LitNot(pLits[1]) ); // NOT(iNewLit)
197 for ( i = 0; i < nFinal; i++ )
198 {
199 if ( pFinal[i] == pLits[0] )
200 continue;
201 Vec_IntPush( p->vLits, pFinal[i] );
202 iVar = Vec_IntFind( p->vDivIds, Abc_Lit2Var(pFinal[i]) ); assert( iVar >= 0 );
203 Abc_TtAndSharp( p->pCube, p->pCube, p->pTtElems[iVar], nWords, !Abc_LitIsCompl(pFinal[i]) );
204 }
205 Abc_TtOr( p->pTruth, p->pTruth, p->pCube, nWords );
206 status = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits) );
207 assert( status );
208 nIter++;
209 }
210 assert( status == l_True );
211 // store the counter-example
212 Vec_IntForEachEntry( p->vDivVars, iVar, i )
213 if ( Vec_IntEntry(p->vValues, i) ^ sat_solver_var_value(p->pSat, iVar) ) // insert 1
214 {
215 pSign = Vec_WrdEntryP( p->vDivCexes, i );
216 assert( !Abc_TtGetBit( pSign, p->nCexes) );
217 Abc_TtXorBit( pSign, p->nCexes );
218 }
219 p->nCexes++;
220 return SFM_SAT_SAT;
221}
222
235{
236 int fOnSet, iMint, iVar, nVars = sat_solver_nvars( p->pSat );
237 int iVarPivot = Sfm_ObjSatVar( p, p->iPivotNode );
238 int status, iNewLit, i, Div, nIter = 0;
239 Truth[0] = Truth[1] = 0;
240 sat_solver_setnvars( p->pSat, nVars + 1 );
241 iNewLit = Abc_Var2Lit( nVars, 0 ); // iNewLit
242 assert( Vec_IntSize(p->vDivIds) <= 6 );
243 Vec_IntFill( p->vValues, (1 << Vec_IntSize(p->vDivIds)) * Vec_IntSize(p->vDivVars), -1 );
244 while ( 1 )
245 {
246 // find care minterm
247 p->nSatCalls++; nIter++;
248 status = sat_solver_solve( p->pSat, &iNewLit, &iNewLit + 1, p->pPars->nBTLimit, 0, 0, 0 );
249 if ( status == l_Undef )
250 return l_Undef;
251 if ( status == l_False )
252 return l_False;
253 assert( status == l_True );
254 // collect values
255 iMint = 0;
256 fOnSet = sat_solver_var_value(p->pSat, iVarPivot);
257 Vec_IntClear( p->vLits );
258 Vec_IntPush( p->vLits, Abc_LitNot(iNewLit) ); // NOT(iNewLit)
259 Vec_IntPush( p->vLits, Abc_LitNot(sat_solver_var_literal(p->pSat, iVarPivot)) );
260 Vec_IntForEachEntry( p->vDivIds, Div, i )
261 {
262 Vec_IntPush( p->vLits, Abc_LitNot(sat_solver_var_literal(p->pSat, Div)) );
263 if ( sat_solver_var_value(p->pSat, Div) )
264 iMint |= 1 << i;
265 }
266 if ( Truth[!fOnSet] & ((word)1 << iMint) )
267 break;
268 assert( !(Truth[fOnSet] & ((word)1 << iMint)) );
269 Truth[fOnSet] |= ((word)1 << iMint);
270 // remember variable values
271 Vec_IntForEachEntry( p->vDivVars, iVar, i )
272 Vec_IntWriteEntry( p->vValues, iMint * Vec_IntSize(p->vDivVars) + i, sat_solver_var_value(p->pSat, iVar) );
273 status = sat_solver_addclause( p->pSat, Vec_IntArray(p->vLits), Vec_IntArray(p->vLits) + Vec_IntSize(p->vLits) );
274 if ( status == 0 )
275 return l_False;
276 }
277 assert( status == l_True );
278 // store the counter-example
279 assert( iMint < (1 << Vec_IntSize(p->vDivIds)) );
280 Vec_IntForEachEntry( p->vDivVars, iVar, i )
281 {
282 int Value = Vec_IntEntry(p->vValues, iMint * Vec_IntSize(p->vDivVars) + i);
283 assert( Value != -1 );
284 if ( Value ^ sat_solver_var_value(p->pSat, iVar) ) // insert 1
285 {
286 word * pSign = Vec_WrdEntryP( p->vDivCexes, i );
287 assert( !Abc_TtGetBit( pSign, p->nCexes) );
288 Abc_TtXorBit( pSign, p->nCexes );
289 }
290 }
291 p->nCexes++;
292 return l_True;
293}
295{
296 word Res, ResP, ResN, Truth[2];
297 int nCubesP = 0, nCubesN = 0;
298 int RetValue = Sfm_ComputeInterpolantInt( p, Truth );
299 if ( RetValue == l_Undef )
300 return SFM_SAT_UNDEC;
301 if ( RetValue == l_True )
302 return SFM_SAT_SAT;
303 assert( RetValue == l_False );
304 //printf( "Offset = %2d. Onset = %2d. DC = %2d. Total = %2d.\n",
305 // Abc_TtCountOnes(Truth[0]), Abc_TtCountOnes(Truth[1]),
306 // (1<<Vec_IntSize(p->vDivIds)) - (Abc_TtCountOnes(Truth[0]) + Abc_TtCountOnes(Truth[1])),
307 // 1<<Vec_IntSize(p->vDivIds) );
308 Truth[0] = Abc_Tt6Stretch( Truth[0], Vec_IntSize(p->vDivIds) );
309 Truth[1] = Abc_Tt6Stretch( Truth[1], Vec_IntSize(p->vDivIds) );
310 ResP = Abc_Tt6Isop( Truth[1], ~Truth[0], Vec_IntSize(p->vDivIds), &nCubesP );
311 ResN = Abc_Tt6Isop( Truth[0], ~Truth[1], Vec_IntSize(p->vDivIds), &nCubesN );
312 Res = nCubesP <= nCubesN ? ResP : ~ResN;
313 //Dau_DsdPrintFromTruth( &Res, Vec_IntSize(p->vDivIds) );
314 return Res;
315}
316
329{
330 int iNode = 3;
331 int iDiv0 = 6;
332 int iDiv1 = 7;
333 word uTruth;
334// int i;
335// Sfm_NtkForEachNode( p, i )
336 {
337 Sfm_NtkCreateWindow( p, iNode, 1 );
339
340 // collect SAT variables of divisors
341 Vec_IntClear( p->vDivIds );
342 Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, iDiv0) );
343 Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, iDiv1) );
344
345 uTruth = Sfm_ComputeInterpolant( p );
346
347 if ( uTruth == SFM_SAT_SAT )
348 printf( "The problem is SAT.\n" );
349 else if ( uTruth == SFM_SAT_UNDEC )
350 printf( "The problem is UNDEC.\n" );
351 else
352 Kit_DsdPrintFromTruth( (unsigned *)&uTruth, 2 ), printf( "\n" );
353 }
354}
355
359
360
362
int nWords
Definition abcNpn.c:127
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
struct Vec_Str_t_ Vec_Str_t
Definition bblif.c:46
#define l_True
Definition bmcBmcS.c:35
#define l_Undef
Definition bmcBmcS.c:34
#define l_False
Definition bmcBmcS.c:36
#define sat_solver_addclause
Definition cecSatG2.c:37
#define sat_solver_add_xor
Definition cecSatG2.c:39
#define sat_solver_solve
Definition cecSatG2.c:45
Cube * p
Definition exorList.c:222
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
void Kit_DsdPrintFromTruth(unsigned *pTruth, int nVars)
Definition kitDsd.c:491
int sat_solver_simplify(sat_solver *s)
Definition satSolver.c:1515
void sat_solver_restart(sat_solver *s)
Definition satSolver.c:1389
void sat_solver_setnvars(sat_solver *s, int n)
Definition satSolver.c:1272
int sat_solver_nvars(sat_solver *s)
Definition satSolver.c:2369
void Sfm_TranslateCnf(Vec_Wec_t *vRes, Vec_Str_t *vCnf, Vec_Int_t *vFaninMap, int iPivotVar)
Definition sfmCnf.c:201
int Sfm_NtkCreateWindow(Sfm_Ntk_t *p, int iNode, int fVerbose)
Definition sfmWin.c:340
#define Sfm_ObjForEachFanin(p, Node, Fan, i)
Definition sfmInt.h:199
#define SFM_SAT_SAT
Definition sfmInt.h:54
#define SFM_FANIN_MAX
INCLUDES ///.
Definition sfmInt.h:51
#define SFM_SAT_UNDEC
Definition sfmInt.h:53
void Sfm_ComputeInterpolantCheck(Sfm_Ntk_t *p)
Definition sfmSat.c:328
ABC_NAMESPACE_IMPL_START int Sfm_NtkWindowToSolver(Sfm_Ntk_t *p)
DECLARATIONS ///.
Definition sfmSat.c:46
word Sfm_ComputeInterpolant(Sfm_Ntk_t *p)
Definition sfmSat.c:154
word Sfm_ComputeInterpolant2(Sfm_Ntk_t *p)
Definition sfmSat.c:294
int Sfm_ComputeInterpolantInt(Sfm_Ntk_t *p, word Truth[2])
Definition sfmSat.c:234
typedefABC_NAMESPACE_HEADER_START struct Sfm_Ntk_t_ Sfm_Ntk_t
INCLUDES ///.
Definition sfm.h:41
#define assert(ex)
Definition util_old.h:213
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54
#define Vec_WecForEachLevel(vGlob, vVec, i)
MACRO DEFINITIONS ///.
Definition vecWec.h:55