void contractor_fixpoint::worklist_fixpoint_alg(contractor_status & cs) { thread_local static queue<unsigned> q; q = queue<unsigned>(); // empty queue thread_local static ibex::BitSet ctc_bitset = ibex::BitSet::empty(m_clist.size()); ctc_bitset.clear(); // Add all contractors to the queue. for (int i = m_clist.size() - 1; i >= 0; --i) { contractor & c_i = m_clist[i]; contractor_status_guard csg(cs); c_i.prune(cs); if (cs.m_box.is_empty()) { return; } ibex::BitSet const & output_i = cs.m_output; if (output_i.empty()) { continue; } q.push(i); ctc_bitset.add(i); } if (q.size() == 0) { return; } // Fixed Point Loop thread_local static box old_box(cs.m_box); do { interruption_point(); old_box = cs.m_box; unsigned const idx = q.front(); q.pop(); ctc_bitset.remove(idx); assert(idx < m_clist.size()); contractor & c = m_clist[idx]; contractor_status_guard csg(cs); c.prune(cs); if (cs.m_box.is_empty()) { return; } auto const & c_output = cs.m_output; if (!c_output.empty()) { // j-th dimension is changed as a result of pruning // need to add a contractor which takes j-th dim as an input for (int j = c_output.min(); j <= c_output.max(); ++j) { if (!c_output.contain(j)) { continue; } for (unsigned k = 0; k < m_clist.size(); ++k) { // Only add if it's not in the current queue if (!ctc_bitset.contain(k)) { contractor const & c_k = m_clist[k]; if (c_k.get_input().contain(j)) { q.push(k); ctc_bitset.add(k); } } } } } } while (q.size() > 0 && cs.m_box.max_diam() >= cs.m_config.nra_precision && !m_term_cond(old_box, cs.m_box)); return; }
void contractor_fixpoint::worklist_fixpoint_alg(contractor_status & cs) { queue<int> q; ibex::BitSet ctc_bitset(m_clist.size()); // Add all contractors to the queue. for (unsigned i = 0; i < m_clist.size(); ++i) { contractor & c_i = m_clist[i]; contractor_status_guard csg(cs); c_i.prune(cs); if (cs.m_box.is_empty()) { return; } ibex::BitSet const & output_i = cs.m_output; if (!output_i.empty()) { assert(!ctc_bitset.contain(i)); q.push(i); ctc_bitset.add(i); } } if (q.size() == 0) { return; } // Fixed Point Loop do { interruption_point(); unsigned const idx = q.front(); q.pop(); ctc_bitset.remove(idx); assert(!ctc_bitset.contain(idx)); assert(idx < m_clist.size()); contractor & c = m_clist[idx]; m_old_box = cs.m_box; contractor_status_guard csg(cs); c.prune(cs); if (cs.m_box.is_empty()) { return; } auto const & c_output = cs.m_output; if (!c_output.empty()) { // j-th dimension is changed as a result of pruning // need to add a contractor which takes j-th dim as an input int j = c_output.min(); do { if (!c_output.contain(j)) { continue; } for (int const dependent_ctc_id : m_dep_map[j]) { if (!ctc_bitset.contain(dependent_ctc_id)) { q.push(dependent_ctc_id); ctc_bitset.add(dependent_ctc_id); } } j = c_output.next(j); } while (j < c_output.max()); } } while (q.size() > 0 && (m_old_box != cs.m_box) && cs.m_box.is_bisectable(cs.m_config.nra_precision) && !m_term_cond(m_old_box, cs.m_box)); return; }
void contractor_cache::prune(contractor_status & cs) { // extract i-th interval where `m_input[i] == 1` vector<ibex::Interval> const in = extract_from_box_using_bitset(cs.m_box, m_input); auto const it = m_cache.find(in); if (it == m_cache.end()) { // not found in cache, run m_ctc ++m_num_nohit; { contractor_status_guard csg(cs); assert(cs.m_output.empty()); assert(cs.m_used_constraints.empty()); try { // run contractor m_ctc.prune(cs); // extract out from box. vector<ibex::Interval> const out = extract_from_box_using_bitset(cs.m_box, cs.m_output); // save the result to the cache m_cache.emplace(in, make_tuple(out, cs.m_output, cs.m_used_constraints, false)); } catch (exception & e) { m_cache.emplace(in, make_tuple(vector<ibex::Interval>(), cs.m_output, cs.m_used_constraints, true)); throw contractor_exception(e.what()); } } } else { ++m_num_hit; // found in cache, use it auto const & m_cached_content = it->second; vector<ibex::Interval> const & out = get<0>(m_cached_content); auto const & output_bitset = get<1>(m_cached_content); auto const & used_constraints = get<2>(m_cached_content); bool const was_exception_thrown = get<3>(m_cached_content); cs.m_used_constraints.insert(used_constraints.begin(), used_constraints.end()); if (was_exception_thrown) { cs.m_output.union_with(output_bitset); throw contractor_exception("previously there was an exception in the cached computation"); } else { // project out to box; update_box_using_bitset(cs.m_box, out, output_bitset); cs.m_output.union_with(output_bitset); } } }