ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
cadical_veripbtracer.cpp
Go to the documentation of this file.
1#include "global.h"
2
3#include "internal.hpp"
4
6
7namespace CaDiCaL {
8
9/*------------------------------------------------------------------------*/
10
11VeripbTracer::VeripbTracer (Internal *i, File *f, bool b, bool a, bool c)
12 : internal (i), file (f), with_antecedents (a), checked_deletions (c),
13 num_clauses (0), size_clauses (0), clauses (0), last_hash (0),
14 last_id (0), last_clause (0)
15#ifndef CADICAL_QUIET
16 ,
17 added (0), deleted (0)
18#endif
19{
20 (void) internal;
21
22 // Initialize random number table for hash function.
23 //
24 Random random (42);
25 for (unsigned n = 0; n < num_nonces; n++) {
26 uint64_t nonce = random.next ();
27 if (!(nonce & 1))
28 nonce++;
29 CADICAL_assert (nonce), CADICAL_assert (nonce & 1);
30 nonces[n] = nonce;
31 }
32#ifndef CADICAL_NDEBUG
33 binary = b;
34#else
35 (void) b;
36#endif
37}
38
40 internal = i;
41 file->connect_internal (internal);
42 LOG ("VERIPB TRACER connected to internal");
43}
44
46 LOG ("VERIPB TRACER delete");
47 delete file;
48 for (size_t i = 0; i < size_clauses; i++)
49 for (HashId *c = clauses[i], *next; c; c = next)
50 next = c->next, delete_clause (c);
51 delete[] clauses;
52}
53
54/*------------------------------------------------------------------------*/
55
56void VeripbTracer::enlarge_clauses () {
57 CADICAL_assert (num_clauses == size_clauses);
58 const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1;
59 LOG ("VeriPB Tracer enlarging clauses from %" PRIu64 " to %" PRIu64,
60 (uint64_t) size_clauses, (uint64_t) new_size_clauses);
61 HashId **new_clauses;
62 new_clauses = new HashId *[new_size_clauses];
63 clear_n (new_clauses, new_size_clauses);
64 for (uint64_t i = 0; i < size_clauses; i++) {
65 for (HashId *c = clauses[i], *next; c; c = next) {
66 next = c->next;
67 const uint64_t h = reduce_hash (c->hash, new_size_clauses);
68 c->next = new_clauses[h];
69 new_clauses[h] = c;
70 }
71 }
72 delete[] clauses;
73 clauses = new_clauses;
74 size_clauses = new_size_clauses;
75}
76
77HashId *VeripbTracer::new_clause () {
78 HashId *res = new HashId ();
79 res->next = 0;
80 res->hash = last_hash;
81 res->id = last_id;
82 last_clause = res;
83 num_clauses++;
84 return res;
85}
86
87void VeripbTracer::delete_clause (HashId *c) {
89 num_clauses--;
90 delete c;
91}
92
93uint64_t VeripbTracer::reduce_hash (uint64_t hash, uint64_t size) {
94 CADICAL_assert (size > 0);
95 unsigned shift = 32;
96 uint64_t res = hash;
97 while ((((uint64_t) 1) << shift) > size) {
98 res ^= res >> shift;
99 shift >>= 1;
100 }
101 res &= size - 1;
102 CADICAL_assert (res < size);
103 return res;
104}
105
106uint64_t VeripbTracer::compute_hash (const int64_t id) {
107 CADICAL_assert (id > 0);
108 unsigned j = id % num_nonces; // Dont know if this is a good
109 uint64_t tmp = nonces[j] * (uint64_t) id; // hash funktion or even better
110 return last_hash = tmp; // than just using id.
111}
112
113bool VeripbTracer::find_and_delete (const int64_t id) {
114 if (!num_clauses)
115 return false;
116 /*
117 if (last_clause && last_clause->id == id) {
118 const uint64_t h = reduce_hash (last_clause->hash, size_clauses);
119 clauses[h] = last_clause->next;
120 delete last_clause;
121 return true;
122 }
123 */
124 HashId **res = 0, *c;
125 const uint64_t hash = compute_hash (id);
126 const uint64_t h = reduce_hash (hash, size_clauses);
127 for (res = clauses + h; (c = *res); res = &c->next) {
128 if (c->hash == hash && c->id == id) {
129 break;
130 }
131 if (!c->next)
132 return false;
133 }
134 if (!c)
135 return false;
136 CADICAL_assert (c && res);
137 *res = c->next;
138 delete_clause (c);
139 return true;
140}
141
142void VeripbTracer::insert () {
143 if (num_clauses == size_clauses)
144 enlarge_clauses ();
145 const uint64_t h = reduce_hash (compute_hash (last_id), size_clauses);
146 HashId *c = new_clause ();
147 c->next = clauses[h];
148 clauses[h] = c;
149}
150
151/*------------------------------------------------------------------------*/
152
153inline void VeripbTracer::put_binary_zero () {
154 CADICAL_assert (binary);
155 CADICAL_assert (file);
156 file->put ((unsigned char) 0);
157}
158
159inline void VeripbTracer::put_binary_lit (int lit) {
160 CADICAL_assert (binary);
161 CADICAL_assert (file);
162 CADICAL_assert (lit != INT_MIN);
163 unsigned x = 2 * abs (lit) + (lit < 0);
164 unsigned char ch;
165 while (x & ~0x7f) {
166 ch = (x & 0x7f) | 0x80;
167 file->put (ch);
168 x >>= 7;
169 }
170 ch = x;
171 file->put (ch);
172}
173
174inline void VeripbTracer::put_binary_id (int64_t id, bool can_be_negative) {
175 CADICAL_assert (binary);
176 CADICAL_assert (file);
177 uint64_t x = abs (id);
178 if (can_be_negative) {
179 x = 2 * x + (id < 0);
180 }
181 unsigned char ch;
182 while (x & ~0x7f) {
183 ch = (x & 0x7f) | 0x80;
184 file->put (ch);
185 x >>= 7;
186 }
187 ch = x;
188 file->put (ch);
189}
190
191/*------------------------------------------------------------------------*/
192
193void VeripbTracer::veripb_add_derived_clause (
194 int64_t id, bool redundant, const vector<int> &clause,
195 const vector<int64_t> &chain) {
196 file->put ("pol ");
197 bool first = true;
198 for (auto p = chain.rbegin (); p != chain.rend (); p++) {
199 auto cid = *p;
200 if (first) {
201 first = false;
202 file->put (cid);
203 } else {
204 file->put (' ');
205 file->put (cid);
206 file->put (" + s");
207 }
208 }
209 file->put ("\n");
210 file->put ("e ");
211 for (const auto &external_lit : clause) {
212 file->put ("1 ");
213 if (external_lit < 0)
214 file->put ('~');
215 file->put ('x');
216 file->put (abs (external_lit));
217 file->put (' ');
218 }
219 file->put (">= 1 ; ");
220 file->put (id);
221 file->put (" ;\n");
222 if (!redundant && checked_deletions) {
223 file->put ("core id ");
224 file->put (id);
225 file->put ("\n");
226 }
227}
228
229void VeripbTracer::veripb_add_derived_clause (int64_t id, bool redundant,
230 const vector<int> &clause) {
231 file->put ("rup ");
232 for (const auto &external_lit : clause) {
233 file->put ("1 ");
234 if (external_lit < 0)
235 file->put ('~');
236 file->put ('x');
237 file->put (abs (external_lit));
238 file->put (' ');
239 }
240 file->put (">= 1 ;\n");
241 if (!redundant && checked_deletions) {
242 file->put ("core id ");
243 file->put (id);
244 file->put ("\n");
245 }
246}
247
248void VeripbTracer::veripb_begin_proof (int64_t reserved_ids) {
249 file->put ("pseudo-Boolean proof version 2.0\n");
250 file->put ("f ");
251 file->put (reserved_ids);
252 file->put ("\n");
253}
254
255void VeripbTracer::veripb_delete_clause (int64_t id, bool redundant) {
256 if (!redundant && checked_deletions && find_and_delete (id))
257 return;
258 if (redundant || !checked_deletions)
259 file->put ("del id ");
260 else {
261 file->put ("delc ");
262 }
263 file->put (id);
264 file->put ("\n");
265}
266
267void VeripbTracer::veripb_report_status (bool unsat, int64_t conflict_id) {
268 file->put ("output NONE\n");
269 if (unsat) {
270 file->put ("conclusion UNSAT : ");
271 file->put (conflict_id);
272 file->put (" \n");
273 } else
274 file->put ("conclusion NONE\n");
275 file->put ("end pseudo-Boolean proof\n");
276}
277
278void VeripbTracer::veripb_strengthen (int64_t id) {
279 if (!checked_deletions)
280 return;
281 file->put ("core id ");
282 file->put (id);
283 file->put ("\n");
284}
285
286/*------------------------------------------------------------------------*/
287
288void VeripbTracer::begin_proof (int64_t id) {
289 if (file->closed ())
290 return;
291 LOG ("VERIPB TRACER tracing start of proof with %" PRId64
292 "original clauses",
293 id);
294 veripb_begin_proof (id);
295}
296
297void VeripbTracer::add_derived_clause (int64_t id, bool redundant,
298 const vector<int> &clause,
299 const vector<int64_t> &chain) {
300 if (file->closed ())
301 return;
302 LOG ("VERIPB TRACER tracing addition of derived clause[%" PRId64 "]", id);
303 if (with_antecedents)
304 veripb_add_derived_clause (id, redundant, clause, chain);
305 else
306 veripb_add_derived_clause (id, redundant, clause);
307#ifndef CADICAL_QUIET
308 added++;
309#endif
310}
311
312void VeripbTracer::delete_clause (int64_t id, bool redundant,
313 const vector<int> &) {
314 if (file->closed ())
315 return;
316 LOG ("VERIPB TRACER tracing deletion of clause[%" PRId64 "]", id);
317 veripb_delete_clause (id, redundant);
318#ifndef CADICAL_QUIET
319 deleted++;
320#endif
321}
322
323void VeripbTracer::report_status (int status, int64_t conflict_id) {
324 if (file->closed ())
325 return;
326#ifdef LOGGING
327 if (conflict_id)
328 LOG ("VERIPB TRACER tracing finalization of proof with empty "
329 "clause[%" PRId64 "]",
330 conflict_id);
331#endif
332 veripb_report_status (status == UNSATISFIABLE, conflict_id);
333}
334
335void VeripbTracer::weaken_minus (int64_t id, const vector<int> &) {
336 if (!checked_deletions)
337 return;
338 if (file->closed ())
339 return;
340 LOG ("VERIPB TRACER tracing weaken minus of clause[%" PRId64 "]", id);
341 last_id = id;
342 insert ();
343}
344
345void VeripbTracer::strengthen (int64_t id) {
346 if (file->closed ())
347 return;
348 LOG ("VERIPB TRACER tracing strengthen of clause[%" PRId64 "]", id);
349 veripb_strengthen (id);
350}
351
352/*------------------------------------------------------------------------*/
353
354bool VeripbTracer::closed () { return file->closed (); }
355
356#ifndef CADICAL_QUIET
357
358void VeripbTracer::print_statistics () {
359 // TODO complete
360 uint64_t bytes = file->bytes ();
361 uint64_t total = added + deleted;
362 MSG ("VeriPB %" PRId64 " added clauses %.2f%%", added,
363 percent (added, total));
364 MSG ("VeriPB %" PRId64 " deleted clauses %.2f%%", deleted,
365 percent (deleted, total));
366 MSG ("VeriPB %" PRId64 " bytes (%.2f MB)", bytes,
367 bytes / (double) (1 << 20));
368}
369
370#endif
371
372void VeripbTracer::close (bool print) {
374 file->close ();
375#ifndef CADICAL_QUIET
376 if (print) {
377 MSG ("VeriPB proof file '%s' closed", file->name ());
378 print_statistics ();
379 }
380#else
381 (void) print;
382#endif
383}
384
385void VeripbTracer::flush (bool print) {
387 file->flush ();
388#ifndef CADICAL_QUIET
389 if (print) {
390 MSG ("VeriPB proof file '%s' flushed", file->name ());
391 print_statistics ();
392 }
393#else
394 (void) print;
395#endif
396}
397
398} // namespace CaDiCaL
399
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define CADICAL_assert(ignore)
Definition global.h:14
#define CADICAL_QUIET
Definition global.h:8
#define LOG(...)
void connect_internal(Internal *i) override
void begin_proof(int64_t) override
VeripbTracer(Internal *, File *file, bool, bool, bool)
void add_derived_clause(int64_t, bool, const vector< int > &, const vector< int64_t > &) override
void report_status(int, int64_t) override
void weaken_minus(int64_t, const vector< int > &) override
void strengthen(int64_t) override
Cube * p
Definition exorList.c:222
#define MSG(...)
Definition message.hpp:49
@ UNSATISFIABLE
Definition cadical.hpp:30
void clear_n(T *base, size_t n)
Definition util.hpp:149
double percent(double a, double b)
Definition util.hpp:21
unsigned long long size
Definition giaNewBdd.h:39
struct clause_t clause
STRUCTURE DEFINITIONS ///.
Definition satClause.h:48
int lit
Definition satVec.h:130
Definition file.h:23
uint64_t bytes
Definition file.h:29
long random()