ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
satClause.h
Go to the documentation of this file.
1
20
21#ifndef ABC__sat__bsat__satMem_h
22#define ABC__sat__bsat__satMem_h
23
27
29
31
35
36//#define LEARNT_MAX_START_DEFAULT 0
37#define LEARNT_MAX_START_DEFAULT 10000
38#define LEARNT_MAX_INCRE_DEFAULT 1000
39#define LEARNT_MAX_RATIO_DEFAULT 50
40
44
45//=================================================================================================
46// Clause datatype + minor functions:
47
48typedef struct clause_t clause;
50{
51 unsigned lrn : 1;
52 unsigned mark : 1;
53 unsigned partA : 1;
54 unsigned lbd : 8;
55 unsigned size : 21;
57};
58
59// learned clauses have "hidden" literal (c->lits[c->size]) to store clause ID
60
61// data-structure for logging entries
62// memory is allocated in 2^nPageSize word-sized pages
63// the first 'word' of each page are stores the word limit
64
65// although clause memory pieces are aligned to 64-bit words
66// the integer clause handles are in terms of 32-bit unsigneds
67// allowing for the first bit to be used for labeling 2-lit clauses
68
69
70typedef struct Sat_Mem_t_ Sat_Mem_t;
72{
73 int nEntries[2]; // entry count
74 int BookMarkH[2]; // bookmarks for handles
75 int BookMarkE[2]; // bookmarks for entries
76 int iPage[2]; // current memory page
77 int nPageSize; // page log size in terms of ints
78 unsigned uPageMask; // page mask
79 unsigned uLearnedMask; // learned mask
80 int nPagesAlloc; // page count allocated
81 int ** pPages; // page pointers
82};
83
84static inline int Sat_MemLimit( int * p ) { return p[0]; }
85static inline int Sat_MemIncLimit( int * p, int nInts ) { return p[0] += nInts; }
86static inline void Sat_MemWriteLimit( int * p, int nInts ) { p[0] = nInts; }
87
88static inline int Sat_MemHandPage( Sat_Mem_t * p, cla h ) { return h >> p->nPageSize; }
89static inline int Sat_MemHandShift( Sat_Mem_t * p, cla h ) { return h & p->uPageMask; }
90
91//static inline int Sat_MemIntSize( int size, int lrn ) { return (size + 2 + lrn) & ~01; }
92static inline int Sat_MemIntSize( int size, int lrn ) { return 2*((size + 2 + lrn)/2); }
93static inline int Sat_MemClauseSize( clause * p ) { return Sat_MemIntSize(p->size, p->lrn); }
94static inline int Sat_MemClauseSize2( clause * p ) { return Sat_MemIntSize(p->size, 1); }
95
96//static inline clause * Sat_MemClause( Sat_Mem_t * p, int i, int k ) { assert(i <= p->iPage[i&1] && k <= Sat_MemLimit(p->pPages[i])); return (clause *)(p->pPages[i] + k ); }
97static inline clause * Sat_MemClause( Sat_Mem_t * p, int i, int k ) { assert( k ); return (clause *)(p->pPages[i] + k); }
98//static inline clause * Sat_MemClauseHand( Sat_Mem_t * p, cla h ) { assert(Sat_MemHandPage(p, h) <= p->iPage[(h & p->uLearnedMask) > 0]); assert(Sat_MemHandShift(p, h) >= 2 && Sat_MemHandShift(p, h) < (int)p->uLearnedMask); return Sat_MemClause( p, Sat_MemHandPage(p, h), Sat_MemHandShift(p, h) ); }
99static inline clause * Sat_MemClauseHand( Sat_Mem_t * p, cla h ) { return h ? Sat_MemClause( p, Sat_MemHandPage(p, h), Sat_MemHandShift(p, h) ) : NULL; }
100static inline int Sat_MemEntryNum( Sat_Mem_t * p, int lrn ) { return p->nEntries[lrn]; }
101
102static inline cla Sat_MemHand( Sat_Mem_t * p, int i, int k ) { return (i << p->nPageSize) | k; }
103static inline cla Sat_MemHandCurrent( Sat_Mem_t * p, int lrn ) { return (p->iPage[lrn] << p->nPageSize) | Sat_MemLimit( p->pPages[p->iPage[lrn]] ); }
104
105static inline int Sat_MemClauseUsed( Sat_Mem_t * p, cla h ) { return h < p->BookMarkH[(h & p->uLearnedMask) > 0]; }
106
107static inline double Sat_MemMemoryHand( Sat_Mem_t * p, cla h ) { return 1.0 * ((Sat_MemHandPage(p, h) + 2)/2 * (1 << (p->nPageSize+2)) + Sat_MemHandShift(p, h) * 4); }
108static inline double Sat_MemMemoryUsed( Sat_Mem_t * p, int lrn ) { return Sat_MemMemoryHand( p, Sat_MemHandCurrent(p, lrn) ); }
109static inline double Sat_MemMemoryAllUsed( Sat_Mem_t * p ) { return Sat_MemMemoryUsed( p, 0 ) + Sat_MemMemoryUsed( p, 1 ); }
110static inline double Sat_MemMemoryAll( Sat_Mem_t * p ) { return 1.0 * (p->iPage[0] + p->iPage[1] + 2) * (1 << (p->nPageSize+2)); }
111
112// p is memory storage
113// c is clause pointer
114// i is page number
115// k is page offset
116
117// print problem clauses NOT in proof mode
118#define Sat_MemForEachClause( p, c, i, k ) \
119 for ( i = 0; i <= p->iPage[0]; i += 2 ) \
120 for ( k = 2; k < Sat_MemLimit(p->pPages[i]) && ((c) = Sat_MemClause( p, i, k )); k += Sat_MemClauseSize(c) ) if ( i == 0 && k == 2 ) {} else
121
122// print problem clauses in proof mode
123#define Sat_MemForEachClause2( p, c, i, k ) \
124 for ( i = 0; i <= p->iPage[0]; i += 2 ) \
125 for ( k = 2; k < Sat_MemLimit(p->pPages[i]) && ((c) = Sat_MemClause( p, i, k )); k += Sat_MemClauseSize2(c) ) if ( i == 0 && k == 2 ) {} else
126
127#define Sat_MemForEachLearned( p, c, i, k ) \
128 for ( i = 1; i <= p->iPage[1]; i += 2 ) \
129 for ( k = 2; k < Sat_MemLimit(p->pPages[i]) && ((c) = Sat_MemClause( p, i, k )); k += Sat_MemClauseSize(c) )
130
134
138
139static inline int clause_from_lit( lit l ) { return l + l + 1; }
140static inline int clause_is_lit( cla h ) { return (h & 1); }
141static inline lit clause_read_lit( cla h ) { return (lit)(h >> 1); }
142
143static inline int clause_learnt_h( Sat_Mem_t * p, cla h ) { return (h & p->uLearnedMask) > 0; }
144static inline int clause_learnt( clause * c ) { return c->lrn; }
145static inline int clause_id( clause * c ) { return c->lits[c->size]; }
146static inline void clause_set_id( clause * c, int id ) { c->lits[c->size] = id; }
147static inline int clause_size( clause * c ) { return c->size; }
148static inline lit * clause_begin( clause * c ) { return c->lits; }
149static inline lit * clause_end( clause * c ) { return c->lits + c->size; }
150static inline void clause_print_( clause * c )
151{
152 int i;
153 printf( "{ " );
154 for ( i = 0; i < clause_size(c); i++ )
155 printf( "%d ", (clause_begin(c)[i] & 1)? -(clause_begin(c)[i] >> 1) : clause_begin(c)[i] >> 1 );
156 printf( "}\n" );
157}
158
162
174static inline int Sat_MemCountL( Sat_Mem_t * p )
175{
176 clause * c;
177 int i, k, Count = 0;
178 Sat_MemForEachLearned( p, c, i, k )
179 Count++;
180 return Count;
181}
182
194static inline void Sat_MemAlloc_( Sat_Mem_t * p, int nPageSize )
195{
196 assert( nPageSize > 8 && nPageSize < 32 );
197 memset( p, 0, sizeof(Sat_Mem_t) );
198 p->nPageSize = nPageSize;
199 p->uLearnedMask = (unsigned)(1 << nPageSize);
200 p->uPageMask = (unsigned)((1 << nPageSize) - 1);
201 p->nPagesAlloc = 256;
202 p->pPages = ABC_CALLOC( int *, p->nPagesAlloc );
203 p->pPages[0] = ABC_ALLOC( int, (int)(((word)1) << p->nPageSize) );
204 p->pPages[1] = ABC_ALLOC( int, (int)(((word)1) << p->nPageSize) );
205 p->iPage[0] = 0;
206 p->iPage[1] = 1;
207 Sat_MemWriteLimit( p->pPages[0], 2 );
208 Sat_MemWriteLimit( p->pPages[1], 2 );
209}
210static inline Sat_Mem_t * Sat_MemAlloc( int nPageSize )
211{
212 Sat_Mem_t * p;
213 p = ABC_CALLOC( Sat_Mem_t, 1 );
214 Sat_MemAlloc_( p, nPageSize );
215 return p;
216}
217
229static inline void Sat_MemRestart( Sat_Mem_t * p )
230{
231 p->nEntries[0] = 0;
232 p->nEntries[1] = 0;
233 p->iPage[0] = 0;
234 p->iPage[1] = 1;
235 Sat_MemWriteLimit( p->pPages[0], 2 );
236 Sat_MemWriteLimit( p->pPages[1], 2 );
237}
238
250static inline void Sat_MemBookMark( Sat_Mem_t * p )
251{
252 p->BookMarkE[0] = p->nEntries[0];
253 p->BookMarkE[1] = p->nEntries[1];
254 p->BookMarkH[0] = Sat_MemHandCurrent( p, 0 );
255 p->BookMarkH[1] = Sat_MemHandCurrent( p, 1 );
256}
257static inline void Sat_MemRollBack( Sat_Mem_t * p )
258{
259 p->nEntries[0] = p->BookMarkE[0];
260 p->nEntries[1] = p->BookMarkE[1];
261 p->iPage[0] = Sat_MemHandPage( p, p->BookMarkH[0] );
262 p->iPage[1] = Sat_MemHandPage( p, p->BookMarkH[1] );
263 Sat_MemWriteLimit( p->pPages[p->iPage[0]], Sat_MemHandShift( p, p->BookMarkH[0] ) );
264 Sat_MemWriteLimit( p->pPages[p->iPage[1]], Sat_MemHandShift( p, p->BookMarkH[1] ) );
265}
266
278static inline void Sat_MemFree_( Sat_Mem_t * p )
279{
280 int i;
281 for ( i = 0; i < p->nPagesAlloc; i++ )
282 ABC_FREE( p->pPages[i] );
283 ABC_FREE( p->pPages );
284}
285static inline void Sat_MemFree( Sat_Mem_t * p )
286{
287 Sat_MemFree_( p );
288 ABC_FREE( p );
289}
290
302static inline int Sat_MemAppend( Sat_Mem_t * p, int * pArray, int nSize, int lrn, int fPlus1 )
303{
304 clause * c;
305 int * pPage = p->pPages[p->iPage[lrn]];
306 int nInts = Sat_MemIntSize( nSize, lrn | fPlus1 );
307 assert( nInts + 3 < (1 << p->nPageSize) );
308 // need two extra at the begining of the page and one extra in the end
309 if ( Sat_MemLimit(pPage) + nInts + 2 >= (1 << p->nPageSize) )
310 {
311 p->iPage[lrn] += 2;
312 if ( p->iPage[lrn] >= p->nPagesAlloc )
313 {
314 p->pPages = ABC_REALLOC( int *, p->pPages, p->nPagesAlloc * 2 );
315 memset( p->pPages + p->nPagesAlloc, 0, sizeof(int *) * p->nPagesAlloc );
316 p->nPagesAlloc *= 2;
317 }
318 if ( p->pPages[p->iPage[lrn]] == NULL )
319 p->pPages[p->iPage[lrn]] = ABC_ALLOC( int, (int)(((word)1) << p->nPageSize) );
320 pPage = p->pPages[p->iPage[lrn]];
321 Sat_MemWriteLimit( pPage, 2 );
322 }
323 pPage[Sat_MemLimit(pPage)] = 0;
324 c = (clause *)(pPage + Sat_MemLimit(pPage));
325 c->size = nSize;
326 c->lrn = lrn;
327 if ( pArray )
328 memcpy( c->lits, pArray, sizeof(int) * nSize );
329 if ( lrn | fPlus1 )
330 c->lits[c->size] = p->nEntries[lrn];
331 p->nEntries[lrn]++;
332 Sat_MemIncLimit( pPage, nInts );
333 return Sat_MemHandCurrent(p, lrn) - nInts;
334}
335
347static inline void Sat_MemShrink( Sat_Mem_t * p, int h, int lrn )
348{
349 assert( clause_learnt_h(p, h) == lrn );
350 assert( h && h <= Sat_MemHandCurrent(p, lrn) );
351 p->iPage[lrn] = Sat_MemHandPage(p, h);
352 Sat_MemWriteLimit( p->pPages[p->iPage[lrn]], Sat_MemHandShift(p, h) );
353}
354
355
367static inline int Sat_MemCompactLearned( Sat_Mem_t * p, int fDoMove )
368{
369 clause * c, * cPivot = NULL;
370 int i, k, iNew = 1, kNew = 2, nInts, fStartLooking, Counter = 0;
371 int hLimit = Sat_MemHandCurrent(p, 1);
372 if ( hLimit == Sat_MemHand(p, 1, 2) )
373 return 0;
374 if ( fDoMove && p->BookMarkH[1] )
375 {
376 // move the pivot
377 assert( p->BookMarkH[1] >= Sat_MemHand(p, 1, 2) && p->BookMarkH[1] <= hLimit );
378 // get the pivot and remember it may be pointed offlimit
379 cPivot = Sat_MemClauseHand( p, p->BookMarkH[1] );
380 if ( p->BookMarkH[1] < hLimit && !cPivot->mark )
381 {
382 p->BookMarkH[1] = cPivot->lits[cPivot->size];
383 cPivot = NULL;
384 }
385 // else find the next used clause after cPivot
386 }
387 // iterate through the learned clauses
388 fStartLooking = 0;
389 Sat_MemForEachLearned( p, c, i, k )
390 {
391 assert( c->lrn );
392 // skip marked entry
393 if ( c->mark )
394 {
395 // if pivot is a marked clause, start looking for the next non-marked one
396 if ( cPivot && cPivot == c )
397 {
398 fStartLooking = 1;
399 cPivot = NULL;
400 }
401 continue;
402 }
403 // if we started looking before, we found it!
404 if ( fStartLooking )
405 {
406 fStartLooking = 0;
407 p->BookMarkH[1] = c->lits[c->size];
408 }
409 // compute entry size
410 nInts = Sat_MemClauseSize(c);
411 assert( !(nInts & 1) );
412 // check if we need to scroll to the next page
413 if ( kNew + nInts >= (1 << p->nPageSize) )
414 {
415 // set the limit of the current page
416 if ( fDoMove )
417 Sat_MemWriteLimit( p->pPages[iNew], kNew );
418 // move writing position to the new page
419 iNew += 2;
420 kNew = 2;
421 }
422 if ( fDoMove )
423 {
424 // make sure the result is the same as previous dry run
425 assert( c->lits[c->size] == Sat_MemHand(p, iNew, kNew) );
426 // only copy the clause if it has changed
427 if ( i != iNew || k != kNew )
428 {
429 memmove( p->pPages[iNew] + kNew, c, sizeof(int) * nInts );
430// c = Sat_MemClause( p, iNew, kNew ); // assersions do not hold during dry run
431 c = (clause *)(p->pPages[iNew] + kNew);
432 assert( nInts == Sat_MemClauseSize(c) );
433 }
434 // set the new ID value
435 c->lits[c->size] = Counter;
436 }
437 else // remember the address of the clause in the new location
438 c->lits[c->size] = Sat_MemHand(p, iNew, kNew);
439 // update writing position
440 kNew += nInts;
441 assert( iNew <= i && kNew < (1 << p->nPageSize) );
442 // update counter
443 Counter++;
444 }
445 if ( fDoMove )
446 {
447 // update the counter
448 p->nEntries[1] = Counter;
449 // update the page count
450 p->iPage[1] = iNew;
451 // set the limit of the last page
452 Sat_MemWriteLimit( p->pPages[iNew], kNew );
453 // check if the pivot need to be updated
454 if ( p->BookMarkH[1] )
455 {
456 if ( cPivot )
457 {
458 p->BookMarkH[1] = Sat_MemHandCurrent(p, 1);
459 p->BookMarkE[1] = p->nEntries[1];
460 }
461 else
462 p->BookMarkE[1] = clause_id(Sat_MemClauseHand( p, p->BookMarkH[1] ));
463 }
464
465 }
466 return Counter;
467}
468
469
471
472#endif
473
477
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Cube * p
Definition exorList.c:222
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
unsigned long long size
Definition giaNewBdd.h:39
#define Sat_MemForEachLearned(p, c, i, k)
Definition satClause.h:127
struct Sat_Mem_t_ Sat_Mem_t
Definition satClause.h:70
int lit
Definition satVec.h:130
int cla
Definition satVec.h:131
int ** pPages
Definition satClause.h:81
int nEntries[2]
Definition satClause.h:73
int iPage[2]
Definition satClause.h:76
int BookMarkH[2]
Definition satClause.h:74
int nPageSize
Definition satClause.h:77
int BookMarkE[2]
Definition satClause.h:75
unsigned uPageMask
Definition satClause.h:78
int nPagesAlloc
Definition satClause.h:80
unsigned uLearnedMask
Definition satClause.h:79
unsigned partA
Definition satClause.h:53
unsigned lrn
Definition satClause.h:51
unsigned mark
Definition satClause.h:52
unsigned size
Definition satClause.h:55
unsigned lbd
Definition satClause.h:54
lit lits[0]
Definition satClause.h:56
unsigned lits[3]
Definition clause.h:39
unsigned size
Definition clause.h:37
#define assert(ex)
Definition util_old.h:213
char * memcpy()
char * memset()
char * memmove()