Skip to content

Commit 3a138ab

Browse files
authored
More small cleanups of the code (#80)
* This todo has been achieved * Removing useless variable * Defensive verb_print * Better usage of mutex thanks to @clonker * Update description * Clenup * Update to satisfy macos gmp * Cleaner code * Add clang analyzer code * Cannot set opt_sampling_vars here * This should fix the mac build * Finally fixing this * Fixing outer_counter * Better printing of error
1 parent f2b2b27 commit 3a138ab

9 files changed

Lines changed: 48 additions & 36 deletions

File tree

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ if (SANITIZE)
307307
#add_compile_options("-Wconversion")
308308
endif()
309309

310+
option(ANALYZE "Run Clang static analyzer. You MUST use clang++ as the compiler for this to work" OFF)
311+
if (ANALYZE)
312+
MESSAGE(WARNING " --Using clang static analyzer -- you MUST use clang++ or the compile WILL fail")
313+
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=clang-analyzer-*")
314+
set(CMAKE_C_CLANG_TIDY "clang-tidy;-checks=clang-analyzer-*")
315+
endif()
316+
310317
# -----------------------------------------------------------------------------
311318
# Dependencies
312319
# -----------------------------------------------------------------------------

scripts/build_clang_analyze.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rm -rf CMake* src cmake* ganak* sharp* Make*
2+
cmake -DANALYZE=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang ..
3+
make -j16

src/common.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ using FF = std::unique_ptr<Field>;
103103
#define CHECK_COUNT_DO(x) do { } while (0)
104104
#endif
105105

106-
#define verb_print(a, b) if (conf.verb >= a) cout << "c o " << b << endl
106+
#define verb_print(a, b) do { if (conf.verb >= a) cout << "c o " << b << endl; } while (0)
107107
#define clear_toclear_seen() \
108108
do {\
109109
for(const auto& x: to_clear) seen[x] = 0;\

src/comp_types/difference_packed_comp.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class DiffPackedComp {
6969
DiffPackedComp() = default;
7070
~DiffPackedComp() { delete[] data; }
7171
DiffPackedComp& operator=(const DiffPackedComp& other) noexcept{
72-
if (data != nullptr) delete[] data;
72+
delete[] data;
7373
data_size = other.data_size;
7474
data = new uint32_t[data_size];
7575
std::memcpy(data, other.data, data_size * sizeof(uint32_t));
@@ -173,7 +173,7 @@ class DiffPackedComp {
173173

174174
private:
175175
uint32_t* data = nullptr; // the packed data
176-
uint32_t data_size = 0; // the size of the packed data in bytes
176+
uint32_t data_size = 0; // the size of the packed data in uint32_t units
177177
};
178178

179179
}

src/counter.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,13 @@ void Counter::td_decompose() {
354354
return;
355355
}
356356

357-
TWD::Graph primal(nVars());
357+
auto primal = std::make_unique<TWD::Graph>(nVars());
358358
all_lits(i) {
359359
Lit l(i/2, i%2 == 0);
360360
for(const auto& l2: watches[l].binaries) {
361361
if (!l2.red() && l < l2.lit()) {
362362
debug_print("bin cl: " << l.var() << " " << l2.lit().var());
363-
primal.addEdge(l.var()-1, l2.lit().var()-1);
363+
primal->addEdge(l.var()-1, l2.lit().var()-1);
364364
}
365365
}
366366
}
@@ -370,7 +370,7 @@ void Counter::td_decompose() {
370370
for(uint32_t i = 0; i < cl.sz; i++) {
371371
for(uint32_t i2 = i+1; i2 < cl.sz; i2++) {
372372
debug_print("long cl: " << cl[i].var() << " " << cl[i2].var());
373-
primal.addEdge(cl[i].var()-1, cl[i2].var()-1);
373+
primal->addEdge(cl[i].var()-1, cl[i2].var()-1);
374374
}
375375
}
376376
}
@@ -379,22 +379,22 @@ void Counter::td_decompose() {
379379
if (!conf.do_td_use_opt_indep) nodes = indep_support_end-1;
380380
if (conf.do_td_contract) {
381381
for(uint32_t i = nodes; i < nVars(); i++) {
382-
primal.contract(i, conf.td_max_edges*100);
383-
if (primal.numEdges() > conf.td_max_edges*100 ) break;
382+
primal->contract(i, conf.td_max_edges*100);
383+
if (primal->numEdges() > conf.td_max_edges*100 ) break;
384384
}
385385
}
386-
verb_print(1, "[td] nodes: " << nodes << " nvars: " << nVars() << " edges: " << primal.numEdges());
386+
verb_print(1, "[td] nodes: " << nodes << " nvars: " << nVars() << " edges: " << primal->numEdges());
387387

388388
const uint64_t n = (uint64_t)nVars()*(uint64_t)nVars();
389-
const double density = (double)primal.numEdges()/(double)n;
390-
const double edge_var_ratio = (double)primal.numEdges()/(double)nVars();
389+
const double density = (double)primal->numEdges()/(double)n;
390+
const double edge_var_ratio = (double)primal->numEdges()/(double)nVars();
391391
verb_print(1, "[td] Primal graph "
392-
<< " nodes: " << primal.numNodes()
393-
<< " edges: " << primal.numEdges()
392+
<< " nodes: " << primal->numNodes()
393+
<< " edges: " << primal->numEdges()
394394
<< " density: " << std::fixed << setprecision(3) << density
395395
<< " edge/var: " << std::fixed << setprecision(3) << edge_var_ratio);
396-
if (primal.numEdges() > conf.td_max_edges) {
397-
verb_print(1, "[td] Too many edges, " << primal.numEdges() << " skipping TD");
396+
if (primal->numEdges() > conf.td_max_edges) {
397+
verb_print(1, "[td] Too many edges, " << primal->numEdges() << " skipping TD");
398398
return;
399399
}
400400
if (density > conf.td_max_density) {
@@ -406,21 +406,22 @@ void Counter::td_decompose() {
406406
return;
407407
}
408408

409-
TWD::Graph* primal_alt = nullptr;
409+
std::unique_ptr<TWD::Graph> primal_alt = nullptr;
410410
if (conf.do_td_contract) {
411-
primal_alt = new TWD::Graph(nodes);
411+
primal_alt = std::make_unique<TWD::Graph>(nodes);
412412
for(uint32_t i = 0 ; i < nodes; i++) {
413-
const auto& k = primal.get_adj_list()[i];
413+
const auto& k = primal->get_adj_list()[i];
414414
for(const auto& i2: k) {
415415
if (i2 < (int)nodes)
416416
primal_alt->addEdge(i, i2);
417417
}
418418
}
419-
} else primal_alt = &primal;
419+
} else primal_alt = std::move(primal);
420420

421421
if (!primal_alt->isConnected()) {
422-
verb_print(1, "ERROR: Primal graph is not connected, this is NOT going to go well!");
423-
verb_print(1, "ERROR: Counter should NOT be fed a disconnected CNF");
422+
cerr << "ERROR: Primal graph is not connected, this is NOT going to go well!" << endl;
423+
cerr << "ERROR: Counter should NOT be fed a disconnected CNF" << endl;
424+
assert(false);
424425
exit(-1);
425426
}
426427

@@ -434,7 +435,6 @@ void Counter::td_decompose() {
434435

435436
compute_td_score(td, conf.do_td_contract ? nodes : nVars(), true);
436437
verb_print(1, "[td] decompose time: " << cpu_time() - my_time);
437-
if (conf.do_td_contract) delete primal_alt;
438438
}
439439

440440
// Self-check count without restart with CMS only
@@ -831,7 +831,7 @@ FF Counter::do_appmc_count() {
831831

832832
mpz_class num_sols(2);
833833
mpz_pow_ui(num_sols.get_mpz_t(), num_sols.get_mpz_t(), appmc_cnt.hashCount);
834-
num_sols *= appmc_cnt.cellSolCount;
834+
num_sols *= mpz_class(static_cast<unsigned long>(appmc_cnt.cellSolCount));
835835
verb_print(1, "[appmc] ApproxMC count: " << num_sols);
836836
return std::make_unique<ArjunNS::FMpz>(num_sols);
837837
}
@@ -1222,9 +1222,7 @@ double Counter::score_of(const uint32_t v, bool ignore_td) const {
12221222
double act_score = 0;
12231223
double td_score = 0;
12241224
double freq_score = 0;
1225-
vector<uint32_t> occ_cnt; // number of occurrences of a variable in the component
12261225

1227-
// TODO Yash idea: let's cut this into activities and incidence
12281226
if (!tdscore.empty() && !ignore_td) td_score = td_weight*tdscore[v];
12291227
act_score = var_act(v)/conf.act_score_divisor;
12301228
freq_score = (double)comp_manager->freq_score_of(v)/conf.freq_score_divisor;

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ void parse_supported_options(int argc, char** argv) {
267267
try {
268268
program.parse_args(argc, argv);
269269
if (program.is_used("--help")) {
270-
cout << "Probilistic Approximate Counter" << endl << endl
271-
<< "approxmc [options] inputfile" << endl;
270+
cout << "Flexible Weighted Model Counter" << endl << endl
271+
<< "ganak [options] inputfile" << endl;
272272
cout << program << endl;
273273
exit(0);
274274
}

src/outer_counter.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ THE SOFTWARE.
3131
#include "counter.hpp"
3232
#include "time_mem.hpp"
3333
#include "common.hpp"
34+
#include "ganak.hpp"
3435

3536
namespace GanakInt {
3637

@@ -128,7 +129,8 @@ void run_arjun(ArjunNS::SimplifiedCNF& cnf) {
128129
/* verb_print(1, "Arjun T: " << (cpu_time()-my_time)); */
129130
}
130131

131-
void setup_ganak(const ArjunNS::SimplifiedCNF& cnf, OuterCounter& counter) {
132+
template<class T>
133+
void setup_ganak(const ArjunNS::SimplifiedCNF& cnf, T& counter) {
132134
cnf.check_sanity();
133135
counter.new_vars(cnf.nVars());
134136

@@ -235,7 +237,7 @@ FF OuterCounter::count_with_parallel(uint8_t bits_jobs, int num_threads) {
235237
ArjunNS::SimplifiedCNF cnf(fg);
236238
cnf.new_vars(nvars);
237239
cnf.set_sampl_vars(ganak_to_cms_vars(indep_support));
238-
cnf.set_opt_sampl_vars(ganak_to_cms_vars(opt_indep_support));
240+
/* cnf.set_opt_sampl_vars(ganak_to_cms_vars(opt_indep_support)); */
239241
for (const auto& [lit, weight] : lit_weights)
240242
cnf.set_lit_weight(ganak_to_cms_lit(lit), weight->dup());
241243
for (const auto& cl : irred_cls) cnf.add_clause(ganak_to_cms_cl(cl));
@@ -255,14 +257,15 @@ FF OuterCounter::count_with_parallel(uint8_t bits_jobs, int num_threads) {
255257
if (cnf.multiplier_weight != fg->zero()) {
256258
auto local_conf = conf;
257259
local_conf.verb = 0; // disable verb for threads
258-
auto counter = std::make_unique<OuterCounter>(local_conf, fg);
260+
auto counter = std::make_unique<Ganak>(local_conf, fg);
259261
setup_ganak(cnf, *counter);
260262
auto ret = counter->count();
261263
num_cache_lookups += counter->get_num_cache_lookups();
262-
stats_mutex.lock();
263-
max_cache_elems = std::max(max_cache_elems, counter->get_max_cache_elems());
264-
count_is_approximate |= counter->get_is_approximate();
265-
stats_mutex.unlock();
264+
{
265+
std::lock_guard<std::mutex> lock(stats_mutex);
266+
max_cache_elems = std::max(max_cache_elems, counter->get_max_cache_elems());
267+
count_is_approximate |= counter->get_is_approximate();
268+
}
266269
*ret *= *cnf.multiplier_weight;
267270
return ret;
268271
} else {

src/outer_counter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ THE SOFTWARE.
2828
#include <set>
2929
#include <vector>
3030
#include <atomic>
31+
#include <mutex>
3132

3233
#include "counter.hpp"
3334
#include "counter_config.hpp"

src/statistics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ void DataAndStatistics::print_short(const Counter* counter, const std::unique_pt
113113

114114
verb_print(1, "comp sortsK/avg sz "
115115
<< setw(5) << comp_sorts/1000 << " / "
116-
<< setw(5) << setprecision(8) << safe_div(comp_sizes, comp_sorts))
117-
<< setprecision(2);
116+
<< setw(5) << setprecision(8) << safe_div(comp_sizes, comp_sorts)
117+
<< setprecision(2));
118118
verb_print(1, "comps recordK/non-reset ratio " << comps_recorded/1000 << " / "
119119
<< safe_div(comps_non_reset, comps_reset+comps_non_reset));
120120

0 commit comments

Comments
 (0)