-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSatTracker.h
369 lines (340 loc) · 11.6 KB
/
SatTracker.h
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
#pragma once
#include "CNF.h"
#include "TrackingSet.h"
#include "Traversal.h"
#include <cstdint>
#include <memory>
#include <atomic>
#include <variant>
#include <execution>
template<typename TCounter> struct SatTracker {
static constexpr const uint32_t cParChunkSize = kCacheLineSize / sizeof(TCounter);
static constexpr const int64_t cSyncContention = 37; // 37 per CPU
std::vector<int64_t> vVars_;
std::unique_ptr<TCounter[]> nSat_;
std::unique_ptr<std::atomic_flag[]> syncs_;
Formula *pFormula_ = nullptr;
std::atomic<int64_t> totSat_ = 0;
void Init(Formula* pFormula) {
pFormula_ = pFormula;
syncs_.reset(new std::atomic_flag[cSyncContention * nSysCpus]);
vVars_.resize(pFormula->nVars_);
constexpr const uint32_t cParChunkSize = kCacheLineSize / sizeof(vVars_[0]);
#pragma omp parallel for schedule(static, cParChunkSize)
for(int64_t i=1; i<=pFormula_->nVars_; i++) {
vVars_[i-1] = i;
}
}
explicit SatTracker(Formula& formula)
{
Init(&formula);
nSat_.reset(new TCounter[pFormula_->nClauses_+1]);
}
SatTracker(const SatTracker& src) {
Init(src.pFormula_);
CopyFrom(src);
}
void CopyFrom(const SatTracker& src) {
if(nSat_ == nullptr || pFormula_ != src.pFormula_) {
nSat_.reset(new TCounter[src.pFormula_->nClauses_+1]);
}
pFormula_ = src.pFormula_;
totSat_.store(src.totSat_.load(std::memory_order_relaxed), std::memory_order_relaxed);
#pragma omp parallel for schedule(static, kRamPageBytes)
for(int64_t i=0; i<=pFormula_->nClauses_; i++) {
nSat_[i] = src.nSat_[i];
}
// Ignore vVars_ here: they're randomized each time anyway
}
SatTracker& operator=(const SatTracker& src) {
if(this != &src) {
CopyFrom(src);
}
return *this;
}
void Swap(SatTracker& fellow) {
std::swap(nSat_, fellow.nSat_);
std::swap(pFormula_, fellow.pFormula_);
TCounter t = totSat_.load(std::memory_order_relaxed);
totSat_.store(fellow.totSat_.load(std::memory_order_relaxed), std::memory_order_relaxed);
fellow.totSat_.store(t, std::memory_order_relaxed);
}
VCTrackingSet Populate(const BitVector& assignment) {
VCTrackingSet ans;
int64_t curTot = 0;
nSat_[0] = 1;
#pragma omp parallel for schedule(static, cParChunkSize) reduction(+:curTot)
for(int64_t i=1; i<=pFormula_->nClauses_; i++) {
nSat_[i] = 0;
for(const int64_t iVar : pFormula_->clause2var_.find(i)->second) {
assert(1 <= llabs(iVar) && llabs(iVar) <= pFormula_->nClauses_);
if( (iVar < 0 && !assignment[-iVar]) || (iVar > 0 && assignment[iVar]) ) {
nSat_[i]++;
}
}
if(nSat_[i]) {
curTot++;
} else {
// Unsatisfied clause
ans.Add(i);
}
}
totSat_ = curTot;
return ans;
}
bool Verify(const BitVector& assignment) {
int64_t curTot = 0;
if(nSat_[0] != 1) {
return false;
}
std::atomic<bool> ans = true;
#pragma omp parallel for
for(int64_t i=1; i<=pFormula_->nClauses_; i++) {
#pragma omp cancellation point for
int64_t curSat = 0;
for(const int64_t iVar : pFormula_->clause2var_.find(i)->second) {
assert(1 <= llabs(iVar) && llabs(iVar) <= pFormula_->nClauses_);
if( (iVar < 0 && !assignment[-iVar]) || (iVar > 0 && assignment[iVar]) ) {
curSat++;
}
}
if(curSat > 0) {
curTot++;
}
if(nSat_[i] != curSat) {
ans = false;
#pragma omp cancel for
}
}
if(totSat_ != curTot) {
ans = false;
}
return ans;
}
bool ReallyUnsat(const VCTrackingSet& unsatClauses) {
std::vector<int64_t> vUnsat = unsatClauses.ToVector();
std::atomic<bool> ans = true;
#pragma omp parallel for
for(int64_t i=0; i<int64_t(vUnsat.size()); i++) {
// It's important it's not interfering with Finally
#pragma omp cancellation point for
const int64_t iClause = vUnsat[i];
assert(1 <= iClause && iClause <= pFormula_->nClauses_);
Lock(iClause);
auto fUnlock = Finally([&] {
Unlock(iClause);
});
if(nSat_[iClause] != 0) {
ans = false;
#pragma omp cancel for
}
}
return ans;
}
void Lock(const int64_t iClause) {
std::atomic_flag& sync = syncs_[iClause % (cSyncContention * nSysCpus)];
while(sync.test_and_set(std::memory_order_acq_rel)) {
while (sync.test(std::memory_order_relaxed)); // keep it hot in cache
}
}
void Unlock(const int64_t iClause) {
std::atomic_flag& sync = syncs_[iClause % (cSyncContention * nSysCpus)];
sync.clear(std::memory_order_release);
}
// The sign of iVar must reflect the new value of the variable.
// Returns the change in satisfiability: positive - more satisfiable, negative - more unsatisfiable.
int64_t FlipVar(const int64_t iVar, VCTrackingSet* unsatClauses, VCTrackingSet* front) {
const std::vector<int64_t>& clauses = pFormula_->listVar2Clause_.find(llabs(iVar))->second;
int64_t ans = 0;
//#pragma omp parallel for reduction(+:ans) schedule(guided, cSyncContention)
for(int64_t i=0; i<int64_t(clauses.size()); i++) {
const int64_t iClause = clauses[i];
const int64_t aClause = llabs(iClause);
if(iClause * iVar > 0) {
Lock(aClause);
nSat_[aClause]++;
assert(nSat_[aClause] >= 1);
if(nSat_[aClause] == 1) { // just became satisfied
ans++;
if(unsatClauses != nullptr) {
unsatClauses->Remove(aClause);
}
if(front != nullptr && front != unsatClauses) {
front->Remove(aClause);
}
}
Unlock(aClause);
}
else {
Lock(aClause);
nSat_[aClause]--;
assert(nSat_[aClause] >= 0);
if(nSat_[aClause] == 0) { // just became unsatisfied
ans--;
if(unsatClauses != nullptr) {
unsatClauses->Add(aClause);
}
if(front != nullptr && front != unsatClauses) {
front->Add(aClause);
}
}
Unlock(aClause);
}
}
totSat_.fetch_add(ans, std::memory_order_relaxed);
return ans;
}
VCTrackingSet GetUnsat() const {
VCTrackingSet ans;
#pragma omp parallel for schedule(static, cParChunkSize)
for(int64_t i=1; i<=pFormula_->nClauses_; i++) {
if(nSat_[i] == 0) {
#pragma omp critical
ans.Add(i);
}
}
return ans;
}
VCTrackingSet GetUnsat(const SatTracker& oldSatTr, VCTrackingSet& newFront) const {
VCTrackingSet ans;
#pragma omp parallel for schedule(static, cParChunkSize)
for(int64_t i=1; i<=pFormula_->nClauses_; i++) {
if(nSat_.get()[i].load(std::memory_order_relaxed) == 0) {
if(oldSatTr.nSat_.get()[i].load(std::memory_order_relaxed) > 0) {
newFront.Add(i);
}
ans.Add(i);
}
}
return ans;
}
int64_t UnsatCount() const {
return pFormula_->nClauses_ - totSat_.load(std::memory_order_relaxed);
}
int64_t GradientDescend(const bool preferMove, Traversal& trav,
const VCTrackingSet* considerClauses, VCTrackingSet& unsatClauses, const VCTrackingSet& startFront,
VCTrackingSet& front, int64_t minUnsat, bool& moved, BitVector& next)
{
std::vector<int64_t> subsetVars, *pvVars = nullptr;
if(considerClauses == nullptr) {
pvVars = &vVars_;
}
else {
subsetVars = pFormula_->ClauseFrontToVars(*considerClauses, next);
pvVars = &subsetVars;
}
ParallelShuffle(pvVars->data(), pvVars->size());
VCTrackingSet revVars;
// TODO: flip a random number of consecutive vars in each step (i.e. new random count in each step)
for(int64_t k=0; k<int64_t(pvVars->size()); k++) {
const int64_t aVar = (*pvVars)[k];
assert(1 <= aVar && aVar <= pFormula_->nVars_);
const int64_t iVar = aVar * (next[aVar] ? 1 : -1);
revVars.Flip(aVar);
if( trav.IsSeenMove(startFront, revVars) ) {
revVars.Flip(aVar);
continue;
}
// TODO: instead of flipping directly the formula, shall we pass an arbitrary assignment as a parameter?
next.Flip(aVar);
FlipVar(-iVar, &unsatClauses, &front);
if(!trav.IsSeenAssignment(next)) {
trav.FoundMove(startFront, revVars, next, unsatClauses.Size());
int64_t newUnsat = UnsatCount();
if(newUnsat < minUnsat + (preferMove ? 1 : 0)) {
moved = true;
minUnsat = newUnsat;
continue;
}
}
// Flip back
next.Flip(aVar);
FlipVar(iVar, &unsatClauses, &front);
revVars.Flip(aVar);
}
return minUnsat;
}
int64_t ParallelGD(const bool preferMove, const int64_t varsAtOnce,
const std::vector<int64_t>& varFront, BitVector& next, Traversal& trav,
VCTrackingSet* unsatClauses, const VCTrackingSet& startClauseFront,
VCTrackingSet& revVars, int64_t minUnsat, bool& moved, int64_t level)
{
for(int64_t i=0; i<int64_t(varFront.size()); i+=varsAtOnce) {
std::vector<int64_t> selVars(varsAtOnce, 0);
const int64_t nVars = std::min<int64_t>(varsAtOnce, int64_t(varFront.size()) - i);
#pragma omp parallel for num_threads(nVars)
for(int64_t j=0; j<nVars; j++) {
int64_t aVar, iVar;
aVar = varFront[i+j];
assert(1 <= aVar && aVar <= pFormula_->nVars_);
iVar = aVar * (next[aVar] ? 1 : -1);
selVars[j] = iVar;
#pragma omp critical
revVars.Flip(aVar);
}
if( trav.IsSeenMove(startClauseFront, revVars) ) {
// Should be better sequential
for(int64_t j=0; j<nVars; j++) {
const int64_t iVar = selVars[j];
const int64_t aVar = llabs(iVar);
revVars.Flip(aVar);
}
continue;
}
VCTrackingSet newClauseFront;
#pragma omp parallel for num_threads(nVars)
for(int64_t j=0; j<nVars; j++) {
const int64_t iVar = selVars[j];
const int64_t aVar = llabs(iVar);
next.Flip(aVar);
FlipVar(aVar * (next[aVar] ? 1 : -1), unsatClauses, &newClauseFront);
}
if(!trav.IsSeenAssignment(next)) {
const int64_t newNUnsat = UnsatCount();
trav.FoundMove(startClauseFront, revVars, next, newNUnsat);
if(newNUnsat < minUnsat + (preferMove ? 1 : 0)) {
moved = true;
minUnsat = newNUnsat;
if(minUnsat == 0) {
break;
}
continue;
}
if(level > 0 && newClauseFront.Size() > 0) {
std::vector<int64_t> newVarFront = pFormula_->ClauseFrontToVars(newClauseFront, next);
ParallelShuffle(newVarFront.data(), newVarFront.size());
bool nextMoved = false;
const int64_t subNUnsat = ParallelGD(
preferMove, varsAtOnce, newVarFront, next, trav, unsatClauses, startClauseFront,
revVars, minUnsat, nextMoved, level-1
);
if(subNUnsat < minUnsat || (preferMove && nextMoved && subNUnsat == minUnsat)) {
minUnsat = subNUnsat;
moved = true;
if(minUnsat == 0) {
break;
}
continue;
}
}
}
#pragma omp parallel for num_threads(nVars)
for(int64_t j=0; j<nVars; j++) {
const int64_t iVar = selVars[j];
const int64_t aVar = llabs(iVar);
next.Flip(aVar);
FlipVar(aVar * (next[aVar] ? 1 : -1), unsatClauses, nullptr);
#pragma omp critical
revVars.Flip(aVar);
}
}
return minUnsat;
}
int64_t NextUnsatCap(const VCTrackingSet& unsatClauses, [[maybe_unused]] const int64_t nStartUnsat) const {
return std::max<int64_t>(
unsatClauses.Size() * 2,
DivUp(pFormula_->nVars_, nStartUnsat)
);
}
};
using DefaultSatTracker = SatTracker<int16_t>;