Skip to content

Commit

Permalink
Merge pull request #2124 from ERGO-Code/refactor-timer
Browse files Browse the repository at this point in the history
Refactor HighsTimer
  • Loading branch information
jajhall authored Jan 12, 2025
2 parents 750452a + 097fe20 commit 1a6a674
Show file tree
Hide file tree
Showing 23 changed files with 180 additions and 261 deletions.
2 changes: 1 addition & 1 deletion src/Highs.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class Highs {
/**
* @brief Get the run time of HiGHS
*/
double getRunTime() { return timer_.readRunHighsClock(); }
double getRunTime() { return timer_.read(); }

/**
* Methods for model output
Expand Down
2 changes: 1 addition & 1 deletion src/ipm/IpxWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ HighsStatus solveLpIpx(const HighsOptions& options, HighsTimer& timer,
parameters.analyse_basis_data =
kHighsAnalysisLevelNlaData & options.highs_analysis_level;
// Determine the run time allowed for IPX
parameters.time_limit = options.time_limit - timer.readRunHighsClock();
parameters.time_limit = options.time_limit - timer.read();
parameters.ipm_maxiter =
options.ipm_iteration_limit - highs_info.ipm_iteration_count;
// Determine if crossover is to be run or not
Expand Down
26 changes: 13 additions & 13 deletions src/lp_data/Highs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ HighsStatus Highs::solve() {
// Zero the iteration counts
zeroIterationCounts();
// Start the HiGHS run clock
timer_.startRunHighsClock();
timer_.start();
// Return immediately if the model has no columns
if (!model_.lp_.num_col_) {
setHighsModelStatusAndClearSolutionAndBasis(HighsModelStatus::kModelEmpty);
Expand Down Expand Up @@ -1166,7 +1166,7 @@ HighsStatus Highs::solve() {
//
// Record the initial time and set the component times and postsolve
// iteration count to -1 to identify whether they are not required
double initial_time = timer_.readRunHighsClock();
double initial_time = timer_.read();
double this_presolve_time = -1;
double this_solve_presolved_lp_time = -1;
double this_postsolve_time = -1;
Expand Down Expand Up @@ -1222,7 +1222,7 @@ HighsStatus Highs::solve() {
return returnFromRun(crossover_status, undo_mods);
assert(options_.simplex_strategy == kSimplexStrategyPrimal);
}
// timer_.stopRunHighsClock();
// timer_.stop();
// run();

// todo: add "dual" values
Expand Down Expand Up @@ -1629,7 +1629,7 @@ HighsStatus Highs::solve() {
// HiGHS info is valid
if (!no_incumbent_lp_solution_or_basis) info_.valid = true;

double lp_solve_final_time = timer_.readRunHighsClock();
double lp_solve_final_time = timer_.read();
double this_solve_time = lp_solve_final_time - initial_time;
if (postsolve_iteration_count < 0) {
highsLogDev(log_options, HighsLogType::kInfo, "Postsolve : \n");
Expand Down Expand Up @@ -3431,9 +3431,9 @@ HighsPresolveStatus Highs::runPresolve(const bool force_lp_presolve,
if (original_lp.num_col_ == 0 && original_lp.num_row_ == 0)
return HighsPresolveStatus::kNullError;

// Ensure that the RunHighsClock is running
if (!timer_.runningRunHighsClock()) timer_.startRunHighsClock();
double start_presolve = timer_.readRunHighsClock();
// Ensure that the timer is running
if (!timer_.running()) timer_.start();
double start_presolve = timer_.read();

// Set time limit.
if (options_.time_limit > 0 && options_.time_limit < kHighsInf) {
Expand All @@ -3459,9 +3459,9 @@ HighsPresolveStatus Highs::runPresolve(const bool force_lp_presolve,
// Presolved model is extracted now since it's part of solver,
// which is lost on return
HighsMipSolver solver(callback_, options_, original_lp, solution_);
// Start the MIP solver's total clock so that timeout in presolve
// can be identified
solver.timer_.start(timer_.total_clock);
// Start the MIP solver's timer so that timeout in presolve can be
// identified
solver.timer_.start();
// Only place that HighsMipSolver::runPresolve is called
solver.runPresolve(options_.presolve_reduction_limit);
presolve_return_status = solver.getPresolveStatus();
Expand All @@ -3475,7 +3475,7 @@ HighsPresolveStatus Highs::runPresolve(const bool force_lp_presolve,
presolve_.init(original_lp, timer_);
presolve_.options_ = &options_;
if (options_.time_limit > 0 && options_.time_limit < kHighsInf) {
double current = timer_.readRunHighsClock();
double current = timer_.read();
double time_init = current - start_presolve;
double left = presolve_.options_->time_limit - time_init;
if (left <= 0) {
Expand Down Expand Up @@ -4530,7 +4530,7 @@ HighsStatus Highs::returnFromHighs(HighsStatus highs_return_status) {
assert(called_return_from_run);
}
// Stop the HiGHS run clock if it is running
if (timer_.runningRunHighsClock()) timer_.stopRunHighsClock();
if (timer_.running()) timer_.stop();
const bool dimensions_ok =
lpDimensionsOk("returnFromHighs", model_.lp_, options_.log_options);
if (!dimensions_ok) {
Expand Down Expand Up @@ -4594,7 +4594,7 @@ void Highs::reportSolvedLpQpStats() {
highsLogUser(log_options, HighsLogType::kInfo,
"Relative P-D gap : %17.10e\n", relative_primal_dual_gap);
}
double run_time = timer_.readRunHighsClock();
double run_time = timer_.read();
highsLogUser(log_options, HighsLogType::kInfo,
"HiGHS run time : %13.2f\n", run_time);
}
Expand Down
6 changes: 3 additions & 3 deletions src/mip/HighsLpRelaxation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,9 @@ void HighsLpRelaxation::setObjectiveLimit(double objlim) {
}

HighsLpRelaxation::Status HighsLpRelaxation::run(bool resolve_on_error) {
lpsolver.setOptionValue(
"time_limit", lpsolver.getRunTime() + mipsolver.options_mip_->time_limit -
mipsolver.timer_.read(mipsolver.timer_.total_clock));
lpsolver.setOptionValue("time_limit", lpsolver.getRunTime() +
mipsolver.options_mip_->time_limit -
mipsolver.timer_.read());
// lpsolver.setOptionValue("output_flag", true);
const bool valid_basis = lpsolver.getBasis().valid;
const HighsInt simplex_solve_clock = valid_basis
Expand Down
18 changes: 7 additions & 11 deletions src/mip/HighsMipSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ void HighsMipSolver::run() {
analysis_.timer_ = &this->timer_;
analysis_.setup(*orig_model_, *options_mip_);
}
// Start the total_clock for the timer that is local to the HighsMipSolver
// instance
timer_.start(timer_.total_clock);
timer_.start();

improving_solution_file_ = nullptr;
if (!submip && options_mip_->mip_improving_solution_file != "")
Expand All @@ -136,7 +134,7 @@ void HighsMipSolver::run() {
"MIP-Timing: %11.2g - completed presolve\n", timer_.read());
// Identify whether time limit has been reached (in presolve)
if (modelstatus_ == HighsModelStatus::kNotset &&
timer_.read(timer_.total_clock) >= options_mip_->time_limit)
timer_.read() >= options_mip_->time_limit)
modelstatus_ = HighsModelStatus::kTimeLimit;

if (modelstatus_ != HighsModelStatus::kNotset) {
Expand All @@ -163,8 +161,7 @@ void HighsMipSolver::run() {
analysis_.mipTimerStop(kMipClockRunSetup);
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed setup\n",
timer_.read(timer_.total_clock));
"MIP-Timing: %11.2g - completed setup\n", timer_.read());
restart:
if (modelstatus_ == HighsModelStatus::kNotset) {
// Check limits have not been reached before evaluating root node
Expand All @@ -186,7 +183,7 @@ void HighsMipSolver::run() {
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - starting evaluate root node\n",
timer_.read(timer_.total_clock));
timer_.read());
analysis_.mipTimerStart(kMipClockEvaluateRootNode);
mipdata_->evaluateRootNode();
analysis_.mipTimerStop(kMipClockEvaluateRootNode);
Expand All @@ -198,7 +195,7 @@ void HighsMipSolver::run() {
if (analysis_.analyse_mip_time & !submip)
highsLogUser(options_mip_->log_options, HighsLogType::kInfo,
"MIP-Timing: %11.2g - completed evaluate root node\n",
timer_.read(timer_.total_clock));
timer_.read());
// age 5 times to remove stored but never violated cuts after root
// separation
analysis_.mipTimerStart(kMipClockPerformAging0);
Expand Down Expand Up @@ -718,7 +715,7 @@ void HighsMipSolver::cleanupSolve() {
}

analysis_.mipTimerStop(kMipClockPostsolve);
timer_.stop(timer_.total_clock);
timer_.stop();

std::string solutionstatus = "-";

Expand Down Expand Up @@ -804,8 +801,7 @@ void HighsMipSolver::cleanupSolve() {
" %llu (strong br.)\n"
" %llu (separation)\n"
" %llu (heuristics)\n",
timer_.read(timer_.total_clock),
analysis_.mipTimerRead(kMipClockPresolve),
timer_.read(), analysis_.mipTimerRead(kMipClockPresolve),
analysis_.mipTimerRead(kMipClockSolve),
analysis_.mipTimerRead(kMipClockPostsolve),
int(max_submip_level), (long long unsigned)mipdata_->num_nodes,
Expand Down
18 changes: 7 additions & 11 deletions src/mip/HighsMipSolverData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,10 +1091,8 @@ double HighsMipSolverData::transformNewIntegerFeasibleSolution(
}
}
this->total_repair_lp++;
double time_available =
std::max(mipsolver.options_mip_->time_limit -
mipsolver.timer_.read(mipsolver.timer_.total_clock),
0.1);
double time_available = std::max(
mipsolver.options_mip_->time_limit - mipsolver.timer_.read(), 0.1);
Highs tmpSolver;
const bool debug_report = false;
if (debug_report) {
Expand Down Expand Up @@ -1598,7 +1596,7 @@ void HighsMipSolverData::printDisplayLine(const int solution_source) {
bool output_flag = *mipsolver.options_mip_->log_options.output_flag;
if (!output_flag) return;

double time = mipsolver.timer_.read(mipsolver.timer_.total_clock);
double time = mipsolver.timer_.read();
if (solution_source == kSolutionSourceNone &&
time - last_disptime < mipsolver.options_mip_->mip_min_logging_interval)
return;
Expand Down Expand Up @@ -2377,11 +2375,10 @@ bool HighsMipSolverData::checkLimits(int64_t nodeOffset) const {
return true;
}

// const double time = mipsolver.timer_.read(mipsolver.timer_.total_clock);
// const double time = mipsolver.timer_.read();
// printf("checkLimits: time = %g\n", time);
if (options.time_limit < kHighsInf &&
mipsolver.timer_.read(mipsolver.timer_.total_clock) >=
options.time_limit) {
mipsolver.timer_.read() >= options.time_limit) {
if (mipsolver.modelstatus_ == HighsModelStatus::kNotset) {
highsLogDev(options.log_options, HighsLogType::kInfo,
"Reached time limit\n");
Expand Down Expand Up @@ -2487,8 +2484,7 @@ bool HighsMipSolverData::interruptFromCallbackWithData(
double primal_bound;
double mip_rel_gap;
limitsToBounds(dual_bound, primal_bound, mip_rel_gap);
mipsolver.callback_->data_out.running_time =
mipsolver.timer_.read(mipsolver.timer_.total_clock);
mipsolver.callback_->data_out.running_time = mipsolver.timer_.read();
mipsolver.callback_->data_out.objective_function_value =
mipsolver_objective_value;
mipsolver.callback_->data_out.mip_node_count = mipsolver.mipdata_->num_nodes;
Expand Down Expand Up @@ -2617,7 +2613,7 @@ void HighsMipSolverData::updatePrimalDualIntegral(const double from_lower_bound,
assert(gap_consistent);
}
if (to_gap < kHighsInf) {
double time = mipsolver.timer_.read(mipsolver.timer_.total_clock);
double time = mipsolver.timer_.read();
if (from_gap < kHighsInf) {
// Need to update the P-D integral
double time_diff = time - pdi.prev_time;
Expand Down
2 changes: 1 addition & 1 deletion src/mip/HighsModkSeparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HighsModkSeparator : public HighsSeparator {
HighsCutPool& cutpool) override;

HighsModkSeparator(const HighsMipSolver& mipsolver)
: HighsSeparator(mipsolver, "Mod-k sepa", "Mod") {}
: HighsSeparator(mipsolver, "Mod-k sepa") {}
};

#endif
2 changes: 1 addition & 1 deletion src/mip/HighsPathSeparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HighsPathSeparator : public HighsSeparator {
HighsCutPool& cutpool) override;

HighsPathSeparator(const HighsMipSolver& mipsolver)
: HighsSeparator(mipsolver, "PathAggr sepa", "Agg") {
: HighsSeparator(mipsolver, "PathAggr sepa") {
randgen.initialise(mipsolver.options_mip_->random_seed);
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/mip/HighsPrimalHeuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ bool HighsPrimalHeuristics::solveSubMip(
submipoptions.mip_max_nodes = maxnodes;
submipoptions.mip_max_stall_nodes = stallnodes;
submipoptions.mip_pscost_minreliable = 0;
submipoptions.time_limit -=
mipsolver.timer_.read(mipsolver.timer_.total_clock);
submipoptions.time_limit -= mipsolver.timer_.read();
submipoptions.objective_bound = mipsolver.mipdata_->upper_limit;

if (!mipsolver.submip) {
Expand Down
4 changes: 2 additions & 2 deletions src/mip/HighsSeparation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include "mip/HighsTransformedLp.h"

HighsSeparation::HighsSeparation(const HighsMipSolver& mipsolver) {
implBoundClock = mipsolver.timer_.clock_def("Implbound sepa", "Ibd");
cliqueClock = mipsolver.timer_.clock_def("Clique sepa", "Clq");
implBoundClock = mipsolver.timer_.clock_def("Implbound sepa");
cliqueClock = mipsolver.timer_.clock_def("Clique sepa");
separators.emplace_back(new HighsTableauSeparator(mipsolver));
separators.emplace_back(new HighsPathSeparator(mipsolver));
separators.emplace_back(new HighsModkSeparator(mipsolver));
Expand Down
4 changes: 2 additions & 2 deletions src/mip/HighsSeparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "mip/HighsMipSolver.h"

HighsSeparator::HighsSeparator(const HighsMipSolver& mipsolver,
const char* name, const char* ch3_name)
const char* name)
: numCutsFound(0), numCalls(0) {
clockIndex = mipsolver.timer_.clock_def(name, ch3_name);
clockIndex = mipsolver.timer_.clock_def(name);
}

void HighsSeparator::run(HighsLpRelaxation& lpRelaxation,
Expand Down
3 changes: 1 addition & 2 deletions src/mip/HighsSeparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class HighsSeparator {
int clockIndex;

public:
HighsSeparator(const HighsMipSolver& mipsolver, const char* name,
const char* ch3_name);
HighsSeparator(const HighsMipSolver& mipsolver, const char* name);

virtual void separateLpSolution(HighsLpRelaxation& lpRelaxation,
HighsLpAggregator& lpAggregator,
Expand Down
2 changes: 1 addition & 1 deletion src/mip/HighsTableauSeparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HighsTableauSeparator : public HighsSeparator {
HighsCutPool& cutpool) override;

HighsTableauSeparator(const HighsMipSolver& mipsolver)
: HighsSeparator(mipsolver, "Tableau sepa", "Tbl"), numTries(0) {}
: HighsSeparator(mipsolver, "Tableau sepa"), numTries(0) {}
};

#endif
2 changes: 1 addition & 1 deletion src/mip/MipTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MipTimer {
HighsTimer* timer_pointer = mip_timer_clock.timer_pointer_;
std::vector<HighsInt>& clock = mip_timer_clock.clock_;
clock.resize(kNumMipClock);
clock[kMipClockTotal] = timer_pointer->total_clock;
clock[kMipClockTotal] = 0;
clock[kMipClockPresolve] = timer_pointer->clock_def("MIP presolve");
clock[kMipClockSolve] = timer_pointer->clock_def("MIP solve");
clock[kMipClockPostsolve] = timer_pointer->clock_def("MIP postsolve");
Expand Down
2 changes: 1 addition & 1 deletion src/presolve/HPresolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4109,7 +4109,7 @@ HPresolve::Result HPresolve::presolve(HighsPostsolveStack& postsolve_stack) {
// the timer is well defined, and that its total time clock is
// running
assert(this->timer);
assert(this->timer->runningRunHighsClock());
assert(this->timer->running());

HPRESOLVE_CHECKED_CALL(initialRowAndColPresolve(postsolve_stack));

Expand Down
2 changes: 1 addition & 1 deletion src/qpsolver/feasibility_highs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void computeStartingPointHighs(
highs.setOptionValue("presolve", "on");
// Set the residual time limit
const double use_time_limit =
std::max(settings.time_limit - timer.readRunHighsClock(), 0.001);
std::max(settings.time_limit - timer.read(), 0.001);
highs.setOptionValue("time_limit", use_time_limit);

HighsLp lp;
Expand Down
6 changes: 3 additions & 3 deletions src/qpsolver/quass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void loginformation(Runtime& rt, Basis& basis, CholeskyFactor& factor,
rt.statistics.nullspacedimension.push_back(rt.instance.num_var -
basis.getnumactive());
rt.statistics.objval.push_back(rt.instance.objval(rt.primal));
rt.statistics.time.push_back(timer.readRunHighsClock());
rt.statistics.time.push_back(timer.read());
SumNum sm =
rt.instance.sumnumprimalinfeasibilities(rt.primal, rt.rowactivity);
rt.statistics.sum_primal_infeasibilities.push_back(sm.sum);
Expand Down Expand Up @@ -337,7 +337,7 @@ void Quass::solve(const QpVector& x0, const QpVector& ra, Basis& b0,
}

// check time limit
if (timer.readRunHighsClock() >= runtime.settings.time_limit) {
if (timer.read() >= runtime.settings.time_limit) {
runtime.status = QpModelStatus::kTimeLimit;
break;
}
Expand All @@ -350,7 +350,7 @@ void Quass::solve(const QpVector& x0, const QpVector& ra, Basis& b0,
}

// LOGGING
double run_time = timer.readRunHighsClock();
double run_time = timer.read();
if ((runtime.statistics.num_iterations %
runtime.settings.reportingfequency ==
0 ||
Expand Down
2 changes: 1 addition & 1 deletion src/simplex/HEkk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3468,7 +3468,7 @@ bool HEkk::bailout() {
model_status_ == HighsModelStatus::kObjectiveBound ||
model_status_ == HighsModelStatus::kObjectiveTarget);
} else if (options_->time_limit < kHighsInf &&
timer_->readRunHighsClock() > options_->time_limit) {
timer_->read() > options_->time_limit) {
solve_bailout_ = true;
model_status_ = HighsModelStatus::kTimeLimit;
} else if (iteration_count_ >= options_->simplex_iteration_limit) {
Expand Down
2 changes: 1 addition & 1 deletion src/simplex/HighsSimplexAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void HighsSimplexAnalysis::userInvertReport(const bool force) {

void HighsSimplexAnalysis::userInvertReport(const bool header,
const bool force) {
const double highs_run_time = timer_->readRunHighsClock();
const double highs_run_time = timer_->read();
if (!force && highs_run_time < last_user_log_time + delta_user_log_time)
return;
analysis_log = std::unique_ptr<std::stringstream>(new std::stringstream());
Expand Down
Loading

0 comments on commit 1a6a674

Please sign in to comment.