1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Kshitij Bansal, Andrew Reynolds, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* An additional layer between commands and invoking them. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "main/command_executor.h" |
17 |
|
|
18 |
|
#ifndef __WIN32__ |
19 |
|
# include <sys/resource.h> |
20 |
|
#endif /* ! __WIN32__ */ |
21 |
|
|
22 |
|
#include <iomanip> |
23 |
|
#include <iostream> |
24 |
|
#include <memory> |
25 |
|
#include <string> |
26 |
|
#include <vector> |
27 |
|
|
28 |
|
#include "main/main.h" |
29 |
|
#include "smt/command.h" |
30 |
|
#include "smt/smt_engine.h" |
31 |
|
|
32 |
|
namespace cvc5 { |
33 |
|
namespace main { |
34 |
|
|
35 |
|
// Function to cancel any (externally-imposed) limit on CPU time. |
36 |
|
// This is used for competitions while a solution (proof or model) |
37 |
|
// is being dumped (so that we don't give "sat" or "unsat" then get |
38 |
|
// interrupted partway through outputting a proof!). |
39 |
|
void setNoLimitCPU() { |
40 |
|
// Windows doesn't have these things, just ignore |
41 |
|
#ifndef __WIN32__ |
42 |
|
struct rlimit rlc; |
43 |
|
int st = getrlimit(RLIMIT_CPU, &rlc); |
44 |
|
if(st == 0) { |
45 |
|
rlc.rlim_cur = rlc.rlim_max; |
46 |
|
setrlimit(RLIMIT_CPU, &rlc); |
47 |
|
} |
48 |
|
#endif /* ! __WIN32__ */ |
49 |
|
} |
50 |
|
|
51 |
6530 |
CommandExecutor::CommandExecutor(std::unique_ptr<api::Solver>& solver) |
52 |
|
: d_solver(solver), |
53 |
6530 |
d_symman(new SymbolManager(d_solver.get())), |
54 |
13060 |
d_result() |
55 |
|
{ |
56 |
6530 |
} |
57 |
13060 |
CommandExecutor::~CommandExecutor() |
58 |
|
{ |
59 |
13060 |
} |
60 |
|
|
61 |
3901 |
void CommandExecutor::storeOptionsAsOriginal() |
62 |
|
{ |
63 |
3901 |
d_solver->d_originalOptions->copyValues(d_solver->d_smtEngine->getOptions()); |
64 |
3901 |
} |
65 |
|
|
66 |
3883 |
void CommandExecutor::printStatistics(std::ostream& out) const |
67 |
|
{ |
68 |
3883 |
if (d_solver->getOptionInfo("stats").boolValue()) |
69 |
|
{ |
70 |
|
const auto& stats = d_solver->getStatistics(); |
71 |
|
auto it = stats.begin(d_solver->getOptionInfo("stats-expert").boolValue(), |
72 |
|
d_solver->getOptionInfo("stats-all").boolValue()); |
73 |
|
for (; it != stats.end(); ++it) |
74 |
|
{ |
75 |
|
out << it->first << " = " << it->second << std::endl; |
76 |
|
} |
77 |
|
} |
78 |
3883 |
} |
79 |
|
|
80 |
|
void CommandExecutor::printStatisticsSafe(int fd) const |
81 |
|
{ |
82 |
|
if (d_solver->getOptionInfo("stats").boolValue()) |
83 |
|
{ |
84 |
|
d_solver->printStatisticsSafe(fd); |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
212811 |
bool CommandExecutor::doCommand(Command* cmd) |
89 |
|
{ |
90 |
212811 |
CommandSequence *seq = dynamic_cast<CommandSequence*>(cmd); |
91 |
212811 |
if(seq != nullptr) { |
92 |
|
// assume no error |
93 |
93 |
bool status = true; |
94 |
|
|
95 |
320 |
for (CommandSequence::iterator subcmd = seq->begin(); |
96 |
320 |
status && subcmd != seq->end(); |
97 |
|
++subcmd) |
98 |
|
{ |
99 |
227 |
status = doCommand(*subcmd); |
100 |
|
} |
101 |
|
|
102 |
93 |
return status; |
103 |
|
} else { |
104 |
212718 |
if (d_solver->getOptionInfo("verbosity").intValue() > 2) |
105 |
|
{ |
106 |
11 |
d_solver->getDriverOptions().out() << "Invoking: " << *cmd << std::endl; |
107 |
|
} |
108 |
|
|
109 |
212718 |
return doCommandSingleton(cmd); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
void CommandExecutor::reset() |
114 |
|
{ |
115 |
|
printStatistics(d_solver->getDriverOptions().err()); |
116 |
|
Command::resetSolver(d_solver.get()); |
117 |
|
} |
118 |
|
|
119 |
212734 |
bool CommandExecutor::doCommandSingleton(Command* cmd) |
120 |
|
{ |
121 |
425468 |
bool status = solverInvoke( |
122 |
638202 |
d_solver.get(), d_symman.get(), cmd, d_solver->getDriverOptions().out()); |
123 |
|
|
124 |
425468 |
api::Result res; |
125 |
212734 |
const CheckSatCommand* cs = dynamic_cast<const CheckSatCommand*>(cmd); |
126 |
212734 |
if(cs != nullptr) { |
127 |
5471 |
d_result = res = cs->getResult(); |
128 |
|
} |
129 |
|
const CheckSatAssumingCommand* csa = |
130 |
212734 |
dynamic_cast<const CheckSatAssumingCommand*>(cmd); |
131 |
212734 |
if (csa != nullptr) |
132 |
|
{ |
133 |
816 |
d_result = res = csa->getResult(); |
134 |
|
} |
135 |
212734 |
const QueryCommand* q = dynamic_cast<const QueryCommand*>(cmd); |
136 |
212734 |
if(q != nullptr) { |
137 |
22 |
d_result = res = q->getResult(); |
138 |
|
} |
139 |
|
|
140 |
212734 |
bool isResultUnsat = res.isUnsat() || res.isEntailed(); |
141 |
212734 |
bool isResultSat = res.isSat() || res.isNotEntailed(); |
142 |
|
|
143 |
|
// dump the model/proof/unsat core if option is set |
144 |
212734 |
if (status) { |
145 |
425398 |
std::vector<std::unique_ptr<Command> > getterCommands; |
146 |
638097 |
if (d_solver->getOptionInfo("dump-models").boolValue() |
147 |
638107 |
&& (isResultSat |
148 |
34 |
|| (res.isSatUnknown() |
149 |
|
&& res.getUnknownExplanation() == api::Result::INCOMPLETE))) |
150 |
|
{ |
151 |
10 |
getterCommands.emplace_back(new GetModelCommand()); |
152 |
|
} |
153 |
212699 |
if (d_solver->getOptionInfo("dump-proofs").boolValue() && isResultUnsat) |
154 |
|
{ |
155 |
|
getterCommands.emplace_back(new GetProofCommand()); |
156 |
|
} |
157 |
|
|
158 |
638097 |
if ((d_solver->getOptionInfo("dump-instantiations").boolValue() |
159 |
425349 |
|| d_solver->getOptionInfo("dump-instantiations-debug").boolValue()) |
160 |
638146 |
&& GetInstantiationsCommand::isEnabled(d_solver.get(), res)) |
161 |
|
{ |
162 |
5 |
getterCommands.emplace_back(new GetInstantiationsCommand()); |
163 |
|
} |
164 |
|
|
165 |
638097 |
if ((d_solver->getOptionInfo("dump-unsat-cores").boolValue() |
166 |
425395 |
|| d_solver->getOptionInfo("dump-unsat-cores-full").boolValue()) |
167 |
638105 |
&& isResultUnsat) |
168 |
|
{ |
169 |
1 |
getterCommands.emplace_back(new GetUnsatCoreCommand()); |
170 |
|
} |
171 |
|
|
172 |
638097 |
if (d_solver->getOptionInfo("dump-difficulty").boolValue() |
173 |
638097 |
&& (isResultUnsat || isResultSat || res.isSatUnknown())) |
174 |
|
{ |
175 |
|
getterCommands.emplace_back(new GetDifficultyCommand()); |
176 |
|
} |
177 |
|
|
178 |
212699 |
if (!getterCommands.empty()) { |
179 |
|
// set no time limit during dumping if applicable |
180 |
16 |
if (d_solver->getOptionInfo("force-no-limit-cpu-while-dump").boolValue()) |
181 |
|
{ |
182 |
|
setNoLimitCPU(); |
183 |
|
} |
184 |
32 |
for (const auto& getterCommand : getterCommands) { |
185 |
16 |
status = doCommandSingleton(getterCommand.get()); |
186 |
16 |
if (!status) |
187 |
|
{ |
188 |
|
break; |
189 |
|
} |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
425468 |
return status; |
194 |
|
} |
195 |
|
|
196 |
212734 |
bool solverInvoke(api::Solver* solver, |
197 |
|
SymbolManager* sm, |
198 |
|
Command* cmd, |
199 |
|
std::ostream& out) |
200 |
|
{ |
201 |
|
// print output for -o raw-benchmark |
202 |
212734 |
if (solver->isOutputOn("raw-benchmark")) |
203 |
|
{ |
204 |
11 |
std::ostream& ss = solver->getOutput("raw-benchmark"); |
205 |
11 |
cmd->toStream(ss); |
206 |
|
} |
207 |
|
|
208 |
212734 |
if (solver->getOptionInfo("parse-only").boolValue()) |
209 |
|
{ |
210 |
|
return true; |
211 |
|
} |
212 |
|
|
213 |
212734 |
cmd->invoke(solver, sm, out); |
214 |
|
// ignore the error if the command-verbosity is 0 for this command |
215 |
|
std::string commandName = |
216 |
425468 |
std::string("command-verbosity:") + cmd->getCommandName(); |
217 |
212734 |
if (solver->getOption(commandName) == "0") |
218 |
|
{ |
219 |
|
return true; |
220 |
|
} |
221 |
212734 |
return !cmd->fail(); |
222 |
|
} |
223 |
|
|
224 |
3883 |
void CommandExecutor::flushOutputStreams() { |
225 |
3883 |
printStatistics(d_solver->getDriverOptions().err()); |
226 |
|
|
227 |
|
// make sure out and err streams are flushed too |
228 |
3883 |
d_solver->getDriverOptions().out() << std::flush; |
229 |
3883 |
d_solver->getDriverOptions().err() << std::flush; |
230 |
3883 |
} |
231 |
|
|
232 |
|
} // namespace main |
233 |
22734 |
} // namespace cvc5 |