abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MainSimp.cpp
Go to the documentation of this file.
1 /*****************************************************************************************[Main.cc]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007, 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 #ifndef _WIN32
27 #include <sys/resource.h>
28 #endif
29 
30 #include "System.h"
31 #include "ParseUtils.h"
32 #include "Options.h"
33 #include "Dimacs.h"
34 #include "SimpSolver.h"
35 
36 using namespace Minisat;
37 
38 //=================================================================================================
39 
40 extern void printStats(Solver& solver);
41 
42 static Solver* solver;
43 // Terminate by notifying the solver and back out gracefully. This is mainly to have a test-case
44 // for this feature of the Solver as it may take longer than an immediate call to '_exit()'.
45 static void SIGINT_interrupt(int signum) { solver->interrupt(); }
46 
47 // Note that '_exit()' rather than 'exit()' has to be used. The reason is that 'exit()' calls
48 // destructors and may cause deadlocks if a malloc/free function happens to be running (these
49 // functions are guarded by locks for multithreaded use).
50 static void SIGINT_exit(int signum) {
51  printf("\n"); printf("*** INTERRUPTED ***\n");
52  if (solver->verbosity > 0){
54  printf("\n"); printf("*** INTERRUPTED ***\n"); }
55  _exit(1); }
56 
57 
58 //=================================================================================================
59 // Main:
60 
61 extern "C" int MainSimp(int argc, char** argv)
62 {
63  try {
64  setUsageHelp("USAGE: %s [options] <input-file> <result-output-file>\n\n where input may be either in plain or gzipped DIMACS.\n");
65  // printf("This is MiniSat 2.0 beta\n");
66 
67 #if defined(__linux__)
68  fpu_control_t oldcw, newcw;
69  _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
70  printf("WARNING: for repeatability, setting FPU to use double precision\n");
71 #endif
72  // Extra options:
73  //
74  IntOption verb ("MAIN", "verb", "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
75  BoolOption pre ("MAIN", "pre", "Completely turn on/off any preprocessing.", true);
76  StringOption dimacs ("MAIN", "dimacs", "If given, stop after preprocessing and write the result to this file.");
77  IntOption cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", INT32_MAX, IntRange(0, INT32_MAX));
78  IntOption mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", INT32_MAX, IntRange(0, INT32_MAX));
79 
80  if ( !parseOptions(argc, argv, true) )
81  return 1;
82 
83  SimpSolver S;
84  double initial_time = cpuTime();
85 
86  if (!pre) S.eliminate(true);
87 
88  S.verbosity = verb;
89 
90  solver = &S;
91 /*
92  // Use signal handlers that forcibly quit until the solver will be able to respond to
93  // interrupts:
94  signal(SIGINT, SIGINT_exit);
95  signal(SIGXCPU,SIGINT_exit);
96 
97  // Set limit on CPU-time:
98  if (cpu_lim != INT32_MAX){
99  rlimit rl;
100  getrlimit(RLIMIT_CPU, &rl);
101  if (rl.rlim_max == RLIM_INFINITY || (rlim_t)cpu_lim < rl.rlim_max){
102  rl.rlim_cur = cpu_lim;
103  if (setrlimit(RLIMIT_CPU, &rl) == -1)
104  printf("WARNING! Could not set resource limit: CPU-time.\n");
105  } }
106 
107  // Set limit on virtual memory:
108  if (mem_lim != INT32_MAX){
109  rlim_t new_mem_lim = (rlim_t)mem_lim * 1024*1024;
110  rlimit rl;
111  getrlimit(RLIMIT_AS, &rl);
112  if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
113  rl.rlim_cur = new_mem_lim;
114  if (setrlimit(RLIMIT_AS, &rl) == -1)
115  printf("WARNING! Could not set resource limit: Virtual memory.\n");
116  } }
117 */
118  if (argc == 1)
119  {
120  printf("Reading from standard input... Use '--help' for help.\n");
121  return 1;
122  }
123 
124  gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
125  if (in == NULL)
126  printf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);
127 
128  if (S.verbosity > 0){
129  printf("============================[ Problem Statistics ]=============================\n");
130  printf("| |\n"); }
131 
132  parse_DIMACS(in, S);
133  gzclose(in);
134  FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
135 
136  if (S.verbosity > 0){
137  printf("| Number of variables: %12d |\n", S.nVars());
138  printf("| Number of clauses: %12d |\n", S.nClauses()); }
139 
140  double parsed_time = cpuTime();
141  if (S.verbosity > 0)
142  printf("| Parse time: %12.2f s |\n", parsed_time - initial_time);
143 
144  // Change to signal-handlers that will only notify the solver and allow it to terminate
145  // voluntarily:
146 // signal(SIGINT, SIGINT_interrupt);
147 // signal(SIGXCPU,SIGINT_interrupt);
148 
149  S.eliminate(true);
150  double simplified_time = cpuTime();
151  if (S.verbosity > 0){
152  printf("| Simplification time: %12.2f s |\n", simplified_time - parsed_time);
153  printf("| |\n"); }
154 
155  if (!S.okay()){
156  if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
157  if (S.verbosity > 0){
158  printf("===============================================================================\n");
159  printf("Solved by simplification\n");
160  printStats(S);
161  printf("\n"); }
162  printf("UNSATISFIABLE\n");
163  exit(20);
164  }
165 
166  if (dimacs){
167  if (S.verbosity > 0)
168  printf("==============================[ Writing DIMACS ]===============================\n");
169  S.toDimacs((const char*)dimacs);
170  if (S.verbosity > 0)
171  printStats(S);
172  exit(0);
173  }
174 
175  vec<Lit> dummy;
176  lbool ret = S.solveLimited(dummy);
177 
178  if (S.verbosity > 0){
179  printStats(S);
180  printf("\n"); }
181  printf(ret == l_True ? "SATISFIABLE\n" : ret == l_False ? "UNSATISFIABLE\n" : "INDETERMINATE\n");
182  if (res != NULL){
183  if (ret == l_True){
184  fprintf(res, "SAT\n");
185  for (int i = 0; i < S.nVars(); i++)
186  if (S.model[i] != l_Undef)
187  fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
188  fprintf(res, " 0\n");
189  }else if (ret == l_False)
190  fprintf(res, "UNSAT\n");
191  else
192  fprintf(res, "INDET\n");
193  fclose(res);
194  }
195 
196 //#ifdef NDEBUG
197 // exit(ret == l_True ? 10 : ret == l_False ? 20 : 0); // (faster than "return", which will invoke the destructor for 'Solver')
198 //#else
199  return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
200 //#endif
201  } catch (OutOfMemoryException&){
202  printf("===============================================================================\n");
203  printf("INDETERMINATE\n");
204  exit(0);
205  }
206 }
#define INT32_MAX
Definition: pstdint.h:415
static double cpuTime(void)
Definition: System.h:53
bool okay() const
Definition: Solver.h:358
VOID_HACK exit()
#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
int parseOptions(int &argc, char **argv, bool strict=false)
Definition: Options.cpp:26
void printStats(Solver &solver)
Definition: MainSat.cpp:37
voidp gzFile
Definition: zlib.h:1173
int nVars() const
Definition: Solver.h:328
typedefABC_NAMESPACE_HEADER_START struct solver_t solver
Definition: solver.h:30
gzFile ZEXPORT gzdopen(int fd, const char *mode)
Definition: gzlib.c:210
lbool solveLimited(const vec< Lit > &assumps, bool do_simp=true, bool turn_off_simp=false)
Definition: SimpSolver.h:191
void setUsageHelp(const char *str)
Definition: Options.cpp:58
int nClauses() const
Definition: Solver.h:326
bool eliminate(bool turn_off_elim=false)
Definition: SimpSolver.cpp:582
void toDimacs(FILE *f, const vec< Lit > &assumps)
Definition: Solver.cpp:831
vec< lbool > model
Definition: Solver.h:110
static void SIGINT_exit(int signum)
Definition: MainSimp.cpp:50
int MainSimp(int argc, char **argv)
Definition: MainSimp.cpp:61
gzFile ZEXPORT gzopen(const char *path, const char *mode)
Definition: gzlib.c:198
static Solver * solver
Definition: MainSimp.cpp:42
#define l_False
Definition: SolverTypes.h:85
static void parse_DIMACS(gzFile input_stream, Solver &S)
Definition: Dimacs.h:82
static void SIGINT_interrupt(int signum)
Definition: MainSimp.cpp:45
static shot S[256]
Definition: kitPerm.c:40