abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MainSat.cpp
Go to the documentation of this file.
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 "misc/zlib/zlib.h"
25 
26 #include "System.h"
27 #include "ParseUtils.h"
28 #include "Options.h"
29 #include "Dimacs.h"
30 #include "Solver.h"
31 
32 using namespace Minisat;
33 
34 //=================================================================================================
35 
36 
38 {
39  double cpu_time = cpuTime();
40  double mem_used = memUsedPeak();
41  printf("restarts : %-12.0f\n", (double)(int64_t)solver.starts);
42  printf("conflicts : %-12.0f (%.0f /sec)\n", (double)(int64_t)solver.conflicts, (double)(int64_t)solver.conflicts / cpu_time);
43  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);
44  printf("propagations : %-12.0f (%.0f /sec)\n", (double)(int64_t)solver.propagations, (double)(int64_t)solver.propagations / cpu_time);
45  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);
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()'.
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).
59 static void SIGINT_exit(int signum) {
60  printf("\n"); printf("*** INTERRUPTED ***\n");
61  if (solver->verbosity > 0){
63  printf("\n"); printf("*** INTERRUPTED ***\n"); }
64  _exit(1); }
65 
66 
67 //=================================================================================================
68 // Main:
69 
70 
71 extern "C" int MainSat(int argc, char** argv)
72 {
73  try {
74  setUsageHelp("USAGE: %s [options] <input-file> <result-output-file>\n\n where input may be either in plain or gzipped DIMACS.\n");
75  // printf("This is MiniSat 2.0 beta\n");
76 
77 #if defined(__linux__)
78  fpu_control_t oldcw, newcw;
79  _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
80  printf("WARNING: for repeatability, setting FPU to use double precision\n");
81 #endif
82  // Extra options:
83  //
84  IntOption verb ("MAIN", "verb", "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
85  IntOption cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", INT32_MAX, IntRange(0, INT32_MAX));
86  IntOption mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", INT32_MAX, IntRange(0, INT32_MAX));
87 
88  if ( !parseOptions(argc, argv, true) )
89  return 1;
90 
91  Solver S;
92  double initial_time = cpuTime();
93 
94  S.verbosity = verb;
95 
96  solver = &S;
97 /*
98  // Use signal handlers that forcibly quit until the solver will be able to respond to
99  // interrupts:
100  signal(SIGINT, SIGINT_exit);
101  signal(SIGXCPU,SIGINT_exit);
102 
103  // Set limit on CPU-time:
104  if (cpu_lim != INT32_MAX){
105  rlimit rl;
106  getrlimit(RLIMIT_CPU, &rl);
107  if (rl.rlim_max == RLIM_INFINITY || (rlim_t)cpu_lim < rl.rlim_max){
108  rl.rlim_cur = cpu_lim;
109  if (setrlimit(RLIMIT_CPU, &rl) == -1)
110  printf("WARNING! Could not set resource limit: CPU-time.\n");
111  } }
112 
113  // Set limit on virtual memory:
114  if (mem_lim != INT32_MAX){
115  rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
116  rlimit rl;
117  getrlimit(RLIMIT_AS, &rl);
118  if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
119  rl.rlim_cur = new_mem_lim;
120  if (setrlimit(RLIMIT_AS, &rl) == -1)
121  printf("WARNING! Could not set resource limit: Virtual memory.\n");
122  } }
123 */
124  if (argc == 1)
125  {
126  printf("Reading from standard input... Use '--help' for help.\n");
127  return 1;
128  }
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 }
#define INT32_MAX
Definition: pstdint.h:415
static double cpuTime(void)
Definition: System.h:53
VOID_HACK exit()
bool simplify()
Definition: Solver.cpp:577
static Solver * solver
Definition: MainSat.cpp:51
#define l_Undef
Definition: SolverTypes.h:86
#define l_True
Definition: SolverTypes.h:84
ABC_NAMESPACE_IMPL_START int ZEXPORT gzclose(gzFile file)
Definition: gzclose.c:18
uint64_t starts
Definition: Solver.h:138
int parseOptions(int &argc, char **argv, bool strict=false)
Definition: Options.cpp:26
voidp gzFile
Definition: zlib.h:1173
int nVars() const
Definition: Solver.h:328
lbool solveLimited(const vec< Lit > &assumps)
Definition: Solver.h:357
double memUsedPeak()
Definition: System.cpp:94
uint64_t decisions
Definition: Solver.h:138
typedefABC_NAMESPACE_HEADER_START struct solver_t solver
Definition: solver.h:30
gzFile ZEXPORT gzdopen(int fd, const char *mode)
Definition: gzlib.c:210
void setUsageHelp(const char *str)
Definition: Options.cpp:58
static void SIGINT_interrupt(int signum)
Definition: MainSat.cpp:54
void printStats(Solver &solver)
Definition: MainSat.cpp:37
int nClauses() const
Definition: Solver.h:326
vec< lbool > model
Definition: Solver.h:110
uint64_t propagations
Definition: Solver.h:138
int MainSat(int argc, char **argv)
Definition: MainSat.cpp:71
gzFile ZEXPORT gzopen(const char *path, const char *mode)
Definition: gzlib.c:198
#define l_False
Definition: SolverTypes.h:85
uint64_t max_literals
Definition: Solver.h:139
static void SIGINT_exit(int signum)
Definition: MainSat.cpp:59
static void parse_DIMACS(gzFile input_stream, Solver &S)
Definition: Dimacs.h:82
uint64_t rnd_decisions
Definition: Solver.h:138
static shot S[256]
Definition: kitPerm.c:40
uint64_t conflicts
Definition: Solver.h:138
uint64_t tot_literals
Definition: Solver.h:139