abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
satStore.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [satStore.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT solver.]
8 
9  Synopsis [Records the trace of SAT solving in the CNF form.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: satStore.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
18 
19 ***********************************************************************/
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 
26 #include "satStore.h"
27 
29 
30 
31 ////////////////////////////////////////////////////////////////////////
32 /// DECLARATIONS ///
33 ////////////////////////////////////////////////////////////////////////
34 
35 ////////////////////////////////////////////////////////////////////////
36 /// FUNCTION DEFINITIONS ///
37 ////////////////////////////////////////////////////////////////////////
38 
39 /**Function*************************************************************
40 
41  Synopsis [Fetches memory.]
42 
43  Description []
44 
45  SideEffects []
46 
47  SeeAlso []
48 
49 ***********************************************************************/
50 char * Sto_ManMemoryFetch( Sto_Man_t * p, int nBytes )
51 {
52  char * pMem;
53  if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
54  {
55  pMem = (char *)ABC_ALLOC( char, p->nChunkSize );
56  *(char **)pMem = p->pChunkLast;
57  p->pChunkLast = pMem;
58  p->nChunkUsed = sizeof(char *);
59  }
60  pMem = p->pChunkLast + p->nChunkUsed;
61  p->nChunkUsed += nBytes;
62  return pMem;
63 }
64 
65 /**Function*************************************************************
66 
67  Synopsis [Frees memory manager.]
68 
69  Description []
70 
71  SideEffects []
72 
73  SeeAlso []
74 
75 ***********************************************************************/
77 {
78  char * pMem, * pNext;
79  if ( p->pChunkLast == NULL )
80  return;
81  for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
82  ABC_FREE( pMem );
83  ABC_FREE( pMem );
84 }
85 
86 /**Function*************************************************************
87 
88  Synopsis [Reports memory usage in bytes.]
89 
90  Description []
91 
92  SideEffects []
93 
94  SeeAlso []
95 
96 ***********************************************************************/
98 {
99  int Total;
100  char * pMem, * pNext;
101  if ( p->pChunkLast == NULL )
102  return 0;
103  Total = p->nChunkUsed;
104  for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
105  Total += p->nChunkSize;
106  return Total;
107 }
108 
109 
110 /**Function*************************************************************
111 
112  Synopsis [Allocate proof manager.]
113 
114  Description []
115 
116  SideEffects []
117 
118  SeeAlso []
119 
120 ***********************************************************************/
122 {
123  Sto_Man_t * p;
124  // allocate the manager
125  p = (Sto_Man_t *)ABC_ALLOC( char, sizeof(Sto_Man_t) );
126  memset( p, 0, sizeof(Sto_Man_t) );
127  // memory management
128  p->nChunkSize = (1<<16); // use 64K chunks
129  return p;
130 }
131 
132 /**Function*************************************************************
133 
134  Synopsis [Deallocate proof manager.]
135 
136  Description []
137 
138  SideEffects []
139 
140  SeeAlso []
141 
142 ***********************************************************************/
144 {
145  Sto_ManMemoryStop( p );
146  ABC_FREE( p );
147 }
148 
149 /**Function*************************************************************
150 
151  Synopsis [Adds one clause to the manager.]
152 
153  Description []
154 
155  SideEffects []
156 
157  SeeAlso []
158 
159 ***********************************************************************/
160 int Sto_ManAddClause( Sto_Man_t * p, lit * pBeg, lit * pEnd )
161 {
162  Sto_Cls_t * pClause;
163  lit Lit, * i, * j;
164  int nSize;
165 
166  // process the literals
167  if ( pBeg < pEnd )
168  {
169  // insertion sort
170  for ( i = pBeg + 1; i < pEnd; i++ )
171  {
172  Lit = *i;
173  for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
174  *j = *(j-1);
175  *j = Lit;
176  }
177  // make sure there is no duplicated variables
178  for ( i = pBeg + 1; i < pEnd; i++ )
179  if ( lit_var(*(i-1)) == lit_var(*i) )
180  {
181  printf( "The clause contains two literals of the same variable: %d and %d.\n", *(i-1), *i );
182  return 0;
183  }
184  // check the largest var size
185  p->nVars = STO_MAX( p->nVars, lit_var(*(pEnd-1)) + 1 );
186  }
187 
188  // get memory for the clause
189  nSize = sizeof(Sto_Cls_t) + sizeof(lit) * (pEnd - pBeg);
190  nSize = (nSize / sizeof(char*) + ((nSize % sizeof(char*)) > 0)) * sizeof(char*); // added by Saurabh on Sep 3, 2009
191  pClause = (Sto_Cls_t *)Sto_ManMemoryFetch( p, nSize );
192  memset( pClause, 0, sizeof(Sto_Cls_t) );
193 
194  // assign the clause
195  pClause->Id = p->nClauses++;
196  pClause->nLits = pEnd - pBeg;
197  memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );
198 // assert( pClause->pLits[0] >= 0 );
199 
200  // add the clause to the list
201  if ( p->pHead == NULL )
202  p->pHead = pClause;
203  if ( p->pTail == NULL )
204  p->pTail = pClause;
205  else
206  {
207  p->pTail->pNext = pClause;
208  p->pTail = pClause;
209  }
210 
211  // add the empty clause
212  if ( pClause->nLits == 0 )
213  {
214  if ( p->pEmpty )
215  {
216  printf( "More than one empty clause!\n" );
217  return 0;
218  }
219  p->pEmpty = pClause;
220  }
221  return 1;
222 }
223 
224 /**Function*************************************************************
225 
226  Synopsis [Mark all clauses added so far as root clauses.]
227 
228  Description []
229 
230  SideEffects []
231 
232  SeeAlso []
233 
234 ***********************************************************************/
236 {
237  Sto_Cls_t * pClause;
238  p->nRoots = 0;
239  Sto_ManForEachClause( p, pClause )
240  {
241  pClause->fRoot = 1;
242  p->nRoots++;
243  }
244 }
245 
246 /**Function*************************************************************
247 
248  Synopsis [Mark all clauses added so far as clause of A.]
249 
250  Description []
251 
252  SideEffects []
253 
254  SeeAlso []
255 
256 ***********************************************************************/
258 {
259  Sto_Cls_t * pClause;
260  p->nClausesA = 0;
261  Sto_ManForEachClause( p, pClause )
262  {
263  pClause->fA = 1;
264  p->nClausesA++;
265  }
266 }
267 
268 /**Function*************************************************************
269 
270  Synopsis [Returns the literal of the last clause.]
271 
272  Description []
273 
274  SideEffects []
275 
276  SeeAlso []
277 
278 ***********************************************************************/
280 {
281  Sto_Cls_t * pClause, * pPrev;
282  pPrev = NULL;
283  Sto_ManForEachClause( p, pClause )
284  pPrev = pClause;
285  assert( pPrev != NULL );
286  assert( pPrev->fA == 1 );
287  assert( pPrev->nLits == 1 );
288  p->nClausesA--;
289  pPrev->fA = 0;
290  return pPrev->pLits[0] >> 1;
291 }
292 
293 
294 /**Function*************************************************************
295 
296  Synopsis [Writes the stored clauses into a file.]
297 
298  Description []
299 
300  SideEffects []
301 
302  SeeAlso []
303 
304 ***********************************************************************/
305 void Sto_ManDumpClauses( Sto_Man_t * p, char * pFileName )
306 {
307  FILE * pFile;
308  Sto_Cls_t * pClause;
309  int i;
310  // start the file
311  pFile = fopen( pFileName, "w" );
312  if ( pFile == NULL )
313  {
314  printf( "Error: Cannot open output file (%s).\n", pFileName );
315  return;
316  }
317  // write the data
318  fprintf( pFile, "p %d %d %d %d\n", p->nVars, p->nClauses, p->nRoots, p->nClausesA );
319  Sto_ManForEachClause( p, pClause )
320  {
321  for ( i = 0; i < (int)pClause->nLits; i++ )
322  fprintf( pFile, " %d", lit_print(pClause->pLits[i]) );
323  fprintf( pFile, " 0\n" );
324  }
325 // fprintf( pFile, " 0\n" );
326  fclose( pFile );
327 }
328 
329 /**Function*************************************************************
330 
331  Synopsis [Reads one literal from file.]
332 
333  Description []
334 
335  SideEffects []
336 
337  SeeAlso []
338 
339 ***********************************************************************/
340 int Sto_ManLoadNumber( FILE * pFile, int * pNumber )
341 {
342  int Char, Number = 0, Sign = 0;
343  // skip space-like chars
344  do {
345  Char = fgetc( pFile );
346  if ( Char == EOF )
347  return 0;
348  } while ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' );
349  // read the literal
350  while ( 1 )
351  {
352  // get the next character
353  Char = fgetc( pFile );
354  if ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' )
355  break;
356  // check that the char is a digit
357  if ( (Char < '0' || Char > '9') && Char != '-' )
358  {
359  printf( "Error: Wrong char (%c) in the input file.\n", Char );
360  return 0;
361  }
362  // check if this is a minus
363  if ( Char == '-' )
364  Sign = 1;
365  else
366  Number = 10 * Number + Char;
367  }
368  // return the number
369  *pNumber = Sign? -Number : Number;
370  return 1;
371 }
372 
373 /**Function*************************************************************
374 
375  Synopsis [Reads CNF from file.]
376 
377  Description []
378 
379  SideEffects []
380 
381  SeeAlso []
382 
383 ***********************************************************************/
384 Sto_Man_t * Sto_ManLoadClauses( char * pFileName )
385 {
386  FILE * pFile;
387  Sto_Man_t * p;
388  Sto_Cls_t * pClause;
389  char pBuffer[1024];
390  int nLits, nLitsAlloc, Counter, Number;
391  lit * pLits;
392 
393  // start the file
394  pFile = fopen( pFileName, "r" );
395  if ( pFile == NULL )
396  {
397  printf( "Error: Cannot open input file (%s).\n", pFileName );
398  return NULL;
399  }
400 
401  // create the manager
402  p = Sto_ManAlloc();
403 
404  // alloc the array of literals
405  nLitsAlloc = 1024;
406  pLits = (lit *)ABC_ALLOC( char, sizeof(lit) * nLitsAlloc );
407 
408  // read file header
409  p->nVars = p->nClauses = p->nRoots = p->nClausesA = 0;
410  while ( fgets( pBuffer, 1024, pFile ) )
411  {
412  if ( pBuffer[0] == 'c' )
413  continue;
414  if ( pBuffer[0] == 'p' )
415  {
416  sscanf( pBuffer + 1, "%d %d %d %d", &p->nVars, &p->nClauses, &p->nRoots, &p->nClausesA );
417  break;
418  }
419  printf( "Warning: Skipping line: \"%s\"\n", pBuffer );
420  }
421 
422  // read the clauses
423  nLits = 0;
424  while ( Sto_ManLoadNumber(pFile, &Number) )
425  {
426  if ( Number == 0 )
427  {
428  int RetValue;
429  RetValue = Sto_ManAddClause( p, pLits, pLits + nLits );
430  assert( RetValue );
431  nLits = 0;
432  continue;
433  }
434  if ( nLits == nLitsAlloc )
435  {
436  nLitsAlloc *= 2;
437  pLits = ABC_REALLOC( lit, pLits, nLitsAlloc );
438  }
439  pLits[ nLits++ ] = lit_read(Number);
440  }
441  if ( nLits > 0 )
442  printf( "Error: The last clause was not saved.\n" );
443 
444  // count clauses
445  Counter = 0;
446  Sto_ManForEachClause( p, pClause )
447  Counter++;
448 
449  // check the number of clauses
450  if ( p->nClauses != Counter )
451  {
452  printf( "Error: The actual number of clauses (%d) is different than declared (%d).\n", Counter, p->nClauses );
453  Sto_ManFree( p );
454  return NULL;
455  }
456 
457  ABC_FREE( pLits );
458  fclose( pFile );
459  return p;
460 }
461 
462 
463 ////////////////////////////////////////////////////////////////////////
464 /// END OF FILE ///
465 ////////////////////////////////////////////////////////////////////////
466 
467 
469 
char * memset()
int Sto_ManMemoryReport(Sto_Man_t *p)
Definition: satStore.c:97
static Llb_Mgr_t * p
Definition: llb3Image.c:950
unsigned fRoot
Definition: satStore.h:75
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
Sto_Cls_t * pEmpty
Definition: satStore.h:91
int nVars
Definition: satStore.h:85
static int lit_var(lit l)
Definition: satVec.h:145
lit pLits[0]
Definition: satStore.h:78
void Sto_ManMarkClausesA(Sto_Man_t *p)
Definition: satStore.c:257
char * memcpy()
int nClausesA
Definition: satStore.h:88
ABC_NAMESPACE_IMPL_START char * Sto_ManMemoryFetch(Sto_Man_t *p, int nBytes)
DECLARATIONS ///.
Definition: satStore.c:50
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nChunkSize
Definition: satStore.h:93
unsigned nLits
Definition: satStore.h:77
int lit
Definition: satVec.h:130
struct Sto_Cls_t_ Sto_Cls_t
BASIC TYPES ///.
Definition: satStore.h:67
Sto_Cls_t * pHead
Definition: satStore.h:89
void Sto_ManMarkRoots(Sto_Man_t *p)
Definition: satStore.c:235
int Sto_ManChangeLastClause(Sto_Man_t *p)
Definition: satStore.c:279
char * pChunkLast
Definition: satStore.h:95
static lit lit_read(int s)
Definition: satVec.h:148
char Char
Definition: bzlib_private.h:43
Sto_Man_t * Sto_ManAlloc()
MACRO DEFINITIONS ///.
Definition: satStore.c:121
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Sto_ManMemoryStop(Sto_Man_t *p)
Definition: satStore.c:76
static int Counter
int Id
Definition: satStore.h:73
void Sto_ManDumpClauses(Sto_Man_t *p, char *pFileName)
Definition: satStore.c:305
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Sto_Cls_t * pTail
Definition: satStore.h:90
Sto_Cls_t * pNext
Definition: satStore.h:70
int nChunkUsed
Definition: satStore.h:94
void Sto_ManFree(Sto_Man_t *p)
Definition: satStore.c:143
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define STO_MAX(a, b)
INCLUDES ///.
Definition: satStore.h:48
int Sto_ManAddClause(Sto_Man_t *p, lit *pBeg, lit *pEnd)
Definition: satStore.c:160
int nClauses
Definition: satStore.h:87
int Sto_ManLoadNumber(FILE *pFile, int *pNumber)
Definition: satStore.c:340
#define assert(ex)
Definition: util_old.h:213
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
int nRoots
Definition: satStore.h:86
unsigned fA
Definition: satStore.h:74
Sto_Man_t * Sto_ManLoadClauses(char *pFileName)
Definition: satStore.c:384
static int lit_print(lit l)
Definition: satVec.h:147