3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-10-31 03:32:28 +00:00

Merge branch 'parallel' into param-tuning

This commit is contained in:
Ilana Shapiro 2025-10-21 13:02:52 -07:00 committed by GitHub
commit 39ec6764b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 122 additions and 189 deletions

View file

@ -412,17 +412,7 @@ namespace smt {
switch (m_state) { switch (m_state) {
case state::is_running: // batch manager is still running, but all threads have processed their cubes, which case state::is_running: // batch manager is still running, but all threads have processed their cubes, which
// means all cubes were unsat // means all cubes were unsat
if (!m_search_tree.is_closed())
throw default_exception("inconsistent end state"); throw default_exception("inconsistent end state");
// case when all cubes were unsat, but depend on nonempty asms, so we need to add these asms to final unsat core
// these asms are stored in the cube tree, at the root node
if (p.ctx.m_unsat_core.empty()) {
SASSERT(root && root->is_closed());
for (auto a : m_search_tree.get_core_from_root())
p.ctx.m_unsat_core.push_back(a);
}
return l_false;
case state::is_unsat: case state::is_unsat:
return l_false; return l_false;
case state::is_sat: case state::is_sat:

View file

@ -35,25 +35,32 @@ namespace search_tree {
enum class status { open, closed, active }; enum class status { open, closed, active };
template<typename Config> template <typename Config> class node {
class node {
typedef typename Config::literal literal; typedef typename Config::literal literal;
literal m_literal; literal m_literal;
node *m_left = nullptr, *m_right = nullptr, *m_parent = nullptr; node *m_left = nullptr, *m_right = nullptr, *m_parent = nullptr;
status m_status; status m_status;
vector<literal> m_core; vector<literal> m_core;
public: public:
node(literal const& l, node* parent) : node(literal const &l, node *parent) : m_literal(l), m_parent(parent), m_status(status::open) {}
m_literal(l), m_parent(parent), m_status(status::open) {}
~node() { ~node() {
dealloc(m_left); dealloc(m_left);
dealloc(m_right); dealloc(m_right);
} }
status get_status() const { return m_status; } status get_status() const {
void set_status(status s) { m_status = s; } return m_status;
literal const& get_literal() const { return m_literal; } }
bool literal_is_null() const { return Config::is_null(m_literal); } void set_status(status s) {
m_status = s;
}
literal const &get_literal() const {
return m_literal;
}
bool literal_is_null() const {
return Config::is_null(m_literal);
}
void split(literal const &a, literal const &b) { void split(literal const &a, literal const &b) {
SASSERT(!Config::literal_is_null(a)); SASSERT(!Config::literal_is_null(a));
SASSERT(!Config::literal_is_null(b)); SASSERT(!Config::literal_is_null(b));
@ -66,9 +73,15 @@ namespace search_tree {
m_status = status::open; m_status = status::open;
} }
node* left() const { return m_left; } node *left() const {
node* right() const { return m_right; } return m_left;
node* parent() const { return m_parent; } }
node *right() const {
return m_right;
}
node *parent() const {
return m_parent;
}
node *find_active_node() { node *find_active_node() {
if (m_status == status::active) if (m_status == status::active)
@ -98,16 +111,18 @@ namespace search_tree {
m_right->display(out, indent + 2); m_right->display(out, indent + 2);
} }
bool has_core() const { return !m_core.empty(); }
void set_core(vector<literal> const &core) { void set_core(vector<literal> const &core) {
m_core = core; m_core = core;
} }
vector<literal> const & get_core() const { return m_core; } vector<literal> const &get_core() const {
void clear_core() { m_core.clear(); } return m_core;
}
void clear_core() {
m_core.clear();
}
}; };
template<typename Config> template <typename Config> class tree {
class tree {
typedef typename Config::literal literal; typedef typename Config::literal literal;
scoped_ptr<node<Config>> m_root = nullptr; scoped_ptr<node<Config>> m_root = nullptr;
literal m_null_literal; literal m_null_literal;
@ -139,138 +154,68 @@ namespace search_tree {
return nullptr; return nullptr;
} }
// Invariants: void close(node<Config> *n) {
// Cores labeling nodes are subsets of the literals on the path to the node and the (external) assumption literals.
// If a parent is open, then the one of the children is open.
void close_with_core(node<Config>* n, vector<literal> const &C, bool allow_resolve = true) {
if (!n || n->get_status() == status::closed) if (!n || n->get_status() == status::closed)
return; return;
n->set_status(status::closed);
close(n->left());
close(n->right());
}
// Invariants:
// Cores labeling nodes are subsets of the literals on the path to the node and the (external) assumption
// literals. If a parent is open, then the one of the children is open.
void close_with_core(node<Config> *n, vector<literal> const &C) {
if (!n || n->get_status() == status::closed)
return;
node<Config> *p = n->parent();
if (p && all_of(C, [n](auto const &l) { return l != n->get_literal(); })) {
close_with_core(p, C);
return;
}
close(n->left());
close(n->right());
n->set_core(C); n->set_core(C);
n->set_status(status::closed); n->set_status(status::closed);
close_with_core(n->left(), C, false); if (!p)
close_with_core(n->right(), C, false); return;
auto left = p->left();
auto right = p->right();
if (!left || !right)
return;
// stop at root // only attempt when both children are closed and each has a core
if (!n->parent()) return; if (left->get_status() != status::closed || right->get_status() != status::closed)
return;
node<Config>* p = n->parent(); auto resolvent = compute_sibling_resolvent(left, right);
if (!p) return; // root reached close_with_core(p, resolvent);
auto is_literal_in_core = [](literal const& l, vector<literal> const& C) {
for (unsigned i = 0; i < C.size(); ++i)
if (C[i] == l) return true;
return false;
};
// case 1: current splitting literal not in the conflict core
if (!is_literal_in_core(n->get_literal(), C)) {
close_with_core(p, C);
// case 2: both siblings closed -> resolve
} else if (allow_resolve && p->left()->get_status() == status::closed && p->right()->get_status() == status::closed) {
try_resolve_upwards(p);
}
} }
// Given complementary sibling nodes for literals x and ¬x, sibling resolvent = (core_left core_right) \ {x, ¬x} // Given complementary sibling nodes for literals x and ¬x, sibling resolvent = (core_left core_right) \ {x,
// ¬x}
vector<literal> compute_sibling_resolvent(node<Config> *left, node<Config> *right) { vector<literal> compute_sibling_resolvent(node<Config> *left, node<Config> *right) {
vector<literal> res; vector<literal> res;
if (!left->has_core() || !right->has_core()) return res;
bool are_sibling_complements = left->parent() == right->parent();
if (!are_sibling_complements)
return res;
auto &core_l = left->get_core(); auto &core_l = left->get_core();
auto &core_r = right->get_core(); auto &core_r = right->get_core();
auto contains = [](vector<literal> const &v, literal const &l) { if (core_l.empty() || core_r.empty() || left->parent() != right->parent())
for (unsigned i = 0; i < v.size(); ++i) return res;
if (v[i] == l) return true;
return false;
};
auto lit_l = left->get_literal(); auto lit_l = left->get_literal();
auto lit_r = right->get_literal(); auto lit_r = right->get_literal();
// Add literals from left core, skipping lit_l for (auto const &lit : core_l)
for (unsigned i = 0; i < core_l.size(); ++i) { if (lit != lit_l && !res.contains(lit))
if (core_l[i] != lit_l && !contains(res, core_l[i])) res.push_back(lit);
res.push_back(core_l[i]); for (auto const &lit : core_r)
} if (lit != lit_l && !res.contains(lit))
res.push_back(lit);
// Add literals from right core, skipping lit_r
for (unsigned i = 0; i < core_r.size(); ++i) {
if (core_r[i] != lit_r && !contains(res, core_r[i]))
res.push_back(core_r[i]);
}
return res; return res;
} }
void try_resolve_upwards(node<Config>* p) {
while (p) {
auto left = p->left();
auto right = p->right();
if (!left || !right) return;
// only attempt when both children are closed and each has a core
if (left->get_status() != status::closed || right->get_status() != status::closed) return;
if (!left->has_core() || !right->has_core()) return;
auto resolvent = compute_sibling_resolvent(left, right);
// empty resolvent of sibling complement (i.e. tautology) -> global UNSAT
if (resolvent.empty()) {
close_with_core(m_root.get(), resolvent, false);
return;
}
// if p already has the same core, nothing more to do
if (p->has_core() && resolvent == p->get_core())
return;
// Bubble to the highest ancestor where ALL literals in the resolvent
// are present somewhere on the path from that ancestor to root
node<Config>* candidate = p;
node<Config>* attach_here = p; // fallback
while (candidate) {
bool all_found = true;
for (auto const& r : resolvent) {
bool found = false;
for (node<Config>* q = candidate; q; q = q->parent()) {
if (q->get_literal() == r) {
found = true;
break;
}
}
if (!found) {
all_found = false;
break;
}
}
if (all_found) {
attach_here = candidate; // bubble up to this node
}
candidate = candidate->parent();
}
// attach the resolvent and close the subtree at attach_here
if (!attach_here->has_core() || attach_here->get_core() != resolvent) {
close_with_core(attach_here, resolvent, false);
}
// continue upward from parent of attach_here
p = attach_here->parent();
}
}
public: public:
tree(literal const &null_literal) : m_null_literal(null_literal) { tree(literal const &null_literal) : m_null_literal(null_literal) {
reset(); reset();
@ -301,8 +246,8 @@ namespace search_tree {
SASSERT(n != m_root.get()); SASSERT(n != m_root.get());
// all literals in conflict are on the path from root to n // all literals in conflict are on the path from root to n
// remove assumptions from conflict to ensure this. // remove assumptions from conflict to ensure this.
DEBUG_CODE( DEBUG_CODE(auto on_path =
auto on_path = [&](literal const& a) { [&](literal const &a) {
node<Config> *p = n; node<Config> *p = n;
while (p) { while (p) {
if (p->get_literal() == a) if (p->get_literal() == a)
@ -311,8 +256,7 @@ namespace search_tree {
} }
return false; return false;
}; };
SASSERT(all_of(conflict, [&](auto const& a) { return on_path(a); })); SASSERT(all_of(conflict, [&](auto const &a) { return on_path(a); })););
);
while (n) { while (n) {
if (any_of(conflict, [&](auto const &a) { return a == n->get_literal(); })) { if (any_of(conflict, [&](auto const &a) { return a == n->get_literal(); })) {
@ -341,8 +285,8 @@ namespace search_tree {
auto p = n->parent(); auto p = n->parent();
while (p) { while (p) {
if (p->left() && p->left()->get_status() == status::closed && if (p->left() && p->left()->get_status() == status::closed && p->right() &&
p->right() && p->right()->get_status() == status::closed) { p->right()->get_status() == status::closed) {
p->set_status(status::closed); p->set_status(status::closed);
n = p; n = p;
p = n->parent(); p = n->parent();
@ -381,6 +325,5 @@ namespace search_tree {
m_root->display(out, 0); m_root->display(out, 0);
return out; return out;
} }
}; };
} } // namespace search_tree