From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sun, 16 Jun 2024 12:08:49 -0700 Subject: [PATCH 4/5] Use wpi::SmallVector --- include/.styleguide | 1 + include/sleipnir/autodiff/Expression.hpp | 5 +++-- include/sleipnir/autodiff/ExpressionGraph.hpp | 15 ++++++++------- include/sleipnir/autodiff/Hessian.hpp | 4 ++-- include/sleipnir/autodiff/Jacobian.hpp | 10 +++++----- include/sleipnir/autodiff/Variable.hpp | 10 +++++----- include/sleipnir/autodiff/VariableMatrix.hpp | 4 ++-- include/sleipnir/optimization/Multistart.hpp | 7 ++++--- .../sleipnir/optimization/OptimizationProblem.hpp | 8 ++++---- include/sleipnir/util/Pool.hpp | 7 ++++--- include/sleipnir/util/Spy.hpp | 4 ++-- src/.styleguide | 1 + src/optimization/solver/InteriorPoint.cpp | 4 ++-- .../solver/util/FeasibilityRestoration.hpp | 10 +++++----- src/optimization/solver/util/Filter.hpp | 4 ++-- 15 files changed, 50 insertions(+), 44 deletions(-) diff --git a/include/.styleguide b/include/.styleguide index 6a7f8ed28f9cb037c9746a7e0ef5e110481d9825..efa36cee1fb593ae810032340c64f1854fbbc523 100644 --- a/include/.styleguide +++ b/include/.styleguide @@ -13,4 +13,5 @@ licenseUpdateExclude { includeOtherLibs { ^Eigen/ ^fmt/ + ^wpi/ } diff --git a/include/sleipnir/autodiff/Expression.hpp b/include/sleipnir/autodiff/Expression.hpp index dff8e2a6ef24413e3e6356bf0ec57286e50654cf..bdbeb4730223f88cfdc0c424a8f7b852b34742f7 100644 --- a/include/sleipnir/autodiff/Expression.hpp +++ b/include/sleipnir/autodiff/Expression.hpp @@ -11,11 +11,12 @@ #include #include +#include + #include "sleipnir/autodiff/ExpressionType.hpp" #include "sleipnir/util/IntrusiveSharedPtr.hpp" #include "sleipnir/util/Pool.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir::detail { @@ -427,7 +428,7 @@ inline void IntrusiveSharedPtrDecRefCount(Expression* expr) { // Expression destructor when expr's refcount reaches zero can cause a stack // overflow. Instead, we iterate over its children to decrement their // refcounts and deallocate them. - small_vector stack; + wpi::SmallVector stack; stack.emplace_back(expr); while (!stack.empty()) { diff --git a/include/sleipnir/autodiff/ExpressionGraph.hpp b/include/sleipnir/autodiff/ExpressionGraph.hpp index c614195d82ad022dfd0c3f6f8240b042c0056c2f..714bcbb82907e754138347334c7fca8a7ccf055d 100644 --- a/include/sleipnir/autodiff/ExpressionGraph.hpp +++ b/include/sleipnir/autodiff/ExpressionGraph.hpp @@ -4,10 +4,11 @@ #include +#include + #include "sleipnir/autodiff/Expression.hpp" #include "sleipnir/util/FunctionRef.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir::detail { @@ -36,7 +37,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph { // https://en.wikipedia.org/wiki/Breadth-first_search // BFS list sorted from parent to child. - small_vector stack; + wpi::SmallVector stack; stack.emplace_back(root.Get()); @@ -119,7 +120,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph { * * @param wrt Variables with respect to which to compute the gradient. */ - small_vector GenerateGradientTree( + wpi::SmallVector GenerateGradientTree( std::span wrt) const { // Read docs/algorithms.md#Reverse_accumulation_automatic_differentiation // for background on reverse accumulation automatic differentiation. @@ -128,7 +129,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph { wrt[row]->row = row; } - small_vector grad; + wpi::SmallVector grad; grad.reserve(wrt.size()); for (size_t row = 0; row < wrt.size(); ++row) { grad.emplace_back(MakeExpressionPtr()); @@ -231,13 +232,13 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph { private: // List that maps nodes to their respective row. - small_vector m_rowList; + wpi::SmallVector m_rowList; // List for updating adjoints - small_vector m_adjointList; + wpi::SmallVector m_adjointList; // List for updating values - small_vector m_valueList; + wpi::SmallVector m_valueList; }; } // namespace sleipnir::detail diff --git a/include/sleipnir/autodiff/Hessian.hpp b/include/sleipnir/autodiff/Hessian.hpp index 4563aa234bd7b0ec22e12d2fc0b6f4569bee7f39..2e60d89e95280bdac422b0d7dab955ba111b0059 100644 --- a/include/sleipnir/autodiff/Hessian.hpp +++ b/include/sleipnir/autodiff/Hessian.hpp @@ -6,6 +6,7 @@ #include #include +#include #include "sleipnir/autodiff/ExpressionGraph.hpp" #include "sleipnir/autodiff/Jacobian.hpp" @@ -13,7 +14,6 @@ #include "sleipnir/autodiff/Variable.hpp" #include "sleipnir/autodiff/VariableMatrix.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -36,7 +36,7 @@ class SLEIPNIR_DLLEXPORT Hessian { Hessian(Variable variable, const VariableMatrix& wrt) noexcept : m_jacobian{ [&] { - small_vector wrtVec; + wpi::SmallVector wrtVec; wrtVec.reserve(wrt.size()); for (auto& elem : wrt) { wrtVec.emplace_back(elem.expr); diff --git a/include/sleipnir/autodiff/Jacobian.hpp b/include/sleipnir/autodiff/Jacobian.hpp index ac00c11ef8c947cbe95c58081bdbfb42bf901051..0c660156c80f94539383b8f0d01d7853e41e0297 100644 --- a/include/sleipnir/autodiff/Jacobian.hpp +++ b/include/sleipnir/autodiff/Jacobian.hpp @@ -5,13 +5,13 @@ #include #include +#include #include "sleipnir/autodiff/ExpressionGraph.hpp" #include "sleipnir/autodiff/Profiler.hpp" #include "sleipnir/autodiff/Variable.hpp" #include "sleipnir/autodiff/VariableMatrix.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -81,7 +81,7 @@ class SLEIPNIR_DLLEXPORT Jacobian { VariableMatrix Get() const { VariableMatrix result{m_variables.Rows(), m_wrt.Rows()}; - small_vector wrtVec; + wpi::SmallVector wrtVec; wrtVec.reserve(m_wrt.size()); for (auto& elem : m_wrt) { wrtVec.emplace_back(elem.expr); @@ -138,16 +138,16 @@ class SLEIPNIR_DLLEXPORT Jacobian { VariableMatrix m_variables; VariableMatrix m_wrt; - small_vector m_graphs; + wpi::SmallVector m_graphs; Eigen::SparseMatrix m_J{m_variables.Rows(), m_wrt.Rows()}; // Cached triplets for gradients of linear rows - small_vector> m_cachedTriplets; + wpi::SmallVector> m_cachedTriplets; // List of row indices for nonlinear rows whose graients will be computed in // Value() - small_vector m_nonlinearRows; + wpi::SmallVector m_nonlinearRows; Profiler m_profiler; }; diff --git a/include/sleipnir/autodiff/Variable.hpp b/include/sleipnir/autodiff/Variable.hpp index c04d629f482efe59497570ca1fd9b09a4af2ae1e..d192fb96e7984b7c0ca30262668c41e5e84ca34e 100644 --- a/include/sleipnir/autodiff/Variable.hpp +++ b/include/sleipnir/autodiff/Variable.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "sleipnir/autodiff/Expression.hpp" #include "sleipnir/autodiff/ExpressionGraph.hpp" @@ -17,7 +18,6 @@ #include "sleipnir/util/Concepts.hpp" #include "sleipnir/util/Print.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -445,8 +445,8 @@ template (ScalarLike> || MatrixLike>) && (!std::same_as, double> || !std::same_as, double>) -small_vector MakeConstraints(LHS&& lhs, RHS&& rhs) { - small_vector constraints; +wpi::SmallVector MakeConstraints(LHS&& lhs, RHS&& rhs) { + wpi::SmallVector constraints; if constexpr (ScalarLike> && ScalarLike>) { @@ -534,7 +534,7 @@ small_vector MakeConstraints(LHS&& lhs, RHS&& rhs) { */ struct SLEIPNIR_DLLEXPORT EqualityConstraints { /// A vector of scalar equality constraints. - small_vector constraints; + wpi::SmallVector constraints; /** * Concatenates multiple equality constraints. @@ -596,7 +596,7 @@ struct SLEIPNIR_DLLEXPORT EqualityConstraints { */ struct SLEIPNIR_DLLEXPORT InequalityConstraints { /// A vector of scalar inequality constraints. - small_vector constraints; + wpi::SmallVector constraints; /** * Concatenates multiple inequality constraints. diff --git a/include/sleipnir/autodiff/VariableMatrix.hpp b/include/sleipnir/autodiff/VariableMatrix.hpp index 47452b8988b3a1a96a78d28644200b1c4cdc89c9..57b09913d15e9590873ad7bf62e2baff9fbc5df9 100644 --- a/include/sleipnir/autodiff/VariableMatrix.hpp +++ b/include/sleipnir/autodiff/VariableMatrix.hpp @@ -11,6 +11,7 @@ #include #include +#include #include "sleipnir/autodiff/Slice.hpp" #include "sleipnir/autodiff/Variable.hpp" @@ -18,7 +19,6 @@ #include "sleipnir/util/Assert.hpp" #include "sleipnir/util/FunctionRef.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -946,7 +946,7 @@ class SLEIPNIR_DLLEXPORT VariableMatrix { } private: - small_vector m_storage; + wpi::SmallVector m_storage; int m_rows = 0; int m_cols = 0; }; diff --git a/include/sleipnir/optimization/Multistart.hpp b/include/sleipnir/optimization/Multistart.hpp index 8055713a2492a9c8473f047a2fb9fe7ca57753c3..09b1b2f3bf33c35ae0aeddb9b5d47c0d80c68cec 100644 --- a/include/sleipnir/optimization/Multistart.hpp +++ b/include/sleipnir/optimization/Multistart.hpp @@ -6,9 +6,10 @@ #include #include +#include + #include "sleipnir/optimization/SolverStatus.hpp" #include "sleipnir/util/FunctionRef.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -44,14 +45,14 @@ MultistartResult Multistart( const DecisionVariables& initialGuess)> solve, std::span initialGuesses) { - small_vector>> futures; + wpi::SmallVector>> futures; futures.reserve(initialGuesses.size()); for (const auto& initialGuess : initialGuesses) { futures.emplace_back(std::async(std::launch::async, solve, initialGuess)); } - small_vector> results; + wpi::SmallVector> results; results.reserve(futures.size()); for (auto& future : futures) { diff --git a/include/sleipnir/optimization/OptimizationProblem.hpp b/include/sleipnir/optimization/OptimizationProblem.hpp index 28b2943f5842229335e79eae14abbda6ff5b7000..e812fa5e3454903d4dfb8572539ebf1b506bdf11 100644 --- a/include/sleipnir/optimization/OptimizationProblem.hpp +++ b/include/sleipnir/optimization/OptimizationProblem.hpp @@ -11,6 +11,7 @@ #include #include +#include #include "sleipnir/autodiff/Variable.hpp" #include "sleipnir/autodiff/VariableMatrix.hpp" @@ -21,7 +22,6 @@ #include "sleipnir/optimization/solver/InteriorPoint.hpp" #include "sleipnir/util/Print.hpp" #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -358,16 +358,16 @@ class SLEIPNIR_DLLEXPORT OptimizationProblem { private: // The list of decision variables, which are the root of the problem's // expression tree - small_vector m_decisionVariables; + wpi::SmallVector m_decisionVariables; // The cost function: f(x) std::optional m_f; // The list of equality constraints: cₑ(x) = 0 - small_vector m_equalityConstraints; + wpi::SmallVector m_equalityConstraints; // The list of inequality constraints: cᵢ(x) ≥ 0 - small_vector m_inequalityConstraints; + wpi::SmallVector m_inequalityConstraints; // The user callback std::function m_callback = diff --git a/include/sleipnir/util/Pool.hpp b/include/sleipnir/util/Pool.hpp index 441fa701d4972bc14973c6269d56d4a124deaee5..1951bd1ee8f3bee8d4a3c044c99354b0fd10031d 100644 --- a/include/sleipnir/util/Pool.hpp +++ b/include/sleipnir/util/Pool.hpp @@ -5,8 +5,9 @@ #include #include +#include + #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -78,8 +79,8 @@ class SLEIPNIR_DLLEXPORT PoolResource { } private: - small_vector> m_buffer; - small_vector m_freeList; + wpi::SmallVector> m_buffer; + wpi::SmallVector m_freeList; size_t blocksPerChunk; /** diff --git a/include/sleipnir/util/Spy.hpp b/include/sleipnir/util/Spy.hpp index cb9b4e191545e96c2bade5f8f99b0bec376b656b..7f526a2d9968e76b385a0ddfb2edf5bab7274fb0 100644 --- a/include/sleipnir/util/Spy.hpp +++ b/include/sleipnir/util/Spy.hpp @@ -7,9 +7,9 @@ #include #include +#include #include "sleipnir/util/SymbolExports.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -32,7 +32,7 @@ SLEIPNIR_DLLEXPORT inline void Spy(std::ostream& file, const int cells_width = mat.cols() + 1; const int cells_height = mat.rows(); - small_vector cells; + wpi::SmallVector cells; // Allocate space for matrix of characters plus trailing newlines cells.reserve(cells_width * cells_height); diff --git a/src/.styleguide b/src/.styleguide index f3b2f0cf9e60b3a86b9654ff2b381f9c48734ff6..ad739cea6dce6f6cb586f538d1d30b92503484c1 100644 --- a/src/.styleguide +++ b/src/.styleguide @@ -8,4 +8,5 @@ cppSrcFileInclude { includeOtherLibs { ^Eigen/ + ^wpi/ } diff --git a/src/optimization/solver/InteriorPoint.cpp b/src/optimization/solver/InteriorPoint.cpp index dcd8b56dc08516b80f89550c43cb7002745b93d8..892d2dd20f7fe92c2c91518a4ecb487425643585 100644 --- a/src/optimization/solver/InteriorPoint.cpp +++ b/src/optimization/solver/InteriorPoint.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "optimization/RegularizedLDLT.hpp" #include "optimization/solver/util/ErrorEstimate.hpp" @@ -23,7 +24,6 @@ #include "sleipnir/optimization/SolverExitCondition.hpp" #include "sleipnir/util/Print.hpp" #include "sleipnir/util/Spy.hpp" -#include "sleipnir/util/small_vector.hpp" #include "util/ScopeExit.hpp" #include "util/ToMilliseconds.hpp" @@ -226,7 +226,7 @@ void InteriorPoint(std::span decisionVariables, }; // Kept outside the loop so its storage can be reused - small_vector> triplets; + wpi::SmallVector> triplets; RegularizedLDLT solver; diff --git a/src/optimization/solver/util/FeasibilityRestoration.hpp b/src/optimization/solver/util/FeasibilityRestoration.hpp index c324ef093cc7dc8ce93af2cba337042a65b28475..0f13676aea0e80549ef1ef43e4972d5498acaa18 100644 --- a/src/optimization/solver/util/FeasibilityRestoration.hpp +++ b/src/optimization/solver/util/FeasibilityRestoration.hpp @@ -8,6 +8,7 @@ #include #include +#include #include "sleipnir/autodiff/Variable.hpp" #include "sleipnir/autodiff/VariableMatrix.hpp" @@ -16,7 +17,6 @@ #include "sleipnir/optimization/SolverStatus.hpp" #include "sleipnir/optimization/solver/InteriorPoint.hpp" #include "sleipnir/util/FunctionRef.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -65,7 +65,7 @@ inline void FeasibilityRestoration( constexpr double ρ = 1000.0; - small_vector fr_decisionVariables; + wpi::SmallVector fr_decisionVariables; fr_decisionVariables.reserve(decisionVariables.size() + 2 * equalityConstraints.size() + 2 * inequalityConstraints.size()); @@ -81,7 +81,7 @@ inline void FeasibilityRestoration( fr_decisionVariables.emplace_back(); } - auto it = fr_decisionVariables.cbegin(); + auto it = fr_decisionVariables.begin(); VariableMatrix xAD{std::span{it, it + decisionVariables.size()}}; it += decisionVariables.size(); @@ -157,7 +157,7 @@ inline void FeasibilityRestoration( } // cₑ(x) - pₑ + nₑ = 0 - small_vector fr_equalityConstraints; + wpi::SmallVector fr_equalityConstraints; fr_equalityConstraints.assign(equalityConstraints.begin(), equalityConstraints.end()); for (size_t row = 0; row < fr_equalityConstraints.size(); ++row) { @@ -166,7 +166,7 @@ inline void FeasibilityRestoration( } // cᵢ(x) - s - pᵢ + nᵢ = 0 - small_vector fr_inequalityConstraints; + wpi::SmallVector fr_inequalityConstraints; fr_inequalityConstraints.assign(inequalityConstraints.begin(), inequalityConstraints.end()); for (size_t row = 0; row < fr_inequalityConstraints.size(); ++row) { diff --git a/src/optimization/solver/util/Filter.hpp b/src/optimization/solver/util/Filter.hpp index 3fbb849edc4a6b3336f94b5af9e018a37b07b123..02bdb5a8db5c80dd86d17ea4421ec564d7e0a2c7 100644 --- a/src/optimization/solver/util/Filter.hpp +++ b/src/optimization/solver/util/Filter.hpp @@ -8,9 +8,9 @@ #include #include +#include #include "sleipnir/autodiff/Variable.hpp" -#include "sleipnir/util/small_vector.hpp" namespace sleipnir { @@ -182,7 +182,7 @@ class Filter { private: Variable* m_f = nullptr; double m_μ = 0.0; - small_vector m_filter; + wpi::SmallVector m_filter; }; } // namespace sleipnir