Skip to content

Commit

Permalink
Make Variable constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Nov 7, 2024
1 parent a925d16 commit 73ae6f1
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 67 deletions.
4 changes: 2 additions & 2 deletions cmake/modules/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro(compiler_flags target)
if(NOT MSVC)
target_compile_options(
${target}
PRIVATE -Wall -pedantic -Wextra -Werror
PRIVATE -Wall -pedantic -Wextra -Werror -std=c++26
)
else()
# Suppress the following warnings:
Expand All @@ -11,7 +11,7 @@ macro(compiler_flags target)
target_compile_options(${target} PRIVATE /wd4244 /wd4251 /WX)
endif()

target_compile_features(${target} PUBLIC cxx_std_23)
# target_compile_features(${target} PUBLIC cxx_std_26)
if(MSVC)
target_compile_options(${target} PUBLIC /MP /utf-8 /bigobj)
endif()
Expand Down
7 changes: 5 additions & 2 deletions include/sleipnir/autodiff/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cmath>
#include <memory>
#include <numbers>
#include <type_traits>
#include <utility>

#include "sleipnir/autodiff/ExpressionType.hpp"
Expand All @@ -29,6 +30,8 @@ inline constexpr bool kUsePoolAllocator = true;

struct SLEIPNIR_DLLEXPORT Expression;

inline PoolResource constPool{16384};

inline constexpr void IntrusiveSharedPtrIncRefCount(Expression* expr);
inline constexpr void IntrusiveSharedPtrDecRefCount(Expression* expr);

Expand All @@ -44,8 +47,8 @@ using ExpressionPtr = IntrusiveSharedPtr<Expression>;
* @param args Constructor arguments for Expression.
*/
template <typename... Args>
static ExpressionPtr MakeExpressionPtr(Args&&... args) {
if constexpr (kUsePoolAllocator) {
static constexpr ExpressionPtr MakeExpressionPtr(Args&&... args) {
if (kUsePoolAllocator && !std::is_constant_evaluated()) {
return AllocateIntrusiveShared<Expression>(
GlobalPoolAllocator<Expression>(), std::forward<Args>(args)...);
} else {
Expand Down
46 changes: 24 additions & 22 deletions include/sleipnir/autodiff/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,37 @@ class SLEIPNIR_DLLEXPORT Variable {
/**
* Constructs a linear Variable with a value of zero.
*/
Variable() = default;
constexpr Variable() = default;

/**
* Constructs a Variable from a double.
*
* @param value The value of the Variable.
*/
Variable(double value) : expr{detail::MakeExpressionPtr(value)} {} // NOLINT
constexpr Variable(double value) // NOLINT
: expr{detail::MakeExpressionPtr(value)} {}

/**
* Constructs a Variable pointing to the specified expression.
*
* @param expr The autodiff variable.
*/
explicit Variable(const detail::ExpressionPtr& expr) : expr{expr} {}
constexpr explicit Variable(const detail::ExpressionPtr& expr) : expr{expr} {}

/**
* Constructs a Variable pointing to the specified expression.
*
* @param expr The autodiff variable.
*/
explicit Variable(detail::ExpressionPtr&& expr) : expr{std::move(expr)} {}
constexpr explicit Variable(detail::ExpressionPtr&& expr)
: expr{std::move(expr)} {}

/**
* Assignment operator for double.
*
* @param value The value of the Variable.
*/
Variable& operator=(double value) {
constexpr Variable& operator=(double value) {
expr = detail::MakeExpressionPtr(value);

return *this;
Expand All @@ -72,7 +74,7 @@ class SLEIPNIR_DLLEXPORT Variable {
*
* @param value The value of the Variable.
*/
void SetValue(double value) {
constexpr void SetValue(double value) {
if (expr->IsConstant(0.0)) {
expr = detail::MakeExpressionPtr(value);
} else {
Expand Down Expand Up @@ -193,7 +195,7 @@ class SLEIPNIR_DLLEXPORT Variable {
/**
* Returns the value of this variable.
*/
double Value() {
constexpr double Value() {
// Updates the value of this variable based on the values of its dependent
// variables
detail::ExpressionGraph{expr}.Update();
Expand All @@ -205,7 +207,7 @@ class SLEIPNIR_DLLEXPORT Variable {
* Returns the type of this expression (constant, linear, quadratic, or
* nonlinear).
*/
ExpressionType Type() const { return expr->type; }
constexpr ExpressionType Type() const { return expr->type; }

private:
/// The expression node.
Expand Down Expand Up @@ -445,7 +447,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
small_vector<Variable> MakeConstraints(LHS&& lhs, RHS&& rhs) {
constexpr small_vector<Variable> MakeConstraints(LHS&& lhs, RHS&& rhs) {
small_vector<Variable> constraints;

if constexpr (ScalarLike<std::decay_t<LHS>> &&
Expand Down Expand Up @@ -541,7 +543,7 @@ struct SLEIPNIR_DLLEXPORT EqualityConstraints {
*
* @param equalityConstraints The list of EqualityConstraints to concatenate.
*/
EqualityConstraints(
constexpr EqualityConstraints(
std::initializer_list<EqualityConstraints> equalityConstraints) {
for (const auto& elem : equalityConstraints) {
constraints.insert(constraints.end(), elem.constraints.begin(),
Expand All @@ -556,7 +558,7 @@ struct SLEIPNIR_DLLEXPORT EqualityConstraints {
*
* @param equalityConstraints The list of EqualityConstraints to concatenate.
*/
explicit EqualityConstraints(
explicit constexpr EqualityConstraints(
const std::vector<EqualityConstraints>& equalityConstraints) {
for (const auto& elem : equalityConstraints) {
constraints.insert(constraints.end(), elem.constraints.begin(),
Expand All @@ -578,13 +580,13 @@ struct SLEIPNIR_DLLEXPORT EqualityConstraints {
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
EqualityConstraints(LHS&& lhs, RHS&& rhs)
constexpr EqualityConstraints(LHS&& lhs, RHS&& rhs)
: constraints{MakeConstraints(lhs, rhs)} {}

/**
* Implicit conversion operator to bool.
*/
operator bool() { // NOLINT
constexpr operator bool() { // NOLINT
return std::all_of(
constraints.begin(), constraints.end(),
[](auto& constraint) { return constraint.Value() == 0.0; });
Expand All @@ -604,7 +606,7 @@ struct SLEIPNIR_DLLEXPORT InequalityConstraints {
* @param inequalityConstraints The list of InequalityConstraints to
* concatenate.
*/
InequalityConstraints(
constexpr InequalityConstraints(
std::initializer_list<InequalityConstraints> inequalityConstraints) {
for (const auto& elem : inequalityConstraints) {
constraints.insert(constraints.end(), elem.constraints.begin(),
Expand All @@ -620,7 +622,7 @@ struct SLEIPNIR_DLLEXPORT InequalityConstraints {
* @param inequalityConstraints The list of InequalityConstraints to
* concatenate.
*/
explicit InequalityConstraints(
explicit constexpr InequalityConstraints(
const std::vector<InequalityConstraints>& inequalityConstraints) {
for (const auto& elem : inequalityConstraints) {
constraints.insert(constraints.end(), elem.constraints.begin(),
Expand All @@ -642,13 +644,13 @@ struct SLEIPNIR_DLLEXPORT InequalityConstraints {
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
InequalityConstraints(LHS&& lhs, RHS&& rhs)
constexpr InequalityConstraints(LHS&& lhs, RHS&& rhs)
: constraints{MakeConstraints(lhs, rhs)} {}

/**
* Implicit conversion operator to bool.
*/
operator bool() { // NOLINT
constexpr operator bool() { // NOLINT
return std::all_of(
constraints.begin(), constraints.end(),
[](auto& constraint) { return constraint.Value() >= 0.0; });
Expand All @@ -666,7 +668,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
EqualityConstraints operator==(LHS&& lhs, RHS&& rhs) {
constexpr EqualityConstraints operator==(LHS&& lhs, RHS&& rhs) {
return EqualityConstraints{lhs, rhs};
}

Expand All @@ -682,7 +684,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
InequalityConstraints operator<(LHS&& lhs, RHS&& rhs) {
constexpr InequalityConstraints operator<(LHS&& lhs, RHS&& rhs) {
return rhs >= lhs;
}

Expand All @@ -698,7 +700,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
InequalityConstraints operator<=(LHS&& lhs, RHS&& rhs) {
constexpr InequalityConstraints operator<=(LHS&& lhs, RHS&& rhs) {
return rhs >= lhs;
}

Expand All @@ -714,7 +716,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
InequalityConstraints operator>(LHS&& lhs, RHS&& rhs) {
constexpr InequalityConstraints operator>(LHS&& lhs, RHS&& rhs) {
return lhs >= rhs;
}

Expand All @@ -730,7 +732,7 @@ template <typename LHS, typename RHS>
(ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
(!std::same_as<std::decay_t<LHS>, double> ||
!std::same_as<std::decay_t<RHS>, double>)
InequalityConstraints operator>=(LHS&& lhs, RHS&& rhs) {
constexpr InequalityConstraints operator>=(LHS&& lhs, RHS&& rhs) {
return InequalityConstraints{lhs, rhs};
}

Expand Down
Loading

0 comments on commit 73ae6f1

Please sign in to comment.