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