forked from arminbiere/gimsatul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.c
448 lines (410 loc) · 13.3 KB
/
import.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "import.h"
#include "assign.h"
#include "backtrack.h"
#include "bump.h"
#include "message.h"
#include "propagate.h"
#include "random.h"
#include "ring.h"
#include "ruler.h"
#include "trace.h"
#include "utilities.h"
static bool import_units (struct ring *ring) {
assert (ring->pool);
struct ruler *ruler = ring->ruler;
#ifndef NFASTPATH
if (ring->ruler_units == ruler->units.end)
return false;
#endif
struct variable *variables = ring->variables;
signed char *values = ring->values;
unsigned imported = 0;
if (pthread_mutex_lock (&ruler->locks.units))
fatal_error ("failed to acquire unit lock");
while (ring->ruler_units != ruler->units.end) {
unsigned unit = *ring->ruler_units++;
LOG ("trying to import unit %s", LOGLIT (unit));
signed char value = values[unit];
unsigned unit_idx = IDX (unit);
struct variable *v = variables + unit_idx;
if (value && v->level) {
backtrack (ring, v->level - 1);
assert (!values[unit]);
value = 0;
}
if (value > 0) {
assert (!v->level);
continue;
}
very_verbose (ring, "importing unit %d",
unmap_and_export_literal (ruler->unmap, unit));
INC_UNIT_CLAUSE_STATISTICS (imported);
assert (imported < UINT_MAX);
imported++;
if (value < 0) {
assert (!v->level);
set_inconsistent (ring, "imported falsified unit");
imported = INVALID;
break;
}
assign_ring_unit (ring, unit);
}
if (pthread_mutex_unlock (&ruler->locks.units))
fatal_error ("failed to release unit lock");
if (ring->inconsistent)
return true;
if (!imported)
return false;
ring->iterating = -1;
return true;
}
static void really_import_binary_clause (struct ring *ring, unsigned lit,
unsigned other) {
(void) new_local_binary_clause (ring, true, lit, other);
trace_add_binary (&ring->trace, lit, other);
INC_BINARY_CLAUSE_STATISTICS (imported);
}
static void force_to_repropagate (struct ring *ring, unsigned lit) {
LOG ("forcing to repropagate %s", LOGLIT (lit));
assert (ring->values[lit] < 0);
unsigned idx = IDX (lit);
size_t pos = ring->trail.pos[idx];
assert (pos < SIZE (ring->trail));
unsigned *propagate = ring->trail.begin + pos;
assert (propagate < ring->trail.end);
assert (*propagate == NOT (lit));
assert (propagate < ring->trail.propagate);
ring->trail.propagate = propagate;
LOG ("setting end of trail to %zu", pos);
if (!ring->level)
ring->iterating = -1;
}
static bool subsumed_binary (struct ring *ring, unsigned lit,
unsigned other) {
if (!ring->options.subsume_imported)
return false;
ring->statistics.subsumed.binary.checked++;
if (SIZE (REFERENCES (lit)) > SIZE (REFERENCES (other)))
SWAP (unsigned, lit, other);
bool res = false;
for (all_watches (watch, REFERENCES (lit)))
if (is_binary_pointer (watch) && other_pointer (watch) == other) {
res = true;
ring->statistics.subsumed.binary.succeeded++;
break;
}
return res;
}
static bool import_binary (struct ring *ring, struct clause *clause) {
assert (is_binary_pointer (clause));
assert (redundant_pointer (clause));
signed char *values = ring->values;
unsigned lit = lit_pointer (clause);
signed char lit_value = values[lit];
unsigned lit_level = 0;
if (lit_value) {
lit_level = VAR (lit)->level;
if (lit_value > 0 && !lit_level)
return false;
}
unsigned other = other_pointer (clause);
signed char other_value = values[other];
unsigned other_level = 0;
if (other_value) {
other_level = VAR (other)->level;
if (other_value > 0 && !other_level)
return false;
}
if (lit_value < other_value ||
(lit_value == other_value &&
((lit_value > 0 && lit_level > other_level) ||
(lit_value < 0 && lit_level < other_level)))) {
SWAP (unsigned, lit, other);
SWAP (signed char, lit_value, other_value);
SWAP (unsigned, lit_level, other_level);
}
LOG ("imported binary clause first watch %s second %s", LOGLIT (lit),
LOGLIT (other));
#define SUBSUME_BINARY(LIT, OTHER) \
do { \
if (subsumed_binary (ring, LIT, OTHER)) { \
LOGBINARY (true, LIT, OTHER, "subsumed imported"); \
return false; \
} \
} while (0)
assert (lit_value >= other_value);
if (other_value >= 0) {
SUBSUME_BINARY (lit, other);
LOGBINARY (true, lit, other, "importing (no propagation)");
really_import_binary_clause (ring, lit, other);
return false;
}
if (lit_value > 0 && lit_level <= other_level) {
SUBSUME_BINARY (lit, other);
LOGBINARY (true, lit, other, "importing (no propagation)");
really_import_binary_clause (ring, lit, other);
if (lit_level < other_level && ring->context == PROBING_CONTEXT) {
ring->statistics.diverged++;
return true;
}
return false;
}
unsigned *pos = ring->trail.pos;
unsigned lit_pos = pos[IDX (lit)];
unsigned other_pos = pos[IDX (other)];
if (lit_value < 0 && lit_level == other_level && lit_pos > other_pos) {
assert (lit_level >= other_level);
SUBSUME_BINARY (lit, other);
LOGBINARY (true, lit, other, "importing (repropagate first watch %s)",
LOGLIT (lit));
force_to_repropagate (ring, lit);
really_import_binary_clause (ring, lit, other);
return true;
}
assert (!lit_value || other_level < lit_level ||
(other_level == lit_level && other_pos > lit_pos));
SUBSUME_BINARY (lit, other);
LOGBINARY (true, lit, other, "importing (repropagate second watch %s))",
LOGLIT (other));
force_to_repropagate (ring, other);
really_import_binary_clause (ring, lit, other);
return true;
}
static bool subsumed_large_clause (struct ring *ring,
struct clause *clause) {
if (!ring->options.subsume_imported)
return false;
ring->statistics.subsumed.large.checked++;
signed char *values = ring->values;
struct variable *variables = ring->variables;
signed char *marks = ring->marks;
unsigned max_occurrences_lit = INVALID;
size_t max_occurrences = 0;
for (all_literals_in_clause (lit, clause)) {
signed char value = values[lit];
unsigned idx = IDX (lit);
struct variable *v = variables + idx;
if (value < 0 && !v->level)
continue;
assert (!value || v->level);
marks[lit] = 1;
if (value < 0)
continue;
struct references *watches = &REFERENCES (lit);
size_t tmp_occurrences = SIZE (*watches);
if (tmp_occurrences <= max_occurrences)
continue;
max_occurrences = tmp_occurrences;
max_occurrences_lit = lit;
}
bool res = false;
for (all_literals_in_clause (lit, clause)) {
if (lit == max_occurrences_lit)
continue;
signed char lit_value = values[lit];
if (lit_value < 0)
continue;
struct references *watches = &REFERENCES (lit);
for (all_watches (watch, *watches)) {
if (!redundant_pointer (watch))
continue;
unsigned blocking = other_pointer (watch);
assert (lit != blocking);
signed char blocking_mark = marks[blocking];
if (!blocking_mark) {
signed char blocking_value = values[blocking];
if (blocking_value >= 0)
continue;
unsigned blocking_idx = IDX (blocking);
struct variable *v = variables + blocking_idx;
if (v->level)
continue;
}
if (is_binary_pointer (watch)) {
res = true;
LOGWATCH (watch, "subsuming");
break;
}
struct watcher *watcher = get_watcher (ring, watch);
res = true;
for (all_watcher_literals (other, watcher)) {
if (other == lit)
continue;
if (other == blocking)
continue;
signed char other_mark = marks[other];
if (other_mark)
continue;
signed char other_value = values[other];
if (other_value < 0) {
unsigned other_idx = IDX (other);
struct variable *other_variable = variables + other_idx;
if (!other_variable->level)
continue;
}
res = false;
break;
}
if (!res)
continue;
LOGWATCH (watch, "subsuming");
break;
}
if (res)
break;
}
for (all_literals_in_clause (lit, clause))
marks[lit] = 0;
if (res)
ring->statistics.subsumed.large.succeeded++;
return res;
}
static void really_import_large_clause (struct ring *ring,
struct clause *clause,
unsigned first, unsigned second) {
watch_literals_in_large_clause (ring, clause, first, second);
assert (clause->redundant);
INC_LARGE_CLAUSE_STATISTICS (imported, clause->glue, clause->size);
}
static unsigned find_literal_to_watch (struct ring *ring,
struct clause *clause,
unsigned ignore,
signed char *res_value_ptr,
unsigned *res_level_ptr) {
signed char *values = ring->values;
unsigned res = INVALID, res_level = 0;
signed char res_value = 0;
for (all_literals_in_clause (lit, clause)) {
if (lit == ignore)
continue;
signed char lit_value = values[lit];
unsigned lit_level = VAR (lit)->level;
if (res != INVALID) {
if (lit_value < 0) {
if (res_value >= 0)
continue;
if (lit_level <= res_level)
continue;
} else if (lit_value > 0) {
if (res_value > 0 && lit_level >= res_level)
continue;
} else {
assert (!lit_value);
if (res_value >= 0)
continue;
}
}
res = lit;
res_level = lit_level;
res_value = lit_value;
}
*res_value_ptr = res_value;
*res_level_ptr = res_level;
return res;
}
static bool import_large_clause (struct ring *ring, struct clause *clause) {
signed char *values = ring->values;
for (all_literals_in_clause (lit, clause)) {
if (values[lit] <= 0)
continue;
if (VAR (lit)->level)
continue;
LOGCLAUSE (clause, "not importing %s satisfied", LOGLIT (lit));
dereference_clause (ring, clause);
return false;
}
unsigned lit_level = 0;
signed char lit_value = 0;
unsigned lit =
find_literal_to_watch (ring, clause, INVALID, &lit_value, &lit_level);
unsigned other_level = 0;
signed char other_value = 0;
unsigned other =
find_literal_to_watch (ring, clause, lit, &other_value, &other_level);
LOGCLAUSE (clause, "imported first watch %s second %s in", LOGLIT (lit),
LOGLIT (other));
#define SUBSUME_LARGE_CLAUSE(CLAUSE) \
do { \
if (subsumed_large_clause (ring, clause)) { \
dereference_clause (ring, clause); \
return false; \
} \
} while (0)
assert (lit_value >= other_value);
if (other_value >= 0) {
SUBSUME_LARGE_CLAUSE (clause);
LOGCLAUSE (clause, "importing (no propagation)");
really_import_large_clause (ring, clause, lit, other);
return false;
}
if (lit_value > 0 && lit_level <= other_level) {
SUBSUME_LARGE_CLAUSE (clause);
LOGCLAUSE (clause, "importing (no propagation)");
really_import_large_clause (ring, clause, lit, other);
if (lit_level < other_level && ring->context == PROBING_CONTEXT) {
ring->statistics.diverged++;
return true;
}
return false;
}
unsigned *pos = ring->trail.pos;
unsigned lit_pos = pos[IDX (lit)];
unsigned other_pos = pos[IDX (other)];
if (lit_value < 0 && lit_level == other_level && lit_pos > other_pos) {
assert (lit_level >= other_level);
SUBSUME_LARGE_CLAUSE (clause);
LOGCLAUSE (clause, "importing (repropagate first watch %s)",
LOGLIT (lit));
force_to_repropagate (ring, lit);
really_import_large_clause (ring, clause, lit, other);
return true;
}
assert (!lit_value || other_level < lit_level ||
(other_level == lit_level && other_pos > lit_pos));
SUBSUME_LARGE_CLAUSE (clause);
LOGCLAUSE (clause, "importing (repropagate second watch %s)",
LOGLIT (other));
force_to_repropagate (ring, other);
really_import_large_clause (ring, clause, lit, other);
return true;
}
bool import_shared (struct ring *ring) {
if (!ring->pool)
return false;
if (import_units (ring))
return true;
if (ring->options.limit_import_rate) {
if (!ring->import_after_propagation_and_conflict)
return false;
ring->import_after_propagation_and_conflict = false;
}
struct ring *src = random_other_ring (ring);
struct pool *pool = src->pool + ring->id;
struct bucket *start = pool->bucket;
struct bucket *end = start + SIZE_POOL;
struct bucket *best = 0;
uint64_t best_redundancy = MAX_REDUNDANCY;
for (struct bucket *b = start; b != end; b++) {
if (!b->shared)
continue;
uint64_t redundancy = b->redundancy;
if (redundancy >= best_redundancy)
continue;
best_redundancy = redundancy;
best = b;
}
struct clause *clause = 0;
if (best) {
LOG ("import from ring %u bucket %zu with redundancy [%u:%u]", src->id,
best - start, LOG_REDUNDANCY (best_redundancy));
atomic_uintptr_t *p = &best->shared;
clause = (struct clause *) atomic_exchange (p, 0);
assert (clause);
} else {
LOG ("import from ring %u failed (nothing to import)", src->id);
return false;
}
if (is_binary_pointer (clause))
return import_binary (ring, clause);
return import_large_clause (ring, clause);
}