-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpropagate.c
309 lines (267 loc) · 11.3 KB
/
propagate.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
#include "propagate.h"
#include "assign.h"
#include "macros.h"
#include "message.h"
#include "ruler.h"
#include "utilities.h"
struct watch *ring_propagate (struct ring *ring, bool stop_at_conflict,
struct clause *ignore) {
assert (!ring->inconsistent);
assert (!ignore || !is_binary_pointer (ignore));
struct ring_trail *trail = &ring->trail;
struct watch *conflict = 0;
#ifdef METRICS
uint64_t *visits = ring->statistics.contexts[ring->context].visits;
#endif
signed char *values = ring->values;
uint64_t ticks = 0, propagations = 0;
while (trail->propagate != trail->end) {
if (stop_at_conflict && conflict)
break;
unsigned lit = *trail->propagate++;
LOG ("propagating %s", LOGLIT (lit));
propagations++;
unsigned not_lit = NOT (lit);
struct references *watches = &REFERENCES (not_lit);
// First traverse all irredundant (globally shared) binary clauses
// with this literal (negation of propagated one) if there are any.
// These binary clauses are encoded by a flat array of the 'other'
// literals in binary clauses for each literal (including this one)
// and only need to be allocated once and can thus be shared among all
// threads, since these irredundant binary clauses do not change
// during search (and are collected during cloning of rings).
unsigned *binaries = watches->binaries;
if (binaries) {
unsigned other, *p;
for (p = binaries; (other = *p) != INVALID; p++) {
struct watch *watch = tag_binary (false, other, not_lit);
signed char other_value = values[other];
if (other_value < 0) {
conflict = watch;
if (stop_at_conflict)
break;
} else if (!other_value) {
struct watch *reason = tag_binary (false, other, not_lit);
assign_with_reason (ring, other, reason);
ticks++;
}
}
ticks += cache_lines (p, binaries);
if (stop_at_conflict && conflict)
break;
}
// Then traverse (and update) the watch list of the literal.
struct watch **begin = watches->begin, **q = begin;
struct watch **end = watches->end, **p = begin;
ticks++;
while (p != end) {
assert (!stop_at_conflict || !conflict);
struct watch *watch = *q++ = *p++;
// This tagged 'watch' pointer is either a binary watch or an
// index to the corresponding watcher in the (ring/thread local)
// watcher stack. Tagging uses bit-stuffing to distinguish these
// two cases, through the least significand bit actually.
// If the clause is binary (least significand bit set) we find the
// other literal of the binary clause in the upper half of the
// pointer (together with another bit which encodes redundancy).
// The lower half of the pointer encodes the negation of the
// propagated literal.
// For larger (non-binary) clauses we have a similar situation and
// store in the upper half of the watch pointer word the blocking
// literal (conceptually an abitrary literal of the clause but
// supposed to be different from the negated propagaged literal).
// The other literal of binary clauses plays the same role.
// Now we check first, which often happens, whether this blocking
// literal of both binary and large clauses is actually already
// satisfied, in which case we just continue (and keep the watch).
unsigned blocking = other_pointer (watch);
assert (lit != blocking);
assert (not_lit != blocking);
signed char blocking_value = values[blocking];
if (blocking_value > 0)
continue;
if (is_binary_pointer (watch)) {
assert (lit_pointer (watch) == not_lit);
if (blocking_value < 0) {
conflict = watch;
if (stop_at_conflict)
break;
} else {
// Only learned and thus redundant clauses are kept as
// virtual binary clauses, where virtual means that
// they only exist in the watch list of this ring. They
// are thus really copied (if shared among rings).
assert (redundant_pointer (watch));
// The 'assign' function expects the literals in the
// opposite order as 'watch' and we have in essence to
// swap upper and lower half of the watch pointer word.
struct watch *reason = tag_binary (true, blocking, not_lit);
assert (reason != watch);
assign_with_reason (ring, blocking, reason);
ticks++;
}
} else {
// We now have to access the actual watcher data ...
unsigned idx = index_pointer (watch);
struct watcher *watcher = index_to_watcher (ring, idx);
ticks++; // ... and pay the prize.
// Satisfied (and vivified) but not removed clauses (actually
// watchers to the clause) might still be watched and should
// be ignored during propagation.
if (watcher->garbage) // This induces the 'tick' above.
continue;
// Ignore the vivified clause during vivification.
struct clause *clause = watcher->clause;
if (ignore && clause == ignore)
continue;
unsigned watcher_glue = watcher->glue;
unsigned clause_glue = clause->glue;
assert (clause_glue <= watcher_glue);
if (clause_glue < watcher_glue) {
watcher->glue = clause_glue;
LOGWATCH (watch, "updated from glue %u to", watcher_glue);
}
// The watchers need to precisely know the two watched
// literals, which might be different from the blocking
// literal. Otherwise unit propagation is not efficient
// (other invariants might also break).
// However as watchers are only accessed while traversing a
// watch list we always know during such a traversal already
// one of the two literals. Therefore we can simply use the
// XOR trick and only store the bit-wise difference (the
// 'XOR') between the two watched literals in the watcher
// instead of both literals and get the other watched literal
// during traversal by adding (with 'XOR') to that difference.
unsigned other = watcher->sum ^ not_lit;
signed char other_value;
if (other == blocking)
other_value = blocking_value;
else {
other_value = values[other];
if (other_value > 0) {
bool redundant = redundant_pointer (watch);
watch = tag_index (redundant, idx, other);
q[-1] = watch;
continue;
}
}
// Now the irredundant and virtual redundant binary clauses
// are handled and neither the blocking literal nor the other
// watched literal (if different) are assigned to true, and
// it is time to either find a non-false replacement watched
// literal, or determine that the clause is unit or
// conflicting (all replacement candidates are false).
unsigned replacement = INVALID;
signed char replacement_value = -1;
// The watchers can store literals of short clauses (currently
// three or four literals long) directly in the watcher data
// structure in order to avoid a second pointer dereference
// (not needed for sequential solvers) to the actual clause
// data (the latter being shared among threads). While
// initializing the watcher the size field is set to the
// actual size of the clause if it is short enough and to zero
// if it is too long (has more than four literals).
unsigned watcher_size = watcher->size;
if (watcher_size) {
unsigned *literals = watcher->aux;
unsigned *end_literals = literals + watcher_size;
for (unsigned *r = literals; r != end_literals; r++) {
replacement = *r;
if (replacement != not_lit && replacement != other) {
replacement_value = values[replacement];
if (replacement_value >= 0)
break;
}
}
} else {
// Now we pay the prize of accessing the actual clause too
// (one of the following 'clause->size' accesses).
// During propagation the 'tick' above for accessing
// watchers and this one form the hot-spots of the solver,
// due to irregular memory access (cache read misses).
// All this special treatment of binary clauses, the
// blocking literal and keeping short clause literals
// directly in the watcher data-structure are all only
// used to reduce the time spent in these two hot-spots.
// The following code matches the same standard
// propagation code in for instance CaDiCaL and Kissat.
ticks++;
#ifdef METRICS
assert (clause->size > 2);
if (clause->size >= SIZE_VISITS)
visits[0]++;
else
visits[clause->size]++;
#endif
unsigned *literals = clause->literals;
unsigned *end_literals = literals + clause->size;
assert (watcher->aux[0] <= clause->size);
unsigned *middle_literals = literals + watcher->aux[0];
unsigned *r = middle_literals;
while (r != end_literals) {
replacement = *r;
if (replacement != not_lit && replacement != other) {
replacement_value = values[replacement];
if (replacement_value >= 0)
break;
}
r++;
}
if (replacement_value < 0) {
r = literals;
while (r != middle_literals) {
replacement = *r;
if (replacement != not_lit && replacement != other) {
replacement_value = values[replacement];
if (replacement_value >= 0)
break;
}
r++;
}
}
watcher->aux[0] = r - literals;
}
if (replacement_value >= 0) {
watcher->sum = other ^ replacement;
LOGCLAUSE (clause, "unwatching %s in", LOGLIT (not_lit));
watch_literal (ring, replacement, other, watcher);
ticks++;
q--;
} else if (other_value) {
assert (other_value < 0);
conflict = watch;
if (stop_at_conflict)
break;
} else {
assign_with_reason (ring, other, watch);
ticks++;
}
}
}
while (p != end)
*q++ = *p++;
ticks += cache_lines (p, begin);
watches->end = q;
if (q == watches->begin)
RELEASE (*watches);
}
struct ring_statistics *statistics = &ring->statistics;
struct context *context = statistics->contexts + ring->context;
if (conflict) {
LOGWATCH (conflict, "conflicting");
context->conflicts++;
ring->import_after_propagation_and_conflict = true;
if (ring->context == SEARCH_CONTEXT && ring->randec) {
if (!--ring->randec)
very_verbose (ring, "last random decision conflict");
else if (ring->randec == 1)
very_verbose (ring, "one more random decision conflict to go");
else
very_verbose (ring, "%u more random decision conflicts to go",
ring->randec);
}
}
context->propagations += propagations;
context->ticks += ticks;
return conflict;
}