abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MainSat.cpp File Reference
#include <errno.h>
#include <signal.h>
#include "misc/zlib/zlib.h"
#include "System.h"
#include "ParseUtils.h"
#include "Options.h"
#include "Dimacs.h"
#include "Solver.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 MainSat (int argc, char **argv)
 

Variables

static Solversolver
 

Function Documentation

int MainSat ( int  argc,
char **  argv 
)

Definition at line 71 of file MainSat.cpp.

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
#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
voidp gzFile
Definition: zlib.h:1173
int nVars() const
Definition: Solver.h:328
lbool solveLimited(const vec< Lit > &assumps)
Definition: Solver.h:357
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
void printStats(Solver &solver)
Definition: MainSat.cpp:37
int nClauses() const
Definition: Solver.h:326
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 59 of file MainSat.cpp.

59  {
60  printf("\n"); printf("*** INTERRUPTED ***\n");
61  if (solver->verbosity > 0){
63  printf("\n"); printf("*** INTERRUPTED ***\n"); }
64  _exit(1); }
typedefABC_NAMESPACE_HEADER_START struct solver_t solver
Definition: solver.h:30
void printStats(Solver &solver)
Definition: MainSat.cpp:37
static void SIGINT_interrupt ( int  signum)
static

Definition at line 54 of file MainSat.cpp.

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

Variable Documentation

Solver* solver
static

Definition at line 51 of file MainSat.cpp.