Skip to content

Commit f053b5e

Browse files
committed
New archetype setup
1 parent 9917788 commit f053b5e

File tree

6 files changed

+83
-145
lines changed

6 files changed

+83
-145
lines changed

src/comp_analyzer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bool CompAnalyzer::explore_comp(const uint32_t v, const uint32_t sup_comp_long_c
207207
if (counter->weighted()) archetype.stack_level().include_solution(counter->get_weight(v));
208208
else archetype.stack_level().include_solution(counter->get_two());
209209
}
210-
archetype.set_var_in_peer_comp(v);
210+
archetype.set_var_clear(v);
211211
return false;
212212
}
213213
return true;
@@ -338,7 +338,7 @@ void CompAnalyzer::record_comp(const uint32_t var, const uint32_t sup_comp_long_
338338
const Lit l1 = d.get_lit1();
339339
const Lit l2 = d.get_lit2();
340340
if (is_true(l1) || is_true(l2)) {
341-
archetype.clear_cl(d.id);
341+
archetype.set_cl_clear(d.id);
342342
sat = true;
343343
goto end_sat;
344344
} else {
@@ -351,7 +351,7 @@ void CompAnalyzer::record_comp(const uint32_t var, const uint32_t sup_comp_long_
351351
} else {
352352
if (archetype.clause_unvisited_in_sup_comp(d.id)) {
353353
if (is_true(d.blk_lit)) {
354-
archetype.clear_cl(d.id);
354+
archetype.set_cl_clear(d.id);
355355
sat = true;
356356
goto end_sat;
357357
}

src/comp_analyzer.hpp

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,36 @@ class CompAnalyzer {
127127
void initialize(const LiteralIndexedVector<LitWatchList> & literals,
128128
const ClauseAllocator* alloc, const vector<ClauseOfs>& long_irred_cls);
129129

130-
bool var_unvisited_sup_comp(const uint32_t v) const {
130+
bool var_unvisited_in_sup_comp(const uint32_t v) const {
131131
SLOW_DEBUG_DO(assert(v <= max_var));
132132
return archetype.var_unvisited_in_sup_comp(v);
133133
}
134134

135135
// manages the literal whenever it occurs in comp analysis
136136
// returns true iff the underlying variable was unvisited before
137-
bool manage_occ_of(const uint32_t v){
137+
void manage_occ_of(const uint32_t v){
138+
// if unvisited, it MUST be unknown
138139
if (archetype.var_unvisited_in_sup_comp(v)) {
139140
__builtin_prefetch(holder.begin_bin(v));
140141
comp_vars.push_back(v);
141142
archetype.set_var_visited(v);
142-
return true;
143143
}
144-
return false;
145144
}
146145

147-
bool manage_occ_and_score_of(uint32_t v){
148-
if (is_unknown(v)) bump_freq_score(v);
149-
return manage_occ_of(v);
146+
void manage_occ_and_score_of(uint32_t v) {
147+
if (is_unknown(v)) {
148+
bump_freq_score(v);
149+
manage_occ_of(v);
150+
}
150151
}
151152

152-
void setup_analysis_context(StackLevel& top, const Comp & super_comp){
153+
void setup_analysis_context(StackLevel& top, const Comp& super_comp){
153154
archetype.re_initialize(top,super_comp);
154-
155155
debug_print("Setting VAR/CL_SUP_COMP_unvisited for unset vars");
156-
all_vars_in_comp(super_comp, vt)
157-
if (is_unknown(*vt)) {
158-
archetype.set_var_in_sup_comp_unvisited_raw(*vt);
159-
var_freq_scores[*vt] = 0;
160-
}
161-
156+
all_vars_in_comp(super_comp, vt) if (is_unknown(*vt)) {
157+
archetype.set_var_in_sup_comp_unvisited(*vt);
158+
var_freq_scores[*vt] = 0;
159+
}
162160
all_cls_in_comp(super_comp, it) archetype.set_clause_in_sup_comp_unvisited(*it);
163161
}
164162

@@ -167,6 +165,7 @@ class CompAnalyzer {
167165
// explore_comp has been called already
168166
// which set up search_stack, seen[] etc.
169167
inline Comp *make_comp_from_archetype(){
168+
SLOW_DEBUG_DO(for (auto&v: comp_vars) assert(is_unknown(v)));
170169
auto p = archetype.make_comp(comp_vars.size());
171170
return p;
172171
}
@@ -225,39 +224,27 @@ class CompAnalyzer {
225224

226225
// This is called from record_comp, i.e. during figuring out what
227226
// belongs to a component. It's called on every long clause.
227+
// The clause is _definitely_ in the supercomponent
228228
bool search_clause(ClData& d, Lit const* cl_start) {
229-
const auto it_v_end = comp_vars.end();
230229
bool sat = false;
230+
for (auto it_l = cl_start; *it_l != SENTINEL_LIT; it_l++) {
231+
if (is_true(*it_l)) {sat = true; break;}
232+
}
233+
234+
if (sat) {
235+
archetype.set_cl_clear(d.id);
236+
return true;
237+
}
231238

232239
for (auto it_l = cl_start; *it_l != SENTINEL_LIT; it_l++) {
233240
const uint32_t v = it_l->var();
234241
assert(v <= max_var);
235-
if (!archetype.var_nil(v)) manage_occ_and_score_of(v);
236-
else {
237-
assert(!is_unknown(*it_l));
238-
if (is_false(*it_l)) continue;
239-
240-
//accidentally entered a satisfied clause: undo the search process
241-
/* cout << "satisfied clause due to: " << *it_l << endl; */
242-
sat = true;
243-
d.blk_lit = *it_l;
244-
while (comp_vars.end() != it_v_end) {
245-
assert(comp_vars.back() <= max_var);
246-
archetype.set_var_in_sup_comp_unvisited(comp_vars.back());
247-
comp_vars.pop_back();
248-
}
249-
archetype.clear_cl(d.id);
250-
it_l--;
251-
while(*it_l != SENTINEL_LIT) {
252-
if(is_unknown(*it_l)) un_bump_score(it_l->var());
253-
it_l--;
254-
}
255-
break;
256-
}
242+
assert(is_false(*it_l) || archetype.var_unvisited_in_sup_comp(v) || archetype.var_visited(v));
243+
manage_occ_and_score_of(v);
257244
}
258245

259-
if (!sat) archetype.set_clause_visited(d.id);
260-
return sat;
246+
archetype.set_clause_visited(d.id);
247+
return false;
261248
}
262249
};
263250

src/comp_management.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void CompManager::record_remaining_comps_for(StackLevel &top) {
7979

8080
all_vars_in_comp(super_comp, vt) {
8181
debug_print("Going to NEXT var that's unvisited & set in this component... if it exists. Var: " << *vt);
82-
if (ana.var_unvisited_sup_comp(*vt) && ana.explore_comp(*vt,
82+
if (ana.var_unvisited_in_sup_comp(*vt) && ana.explore_comp(*vt,
8383
super_comp.num_long_cls(), super_comp.num_bin_cls())) {
8484
Comp *p_new_comp = ana.make_comp_from_archetype();
8585
CacheableComp packed_comp(hash_seed, *p_new_comp);

src/comp_types/comp_archetype.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Comp* CompArchetype::make_comp(const uint32_t comp_vars_size) {
2121
all_vars_in_comp(super_comp(), v_it)
2222
if (var_visited(*v_it)) {
2323
p_new_comp->add_var(*v_it);
24-
set_var_in_peer_comp(*v_it);
24+
set_var_clear(*v_it);
2525
}
2626
p_new_comp->close_vars_data();
2727

2828
// Fill (long) clause IDs in new comp
2929
all_cls_in_comp(super_comp(), cl_it)
3030
if (clause_visited(*cl_it)) {
3131
p_new_comp->add_cl(*cl_it);
32-
set_clause_in_peer_comp(*cl_it);
32+
set_cl_clear(*cl_it);
3333
}
3434
p_new_comp->set_num_bin_cls(num_bin_cls);
3535
p_new_comp->close_cls_data();

src/comp_types/comp_archetype.hpp

Lines changed: 49 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
1-
/*
2-
* comp_archetype.h
3-
*
4-
* Created on: Feb 9, 2013
5-
* Author: mthurley
6-
*/
1+
/******************************************
2+
Copyright (C) 2023 Authors of GANAK, see AUTHORS file
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
***********************************************/
722

823
#pragma once
924

1025
#include <cstring>
11-
#include <algorithm>
1226
#include <iostream>
27+
#include <limits>
1328

1429
#include "../common.hpp"
1530
#include "comp.hpp"
1631
#include "cacheable_comp.hpp"
1732

33+
using std::numeric_limits;
1834
using std::cout;
1935
using std::endl;
2036

2137
namespace GanakInt {
2238

23-
// State values for variables found during comp analysis (CA)
24-
#define CA_VAR_IN_SUP_COMP_UNVISITED 1
25-
#define CA_VAR_VISITED 2
26-
#define CA_VAR_IN_PEER_COMP 4
27-
// 1 + 2 + 4 == 7
28-
#define CA_VAR_MASK 7
29-
30-
#define CA_CL_IN_SUP_COMP_UNVISITED 8
31-
#define CA_CL_VISITED 16
32-
#define CA_CL_IN_PEER_COMP 32
33-
/* #define WHATEVER 64 */
34-
// 64+32+16+8 == 120
35-
#define CA_CL_MASK 120
36-
3739
class StackLevel;
3840

3941
// There is exactly ONE of this. Inside CompAnalyzer, which is inside CompManager, which is inside Solver
4042
class CompArchetype {
4143
public:
4244
CompArchetype() = default;
43-
~CompArchetype() { delete[] data; }
44-
CompArchetype(StackLevel &stack_level, const Comp &super_comp) :
45-
super_comp_ptr(&super_comp), stack_lvl_ptr(&stack_level) {
46-
}
45+
~CompArchetype() { delete[] raw_data; }
4746

4847
// called every time we want to deal with a new component
4948
void re_initialize(StackLevel &stack_level, const Comp &super_comp) {
@@ -53,92 +52,41 @@ class CompArchetype {
5352
clear_data();
5453
}
5554

56-
const Comp &super_comp() {
57-
return *super_comp_ptr;
58-
}
59-
60-
StackLevel& stack_level() {
61-
return *stack_lvl_ptr;
62-
}
63-
64-
void set_var_in_sup_comp_unvisited(const uint32_t v) {
65-
data[v] = CA_VAR_IN_SUP_COMP_UNVISITED | (data[v] & CA_CL_MASK);
66-
}
67-
68-
void set_var_in_sup_comp_unvisited_raw(const uint32_t v) {
69-
data[v] = CA_VAR_IN_SUP_COMP_UNVISITED;
70-
}
71-
72-
void set_clause_in_sup_comp_unvisited(const ClauseIndex cl) {
73-
data[cl] = CA_CL_IN_SUP_COMP_UNVISITED | (data[cl] & CA_VAR_MASK);
74-
}
75-
76-
void clear_var(const uint32_t v) {
77-
data[v] &= CA_CL_MASK;
78-
}
79-
80-
void clear_cl(const ClauseIndex cl) {
81-
data[cl] &= CA_VAR_MASK;
82-
}
83-
84-
// REMOVES from unseen of super, sets visited
85-
void set_var_visited(const uint32_t v) {
86-
data[v] = CA_VAR_VISITED | (data[v] & CA_CL_MASK);
87-
}
55+
const Comp &super_comp() { return *super_comp_ptr; }
56+
StackLevel& stack_level() { return *stack_lvl_ptr; }
8857

89-
void set_clause_visited(const ClauseIndex cl) {
90-
clear_cl(cl);
91-
data[cl] = CA_CL_VISITED | (data[cl] & CA_VAR_MASK);
92-
}
58+
void set_var_visited(const uint32_t v) { v_data[v] = tstamp; }
59+
void set_clause_visited(const ClauseIndex cl) { cl_data[cl] = tstamp; }
9360

94-
void set_var_in_peer_comp(const uint32_t v) {
95-
data[v] = CA_VAR_IN_PEER_COMP | (data[v] & CA_CL_MASK);
96-
}
61+
// bool var_clear(const uint32_t v) { return v_data[v] < tstamp; }
62+
void set_var_clear(const uint32_t v) { v_data[v] = 0; }
63+
void set_cl_clear(const uint32_t cl) { cl_data[cl] = 0; }
9764

98-
bool var_in_peer_comp(const uint32_t v) const {
99-
return CA_VAR_IN_PEER_COMP & (data[v] & CA_CL_MASK);
100-
}
65+
void set_var_in_sup_comp_unvisited(const uint32_t v) { v_data[v] = tstamp-1; }
66+
void set_clause_in_sup_comp_unvisited(const ClauseIndex cl) { cl_data[cl] = tstamp-1; }
67+
bool var_unvisited_in_sup_comp(uint32_t v) const { return v_data[v] == tstamp-1; }
68+
bool clause_unvisited_in_sup_comp(ClauseIndex cl) const { return cl_data[cl] == tstamp-1; }
10169

102-
void set_clause_in_peer_comp(const ClauseIndex cl) {
103-
data[cl] = CA_CL_IN_PEER_COMP | (data[cl] & CA_VAR_MASK);
104-
}
105-
106-
bool var_visited(const uint32_t v) const {
107-
return data[v] & CA_VAR_VISITED;
108-
}
109-
110-
bool clause_visited(const ClauseIndex cl) const {
111-
return data[cl] & CA_CL_VISITED;
112-
}
113-
114-
bool var_nil(const uint32_t v) const {
115-
return (data[v] & CA_VAR_MASK) == 0;
116-
}
117-
118-
bool clause_nil(const ClauseIndex cl) const {
119-
return (data[cl] & CA_CL_MASK) == 0;
120-
}
121-
122-
bool var_unvisited_in_sup_comp(uint32_t v) const {
123-
return data[v] & CA_VAR_IN_SUP_COMP_UNVISITED;
124-
}
125-
126-
bool clause_unvisited_in_sup_comp(ClauseIndex cl) const {
127-
return data[cl] & CA_CL_IN_SUP_COMP_UNVISITED;
128-
}
70+
bool var_visited(const uint32_t v) const { return v_data[v] == tstamp; }
71+
bool clause_visited(const ClauseIndex cl) const { return cl_data[cl] == tstamp; }
12972

13073
// called exactly once during lifetime of counter
13174
void init_data(uint32_t max_var_id, uint32_t max_cl_id) {
132-
data_sz = std::max(max_var_id,max_cl_id) + 1;
75+
assert(tstamp == 0);
76+
data_sz = max_var_id +1 + max_cl_id + 1;
13377
debug_print("Creating new data[] of size: " << data_sz << " and zeroing it.");
134-
data = new uint8_t[data_sz];
78+
raw_data = new uint32_t[data_sz];
79+
memset(raw_data, 0, data_sz * sizeof(uint32_t));
80+
v_data = raw_data;
81+
cl_data = raw_data + max_var_id + 1;
13582
clear_data();
13683
}
13784

13885
void clear_data() {
13986
num_long_cls = 0;
14087
num_bin_cls = 0;
141-
memset(data, 0, data_sz);
88+
tstamp+=2;
89+
memset(raw_data, 0, data_sz * sizeof(uint32_t));
14290
}
14391

14492
// At this point explore_comp has been called already which
@@ -148,9 +96,12 @@ class CompArchetype {
14896
uint32_t num_long_cls = 0;
14997
uint32_t num_bin_cls = 0;
15098
private:
99+
uint32_t tstamp = 0;
151100
Comp const* super_comp_ptr;
152101
StackLevel *stack_lvl_ptr;
153-
uint8_t* data = nullptr; // all variables and all clause IDXs can be indexed here
102+
uint32_t* raw_data = nullptr;
103+
uint32_t* cl_data = nullptr;
104+
uint32_t* v_data = nullptr;
154105
uint32_t data_sz = 0;
155106
};
156107

src/counter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ uint32_t Counter::find_best_branch(const bool ignore_td, const bool also_noninde
11771177
bool only_optional_indep = true;
11781178
uint32_t best_var = 0;
11791179
double best_var_score = -1e8;
1180-
uint64_t* at;
1180+
uint64_t* at = nullptr;
11811181
is_indep = false;
11821182
bool couldnt_find_indep = false; // only used when also_nonindep is true
11831183

0 commit comments

Comments
 (0)