2 #include "solver_interface.h"
7 #include <sys/resource.h>
9 #include "utils/System.h"
10 #include "utils/ParseUtils.h"
11 #include "utils/Options.h"
12 #include "core/Dimacs.h"
13 #include "simp/SimpSolver.h"
15 using namespace Glucose;
18 static const char* _certified = "CORE -- CERTIFIED UNSAT";
20 void printStats(Solver& solver)
22 double cpu_time = cpuTime();
23 double mem_used = 0;//memUsedPeak();
24 printf("c restarts : %" PRIu64" (%" PRIu64" conflicts in avg)\n", solver.starts,(solver.starts>0 ?solver.conflicts/solver.starts : 0));
25 printf("c blocked restarts : %" PRIu64" (multiple: %" PRIu64") \n", solver.nbstopsrestarts,solver.nbstopsrestartssame);
26 printf("c last block at restart : %" PRIu64"\n",solver.lastblockatrestart);
27 printf("c nb ReduceDB : %" PRIu64"\n", solver.nbReduceDB);
28 printf("c nb removed Clauses : %" PRIu64"\n",solver.nbRemovedClauses);
29 printf("c nb learnts DL2 : %" PRIu64"\n", solver.nbDL2);
30 printf("c nb learnts size 2 : %" PRIu64"\n", solver.nbBin);
31 printf("c nb learnts size 1 : %" PRIu64"\n", solver.nbUn);
33 printf("c conflicts : %-12" PRIu64" (%.0f /sec)\n", solver.conflicts , solver.conflicts /cpu_time);
34 printf("c decisions : %-12" PRIu64" (%4.2f %% random) (%.0f /sec)\n", solver.decisions, (float)solver.rnd_decisions*100 / (float)solver.decisions, solver.decisions /cpu_time);
35 printf("c propagations : %-12" PRIu64" (%.0f /sec)\n", solver.propagations, solver.propagations/cpu_time);
36 printf("c conflict literals : %-12" PRIu64" (%4.2f %% deleted)\n", solver.tot_literals, (solver.max_literals - solver.tot_literals)*100 / (double)solver.max_literals);
37 printf("c nb reduced Clauses : %" PRIu64"\n",solver.nbReducedClauses);
39 if (mem_used != 0) printf("Memory used : %.2f MB\n", mem_used);
40 printf("c CPU time : %g s\n", cpu_time);
45 static Solver* solver;
46 // Terminate by notifying the solver and back out gracefully. This is mainly to have a test-case
47 // for this feature of the Solver as it may take longer than an immediate call to '_exit()'.
48 static void SIGINT_interrupt(int signum) { solver->interrupt(); }
50 // Note that '_exit()' rather than 'exit()' has to be used. The reason is that 'exit()' calls
51 // destructors and may cause deadlocks if a malloc/free function happens to be running (these
52 // functions are guarded by locks for multithreaded use).
53 static void SIGINT_exit(int signum) {
54 printf("\n"); printf("*** INTERRUPTED ***\n");
55 if (solver->verbosity > 0){
57 printf("\n"); printf("*** INTERRUPTED ***\n"); }
70 ptr=read(0, buffer, sizeof(int)*IS_BUFFERSIZE);
74 ssize bytestoread=(4-(ptr & 3)) & 3;
75 while(bytestoread != 0) {
76 ssize_t p=read(0, &((char *)buffer)[ptr], bytestoread);
86 return buffer[offset++];
89 void putInt(int value) {
92 void readClauses(Solver *solver) {
94 int numvars = solver->nVars();
95 bool haveClause = false;
99 int var = abs(lit) - 1;
100 while (var >= numvars) {
104 clause.push( (lit>0) ? mkLit(var) : ~mkLit(var));
108 solver->addClause_(clause);
119 void processCommands(Solver *solver) {
121 int command=getInt();
125 solver->setFrozen(var, true);
129 lbool ret = solver->solveLimited(dummy);
132 putInt(solver->nVars());
133 for(int i=0;i<solver->nVars();i++) {
134 putInt(solver->model[i]==l_True);
136 } else if (ret == l_False) {
145 fprintf(stderr, "Unreconized command\n");
151 void processSAT(Solver *solver) {
152 buffer=(int *) malloc(sizeof(int)*BUFFERSIZE);
157 double initial_time = cpuTime();
159 double parse_time = cpuTime();
160 processCommands(solver);
161 double finish_time = cpuTime();
168 //=================================================================================================
171 int main(int argc, char** argv) {
173 printf("c\nc This is glucose 4.0 -- based on MiniSAT (Many thanks to MiniSAT team)\nc\n");
176 setUsageHelp("c USAGE: %s [options] <input-file> <result-output-file>\n\n where input may be either in plain or gzipped DIMACS.\n");
179 #if defined(__linux__)
180 fpu_control_t oldcw, newcw;
181 _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
182 //printf("c WARNING: for repeatability, setting FPU to use double precision\n");
186 IntOption verb ("MAIN", "verb", "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
187 BoolOption mod ("MAIN", "model", "show model.", false);
188 IntOption vv ("MAIN", "vv", "Verbosity every vv conflicts", 10000, IntRange(1,INT32_MAX));
189 BoolOption pre ("MAIN", "pre", "Completely turn on/off any preprocessing.", true);
190 StringOption dimacs ("MAIN", "dimacs", "If given, stop after preprocessing and write the result to this file.");
191 IntOption cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", INT32_MAX, IntRange(0, INT32_MAX));
192 IntOption mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", INT32_MAX, IntRange(0, INT32_MAX));
194 BoolOption opt_certified (_certified, "certified", "Certified UNSAT using DRUP format", false);
195 StringOption opt_certified_file (_certified, "certified-output", "Certified UNSAT output file", "NULL");
197 parseOptions(argc, argv, true);
202 S.verbEveryConflicts = vv;
205 // Use signal handlers that forcibly quit until the solver will be
206 // able to respond to interrupts:
207 signal(SIGINT, SIGINT_exit);
208 signal(SIGXCPU,SIGINT_exit);
211 // Set limit on CPU-time:
212 if (cpu_lim != INT32_MAX){
214 getrlimit(RLIMIT_CPU, &rl);
215 if (rl.rlim_max == RLIM_INFINITY || (rlim_t)cpu_lim < rl.rlim_max){
216 rl.rlim_cur = cpu_lim;
217 if (setrlimit(RLIMIT_CPU, &rl) == -1)
218 printf("c WARNING! Could not set resource limit: CPU-time.\n");
221 // Set limit on virtual memory:
222 if (mem_lim != INT32_MAX){
223 rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
225 getrlimit(RLIMIT_AS, &rl);
226 if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
227 rl.rlim_cur = new_mem_lim;
228 if (setrlimit(RLIMIT_AS, &rl) == -1)
229 printf("c WARNING! Could not set resource limit: Virtual memory.\n");
232 //do solver stuff here
235 printf("c | Number of variables: %12d |\n", S.nVars());
236 printf("c | Number of clauses: %12d |\n", S.nClauses());
238 double parsed_time = cpuTime();
239 if (S.verbosity > 0){
240 printf("c | Parse time: %12.2f s |\n", parsed_time - initial_time);
243 // Change to signal-handlers that will only notify the solver and allow it to terminate
245 signal(SIGINT, SIGINT_interrupt);
246 signal(SIGXCPU,SIGINT_interrupt);
251 exit(ret == l_True ? 10 : ret == l_False ? 20 : 0); // (faster than "return", which will invoke the destructor for 'Solver')
253 return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
255 } catch (OutOfMemoryException&){
256 printf("c =========================================================================================================\n");
257 printf("INDETERMINATE\n");