5#ifndef LINE_UTIL_LINE_TIMEOUT_H
6#define LINE_UTIL_LINE_TIMEOUT_H
60 std::chrono::steady_clock::time_point
start;
65inline std::vector<Frame>&
stack() {
66 thread_local std::vector<Frame> s;
71 return std::chrono::duration<double>(std::chrono::steady_clock::now() - f.
start).count();
79 if (s.empty())
return std::numeric_limits<double>::infinity();
81 return r > 0.0 ? r : 0.0;
94 if (s.empty())
return false;
107 return !s.empty() && s.back().tripped;
117 if (!s.empty()) s.back().tripped =
true;
128 ": the wall-clock time budget (options.timeout) ran out before the "
129 "computation finished, and it has no valid partial result");
139 if (!std::isfinite(r))
return 0;
140 const double s = std::ceil(r + grace);
141 return s < 1.0 ? 1 : (s > 2.0e9 ? 2000000000 :
static_cast<int>(s));
150 if (explicit_seconds <= 0)
return b;
151 return (b > 0 && b < explicit_seconds) ? b : explicit_seconds;
162 who +
" was killed on the wall-clock time budget (options.timeout) "
163 "before it wrote a result, so there is no partial solution to return");
188 bool active()
const {
return depth_ > 0; }
195 if (depth_ == 0)
return;
197 if (s.size() >= depth_) {
198 bool tripped =
false;
199 for (std::size_t k = depth_ - 1; k < s.size(); ++k) tripped = tripped || s[k].tripped;
200 s.resize(depth_ - 1);
201 if (tripped && !s.empty()) s.back().tripped =
true;
215 if (!(budget > 0.0) || !std::isfinite(budget))
return TimeoutScope();
219 f.
start = std::chrono::steady_clock::now();
220 f.
budget = budget < outer ? budget : outer;
232 "[LINE] Warning: %s stopped early on the wall-clock time budget "
233 "(options.timeout=%gs); the returned metrics are the interim solution.\n",
234 solver.c_str(), budget);
NumericError(const std::string &what)
The RAII frame timeout_arm returns.
TimeoutScope & operator=(TimeoutScope &&o) noexcept
void pop()
Pop the frame now (idempotent).
TimeoutScope(std::size_t depth)
TimeoutScope & operator=(const TimeoutScope &)=delete
TimeoutScope(const TimeoutScope &)=delete
TimeoutScope(TimeoutScope &&o) noexcept
bool active() const
Whether this scope pushed a frame.
The exception types the port throws.
double elapsed(const Frame &f)
std::vector< Frame > & stack()
bool timeout_exceeded()
The checkpoint: true when the innermost budget has run out, marking it tripped.
void timeout_trip()
Mark the innermost budget tripped without reading the clock: for an external engine that REPORTS it s...
TimeoutScope timeout_arm(double budget)
Arm budget seconds for the scope's lifetime, capped by the outer frame's remaining time.
int timeout_kill_bound(int explicit_seconds, double grace=30.0)
The kill bound of a child that also has a bound of its OWN (explicit_seconds, not positive = none): t...
double timeout_remaining()
Seconds left on the innermost armed budget; +Inf when none is armed.
int timeout_kill_seconds(double grace=30.0)
The hard kill bound of a child process under the armed budget: the remaining seconds plus grace,...
void timeout_check(const char *where)
The raising checkpoint, for a computation with no valid partial result: throws LINE:numerical:Timeout...
bool timeout_tripped()
Whether the innermost budget was tripped by a checkpoint or by timeout_trip.
bool timeout_armed()
Whether any budget is armed on this thread.
void timeout_warn(const std::string &solver, double budget)
The one warning a solve that stopped on its budget prints, MATLAB's flagTimeout text,...
void timeout_raise_if_killed_on_budget(const std::string &who)
A child process was killed on its bound: raise LINE:numerical:Timeout when the armed budget is what r...
Conservation laws of a layered queueing network, enumerated from its structure.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
std::chrono::steady_clock::time_point start
double budget
seconds, finite and positive