less verbose
[satlib.git] / glucose-syrup / incremental / Main.cc
1
2 #include "solver_interface.h"
3 #include <errno.h>
4
5 #include <signal.h>
6 #include <zlib.h>
7 #include <sys/resource.h>
8
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"
14
15 using namespace Glucose;
16
17
18 static const char* _certified = "CORE -- CERTIFIED UNSAT";
19
20 void printStats(Solver& solver)
21 {
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);
32
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);
38     
39     if (mem_used != 0) printf("Memory used           : %.2f MB\n", mem_used);
40     printf("c CPU time              : %g s\n", cpu_time);
41 }
42
43
44
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(); }
49
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){
56         printStats(*solver);
57         printf("\n"); printf("*** INTERRUPTED ***\n"); }
58     _exit(1); }
59
60
61 int *buffer;
62 int length;
63 int offset;
64
65 int *outbuffer;
66 int outoffset;
67
68 int getInt() {
69   if (offset>=length) {
70     ssize_t ptr;
71     offset = 0;
72     do {
73       ptr=read(0, buffer, sizeof(int)*IS_BUFFERSIZE);
74       if (ptr == -1)
75         exit(-1);
76     } while(ptr==0);
77     ssize_t bytestoread=(4-(ptr & 3)) & 3;
78     while(bytestoread != 0) {
79       ssize_t p=read(0, &((char *)buffer)[ptr], bytestoread);
80       if (p == -1)
81         exit(-1);
82       bytestoread -= p;
83       ptr += p;
84     }
85     length = ptr / 4;
86     offset = 0;
87   }
88   
89   return buffer[offset++];
90 }
91
92 void flushInts() {
93   ssize_t bytestowrite=sizeof(int)*outoffset;
94   ssize_t byteswritten=0;
95   do {
96     ssize_t n=write(IS_OUT_FD, &((char *)outbuffer)[byteswritten], bytestowrite);
97     if (n == -1) {
98       fprintf(stderr, "Write failure\n");
99       exit(-1);
100     }
101     bytestowrite -= n;
102     byteswritten += n;
103   } while(bytestowrite != 0);
104   outoffset = 0;
105 }
106
107 void putInt(int value) {
108   if (outoffset>=IS_BUFFERSIZE) {
109     flushInts();
110   }
111   outbuffer[outoffset++]=value;
112 }
113
114 void readClauses(Solver *solver) {
115   vec<Lit> clause;
116   int numvars = solver->nVars();
117   bool haveClause = false;
118   while(true) {
119     int lit=getInt();
120     if (lit!=0) {
121       int var = abs(lit) - 1;
122       while (var >= numvars) {
123         numvars++;
124         solver->newVar();
125       }
126       clause.push( (lit>0) ? mkLit(var) : ~mkLit(var));
127       haveClause = true;
128     } else {
129       if (haveClause) {
130         solver->addClause_(clause);
131         haveClause = false;
132         clause.clear();
133       } else {
134         //done with clauses
135         return;
136       }
137     }
138   }
139 }
140
141 void processCommands(SimpSolver *solver) {
142   while(true) {
143     int command=getInt();
144     switch(command) {
145     case IS_FREEZE: {
146       int var=getInt()-1;
147       solver->setFrozen(var, true);
148       break;
149     }
150     case IS_RUNSOLVER: {
151       vec<Lit> dummy;
152       lbool ret = solver->solveLimited(dummy);
153       if (ret == l_True) {
154         putInt(IS_SAT);
155         putInt(solver->nVars());
156         for(int i=0;i<solver->nVars();i++) {
157           putInt(solver->model[i]==l_True);
158         }
159       } else if (ret == l_False) {
160         putInt(IS_UNSAT);
161       } else {
162         putInt(IS_INDETER);
163       }
164       flushInts();
165       return;
166     }
167     default:
168       fprintf(stderr, "Unreconized command\n");
169       exit(-1);
170     }
171   }
172 }
173   
174 void processSAT(SimpSolver *solver) {
175   buffer=(int *) malloc(sizeof(int)*IS_BUFFERSIZE);
176   offset=0;
177   length=0;
178   outbuffer=(int *) malloc(sizeof(int)*IS_BUFFERSIZE);
179   outoffset=0;
180   
181   while(true) {
182     double initial_time = cpuTime();    
183     readClauses(solver);
184     double parse_time = cpuTime();
185     processCommands(solver);
186     double finish_time = cpuTime();    
187     printf("Parse time: %12.2f s Solve time:%12.2f s\n", parse_time-initial_time, finish_time-parse_time);
188   }
189 }
190
191
192
193 //=================================================================================================
194 // Main:
195
196 int main(int argc, char** argv) {
197   try {
198     printf("c\nc This is glucose 4.0 --  based on MiniSAT (Many thanks to MiniSAT team)\nc\n");
199     
200     
201     setUsageHelp("c USAGE: %s [options] <input-file> <result-output-file>\n\n  where input may be either in plain or gzipped DIMACS.\n");
202     
203     
204 #if defined(__linux__)
205     fpu_control_t oldcw, newcw;
206     _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
207     //printf("c WARNING: for repeatability, setting FPU to use double precision\n");
208 #endif
209     // Extra options:
210     //
211     IntOption    verb   ("MAIN", "verb",   "Verbosity level (0=silent, 1=some, 2=more).", 0, IntRange(0, 2));
212     BoolOption   mod   ("MAIN", "model",   "show model.", false);
213     IntOption    vv  ("MAIN", "vv",   "Verbosity every vv conflicts", 10000, IntRange(1,INT32_MAX));
214     BoolOption   pre    ("MAIN", "pre",    "Completely turn on/off any preprocessing.", true);
215     StringOption dimacs ("MAIN", "dimacs", "If given, stop after preprocessing and write the result to this file.");
216     IntOption    cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", INT32_MAX, IntRange(0, INT32_MAX));
217     IntOption    mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", INT32_MAX, IntRange(0, INT32_MAX));
218     
219     BoolOption    opt_certified      (_certified, "certified",    "Certified UNSAT using DRUP format", false);
220     StringOption  opt_certified_file      (_certified, "certified-output",    "Certified UNSAT output file", "NULL");
221          
222     parseOptions(argc, argv, true);
223     
224     SimpSolver  S;
225     S.parsing = 1;
226     S.verbosity = verb;
227     S.verbEveryConflicts = vv;
228     S.showModel = mod;
229     solver = &S;
230     // Use signal handlers that forcibly quit until the solver will be
231     // able to respond to interrupts:
232     signal(SIGINT, SIGINT_exit);
233     signal(SIGXCPU,SIGINT_exit);
234
235
236     // Set limit on CPU-time:
237     if (cpu_lim != INT32_MAX){
238       rlimit rl;
239       getrlimit(RLIMIT_CPU, &rl);
240       if (rl.rlim_max == RLIM_INFINITY || (rlim_t)cpu_lim < rl.rlim_max){
241         rl.rlim_cur = cpu_lim;
242         if (setrlimit(RLIMIT_CPU, &rl) == -1)
243           printf("c WARNING! Could not set resource limit: CPU-time.\n");
244       } }
245     
246     // Set limit on virtual memory:
247     if (mem_lim != INT32_MAX){
248       rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
249       rlimit rl;
250       getrlimit(RLIMIT_AS, &rl);
251       if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
252         rl.rlim_cur = new_mem_lim;
253         if (setrlimit(RLIMIT_AS, &rl) == -1)
254           printf("c WARNING! Could not set resource limit: Virtual memory.\n");
255       } }
256     
257     //do solver stuff here
258     processSAT(&S);
259     
260     printf("c |  Number of variables:  %12d                                                                   |\n", S.nVars());
261     printf("c |  Number of clauses:    %12d                                                                   |\n", S.nClauses());
262     
263     
264     // Change to signal-handlers that will only notify the solver and allow it to terminate
265     // voluntarily:
266     signal(SIGINT, SIGINT_interrupt);
267     signal(SIGXCPU,SIGINT_interrupt);
268     
269     S.parsing = 0;
270
271 #ifdef NDEBUG
272     exit(0);
273 #else
274     return (0);
275 #endif
276   } catch (OutOfMemoryException&){
277     printf("c =========================================================================================================\n");
278     printf("INDETERMINATE\n");
279     exit(0);
280   }
281 }