1 /*****************************************************************************************[Main.cc]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20
21 #include <errno.h>
22
23 #include <signal.h>
24 #include <zlib.h>
25
26 #include "prop/minisat/utils/System.h"
27 #include "prop/minisat/utils/ParseUtils.h"
28 #include "prop/minisat/utils/Options.h"
29 #include "prop/minisat/core/Dimacs.h"
30 #include "prop/minisat/core/Solver.h"
31
32 //=================================================================================================
33
34
35 namespace CVC4 {
36 namespace Minisat {
printStats(Solver & solver)37 void printStats(Solver& solver)
38 {
39 double cpu_time = cpuTime();
40 double mem_used = memUsedPeak();
41 printf("restarts : %"PRIu64"\n", solver.starts);
42 printf("conflicts : %-12"PRIu64" (%.0f /sec)\n", solver.conflicts , solver.conflicts /cpu_time);
43 printf("decisions : %-12"PRIu64" (%4.2f %% random) (%.0f /sec)\n", solver.decisions, (float)solver.rnd_decisions*100 / (float)solver.decisions, solver.decisions /cpu_time);
44 printf("propagations : %-12"PRIu64" (%.0f /sec)\n", solver.propagations, solver.propagations/cpu_time);
45 printf("conflict literals : %-12"PRIu64" (%4.2f %% deleted)\n", solver.tot_literals, (solver.max_literals - solver.tot_literals)*100 / (double)solver.max_literals);
46 if (mem_used != 0) printf("Memory used : %.2f MB\n", mem_used);
47 printf("CPU time : %g s\n", cpu_time);
48 }
49
50
51 static Solver* solver;
52 // Terminate by notifying the solver and back out gracefully. This is mainly to have a test-case
53 // for this feature of the Solver as it may take longer than an immediate call to '_exit()'.
SIGINT_interrupt(int signum)54 static void SIGINT_interrupt(int signum) { solver->interrupt(); }
55
56 // Note that '_exit()' rather than 'exit()' has to be used. The reason is that 'exit()' calls
57 // destructors and may cause deadlocks if a malloc/free function happens to be running (these
58 // functions are guarded by locks for multithreaded use).
SIGINT_exit(int signum)59 static void SIGINT_exit(int signum) {
60 printf("\n"); printf("*** INTERRUPTED ***\n");
61 if (solver->verbosity > 0){
62 printStats(*solver);
63 printf("\n"); printf("*** INTERRUPTED ***\n"); }
64 _exit(1); }
65
66 } /* CVC4::Minisat namespace */
67 } /* CVC4 namespace */
68
69
70 //=================================================================================================
71 // Main:
72
73
main(int argc,char ** argv)74 int main(int argc, char** argv)
75 {
76 using namespace CVC4;
77 using namespace CVC4::Minisat;
78 try {
79 setUsageHelp("USAGE: %s [options] <input-file> <result-output-file>\n\n where input may be either in plain or gzipped DIMACS.\n");
80 // printf("This is MiniSat 2.0 beta\n");
81
82 #if defined(__linux__)
83 fpu_control_t oldcw, newcw;
84 _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
85 printf("WARNING: for repeatability, setting FPU to use double precision\n");
86 #endif
87 // Extra options:
88 //
89 IntOption verb ("MAIN", "verb", "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
90 IntOption cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", INT32_MAX, IntRange(0, INT32_MAX));
91 IntOption mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", INT32_MAX, IntRange(0, INT32_MAX));
92
93 parseOptions(argc, argv, true);
94
95 Solver S;
96 double initial_time = cpuTime();
97
98 S.verbosity = verb;
99
100 solver = &S;
101 // Use signal handlers that forcibly quit until the solver will be able to respond to
102 // interrupts:
103 signal(SIGINT, SIGINT_exit);
104 signal(SIGXCPU,SIGINT_exit);
105
106 // Set limit on CPU-time:
107 if (cpu_lim != INT32_MAX){
108 rlimit rl;
109 getrlimit(RLIMIT_CPU, &rl);
110 if (rl.rlim_max == RLIM_INFINITY || (rlim_t)cpu_lim < rl.rlim_max){
111 rl.rlim_cur = cpu_lim;
112 if (setrlimit(RLIMIT_CPU, &rl) == -1)
113 printf("WARNING! Could not set resource limit: CPU-time.\n");
114 } }
115
116 // Set limit on virtual memory:
117 if (mem_lim != INT32_MAX){
118 rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
119 rlimit rl;
120 getrlimit(RLIMIT_AS, &rl);
121 if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
122 rl.rlim_cur = new_mem_lim;
123 if (setrlimit(RLIMIT_AS, &rl) == -1)
124 printf("WARNING! Could not set resource limit: Virtual memory.\n");
125 } }
126
127 if (argc == 1)
128 printf("Reading from standard input... Use '--help' for help.\n");
129
130 gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
131 if (in == NULL)
132 printf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);
133
134 if (S.verbosity > 0){
135 printf("============================[ Problem Statistics ]=============================\n");
136 printf("| |\n"); }
137
138 parse_DIMACS(in, S);
139 gzclose(in);
140 FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
141
142 if (S.verbosity > 0){
143 printf("| Number of variables: %12d |\n", S.nVars());
144 printf("| Number of clauses: %12d |\n", S.nClauses()); }
145
146 double parsed_time = cpuTime();
147 if (S.verbosity > 0){
148 printf("| Parse time: %12.2f s |\n", parsed_time - initial_time);
149 printf("| |\n"); }
150
151 // Change to signal-handlers that will only notify the solver and allow it to terminate
152 // voluntarily:
153 signal(SIGINT, SIGINT_interrupt);
154 signal(SIGXCPU,SIGINT_interrupt);
155
156 if (!S.simplify()){
157 if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
158 if (S.verbosity > 0){
159 printf("===============================================================================\n");
160 printf("Solved by unit propagation\n");
161 printStats(S);
162 printf("\n"); }
163 printf("UNSATISFIABLE\n");
164 exit(20);
165 }
166
167 vec<Lit> dummy;
168 lbool ret = S.solveLimited(dummy);
169 if (S.verbosity > 0){
170 printStats(S);
171 printf("\n"); }
172 printf(ret == l_True ? "SATISFIABLE\n" : ret == l_False ? "UNSATISFIABLE\n" : "INDETERMINATE\n");
173 if (res != NULL){
174 if (ret == l_True){
175 fprintf(res, "SAT\n");
176 for (int i = 0; i < S.nVars(); i++)
177 if (S.model[i] != l_Undef)
178 fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
179 fprintf(res, " 0\n");
180 }else if (ret == l_False)
181 fprintf(res, "UNSAT\n");
182 else
183 fprintf(res, "INDET\n");
184 fclose(res);
185 }
186
187 #ifdef NDEBUG
188 exit(ret == l_True ? 10 : ret == l_False ? 20 : 0); // (faster than "return", which will invoke the destructor for 'Solver')
189 #else
190 return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
191 #endif
192 } catch (OutOfMemoryException&){
193 printf("===============================================================================\n");
194 printf("INDETERMINATE\n");
195 exit(0);
196 }
197 }
198