/****************************************** Copyright (c) 2016, Mate Soos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ***********************************************/ #include "reducedb.h" #include "solver.h" #include "solverconf.h" #include "sqlstats.h" #include using namespace CMSat; struct SortRedClsGlue { explicit SortRedClsGlue(ClauseAllocator& _cl_alloc) : cl_alloc(_cl_alloc) {} ClauseAllocator& cl_alloc; bool operator () (const ClOffset xOff, const ClOffset yOff) const { const Clause* x = cl_alloc.ptr(xOff); const Clause* y = cl_alloc.ptr(yOff); return x->stats.glue < y->stats.glue; } }; struct SortRedClsSize { explicit SortRedClsSize(ClauseAllocator& _cl_alloc) : cl_alloc(_cl_alloc) {} ClauseAllocator& cl_alloc; bool operator () (const ClOffset xOff, const ClOffset yOff) const { const Clause* x = cl_alloc.ptr(xOff); const Clause* y = cl_alloc.ptr(yOff); return x->size() < y->size(); } }; struct SortRedClsAct { explicit SortRedClsAct(ClauseAllocator& _cl_alloc) : cl_alloc(_cl_alloc) {} ClauseAllocator& cl_alloc; bool operator () (const ClOffset xOff, const ClOffset yOff) const { const Clause* x = cl_alloc.ptr(xOff); const Clause* y = cl_alloc.ptr(yOff); return x->stats.activity > y->stats.activity; } }; ReduceDB::ReduceDB(Solver* _solver) : solver(_solver) { } void ReduceDB::sort_red_cls(ClauseClean clean_type) { switch (clean_type) { case ClauseClean::glue : { std::sort(solver->longRedCls[2].begin(), solver->longRedCls[2].end(), SortRedClsGlue(solver->cl_alloc)); break; } case ClauseClean::activity : { std::sort(solver->longRedCls[2].begin(), solver->longRedCls[2].end(), SortRedClsAct(solver->cl_alloc)); break; } default: { assert(false && "Unknown cleaning type"); } } } //TODO maybe we chould count binary learnt clauses as well into the //kept no. of clauses as other solvers do void ReduceDB::handle_lev2() { nbReduceDB_lev1++; solver->dump_memory_stats_to_sql(); const double myTime = cpuTime(); assert(solver->watches.get_smudged_list().empty()); //lev2 -- clean int64_t num_to_reduce = solver->longRedCls[2].size(); for(unsigned keep_type = 0 ; keep_type < sizeof(solver->conf.ratio_keep_clauses)/sizeof(double) ; keep_type++ ) { const uint64_t keep_num = (double)num_to_reduce*solver->conf.ratio_keep_clauses[keep_type]; if (keep_num == 0) { continue; } sort_red_cls(static_cast(keep_type)); mark_top_N_clauses(keep_num); } assert(delayed_clause_free.empty()); cl_marked = 0; cl_ttl = 0; cl_locked_solver = 0; remove_cl_from_lev2(); solver->clean_occur_from_removed_clauses_only_smudged(); for(ClOffset offset: delayed_clause_free) { solver->cl_alloc.clauseFree(offset); } delayed_clause_free.clear(); #ifdef SLOW_DEBUG solver->check_no_removed_or_freed_cl_in_watch(); #endif if (solver->conf.verbosity) { cout << "c [DBclean lev2]" << " marked: " << cl_marked << " ttl:" << cl_ttl << " locked_solver:" << cl_locked_solver << solver->conf.print_times(cpuTime()-myTime) << endl; } if (solver->sqlStats) { solver->sqlStats->time_passed_min( solver , "dbclean-lev2" , cpuTime()-myTime ); } total_time += cpuTime()-myTime; last_reducedb_num_conflicts = solver->sumConflicts; if (solver->sqlStats) { //solver->sqlStats->reduceDB(2, nbReduceDB_lev2, solver); } } void ReduceDB::handle_lev1() { nbReduceDB_lev1++; uint32_t moved_w0 = 0; uint32_t used_recently = 0; uint32_t non_recent_use = 0; double myTime = cpuTime(); size_t j = 0; for(size_t i = 0 ; i < solver->longRedCls[1].size() ; i++ ) { const ClOffset offset = solver->longRedCls[1][i]; Clause* cl = solver->cl_alloc.ptr(offset); if (cl->stats.which_red_array == 0) { solver->longRedCls[0].push_back(offset); moved_w0++; } else if (cl->stats.which_red_array == 2) { assert(false && "we should never move up through any other means"); } else { if (!solver->clause_locked(*cl, offset) && cl->stats.last_touched + solver->conf.must_touch_lev1_within < solver->sumConflicts ) { solver->longRedCls[2].push_back(offset); cl->stats.which_red_array = 2; cl->stats.activity = 0; solver->bumpClauseAct(cl); non_recent_use++; } else { solver->longRedCls[1][j++] = offset; used_recently++; } } } solver->longRedCls[1].resize(j); if (solver->conf.verbosity) { cout << "c [DBclean lev1]" << " used recently: " << used_recently << " not used recently: " << non_recent_use << " moved w0: " << moved_w0 << solver->conf.print_times(cpuTime()-myTime) << endl; } if (solver->sqlStats) { solver->sqlStats->time_passed_min( solver , "dbclean-lev1" , cpuTime()-myTime ); } total_time += cpuTime()-myTime; if (solver->sqlStats) { //solver->sqlStats->reduceDB(1, nbReduceDB_lev1, solver); } } void ReduceDB::mark_top_N_clauses(const uint64_t keep_num) { size_t marked = 0; for(size_t i = 0 ; i < solver->longRedCls[2].size() && marked < keep_num ; i++ ) { const ClOffset offset = solver->longRedCls[2][i]; Clause* cl = solver->cl_alloc.ptr(offset); if (cl->stats.glue <= solver->conf.glue_put_lev0_if_below_or_eq) { cl->stats.which_red_array = 0; } else if (cl->stats.glue <= solver->conf.glue_put_lev1_if_below_or_eq && solver->conf.glue_put_lev1_if_below_or_eq != 0 ) { cl->stats.which_red_array = 1; } if (cl->used_in_xor() || cl->stats.ttl > 0 || solver->clause_locked(*cl, offset) || cl->stats.which_red_array != 2 ) { //no need to mark, skip continue; } if (!cl->stats.marked_clause) { marked++; cl->stats.marked_clause = true; } } } bool ReduceDB::cl_needs_removal(const Clause* cl, const ClOffset offset) const { assert(cl->red()); return !cl->used_in_xor() && !cl->stats.marked_clause && cl->stats.ttl == 0 && !solver->clause_locked(*cl, offset); } void ReduceDB::remove_cl_from_lev2() { size_t i, j; for (i = j = 0 ; i < solver->longRedCls[2].size() ; i++ ) { ClOffset offset = solver->longRedCls[2][i]; Clause* cl = solver->cl_alloc.ptr(offset); assert(cl->size() > 2); // check and move to lower (better) levels, if possible if (cl->stats.glue <= solver->conf.glue_put_lev0_if_below_or_eq) { cl->stats.which_red_array = 0; } if (cl->stats.glue <= solver->conf.glue_put_lev1_if_below_or_eq && solver->conf.glue_put_lev1_if_below_or_eq != 0 ) { cl->stats.which_red_array = 1; } if (cl->stats.which_red_array < 2) { solver->longRedCls[cl->stats.which_red_array].push_back(offset); continue; } assert(cl->stats.which_red_array == 2); //Check if locked, or marked or ttl-ed if (cl->stats.marked_clause) { cl_marked++; } else if (cl->stats.ttl != 0) { cl_ttl++; } else if (solver->clause_locked(*cl, offset)) { cl_locked_solver++; } if (!cl_needs_removal(cl, offset)) { if (cl->stats.ttl > 0) { cl->stats.ttl--; } solver->longRedCls[2][j++] = offset; cl->stats.marked_clause = 0; continue; } //Stats Update cl->setRemoved(); solver->watches.smudge((*cl)[0]); solver->watches.smudge((*cl)[1]); solver->litStats.redLits -= cl->size(); *solver->drat << del << *cl << fin; delayed_clause_free.push_back(offset); } solver->longRedCls[2].resize(j); }