abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcProve.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [abcProve.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Network and node package.]
8 
9  Synopsis [Proves the miter using AIG rewriting, FRAIGing, and SAT solving.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: abcProve.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include <math.h>
22 
23 #include "base/abc/abc.h"
24 #include "proof/fraig/fraig.h"
25 #include "misc/extra/extraBdd.h"
26 
28 
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// DECLARATIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 extern int Abc_NtkRefactor( Abc_Ntk_t * pNtk, int nNodeSizeMax, int nConeSizeMax, int fUpdateLevel, int fUseZeros, int fUseDcs, int fVerbose );
35 extern Abc_Ntk_t * Abc_NtkFromFraig( Fraig_Man_t * pMan, Abc_Ntk_t * pNtk );
36 
37 static Abc_Ntk_t * Abc_NtkMiterFraig( Abc_Ntk_t * pNtk, int nBTLimit, ABC_INT64_T nInspLimit, int * pRetValue, int * pNumFails, ABC_INT64_T * pNumConfs, ABC_INT64_T * pNumInspects );
38 static void Abc_NtkMiterPrint( Abc_Ntk_t * pNtk, char * pString, abctime clk, int fVerbose );
39 
40 
41 ////////////////////////////////////////////////////////////////////////
42 /// FUNCTION DEFINITIONS ///
43 ////////////////////////////////////////////////////////////////////////
44 
45 /**Function*************************************************************
46 
47  Synopsis [Attempts to solve the miter using a number of tricks.]
48 
49  Description [Returns -1 if timed out; 0 if SAT; 1 if UNSAT. Returns
50  a simplified version of the original network (or a constant 0 network).
51  In case the network is not a constant zero and a SAT assignment is found,
52  pNtk->pModel contains a satisfying assignment.]
53 
54  SideEffects []
55 
56  SeeAlso []
57 
58 ***********************************************************************/
59 int Abc_NtkMiterProve( Abc_Ntk_t ** ppNtk, void * pPars )
60 {
61  Prove_Params_t * pParams = (Prove_Params_t *)pPars;
62  Abc_Ntk_t * pNtk, * pNtkTemp;
63  int RetValue = -1, nIter, nSatFails, Counter;
64  abctime clk; //, timeStart = Abc_Clock();
65  ABC_INT64_T nSatConfs, nSatInspects, nInspectLimit;
66 
67  // get the starting network
68  pNtk = *ppNtk;
69  assert( Abc_NtkIsStrash(pNtk) );
70  assert( Abc_NtkPoNum(pNtk) == 1 );
71 
72  if ( pParams->fVerbose )
73  {
74  printf( "RESOURCE LIMITS: Iterations = %d. Rewriting = %s. Fraiging = %s.\n",
75  pParams->nItersMax, pParams->fUseRewriting? "yes":"no", pParams->fUseFraiging? "yes":"no" );
76  printf( "Miter = %d (%3.1f). Rwr = %d (%3.1f). Fraig = %d (%3.1f). Last = %d.\n",
77  pParams->nMiteringLimitStart, pParams->nMiteringLimitMulti,
78  pParams->nRewritingLimitStart, pParams->nRewritingLimitMulti,
79  pParams->nFraigingLimitStart, pParams->nFraigingLimitMulti, pParams->nMiteringLimitLast );
80  }
81 
82  // if SAT only, solve without iteration
83  if ( !pParams->fUseRewriting && !pParams->fUseFraiging )
84  {
85  clk = Abc_Clock();
86  RetValue = Abc_NtkMiterSat( pNtk, (ABC_INT64_T)pParams->nMiteringLimitLast, (ABC_INT64_T)0, 0, NULL, NULL );
87  Abc_NtkMiterPrint( pNtk, "SAT solving", clk, pParams->fVerbose );
88  *ppNtk = pNtk;
89  return RetValue;
90  }
91 
92  // check the current resource limits
93  for ( nIter = 0; nIter < pParams->nItersMax; nIter++ )
94  {
95  if ( pParams->fVerbose )
96  {
97  printf( "ITERATION %2d : Confs = %6d. FraigBTL = %3d. \n", nIter+1,
98  (int)(pParams->nMiteringLimitStart * pow(pParams->nMiteringLimitMulti,nIter)),
99  (int)(pParams->nFraigingLimitStart * pow(pParams->nFraigingLimitMulti,nIter)) );
100  fflush( stdout );
101  }
102 
103  // try brute-force SAT
104  clk = Abc_Clock();
105  nInspectLimit = pParams->nTotalInspectLimit? pParams->nTotalInspectLimit - pParams->nTotalInspectsMade : 0;
106  RetValue = Abc_NtkMiterSat( pNtk, (ABC_INT64_T)(pParams->nMiteringLimitStart * pow(pParams->nMiteringLimitMulti,nIter)), (ABC_INT64_T)nInspectLimit, 0, &nSatConfs, &nSatInspects );
107  Abc_NtkMiterPrint( pNtk, "SAT solving", clk, pParams->fVerbose );
108  if ( RetValue >= 0 )
109  break;
110 
111  // add to the number of backtracks and inspects
112  pParams->nTotalBacktracksMade += nSatConfs;
113  pParams->nTotalInspectsMade += nSatInspects;
114  // check if global resource limit is reached
115  if ( (pParams->nTotalBacktrackLimit && pParams->nTotalBacktracksMade >= pParams->nTotalBacktrackLimit) ||
116  (pParams->nTotalInspectLimit && pParams->nTotalInspectsMade >= pParams->nTotalInspectLimit) )
117  {
118  printf( "Reached global limit on conflicts/inspects. Quitting.\n" );
119  *ppNtk = pNtk;
120  return -1;
121  }
122 
123  // try rewriting
124  if ( pParams->fUseRewriting )
125  {
126  clk = Abc_Clock();
127  Counter = (int)(pParams->nRewritingLimitStart * pow(pParams->nRewritingLimitMulti,nIter));
128 // Counter = 1;
129  while ( 1 )
130  {
131 /*
132  extern Abc_Ntk_t * Abc_NtkIvyResyn( Abc_Ntk_t * pNtk, int fUpdateLevel, int fVerbose );
133  pNtk = Abc_NtkIvyResyn( pNtkTemp = pNtk, 0, 0 ); Abc_NtkDelete( pNtkTemp );
134  if ( (RetValue = Abc_NtkMiterIsConstant(pNtk)) >= 0 )
135  break;
136  if ( --Counter == 0 )
137  break;
138 */
139 /*
140  Abc_NtkRewrite( pNtk, 0, 0, 0, 0, 0 );
141  if ( (RetValue = Abc_NtkMiterIsConstant(pNtk)) >= 0 )
142  break;
143  if ( --Counter == 0 )
144  break;
145 */
146  Abc_NtkRewrite( pNtk, 0, 0, 0, 0, 0 );
147  if ( (RetValue = Abc_NtkMiterIsConstant(pNtk)) >= 0 )
148  break;
149  if ( --Counter == 0 )
150  break;
151  Abc_NtkRefactor( pNtk, 10, 16, 0, 0, 0, 0 );
152  if ( (RetValue = Abc_NtkMiterIsConstant(pNtk)) >= 0 )
153  break;
154  if ( --Counter == 0 )
155  break;
156  pNtk = Abc_NtkBalance( pNtkTemp = pNtk, 0, 0, 0 ); Abc_NtkDelete( pNtkTemp );
157  if ( (RetValue = Abc_NtkMiterIsConstant(pNtk)) >= 0 )
158  break;
159  if ( --Counter == 0 )
160  break;
161  }
162  Abc_NtkMiterPrint( pNtk, "Rewriting ", clk, pParams->fVerbose );
163  }
164 
165  if ( pParams->fUseFraiging )
166  {
167  // try FRAIGing
168  clk = Abc_Clock();
169  nInspectLimit = pParams->nTotalInspectLimit? pParams->nTotalInspectLimit - pParams->nTotalInspectsMade : 0;
170  pNtk = Abc_NtkMiterFraig( pNtkTemp = pNtk, (int)(pParams->nFraigingLimitStart * pow(pParams->nFraigingLimitMulti,nIter)), nInspectLimit, &RetValue, &nSatFails, &nSatConfs, &nSatInspects ); Abc_NtkDelete( pNtkTemp );
171  Abc_NtkMiterPrint( pNtk, "FRAIGing ", clk, pParams->fVerbose );
172 // printf( "NumFails = %d\n", nSatFails );
173  if ( RetValue >= 0 )
174  break;
175 
176  // add to the number of backtracks and inspects
177  pParams->nTotalBacktracksMade += nSatConfs;
178  pParams->nTotalInspectsMade += nSatInspects;
179  // check if global resource limit is reached
180  if ( (pParams->nTotalBacktrackLimit && pParams->nTotalBacktracksMade >= pParams->nTotalBacktrackLimit) ||
181  (pParams->nTotalInspectLimit && pParams->nTotalInspectsMade >= pParams->nTotalInspectLimit) )
182  {
183  printf( "Reached global limit on conflicts/inspects. Quitting.\n" );
184  *ppNtk = pNtk;
185  return -1;
186  }
187  }
188 
189  }
190 
191  // try to prove it using brute force SAT
192  if ( RetValue < 0 && pParams->fUseBdds )
193  {
194  if ( pParams->fVerbose )
195  {
196  printf( "Attempting BDDs with node limit %d ...\n", pParams->nBddSizeLimit );
197  fflush( stdout );
198  }
199  clk = Abc_Clock();
200  pNtk = Abc_NtkCollapse( pNtkTemp = pNtk, pParams->nBddSizeLimit, 0, pParams->fBddReorder, 0 );
201  if ( pNtk )
202  {
203  Abc_NtkDelete( pNtkTemp );
204  RetValue = ( (Abc_NtkNodeNum(pNtk) == 1) && (Abc_ObjFanin0(Abc_NtkPo(pNtk,0))->pData == Cudd_ReadLogicZero((DdManager *)pNtk->pManFunc)) );
205  }
206  else
207  pNtk = pNtkTemp;
208  Abc_NtkMiterPrint( pNtk, "BDD building", clk, pParams->fVerbose );
209  }
210 
211  if ( RetValue < 0 )
212  {
213  if ( pParams->fVerbose )
214  {
215  printf( "Attempting SAT with conflict limit %d ...\n", pParams->nMiteringLimitLast );
216  fflush( stdout );
217  }
218  clk = Abc_Clock();
219  nInspectLimit = pParams->nTotalInspectLimit? pParams->nTotalInspectLimit - pParams->nTotalInspectsMade : 0;
220  RetValue = Abc_NtkMiterSat( pNtk, (ABC_INT64_T)pParams->nMiteringLimitLast, (ABC_INT64_T)nInspectLimit, 0, NULL, NULL );
221  Abc_NtkMiterPrint( pNtk, "SAT solving", clk, pParams->fVerbose );
222  }
223 
224  // assign the model if it was proved by rewriting (const 1 miter)
225  if ( RetValue == 0 && pNtk->pModel == NULL )
226  {
227  pNtk->pModel = ABC_ALLOC( int, Abc_NtkCiNum(pNtk) );
228  memset( pNtk->pModel, 0, sizeof(int) * Abc_NtkCiNum(pNtk) );
229  }
230  *ppNtk = pNtk;
231  return RetValue;
232 }
233 
234 /**Function*************************************************************
235 
236  Synopsis [Attempts to solve the miter using a number of tricks.]
237 
238  Description [Returns -1 if timed out; 0 if SAT; 1 if UNSAT.]
239 
240  SideEffects []
241 
242  SeeAlso []
243 
244 ***********************************************************************/
245 Abc_Ntk_t * Abc_NtkMiterFraig( Abc_Ntk_t * pNtk, int nBTLimit, ABC_INT64_T nInspLimit, int * pRetValue, int * pNumFails, ABC_INT64_T * pNumConfs, ABC_INT64_T * pNumInspects )
246 {
247  Abc_Ntk_t * pNtkNew;
248  Fraig_Params_t Params, * pParams = &Params;
249  Fraig_Man_t * pMan;
250  int nWords1, nWords2, nWordsMin, RetValue;
251  int * pModel;
252 
253  // to determine the number of simulation patterns
254  // use the following strategy
255  // at least 64 words (32 words random and 32 words dynamic)
256  // no more than 256M for one circuit (128M + 128M)
257  nWords1 = 32;
258  nWords2 = (1<<27) / (Abc_NtkNodeNum(pNtk) + Abc_NtkCiNum(pNtk));
259  nWordsMin = Abc_MinInt( nWords1, nWords2 );
260 
261  // set the FRAIGing parameters
262  Fraig_ParamsSetDefault( pParams );
263  pParams->nPatsRand = nWordsMin * 32; // the number of words of random simulation info
264  pParams->nPatsDyna = nWordsMin * 32; // the number of words of dynamic simulation info
265  pParams->nBTLimit = nBTLimit; // the max number of backtracks
266  pParams->nSeconds = -1; // the runtime limit
267  pParams->fTryProve = 0; // do not try to prove the final miter
268  pParams->fDoSparse = 1; // try proving sparse functions
269  pParams->fVerbose = 0;
270  pParams->nInspLimit = nInspLimit;
271 
272  // transform the target into a fraig
273  pMan = (Fraig_Man_t *)Abc_NtkToFraig( pNtk, pParams, 0, 0 );
274  Fraig_ManProveMiter( pMan );
275  RetValue = Fraig_ManCheckMiter( pMan );
276 
277  // create the network
278  pNtkNew = Abc_NtkFromFraig( pMan, pNtk );
279 
280  // save model
281  if ( RetValue == 0 )
282  {
283  pModel = Fraig_ManReadModel( pMan );
284  ABC_FREE( pNtkNew->pModel );
285  pNtkNew->pModel = ABC_ALLOC( int, Abc_NtkCiNum(pNtkNew) );
286  memcpy( pNtkNew->pModel, pModel, sizeof(int) * Abc_NtkCiNum(pNtkNew) );
287  }
288 
289  // save the return values
290  *pRetValue = RetValue;
291  *pNumFails = Fraig_ManReadSatFails( pMan );
292  *pNumConfs = Fraig_ManReadConflicts( pMan );
293  *pNumInspects = Fraig_ManReadInspects( pMan );
294 
295  // delete the fraig manager
296  Fraig_ManFree( pMan );
297  return pNtkNew;
298 }
299 
300 /**Function*************************************************************
301 
302  Synopsis [Attempts to solve the miter using a number of tricks.]
303 
304  Description [Returns -1 if timed out; 0 if SAT; 1 if UNSAT.]
305 
306  SideEffects []
307 
308  SeeAlso []
309 
310 ***********************************************************************/
311 void Abc_NtkMiterPrint( Abc_Ntk_t * pNtk, char * pString, abctime clk, int fVerbose )
312 {
313  if ( !fVerbose )
314  return;
315  printf( "Nodes = %7d. Levels = %4d. ", Abc_NtkNodeNum(pNtk),
316  Abc_NtkIsStrash(pNtk)? Abc_AigLevel(pNtk) : Abc_NtkLevel(pNtk) );
317  ABC_PRT( pString, Abc_Clock() - clk );
318 }
319 
320 
321 /**Function*************************************************************
322 
323  Synopsis [Implements resynthesis for CEC.]
324 
325  Description []
326 
327  SideEffects []
328 
329  SeeAlso []
330 
331 ***********************************************************************/
333 {
334  Abc_Ntk_t * pNtkTemp;
335  Abc_NtkRewrite( pNtk, 0, 0, 0, 0, 0 );
336  pNtk = Abc_NtkBalance( pNtkTemp = pNtk, 0, 0, 0 ); Abc_NtkDelete( pNtkTemp );
337  Abc_NtkRewrite( pNtk, 0, 0, 0, 0, 0 );
338  Abc_NtkRefactor( pNtk, 10, 16, 0, 0, 0, 0 );
339  return pNtk;
340 }
341 
342 
343 ////////////////////////////////////////////////////////////////////////
344 /// END OF FILE ///
345 ////////////////////////////////////////////////////////////////////////
346 
347 
349 
char * memset()
static int Abc_NtkIsStrash(Abc_Ntk_t *pNtk)
Definition: abc.h:251
typedefABC_NAMESPACE_HEADER_START struct Fraig_ManStruct_t_ Fraig_Man_t
INCLUDES ///.
Definition: fraig.h:40
static Abc_Ntk_t * Abc_NtkMiterFraig(Abc_Ntk_t *pNtk, int nBTLimit, ABC_INT64_T nInspLimit, int *pRetValue, int *pNumFails, ABC_INT64_T *pNumConfs, ABC_INT64_T *pNumInspects)
Definition: abcProve.c:245
ABC_NAMESPACE_IMPL_START int Abc_NtkRefactor(Abc_Ntk_t *pNtk, int nNodeSizeMax, int nConeSizeMax, int fUpdateLevel, int fUseZeros, int fUseDcs, int fVerbose)
DECLARATIONS ///.
Definition: abcRefactor.c:89
void Fraig_ManFree(Fraig_Man_t *pMan)
Definition: fraigMan.c:262
DdNode * Cudd_ReadLogicZero(DdManager *dd)
Definition: cuddAPI.c:1058
ABC_INT64_T nInspLimit
Definition: fraig.h:64
char * memcpy()
static int Abc_NtkCiNum(Abc_Ntk_t *pNtk)
Definition: abc.h:287
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int Fraig_ManReadInspects(Fraig_Man_t *p)
Definition: fraigApi.c:75
static abctime Abc_Clock()
Definition: abc_global.h:279
static Abc_Obj_t * Abc_ObjFanin0(Abc_Obj_t *pObj)
Definition: abc.h:373
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
int * pModel
Definition: abc.h:198
ABC_DLL Abc_Ntk_t * Abc_NtkCollapse(Abc_Ntk_t *pNtk, int fBddSizeMax, int fDualRail, int fReorder, int fVerbose)
FUNCTION DEFINITIONS ///.
Definition: abcCollapse.c:49
ABC_INT64_T nTotalInspectsMade
Definition: ivyFraig.c:137
void * pManFunc
Definition: abc.h:191
static int Abc_NtkNodeNum(Abc_Ntk_t *pNtk)
Definition: abc.h:293
static int Abc_MinInt(int a, int b)
Definition: abc_global.h:239
ABC_INT64_T nTotalBacktracksMade
Definition: ivyFraig.c:136
Abc_Ntk_t * Abc_NtkMiterRwsat(Abc_Ntk_t *pNtk)
Definition: abcProve.c:332
ABC_DLL int Abc_NtkRewrite(Abc_Ntk_t *pNtk, int fUpdateLevel, int fUseZeros, int fVerbose, int fVeryVerbose, int fPlaceEnable)
FUNCTION DEFINITIONS ///.
Definition: abcRewrite.c:61
ABC_DLL int Abc_AigLevel(Abc_Ntk_t *pNtk)
Definition: abcAig.c:292
static void Abc_NtkMiterPrint(Abc_Ntk_t *pNtk, char *pString, abctime clk, int fVerbose)
Definition: abcProve.c:311
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Fraig_ParamsSetDefault(Fraig_Params_t *pParams)
Definition: fraigMan.c:122
ABC_DLL int Abc_NtkMiterSat(Abc_Ntk_t *pNtk, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, int fVerbose, ABC_INT64_T *pNumConfs, ABC_INT64_T *pNumInspects)
FUNCTION DEFINITIONS ///.
Definition: abcSat.c:53
ABC_INT64_T nTotalInspectLimit
Definition: ivyFraig.c:134
static int Counter
float nRewritingLimitMulti
Definition: ivyFraig.c:123
Abc_Ntk_t * Abc_NtkFromFraig(Fraig_Man_t *pMan, Abc_Ntk_t *pNtk)
DECLARATIONS ///.
Definition: abcFraig.c:279
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
ABC_INT64_T nTotalBacktrackLimit
Definition: ivyFraig.c:133
ABC_DLL int Abc_NtkMiterIsConstant(Abc_Ntk_t *pMiter)
Definition: abcMiter.c:679
int Fraig_ManReadSatFails(Fraig_Man_t *p)
Definition: fraigApi.c:71
static Abc_Obj_t * Abc_NtkPo(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:316
static int Abc_NtkPoNum(Abc_Ntk_t *pNtk)
Definition: abc.h:286
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define ABC_PRT(a, t)
Definition: abc_global.h:220
ABC_DLL Abc_Ntk_t * Abc_NtkBalance(Abc_Ntk_t *pNtk, int fDuplicate, int fSelective, int fUpdateLevel)
FUNCTION DEFINITIONS ///.
Definition: abcBalance.c:53
int Abc_NtkMiterProve(Abc_Ntk_t **ppNtk, void *pPars)
FUNCTION DEFINITIONS ///.
Definition: abcProve.c:59
#define assert(ex)
Definition: util_old.h:213
void * pData
Definition: abc.h:145
ABC_INT64_T abctime
Definition: abc_global.h:278
ABC_DLL int Abc_NtkLevel(Abc_Ntk_t *pNtk)
Definition: abcDfs.c:1265
int * Fraig_ManReadModel(Fraig_Man_t *p)
Definition: fraigApi.c:63
int Fraig_ManCheckMiter(Fraig_Man_t *p)
Definition: fraigSat.c:130
int Fraig_ManReadConflicts(Fraig_Man_t *p)
Definition: fraigApi.c:73
ABC_DLL void * Abc_NtkToFraig(Abc_Ntk_t *pNtk, void *pParams, int fAllNodes, int fExdc)
Definition: abcFraig.c:103
void Fraig_ManProveMiter(Fraig_Man_t *p)
Definition: fraigSat.c:85