abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MainSimp.cpp File Reference
#include <errno.h>
#include <signal.h>
#include "misc/zlib/zlib.h"
#include <sys/resource.h>
#include "System.h"
#include "ParseUtils.h"
#include "Options.h"
#include "Dimacs.h"
#include "SimpSolver.h"

Go to the source code of this file.

Functions

void printStats (Solver &solver)
 
static void SIGINT_interrupt (int signum)
 
static void SIGINT_exit (int signum)
 
int MainSimp (int argc, char **argv)
 

Variables

static Solversolver
 

Function Documentation

int MainSimp ( int  argc,
char **  argv 
)

Definition at line 61 of file MainSimp.cpp.

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
gzFile ZEXPORT gzopen(const char *path, const char *mode)
Definition: gzlib.c:198
#define l_False
Definition: SolverTypes.h:85
static void parse_DIMACS(gzFile input_stream, Solver &S)
Definition: Dimacs.h:82
static shot S[256]
Definition: kitPerm.c:40
void printStats ( Solver solver)

Definition at line 37 of file MainSat.cpp.

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 }
static double cpuTime(void)
Definition: System.h:53
uint64_t starts
Definition: Solver.h:138
double memUsedPeak()
Definition: System.cpp:94
uint64_t decisions
Definition: Solver.h:138
uint64_t propagations
Definition: Solver.h:138
uint64_t max_literals
Definition: Solver.h:139
uint64_t rnd_decisions
Definition: Solver.h:138
uint64_t conflicts
Definition: Solver.h:138
uint64_t tot_literals
Definition: Solver.h:139
static void SIGINT_exit ( int  signum)
static

Definition at line 50 of file MainSimp.cpp.

50  {
51  printf("\n"); printf("*** INTERRUPTED ***\n");
52  if (solver->verbosity > 0){
54  printf("\n"); printf("*** INTERRUPTED ***\n"); }
55  _exit(1); }
void printStats(Solver &solver)
Definition: MainSat.cpp:37
typedefABC_NAMESPACE_HEADER_START struct solver_t solver
Definition: solver.h:30
static void SIGINT_interrupt ( int  signum)
static

Definition at line 45 of file MainSimp.cpp.

45 { solver->interrupt(); }
typedefABC_NAMESPACE_HEADER_START struct solver_t solver
Definition: solver.h:30

Variable Documentation

Solver* solver
static

Definition at line 42 of file MainSimp.cpp.