Skip to content

Commit

Permalink
Use small-buffer-optimization vector type
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Apr 29, 2024
1 parent 44bbd46 commit ea478c4
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 39 deletions.
4 changes: 2 additions & 2 deletions include/sleipnir/autodiff/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <memory>
#include <numbers>
#include <utility>
#include <vector>

#include "sleipnir/autodiff/ExpressionType.hpp"
#include "sleipnir/util/IntrusiveSharedPtr.hpp"
#include "sleipnir/util/Pool.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir::detail {
Expand Down Expand Up @@ -430,7 +430,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.
std::vector<Expression*> stack;
small_vector<Expression*> stack;
stack.emplace_back(expr);

while (!stack.empty()) {
Expand Down
14 changes: 7 additions & 7 deletions include/sleipnir/autodiff/ExpressionGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#pragma once

#include <span>
#include <vector>

#include "sleipnir/autodiff/Expression.hpp"
#include "sleipnir/util/FunctionRef.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir::detail {
Expand Down Expand Up @@ -36,7 +36,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph {
// https://en.wikipedia.org/wiki/Breadth-first_search

// BFS list sorted from parent to child.
std::vector<Expression*> stack;
small_vector<Expression*> stack;

stack.emplace_back(root.Get());

Expand Down Expand Up @@ -119,7 +119,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph {
*
* @param wrt Variables with respect to which to compute the gradient.
*/
std::vector<ExpressionPtr> GenerateGradientTree(
small_vector<ExpressionPtr> GenerateGradientTree(
std::span<const ExpressionPtr> wrt) const {
// Read docs/algorithms.md#Reverse_accumulation_automatic_differentiation
// for background on reverse accumulation automatic differentiation.
Expand All @@ -128,7 +128,7 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph {
wrt[row]->row = row;
}

std::vector<ExpressionPtr> grad;
small_vector<ExpressionPtr> grad;
grad.reserve(wrt.size());
for (size_t row = 0; row < wrt.size(); ++row) {
grad.emplace_back(MakeExpressionPtr());
Expand Down Expand Up @@ -231,13 +231,13 @@ class SLEIPNIR_DLLEXPORT ExpressionGraph {

private:
// List that maps nodes to their respective row.
std::vector<int> m_rowList;
small_vector<int> m_rowList;

// List for updating adjoints
std::vector<Expression*> m_adjointList;
small_vector<Expression*> m_adjointList;

// List for updating values
std::vector<Expression*> m_valueList;
small_vector<Expression*> m_valueList;
};

} // namespace sleipnir::detail
4 changes: 2 additions & 2 deletions include/sleipnir/autodiff/Hessian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#pragma once

#include <utility>
#include <vector>

#include <Eigen/Core>
#include <Eigen/SparseCore>
Expand All @@ -13,6 +12,7 @@
#include "sleipnir/autodiff/Profiler.hpp"
#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/autodiff/VariableMatrix.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand All @@ -36,7 +36,7 @@ class SLEIPNIR_DLLEXPORT Hessian {
Hessian(Variable variable, const VariableMatrix& wrt) noexcept
: m_jacobian{
[&] {
std::vector<detail::ExpressionPtr> wrtVec;
small_vector<detail::ExpressionPtr> wrtVec;
wrtVec.reserve(wrt.size());
for (auto& elem : wrt) {
wrtVec.emplace_back(elem.expr);
Expand Down
10 changes: 5 additions & 5 deletions include/sleipnir/autodiff/Jacobian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#pragma once

#include <utility>
#include <vector>

#include <Eigen/SparseCore>

#include "sleipnir/autodiff/ExpressionGraph.hpp"
#include "sleipnir/autodiff/Profiler.hpp"
#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/autodiff/VariableMatrix.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand Down Expand Up @@ -81,7 +81,7 @@ class SLEIPNIR_DLLEXPORT Jacobian {
VariableMatrix Get() const {
VariableMatrix result{m_variables.Rows(), m_wrt.Rows()};

std::vector<detail::ExpressionPtr> wrtVec;
small_vector<detail::ExpressionPtr> wrtVec;
wrtVec.reserve(m_wrt.size());
for (auto& elem : m_wrt) {
wrtVec.emplace_back(elem.expr);
Expand Down Expand Up @@ -145,16 +145,16 @@ class SLEIPNIR_DLLEXPORT Jacobian {
VariableMatrix m_variables;
VariableMatrix m_wrt;

std::vector<detail::ExpressionGraph> m_graphs;
small_vector<detail::ExpressionGraph> m_graphs;

Eigen::SparseMatrix<double> m_J{m_variables.Rows(), m_wrt.Rows()};

// Cached triplets for gradients of linear rows
std::vector<Eigen::Triplet<double>> m_cachedTriplets;
small_vector<Eigen::Triplet<double>> m_cachedTriplets;

// List of row indices for nonlinear rows whose graients will be computed in
// Value()
std::vector<int> m_nonlinearRows;
small_vector<int> m_nonlinearRows;

Profiler m_profiler;
};
Expand Down
10 changes: 5 additions & 5 deletions include/sleipnir/optimization/Constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#include <algorithm>
#include <concepts>
#include <vector>

#include "sleipnir/autodiff/Variable.hpp"
#include "sleipnir/util/Assert.hpp"
#include "sleipnir/util/Concepts.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand All @@ -28,8 +28,8 @@ template <typename LHS, typename RHS>
requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
(ScalarLike<RHS> || MatrixLike<RHS>) &&
(!std::same_as<LHS, double> || !std::same_as<RHS, double>)
std::vector<Variable> MakeConstraints(const LHS& lhs, const RHS& rhs) {
std::vector<Variable> constraints;
small_vector<Variable> MakeConstraints(const LHS& lhs, const RHS& rhs) {
small_vector<Variable> constraints;

if constexpr (ScalarLike<LHS> && ScalarLike<RHS>) {
constraints.emplace_back(lhs - rhs);
Expand Down Expand Up @@ -113,7 +113,7 @@ std::vector<Variable> MakeConstraints(const LHS& lhs, const RHS& rhs) {
*/
struct SLEIPNIR_DLLEXPORT EqualityConstraints {
/// A vector of scalar equality constraints.
std::vector<Variable> constraints;
small_vector<Variable> constraints;

/**
* Constructs an equality constraint from a left and right side.
Expand Down Expand Up @@ -146,7 +146,7 @@ struct SLEIPNIR_DLLEXPORT EqualityConstraints {
*/
struct SLEIPNIR_DLLEXPORT InequalityConstraints {
/// A vector of scalar inequality constraints.
std::vector<Variable> constraints;
small_vector<Variable> constraints;

/**
* Constructs an inequality constraint from a left and right side.
Expand Down
6 changes: 3 additions & 3 deletions include/sleipnir/optimization/Multistart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <algorithm>
#include <future>
#include <span>
#include <vector>

#include "sleipnir/optimization/SolverStatus.hpp"
#include "sleipnir/util/FunctionRef.hpp"
#include "sleipnir/util/SmallVector.hpp"

namespace sleipnir {

Expand Down Expand Up @@ -43,12 +43,12 @@ MultistartResult<DecisionVariables> Multistart(
function_ref<MultistartResult<DecisionVariables>(const DecisionVariables&)>
solve,
std::span<const DecisionVariables> initialGuesses) {
std::vector<std::future<MultistartResult<DecisionVariables>>> futures;
small_vector<std::future<MultistartResult<DecisionVariables>>> futures;
for (const auto& initialGuess : initialGuesses) {
futures.emplace_back(std::async(std::launch::async, solve, initialGuess));
}

std::vector<MultistartResult<DecisionVariables>> results;
small_vector<MultistartResult<DecisionVariables>> results;
for (auto& future : futures) {
results.emplace_back(future.get());
}
Expand Down
8 changes: 4 additions & 4 deletions include/sleipnir/optimization/OptimizationProblem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>

#include <Eigen/Core>

Expand All @@ -23,6 +22,7 @@
#include "sleipnir/optimization/SolverStatus.hpp"
#include "sleipnir/optimization/solver/InteriorPoint.hpp"
#include "sleipnir/util/Print.hpp"
#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand Down Expand Up @@ -531,16 +531,16 @@ class SLEIPNIR_DLLEXPORT OptimizationProblem {
private:
// The list of decision variables, which are the root of the problem's
// expression tree
std::vector<Variable> m_decisionVariables;
small_vector<Variable> m_decisionVariables;

// The cost function: f(x)
std::optional<Variable> m_f;

// The list of equality constraints: cₑ(x) = 0
std::vector<Variable> m_equalityConstraints;
small_vector<Variable> m_equalityConstraints;

// The list of inequality constraints: cᵢ(x) ≥ 0
std::vector<Variable> m_inequalityConstraints;
small_vector<Variable> m_inequalityConstraints;

// The user callback
std::function<bool(const SolverIterationInfo&)> m_callback =
Expand Down
6 changes: 3 additions & 3 deletions include/sleipnir/util/Pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <cstddef>
#include <memory>
#include <vector>

#include "sleipnir/util/SmallVector.hpp"
#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {
Expand Down Expand Up @@ -76,8 +76,8 @@ class SLEIPNIR_DLLEXPORT PoolResource {
}

private:
std::vector<std::unique_ptr<std::byte[]>> m_buffer;
std::vector<void*> m_freeList;
small_vector<std::unique_ptr<std::byte[]>> m_buffer;
small_vector<void*> m_freeList;
size_t blocksPerChunk;

/**
Expand Down
Loading

0 comments on commit ea478c4

Please sign in to comment.