Skip to content

Commit

Permalink
Expand timing info in final diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Jan 30, 2025
1 parent 79d3938 commit f2e80e8
Show file tree
Hide file tree
Showing 13 changed files with 684 additions and 426 deletions.
8 changes: 5 additions & 3 deletions include/sleipnir/autodiff/Gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <Eigen/SparseCore>

#include "sleipnir/autodiff/Jacobian.hpp"
#include "sleipnir/autodiff/Profiler.hpp"
#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/autodiff/VariableMatrix.hpp"
#include "sleipnir/util/SolveProfiler.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand Down Expand Up @@ -60,9 +60,11 @@ class SLEIPNIR_DLLEXPORT Gradient {
}

/**
* Returns the profiler.
* Returns the solve profiler.
*/
Profiler& GetProfiler() { return m_jacobian.GetProfiler(); }
const SolveProfiler& GetSolveProfiler() const {
return m_jacobian.GetSolveProfiler();
}

private:
Eigen::SparseVector<double> m_g;
Expand Down
17 changes: 7 additions & 10 deletions include/sleipnir/autodiff/Hessian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "sleipnir/autodiff/AdjointExpressionGraph.hpp"
#include "sleipnir/autodiff/Jacobian.hpp"
#include "sleipnir/autodiff/Profiler.hpp"
#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/autodiff/VariableMatrix.hpp"
#include "sleipnir/util/SolveProfiler.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand All @@ -31,13 +31,10 @@ class SLEIPNIR_DLLEXPORT Hessian {
*/
Hessian(Variable variable, const VariableMatrix& wrt) noexcept
: m_jacobian{[&] {
m_profiler.StartSetup();
return detail::AdjointExpressionGraph{variable}
.GenerateGradientTree(wrt);
}(),
wrt} {
m_profiler.StopSetup();
}
wrt} {}

/**
* Returns the Hessian as a VariableMatrix.
Expand All @@ -51,19 +48,19 @@ class SLEIPNIR_DLLEXPORT Hessian {
* Evaluates the Hessian at wrt's value.
*/
const Eigen::SparseMatrix<double>& Value() {
m_profiler.StartSolve();
m_solveProfiler.Start();
const auto& H = m_jacobian.Value();
m_profiler.StopSolve();
m_solveProfiler.Stop();
return H;
}

/**
* Returns the profiler.
* Returns the solve profiler.
*/
Profiler& GetProfiler() { return m_profiler; }
const SolveProfiler& GetSolveProfiler() const { return m_solveProfiler; }

private:
Profiler m_profiler;
SolveProfiler m_solveProfiler;
Jacobian m_jacobian;
};

Expand Down
18 changes: 7 additions & 11 deletions include/sleipnir/autodiff/Jacobian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <Eigen/SparseCore>

#include "sleipnir/autodiff/AdjointExpressionGraph.hpp"
#include "sleipnir/autodiff/Profiler.hpp"
#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/autodiff/VariableMatrix.hpp"
#include "sleipnir/util/SolveProfiler.hpp"
#include "sleipnir/util/SymbolExports.hpp"
#include "sleipnir/util/small_vector.hpp"

Expand All @@ -33,8 +33,6 @@ class SLEIPNIR_DLLEXPORT Jacobian {
*/
Jacobian(const VariableMatrix& variables, const VariableMatrix& wrt) noexcept
: m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
m_profiler.StartSetup();

// Initialize column each expression's adjoint occupies in the Jacobian
for (size_t col = 0; col < m_wrt.size(); ++col) {
m_wrt(col).expr->col = col;
Expand Down Expand Up @@ -69,8 +67,6 @@ class SLEIPNIR_DLLEXPORT Jacobian {
if (m_nonlinearRows.empty()) {
m_J.setFromTriplets(m_cachedTriplets.begin(), m_cachedTriplets.end());
}

m_profiler.StopSetup();
}

/**
Expand Down Expand Up @@ -101,10 +97,10 @@ class SLEIPNIR_DLLEXPORT Jacobian {
* Evaluates the Jacobian at wrt's value.
*/
const Eigen::SparseMatrix<double>& Value() {
m_profiler.StartSolve();
m_solveProfiler.Start();

if (m_nonlinearRows.empty()) {
m_profiler.StopSolve();
m_solveProfiler.Stop();
return m_J;
}

Expand All @@ -129,15 +125,15 @@ class SLEIPNIR_DLLEXPORT Jacobian {
m_J.setZero();
}

m_profiler.StopSolve();
m_solveProfiler.Stop();

return m_J;
}

/**
* Returns the profiler.
* Returns the solve profiler.
*/
Profiler& GetProfiler() { return m_profiler; }
const SolveProfiler& GetSolveProfiler() const { return m_solveProfiler; }

private:
VariableMatrix m_variables;
Expand All @@ -154,7 +150,7 @@ class SLEIPNIR_DLLEXPORT Jacobian {
// Value()
small_vector<int> m_nonlinearRows;

Profiler m_profiler;
SolveProfiler m_solveProfiler;
};

} // namespace sleipnir
77 changes: 0 additions & 77 deletions include/sleipnir/autodiff/Profiler.hpp

This file was deleted.

58 changes: 58 additions & 0 deletions include/sleipnir/util/SetupProfiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <chrono>
#include <string>
#include <string_view>

namespace sleipnir {

/**
* Records the number of profiler measurements (start/stop pairs) and the
* average duration between each start and stop call.
*/
class SetupProfiler {
public:
std::string name;

/**
* Constructs a SetupProfiler.
*
* @param name Name of measurement to show in diagnostics.
*/
explicit SetupProfiler(std::string_view name) : name{name} {}

/**
* Tell the profiler to start measuring setup time.
*/
void Start() {
#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
m_setupStartTime = std::chrono::steady_clock::now();
#endif
}

/**
* Tell the profiler to stop measuring setup time.
*/
void Stop() {
#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
m_setupStopTime = std::chrono::steady_clock::now();
m_setupDuration = m_setupStopTime - m_setupStartTime;
#endif
}

/**
* The setup duration in milliseconds as a double.
*/
const std::chrono::duration<double>& Duration() const {
return m_setupDuration;
}

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

} // namespace sleipnir
93 changes: 93 additions & 0 deletions include/sleipnir/util/SolveProfiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <chrono>
#include <string>
#include <string_view>

namespace sleipnir {

/**
* Records the number of profiler measurements (start/stop pairs) and the
* average duration between each start and stop call.
*/
class SolveProfiler {
public:
std::string name;

/**
* Constructs a SolveProfiler.
*/
SolveProfiler() = default;

/**
* Constructs a SolveProfiler.
*
* @param name Name of measurement to show in diagnostics.
*/
explicit SolveProfiler(std::string_view name) : name{name} {}

/**
* Tell the profiler to start measuring solve time.
*/
void Start() {
#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
m_currentSolveStartTime = std::chrono::steady_clock::now();
#endif
}

/**
* Tell the profiler to stop measuring solve time, increment the number of
* averages, and incorporate the latest measurement into the average.
*/
void Stop() {
#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
m_currentSolveStopTime = std::chrono::steady_clock::now();
m_currentSolveDuration = m_currentSolveStopTime - m_currentSolveStartTime;
m_totalSolveDuration += m_currentSolveDuration;

++m_numSolves;
m_averageSolveDuration =
(m_numSolves - 1.0) / m_numSolves * m_averageSolveDuration +
1.0 / m_numSolves * m_currentSolveDuration;
#endif
}

/**
* The number of solves.
*/
int NumSolves() const { return m_numSolves; }

/**
* The most recent solve duration in milliseconds as a double.
*/
const std::chrono::duration<double>& CurrentDuration() const {
return m_currentSolveDuration;
}

/**
* The average solve duration in milliseconds as a double.
*/
const std::chrono::duration<double>& AverageDuration() const {
return m_averageSolveDuration;
}

/**
* The sum of all solve durations in milliseconds as a double.
*/
const std::chrono::duration<double>& TotalDuration() const {
return m_totalSolveDuration;
}

private:
std::chrono::steady_clock::time_point m_currentSolveStartTime;
std::chrono::steady_clock::time_point m_currentSolveStopTime;
std::chrono::duration<double> m_currentSolveDuration{0.0};
std::chrono::duration<double> m_totalSolveDuration{0.0};

int m_numSolves = 0;
std::chrono::duration<double> m_averageSolveDuration{0.0};
};

} // namespace sleipnir
Loading

0 comments on commit f2e80e8

Please sign in to comment.