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