You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
9.2 KiB

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