-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.c
158 lines (148 loc) · 3.92 KB
/
search.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
#include "search.h"
#include "analyze.h"
#include "backtrack.h"
#include "decide.h"
#include "export.h"
#include "import.h"
#include "message.h"
#include "mode.h"
#include "probe.h"
#include "propagate.h"
#include "reduce.h"
#include "rephase.h"
#include "report.h"
#include "restart.h"
#include "ruler.h"
#include "simplify.h"
#include "walk.h"
#include <assert.h>
#include <inttypes.h>
static bool iterating (struct ring *ring) {
struct ring_units *units = &ring->ring_units;
return units->iterate < units->end;
}
void iterate (struct ring *ring) {
struct ring_units *units = &ring->ring_units;
if (units->iterate < units->end) {
#ifndef QUIET
size_t new_units = units->end - units->iterate;
very_verbose (ring, "iterating %zu units", new_units);
int report_level = (ring->iterating <= 0);
verbose_report (ring, 'i', report_level);
#endif
export_units (ring);
units->iterate = units->end;
}
ring->iterating = 0;
}
bool backtrack_propagate_iterate (struct ring *ring) {
assert (!ring->inconsistent);
if (ring->level)
backtrack (ring, 0);
ring->trail.propagate = ring->trail.begin;
if (ring_propagate (ring, true, 0)) {
set_inconsistent (ring,
"failed propagation after root-level backtracking");
return false;
}
iterate (ring);
assert (!ring->inconsistent);
return true;
}
static void start_search (struct ring *ring) {
ring->stable = !ring->options.focus_initially;
#ifndef QUIET
double t = START (ring, search);
ring->last.mode.time = t;
#endif
if (ring->stable) {
report (ring, '[');
START (ring, stable);
} else {
report (ring, '{');
START (ring, focus);
}
}
static void stop_search (struct ring *ring, int res) {
if (ring->stable) {
report (ring, ']');
STOP (ring, stable);
} else {
report (ring, '}');
STOP (ring, focus);
}
if (res == 10)
report (ring, '1');
else if (res == 20)
report (ring, '0');
else
report (ring, '?');
STOP (ring, search);
}
static bool conflict_limit_hit (struct ring *ring) {
long limit = ring->limits.conflicts;
if (limit < 0)
return false;
uint64_t conflicts = SEARCH_CONFLICTS;
if (conflicts < (unsigned long) limit)
return false;
verbose (ring, "conflict limit %ld hit at %" PRIu64 " conflicts", limit,
conflicts);
set_terminate (ring->ruler, ring);
return true;
}
bool terminate_ring (struct ring *ring) {
struct ruler *ruler = ring->ruler;
#ifdef NFASTPATH
if (pthread_mutex_lock (&ruler->locks.terminate))
fatal_error ("failed to acquire terminate lock");
#endif
bool res = ruler->terminate;
#ifdef NFASTPATH
if (pthread_mutex_unlock (&ruler->locks.terminate))
fatal_error ("failed to release terminate lock");
#endif
return res;
}
static bool walk_initially (struct ring *ring) {
return !ring->statistics.walked && ring->ruler->options.walk_initially;
}
int search (struct ring *ring) {
start_search (ring);
int res = ring->inconsistent ? 20 : 0;
while (!res) {
struct watch *conflict = ring_propagate (ring, true, 0);
if (conflict) {
if (!analyze (ring, conflict))
res = 20;
} else if (!ring->unassigned)
set_satisfied (ring), res = 10;
else if (iterating (ring))
iterate (ring);
else if (terminate_ring (ring))
break;
else if (walk_initially (ring))
local_search (ring);
else if (conflict_limit_hit (ring))
break;
else if (reducing (ring))
reduce (ring);
else if (restarting (ring))
restart (ring);
else if (switching_mode (ring))
switch_mode (ring);
else if (rephasing (ring))
rephase (ring);
else if (probing (ring))
res = probe (ring);
else if (simplifying (ring))
res = simplify_ring (ring);
else if (!import_shared (ring))
decide (ring);
else if (ring->inconsistent)
res = 20;
}
stop_search (ring, res);
assert (ring->ruler->terminate); // Might break due to races.
return res;
}