Incremental frontend for glucose solver
[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 getInt() {
66   if (offset>=length) {
67     ssize_t ptr;
68     offset = 0;
69     do {
70       ptr=read(0, buffer, sizeof(int)*IS_BUFFERSIZE);
71       if (ptr == -1)
72         exit(-1);
73     } while(ptr==0);
74     ssize bytestoread=(4-(ptr & 3)) & 3;
75     while(bytestoread != 0) {
76       ssize_t p=read(0, &((char *)buffer)[ptr], bytestoread);
77       if (p == -1)
78         exit(-1);
79       bytestoread -= p;
80       ptr += p;
81     }
82     length = ptr / 4;
83     offset = 0;
84   }
85   
86   return buffer[offset++];
87 }
88
89 void putInt(int value) {
90 }
91
92 void readClauses(Solver *solver) {
93   vec<Lit> clause;
94   int numvars = solver->nVars();
95   bool haveClause = false;
96   while(true) {
97     int lit=getInt();
98     if (lit!=0) {
99       int var = abs(lit) - 1;
100       while (var >= numvars) {
101         numvars++;
102         solver->newVar();
103       }
104       clause.push( (lit>0) ? mkLit(var) : ~mkLit(var));
105       haveClause = true;
106     } else {
107       if (haveClause) {
108         solver->addClause_(clause);
109         haveClause = false;
110         clause.clear();
111       } else {
112         //done with clauses
113         return;
114       }
115     }
116   }
117 }
118
119 void processCommands(Solver *solver) {
120   while(true) {
121     int command=getInt();
122     switch(command) {
123     case IS_FREEZE:
124       int var=getInt();
125       solver->setFrozen(var, true);
126       break;
127     case IS_RUNSOLVER: {
128       vec<Lit> dummy;
129       lbool ret = solver->solveLimited(dummy);
130       if (ret == l_True) {
131         putInt(IS_SAT);
132         putInt(solver->nVars());
133         for(int i=0;i<solver->nVars();i++) {
134           putInt(solver->model[i]==l_True);
135         }
136       } else if (ret == l_False) {
137         putInt(IS_UNSAT);
138       } else {
139         putInt(IS_INDETER);
140       }
141       flushInts();
142       return;
143     }
144     default:
145       fprintf(stderr, "Unreconized command\n");
146       exit(-1);
147     }
148   }
149 }
150   
151 void processSAT(Solver *solver) {
152   buffer=(int *) malloc(sizeof(int)*BUFFERSIZE);
153   offset=0;
154   length=0;
155   
156   while(true) {
157     double initial_time = cpuTime();    
158     readClauses(solver);
159     double parse_time = cpuTime();
160     processCommands(solver);
161     double finish_time = cpuTime();    
162     
163   }
164 }
165
166
167
168 //=================================================================================================
169 // Main:
170
171 int main(int argc, char** argv) {
172   try {
173     printf("c\nc This is glucose 4.0 --  based on MiniSAT (Many thanks to MiniSAT team)\nc\n");
174     
175     
176     setUsageHelp("c USAGE: %s [options] <input-file> <result-output-file>\n\n  where input may be either in plain or gzipped DIMACS.\n");
177     
178     
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");
183 #endif
184     // Extra options:
185     //
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));
193     
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");
196          
197     parseOptions(argc, argv, true);
198     
199     SimpSolver  S;
200     S.parsing = 1;
201     S.verbosity = verb;
202     S.verbEveryConflicts = vv;
203     S.showModel = mod;
204     solver = &S;
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);
209
210
211     // Set limit on CPU-time:
212     if (cpu_lim != INT32_MAX){
213       rlimit rl;
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");
219       } }
220     
221     // Set limit on virtual memory:
222     if (mem_lim != INT32_MAX){
223       rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
224       rlimit rl;
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");
230       } }
231     
232     //do solver stuff here
233     processSAT(&S);
234     
235     printf("c |  Number of variables:  %12d                                                                   |\n", S.nVars());
236     printf("c |  Number of clauses:    %12d                                                                   |\n", S.nClauses());
237     
238     double parsed_time = cpuTime();
239     if (S.verbosity > 0){
240       printf("c |  Parse time:           %12.2f s                                                                 |\n", parsed_time - initial_time);
241       printf("c |                                                                                                       |\n"); }
242     
243     // Change to signal-handlers that will only notify the solver and allow it to terminate
244     // voluntarily:
245     signal(SIGINT, SIGINT_interrupt);
246     signal(SIGXCPU,SIGINT_interrupt);
247     
248     S.parsing = 0;
249
250 #ifdef NDEBUG
251     exit(ret == l_True ? 10 : ret == l_False ? 20 : 0);     // (faster than "return", which will invoke the destructor for 'Solver')
252 #else
253     return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
254 #endif
255   } catch (OutOfMemoryException&){
256     printf("c =========================================================================================================\n");
257     printf("INDETERMINATE\n");
258     exit(0);
259   }
260 }