Skip to content

Commit

Permalink
Use steady_clock instead of system_clock for timing (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Dec 14, 2024
1 parent cf1e097 commit 6683ad5
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 69 deletions.
8 changes: 4 additions & 4 deletions benchmarks/scalability/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void RunBenchmark(std::ofstream& results,
sleipnir::function_ref<Problem()> setup,
sleipnir::function_ref<void(Problem& problem)> solve) {
// Record setup time
auto setupStartTime = std::chrono::system_clock::now();
auto setupStartTime = std::chrono::steady_clock::now();
auto problem = setup();
auto setupEndTime = std::chrono::system_clock::now();
auto setupEndTime = std::chrono::steady_clock::now();

results << ToMilliseconds(setupEndTime - setupStartTime);
std::flush(results);
Expand All @@ -53,9 +53,9 @@ void RunBenchmark(std::ofstream& results,
std::flush(results);

// Record solve time
auto solveStartTime = std::chrono::system_clock::now();
auto solveStartTime = std::chrono::steady_clock::now();
solve(problem);
auto solveEndTime = std::chrono::system_clock::now();
auto solveEndTime = std::chrono::steady_clock::now();

results << ToMilliseconds(solveEndTime - solveStartTime);
std::flush(results);
Expand Down
10 changes: 5 additions & 5 deletions cart-pole-scalability-results-casadi.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Samples,Setup time (ms),Solve time (ms)
100,39.725,1769.35
150,63.212,3550.44
200,93.989,4641.65
250,128.804,6928.38
300,173.019,9095.47
100,41.523,1845.5
150,66.571,3654.27
200,96.399,4777.22
250,134.222,7152.82
300,178.536,9550.88
10 changes: 5 additions & 5 deletions cart-pole-scalability-results-sleipnir.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Samples,Setup time (ms),Solve time (ms)
100,1.628,487.234
150,1.094,1540.46
200,2.138,2418.88
250,2.146,3156.54
300,2.84,5273.54
100,3.943,587.778
150,1.201,1775.05
200,1.601,2690.66
250,2.472,3877.39
300,3.11,5177.69
Binary file modified cart-pole-scalability-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions flywheel-scalability-results-casadi.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Samples,Setup time (ms),Solve time (ms)
100,2.686,22.266
200,3.603,34.24
300,5.603,52.236
400,7.337,68.693
500,8.819,88.432
600,10.678,106.75
700,12.94,122.699
800,14.558,138.024
900,16.137,161.657
1000,20.091,181.467
2000,38.56,469.203
3000,57.943,838.244
4000,74.197,1276.26
5000,95.67,1795.55
100,2.627,39.004
200,3.734,32.777
300,5.5,51.845
400,7.462,71.102
500,9.127,92.331
600,10.9,120.477
700,12.644,131.438
800,16.365,145.515
900,18.325,168.521
1000,20.555,207.737
2000,39.378,530.821
3000,58.111,972.038
4000,77.53,1487.41
5000,95.605,2082.94
28 changes: 14 additions & 14 deletions flywheel-scalability-results-sleipnir.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Samples,Setup time (ms),Solve time (ms)
100,0.305,2.647
200,0.104,4.115
300,0.153,6.395
400,0.208,8.825
500,0.258,11.78
600,0.313,21.697
700,0.381,24.181
800,0.485,25.928
900,0.513,28.487
1000,0.569,33.35
2000,1.357,72.244
3000,1.925,133.532
4000,3.516,208.585
5000,4.462,301.811
100,0.267,2.18
200,0.115,4.305
300,0.166,6.45
400,0.217,9.12
500,0.281,12.003
600,0.359,19.209
700,0.383,18.082
800,0.448,21.112
900,0.63,24.82
1000,0.566,29.85
2000,1.544,70.348
3000,1.945,149.286
4000,3.692,200.443
5000,4.021,353.937
Binary file modified flywheel-scalability-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions include/sleipnir/autodiff/Profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ class SLEIPNIR_DLLEXPORT Profiler {
/**
* Tell the profiler to start measuring setup time.
*/
void StartSetup() { m_setupStartTime = std::chrono::system_clock::now(); }
void StartSetup() { m_setupStartTime = std::chrono::steady_clock::now(); }

/**
* Tell the profiler to stop measuring setup time.
*/
void StopSetup() {
m_setupDuration = std::chrono::system_clock::now() - m_setupStartTime;
m_setupDuration = std::chrono::steady_clock::now() - m_setupStartTime;
}

/**
* Tell the profiler to start measuring solve time.
*/
void StartSolve() { m_solveStartTime = std::chrono::system_clock::now(); }
void StartSolve() { m_solveStartTime = std::chrono::steady_clock::now(); }

/**
* Tell the profiler to stop measuring solve time, increment the number of
* averages, and incorporate the latest measurement into the average.
*/
void StopSolve() {
auto now = std::chrono::system_clock::now();
auto now = std::chrono::steady_clock::now();
++m_solveMeasurements;
m_averageSolveDuration =
(m_solveMeasurements - 1.0) / m_solveMeasurements *
Expand Down Expand Up @@ -68,12 +68,12 @@ class SLEIPNIR_DLLEXPORT Profiler {
}

private:
std::chrono::system_clock::time_point m_setupStartTime;
std::chrono::steady_clock::time_point m_setupStartTime;
std::chrono::duration<double> m_setupDuration{0.0};

int m_solveMeasurements = 0;
std::chrono::duration<double> m_averageSolveDuration{0.0};
std::chrono::system_clock::time_point m_solveStartTime;
std::chrono::steady_clock::time_point m_solveStartTime;
};

} // namespace sleipnir
14 changes: 7 additions & 7 deletions src/optimization/solver/InteriorPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void InteriorPoint(std::span<Variable> decisionVariables,
const SolverConfig& config, bool feasibilityRestoration,
Eigen::VectorXd& x, Eigen::VectorXd& s,
SolverStatus* status) {
const auto solveStartTime = std::chrono::system_clock::now();
const auto solveStartTime = std::chrono::steady_clock::now();

// Map decision variables and constraints to VariableMatrices for Lagrangian
VariableMatrix xAD{decisionVariables};
Expand Down Expand Up @@ -137,7 +137,7 @@ void InteriorPoint(std::span<Variable> decisionVariables,
sleipnir::println("Error tolerance: {}\n", config.tolerance);
}

std::chrono::system_clock::time_point iterationsStartTime;
std::chrono::steady_clock::time_point iterationsStartTime;

int iterations = 0;

Expand All @@ -146,7 +146,7 @@ void InteriorPoint(std::span<Variable> decisionVariables,
status->cost = f.Value();

if (config.diagnostics && !feasibilityRestoration) {
auto solveEndTime = std::chrono::system_clock::now();
auto solveEndTime = std::chrono::steady_clock::now();

sleipnir::println("\nSolve time: {:.3f} ms",
ToMilliseconds(solveEndTime - solveStartTime));
Expand Down Expand Up @@ -241,14 +241,14 @@ void InteriorPoint(std::span<Variable> decisionVariables,
double E_0 = std::numeric_limits<double>::infinity();

if (config.diagnostics) {
iterationsStartTime = std::chrono::system_clock::now();
iterationsStartTime = std::chrono::steady_clock::now();
}

while (E_0 > config.tolerance &&
acceptableIterCounter < config.maxAcceptableIterations) {
std::chrono::system_clock::time_point innerIterStartTime;
std::chrono::steady_clock::time_point innerIterStartTime;
if (config.diagnostics) {
innerIterStartTime = std::chrono::system_clock::now();
innerIterStartTime = std::chrono::steady_clock::now();
}

// Check for local equality constraint infeasibility
Expand Down Expand Up @@ -784,7 +784,7 @@ void InteriorPoint(std::span<Variable> decisionVariables,
}
}

const auto innerIterEndTime = std::chrono::system_clock::now();
const auto innerIterEndTime = std::chrono::steady_clock::now();

// Diagnostics for current iteration
if (config.diagnostics) {
Expand Down
14 changes: 7 additions & 7 deletions src/optimization/solver/Newton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Newton(std::span<Variable> decisionVariables, Variable& f,
function_ref<bool(const SolverIterationInfo& info)> callback,
const SolverConfig& config, Eigen::VectorXd& x,
SolverStatus* status) {
const auto solveStartTime = std::chrono::system_clock::now();
const auto solveStartTime = std::chrono::steady_clock::now();

// Map decision variables and constraints to VariableMatrices for Lagrangian
VariableMatrix xAD{decisionVariables};
Expand Down Expand Up @@ -68,7 +68,7 @@ void Newton(std::span<Variable> decisionVariables, Variable& f,
sleipnir::println("Error tolerance: {}\n", config.tolerance);
}

std::chrono::system_clock::time_point iterationsStartTime;
std::chrono::steady_clock::time_point iterationsStartTime;

int iterations = 0;

Expand All @@ -77,7 +77,7 @@ void Newton(std::span<Variable> decisionVariables, Variable& f,
status->cost = f.Value();

if (config.diagnostics) {
auto solveEndTime = std::chrono::system_clock::now();
auto solveEndTime = std::chrono::steady_clock::now();

sleipnir::println("\nSolve time: {:.3f} ms",
ToMilliseconds(solveEndTime - solveStartTime));
Expand Down Expand Up @@ -118,14 +118,14 @@ void Newton(std::span<Variable> decisionVariables, Variable& f,
double E_0 = std::numeric_limits<double>::infinity();

if (config.diagnostics) {
iterationsStartTime = std::chrono::system_clock::now();
iterationsStartTime = std::chrono::steady_clock::now();
}

while (E_0 > config.tolerance &&
acceptableIterCounter < config.maxAcceptableIterations) {
std::chrono::system_clock::time_point innerIterStartTime;
std::chrono::steady_clock::time_point innerIterStartTime;
if (config.diagnostics) {
innerIterStartTime = std::chrono::system_clock::now();
innerIterStartTime = std::chrono::steady_clock::now();
}

// Check for diverging iterates
Expand Down Expand Up @@ -248,7 +248,7 @@ void Newton(std::span<Variable> decisionVariables, Variable& f,
acceptableIterCounter = 0;
}

const auto innerIterEndTime = std::chrono::system_clock::now();
const auto innerIterEndTime = std::chrono::steady_clock::now();

// Diagnostics for current iteration
if (config.diagnostics) {
Expand Down
14 changes: 7 additions & 7 deletions src/optimization/solver/SQP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void SQP(std::span<Variable> decisionVariables,
std::span<Variable> equalityConstraints, Variable& f,
function_ref<bool(const SolverIterationInfo& info)> callback,
const SolverConfig& config, Eigen::VectorXd& x, SolverStatus* status) {
const auto solveStartTime = std::chrono::system_clock::now();
const auto solveStartTime = std::chrono::steady_clock::now();

// Map decision variables and constraints to VariableMatrices for Lagrangian
VariableMatrix xAD{decisionVariables};
Expand Down Expand Up @@ -110,7 +110,7 @@ void SQP(std::span<Variable> decisionVariables,
sleipnir::println("Error tolerance: {}\n", config.tolerance);
}

std::chrono::system_clock::time_point iterationsStartTime;
std::chrono::steady_clock::time_point iterationsStartTime;

int iterations = 0;

Expand All @@ -119,7 +119,7 @@ void SQP(std::span<Variable> decisionVariables,
status->cost = f.Value();

if (config.diagnostics) {
auto solveEndTime = std::chrono::system_clock::now();
auto solveEndTime = std::chrono::steady_clock::now();

sleipnir::println("\nSolve time: {:.3f} ms",
ToMilliseconds(solveEndTime - solveStartTime));
Expand Down Expand Up @@ -169,14 +169,14 @@ void SQP(std::span<Variable> decisionVariables,
double E_0 = std::numeric_limits<double>::infinity();

if (config.diagnostics) {
iterationsStartTime = std::chrono::system_clock::now();
iterationsStartTime = std::chrono::steady_clock::now();
}

while (E_0 > config.tolerance &&
acceptableIterCounter < config.maxAcceptableIterations) {
std::chrono::system_clock::time_point innerIterStartTime;
std::chrono::steady_clock::time_point innerIterStartTime;
if (config.diagnostics) {
innerIterStartTime = std::chrono::system_clock::now();
innerIterStartTime = std::chrono::steady_clock::now();
}

// Check for local equality constraint infeasibility
Expand Down Expand Up @@ -518,7 +518,7 @@ void SQP(std::span<Variable> decisionVariables,
acceptableIterCounter = 0;
}

const auto innerIterEndTime = std::chrono::system_clock::now();
const auto innerIterEndTime = std::chrono::steady_clock::now();

// Diagnostics for current iteration
if (config.diagnostics) {
Expand Down

0 comments on commit 6683ad5

Please sign in to comment.