ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
sbdLut.c
Go to the documentation of this file.
1
20
21#include "sbdInt.h"
22#include "misc/util/utilTruth.h"
23
25
29
33
45// count the number of parameter variables in the structure
46int Sbd_ProblemCountParams( int nStrs, Sbd_Str_t * pStr0 )
47{
48 Sbd_Str_t * pStr; int nPars = 0;
49 for ( pStr = pStr0; pStr < pStr0 + nStrs; pStr++ )
50 nPars += pStr->fLut ? 1 << pStr->nVarIns : pStr->nVarIns;
51 return nPars;
52}
53// add clauses for the structure
54int Sbd_ProblemAddClauses( sat_solver * pSat, int nVars, int nStrs, int * pVars, Sbd_Str_t * pStr0 )
55{
56 // variable order: inputs, structure outputs, parameters
57 Sbd_Str_t * pStr;
58 int VarOut = nVars;
59 int VarPar = nVars + nStrs;
60 int m, k, n, status, pLits[SBD_SIZE_MAX+2];
61//printf( "Start par = %d. ", VarPar );
62 for ( pStr = pStr0; pStr < pStr0 + nStrs; pStr++, VarOut++ )
63 {
64 if ( pStr->fLut )
65 {
66 int nMints = 1 << pStr->nVarIns;
67 assert( pStr->nVarIns <= 6 );
68 for ( m = 0; m < nMints; m++, VarPar++ )
69 {
70 for ( k = 0; k < pStr->nVarIns; k++ )
71 pLits[k] = Abc_Var2Lit( pVars[pStr->VarIns[k]], (m >> k) & 1 );
72 for ( n = 0; n < 2; n++ )
73 {
74 pLits[pStr->nVarIns] = Abc_Var2Lit( pVars[VarPar], n );
75 pLits[pStr->nVarIns+1] = Abc_Var2Lit( pVars[VarOut], !n );
76 status = sat_solver_addclause( pSat, pLits, pLits + pStr->nVarIns + 2 );
77 if ( !status )
78 return 0;
79 }
80 }
81 }
82 else
83 {
84 assert( pStr->nVarIns <= SBD_DIV_MAX );
85 for ( k = 0; k < pStr->nVarIns; k++, VarPar++ )
86 {
87 for ( n = 0; n < 2; n++ )
88 {
89 pLits[0] = Abc_Var2Lit( pVars[VarPar], 1 );
90 pLits[1] = Abc_Var2Lit( pVars[VarOut], n );
91 pLits[2] = Abc_Var2Lit( pVars[pStr->VarIns[k]], !n );
92 status = sat_solver_addclause( pSat, pLits, pLits + 3 );
93 if ( !status )
94 return 0;
95 }
96 }
97 }
98 }
99 return 1;
100}
101void Sbd_ProblemAddClausesInit( sat_solver * pSat, int nVars, int nStrs, int * pVars, Sbd_Str_t * pStr0 )
102{
103 Sbd_Str_t * pStr;
104 int VarPar = nVars + nStrs;
105 int m, m2, status, pLits[SBD_DIV_MAX];
106 // make sure selector parameters are mutually exclusive
107 for ( pStr = pStr0; pStr < pStr0 + nStrs; VarPar += pStr->fLut ? 1 << pStr->nVarIns : pStr->nVarIns, pStr++ )
108 {
109 if ( pStr->fLut )
110 continue;
111 // one variable should be selected
112 assert( pStr->nVarIns <= SBD_DIV_MAX );
113 for ( m = 0; m < pStr->nVarIns; m++ )
114 pLits[m] = Abc_Var2Lit( pVars[VarPar + m], 0 );
115 status = sat_solver_addclause( pSat, pLits, pLits + pStr->nVarIns );
116 assert( status );
117 // two variables cannot be selected
118 for ( m = 0; m < pStr->nVarIns; m++ )
119 for ( m2 = m+1; m2 < pStr->nVarIns; m2++ )
120 {
121 pLits[0] = Abc_Var2Lit( pVars[VarPar + m], 1 );
122 pLits[1] = Abc_Var2Lit( pVars[VarPar + m2], 1 );
123 status = sat_solver_addclause( pSat, pLits, pLits + 2 );
124 assert( status );
125 }
126 }
127}
128void Sbd_ProblemPrintSolution( int nStrs, Sbd_Str_t * pStr0, Vec_Int_t * vLits )
129{
130 Sbd_Str_t * pStr;
131 int m, nIters, iLit = 0;
132 printf( "Solution found:\n" );
133 for ( pStr = pStr0; pStr < pStr0 + nStrs; pStr++ )
134 {
135 nIters = pStr->fLut ? 1 << pStr->nVarIns : pStr->nVarIns;
136 printf( "%s%d : ", pStr->fLut ? "LUT":"SEL", (int)(pStr-pStr0) );
137 for ( m = 0; m < nIters; m++, iLit++ )
138 printf( "%d", !Abc_LitIsCompl(Vec_IntEntry(vLits, iLit)) );
139 printf( " {" );
140 for ( m = 0; m < pStr->nVarIns; m++ )
141 printf( " %d", pStr->VarIns[m] );
142 printf( " }\n" );
143 }
144 assert( iLit == Vec_IntSize(vLits) );
145}
146void Sbd_ProblemCollectSolution( int nStrs, Sbd_Str_t * pStr0, Vec_Int_t * vLits )
147{
148 Sbd_Str_t * pStr;
149 int m, nIters, iLit = 0;
150 for ( pStr = pStr0; pStr < pStr0 + nStrs; pStr++ )
151 {
152 pStr->Res = 0;
153 if ( pStr->fLut )
154 {
155 nIters = 1 << pStr->nVarIns;
156 for ( m = 0; m < nIters; m++, iLit++ )
157 if ( !Abc_LitIsCompl(Vec_IntEntry(vLits, iLit)) )
158 Abc_TtSetBit( &pStr->Res, m );
159 pStr->Res = Abc_Tt6Stretch( pStr->Res, pStr->nVarIns );
160 }
161 else
162 {
163 nIters = 0;
164 for ( m = 0; m < pStr->nVarIns; m++, iLit++ )
165 if ( !Abc_LitIsCompl(Vec_IntEntry(vLits, iLit)) )
166 {
167 pStr->Res = pStr->VarIns[m];
168 nIters++;
169 }
170 assert( nIters == 1 );
171 }
172 }
173 assert( iLit == Vec_IntSize(vLits) );
174}
175
188 int Pivot, Vec_Int_t * vWinObjs, Vec_Int_t * vObj2Var,
189 Vec_Int_t * vTfo, Vec_Int_t * vRoots,
190 Vec_Int_t * vDivSet, int nStrs, Sbd_Str_t * pStr0 ) // divisors, structures
191{
192 extern sat_solver * Sbd_ManSatSolver( sat_solver * pSat, Gia_Man_t * p, Vec_Int_t * vMirrors, int Pivot, Vec_Int_t * vWinObjs, Vec_Int_t * vObj2Var, Vec_Int_t * vTfo, Vec_Int_t * vRoots, int fQbf );
193
194 int fVerbose = 0;
195 abctime clk = Abc_Clock();
196 Vec_Int_t * vLits = Vec_IntAlloc( 100 );
197 sat_solver * pSatCec = Sbd_ManSatSolver( NULL, p, vMirrors, Pivot, vWinObjs, vObj2Var, vTfo, vRoots, 1 );
198 sat_solver * pSatQbf = sat_solver_new();
199
200 int nVars = Vec_IntSize( vDivSet );
201 int nPars = Sbd_ProblemCountParams( nStrs, pStr0 );
202
203 int VarCecOut = Vec_IntSize(vWinObjs) + Vec_IntSize(vTfo) + Vec_IntSize(vRoots);
204 int VarCecPar = VarCecOut + nStrs;
205
206 int VarQbfPar = 0;
207 int VarQbfFree = nPars;
208
209 int pVarsCec[256];
210 int pVarsQbf[256];
211 int i, iVar, iLit, nIters;
212 int RetValue = 0;
213
214 assert( Vec_IntSize(vDivSet) <= SBD_DIV_MAX );
215 assert( nVars + nStrs + nPars <= 256 );
216
217 // collect CEC variables
218 Vec_IntForEachEntry( vDivSet, iVar, i )
219 pVarsCec[i] = iVar;
220 for ( i = 0; i < nStrs; i++ )
221 pVarsCec[nVars + i] = VarCecOut + i;
222 for ( i = 0; i < nPars; i++ )
223 pVarsCec[nVars + nStrs + i] = VarCecPar + i;
224
225 // collect QBF variables
226 for ( i = 0; i < nVars + nStrs; i++ )
227 pVarsQbf[i] = -1;
228 for ( i = 0; i < nPars; i++ )
229 pVarsQbf[nVars + nStrs + i] = VarQbfPar + i;
230
231 // add clauses to the CEC problem
232 Sbd_ProblemAddClauses( pSatCec, nVars, nStrs, pVarsCec, pStr0 );
233
234 // create QBF solver
235 sat_solver_setnvars( pSatQbf, 1000 );
236 Sbd_ProblemAddClausesInit( pSatQbf, nVars, nStrs, pVarsQbf, pStr0 );
237
238 // assume all parameter variables are 0
239 Vec_IntClear( vLits );
240 for ( i = 0; i < nPars; i++ )
241 Vec_IntPush( vLits, Abc_Var2Lit(VarCecPar + i, 1) );
242 for ( nIters = 0; nIters < (1 << nVars); nIters++ )
243 {
244 // check if these parameters solve the problem
245 int status = sat_solver_solve( pSatCec, Vec_IntArray(vLits), Vec_IntLimit(vLits), 0, 0, 0, 0 );
246 if ( status == l_False ) // solution found
247 break;
248 assert( status == l_True );
249
250 if ( fVerbose )
251 {
252 printf( "Iter %3d : ", nIters );
253 for ( i = 0; i < nPars; i++ )
254 printf( "%d", !Abc_LitIsCompl(Vec_IntEntry(vLits, i)) );
255 printf( " " );
256 }
257
258 Vec_IntClear( vLits );
259 // create new QBF variables
260 for ( i = 0; i < nVars + nStrs; i++ )
261 pVarsQbf[i] = VarQbfFree++;
262 // set their values
263 Vec_IntForEachEntry( vDivSet, iVar, i )
264 {
265 iLit = Abc_Var2Lit( pVarsQbf[i], !sat_solver_var_value(pSatCec, iVar) );
266 status = sat_solver_addclause( pSatQbf, &iLit, &iLit + 1 );
267 assert( status );
268 if ( fVerbose )
269 printf( "%d", sat_solver_var_value(pSatCec, iVar) );
270 }
271 iLit = Abc_Var2Lit( pVarsQbf[nVars], sat_solver_var_value(pSatCec, VarCecOut) );
272 status = sat_solver_addclause( pSatQbf, &iLit, &iLit + 1 );
273 assert( status );
274 if ( fVerbose )
275 printf( " %d\n", !sat_solver_var_value(pSatCec, VarCecOut) );
276 // add clauses to the QBF problem
277 if ( !Sbd_ProblemAddClauses( pSatQbf, nVars, nStrs, pVarsQbf, pStr0 ) )
278 break; // solution does not exist
279 // check if solution still exists
280 status = sat_solver_solve( pSatQbf, NULL, NULL, 0, 0, 0, 0 );
281 if ( status == l_False ) // solution does not exist
282 break;
283 assert( status == l_True );
284 // find the new values of parameters
285 assert( Vec_IntSize(vLits) == 0 );
286 for ( i = 0; i < nPars; i++ )
287 Vec_IntPush( vLits, Abc_Var2Lit(VarCecPar + i, !sat_solver_var_value(pSatQbf, VarQbfPar + i)) );
288 }
289 if ( Vec_IntSize(vLits) > 0 )
290 {
291 //Sbd_ProblemPrintSolution( nStrs, pStr0, vLits );
292 Sbd_ProblemCollectSolution( nStrs, pStr0, vLits );
293 RetValue = 1;
294 }
295 sat_solver_delete( pSatCec );
296 sat_solver_delete( pSatQbf );
297 Vec_IntFree( vLits );
298
299 if ( fVerbose )
300 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
301 return RetValue;
302}
303
304
308
309
311
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
#define l_True
Definition bmcBmcS.c:35
#define l_False
Definition bmcBmcS.c:36
#define sat_solver_addclause
Definition cecSatG2.c:37
#define sat_solver
Definition cecSatG2.c:34
#define sat_solver_solve
Definition cecSatG2.c:45
Cube * p
Definition exorList.c:222
struct Gia_Man_t_ Gia_Man_t
Definition gia.h:96
sat_solver * sat_solver_new(void)
Definition satSolver.c:1137
void sat_solver_setnvars(sat_solver *s, int n)
Definition satSolver.c:1272
void sat_solver_delete(sat_solver *s)
Definition satSolver.c:1341
#define SBD_SIZE_MAX
Definition sbdInt.h:56
sat_solver * Sbd_ManSatSolver(sat_solver *pSat, Gia_Man_t *p, Vec_Int_t *vMirrors, int Pivot, Vec_Int_t *vWinObjs, Vec_Int_t *vObj2Var, Vec_Int_t *vTfo, Vec_Int_t *vRoots, int fQbf)
DECLARATIONS ///.
Definition sbdWin.c:52
#define SBD_DIV_MAX
Definition sbdInt.h:57
struct Sbd_Str_t_ Sbd_Str_t
Definition sbdInt.h:67
void Sbd_ProblemPrintSolution(int nStrs, Sbd_Str_t *pStr0, Vec_Int_t *vLits)
Definition sbdLut.c:128
void Sbd_ProblemCollectSolution(int nStrs, Sbd_Str_t *pStr0, Vec_Int_t *vLits)
Definition sbdLut.c:146
ABC_NAMESPACE_IMPL_START int Sbd_ProblemCountParams(int nStrs, Sbd_Str_t *pStr0)
DECLARATIONS ///.
Definition sbdLut.c:46
void Sbd_ProblemAddClausesInit(sat_solver *pSat, int nVars, int nStrs, int *pVars, Sbd_Str_t *pStr0)
Definition sbdLut.c:101
int Sbd_ProblemSolve(Gia_Man_t *p, Vec_Int_t *vMirrors, int Pivot, Vec_Int_t *vWinObjs, Vec_Int_t *vObj2Var, Vec_Int_t *vTfo, Vec_Int_t *vRoots, Vec_Int_t *vDivSet, int nStrs, Sbd_Str_t *pStr0)
Definition sbdLut.c:187
int Sbd_ProblemAddClauses(sat_solver *pSat, int nVars, int nStrs, int *pVars, Sbd_Str_t *pStr0)
Definition sbdLut.c:54
word Res
Definition sbdInt.h:73
int nVarIns
Definition sbdInt.h:71
int fLut
Definition sbdInt.h:70
int VarIns[SBD_DIV_MAX]
Definition sbdInt.h:72
#define assert(ex)
Definition util_old.h:213
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54