Skip to content

Commit

Permalink
fix perfect forwarding in coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
supersega committed Feb 3, 2024
1 parent b540959 commit 13906ab
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ find_package(GSL REQUIRED)
add_subdirectory(autodiff)
add_subdirectory(nld)

if (ENABLE_ASAN)
message(STATUS "Enabling AddressSanitizer")
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=ad:dress)
endif ()

if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(examples)
enable_testing()
Expand Down
4 changes: 2 additions & 2 deletions nld/autocont/arc_length_continuation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace nld {
template <typename F, typename V, typename T, typename M>
auto arc_length(F &&function, nld::continuation_parameters parameters,
V unknowns, T tangential, M map) {
return nld::internal::arc_length_raw(std::forward<F>(function), parameters,
unknowns, tangential, map);
return nld::internal::arc_length_raw<F, V, T, M>(
std::forward<F>(function), parameters, unknowns, tangential, map);
}

/// @brief Solves continuation problem for nonlinear function using arc length
Expand Down
5 changes: 4 additions & 1 deletion nld/autocont/arc_length_raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ namespace nld::internal {
/// @param tangential Initial guess vector which has structure (u, lambda)
/// @param map Function to map solution f: (V, R) -> MappedType.
/// @return Generator which yields solution on iteration.
/// @note This takes F as a value, because coroutines can't handle
/// references properly, for more details see:
/// https://toby-allsopp.github.io/2017/04/22/coroutines-reference-params.html
template <typename F, Vector V, Vector T, typename M>
auto arc_length_raw(F &&function, nld::continuation_parameters parameters,
auto arc_length_raw(F function, nld::continuation_parameters parameters,
V unknowns, T tangential, M map) noexcept
-> cppcoro::generator<decltype(map(std::declval<F>(), std::declval<V>()))> {
auto [newton_parameters, tail, min_step, max_step, dir] = parameters;
Expand Down
2 changes: 1 addition & 1 deletion nld/autocont/arc_length_representation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct arc_length_representation final {

auto value = v.head(dim - 1);

auto keller = [*this](auto &val) { return arc_length_equation(val); };
auto keller = [this](auto &val) { return arc_length_equation(val); };
auto dkeller = autodiff::forward::gradient(keller, nld::wrt(at),
nld::at(at), v(dim - 1));

Expand Down
10 changes: 8 additions & 2 deletions nld/systems/ode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct non_autonomous final {
using vector_t = decltype(concepts::non_autonomous::call_fn<Fn>());

/// @brief Make non autonomous system.
explicit non_autonomous(Fn f) : function(f) {}
explicit non_autonomous(Fn &&f) : function(std::forward<Fn>(f)) {}

/// @brief Operator to wrap function for earthier usage.
/// @param y State variables for ODE.
Expand Down Expand Up @@ -92,6 +92,9 @@ struct non_autonomous final {
Fn function; ///< Underlying function represented dynamic system.
};

template <typename Fn>
non_autonomous(Fn &&) -> non_autonomous<Fn>;

template <typename T>
struct is_non_autonomous : std::false_type {};

Expand Down Expand Up @@ -136,7 +139,7 @@ struct autonomous final {
using vector_t = decltype(concepts::autonomous::call_fn<Fn>());

/// @brief Make autonomous system.
explicit autonomous(Fn f) : function(f) {}
explicit autonomous(Fn &&f) : function(std::forward<Fn>(f)) {}

/// @brief Operator to wrap function for earthier usage.
/// @param y State variables for ODE.
Expand Down Expand Up @@ -176,6 +179,9 @@ struct autonomous final {
Fn function; ///< Underlying function represented dynamic system.
};

template <typename Fn>
autonomous(Fn &&) -> autonomous<Fn>;

template <typename T>
struct is_autonomous : std::false_type {};

Expand Down
4 changes: 2 additions & 2 deletions nld/systems/periodic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ auto integration_arguments(
/// \f$\dot{q} = \theta(q, t, \lambda)\f$.
/// @param parameters parameters of ODE integration
template <OdeSolver S, typename Ds, typename P>
auto periodic(Ds dynamic_system, P parameters) {
return internal::periodic<S, Ds>(std::move(dynamic_system),
auto periodic(Ds &&dynamic_system, P parameters) {
return internal::periodic<S, Ds>(std::forward<Ds>(dynamic_system),
std::move(parameters));
}

Expand Down
24 changes: 22 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_TOOLCHAIN_FILE:STRING=/Volumes/Data/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/Volumes/Data/clang/clang+llvm-12.0.0-x86_64-apple-darwin/bin/clang -DCMAKE_CXX_COMPILER:FILEPATH=/Volumes/Data/clang/clang+llvm-12.0.0-x86_64-apple-darwin/bin/clang++ -S/Volumes/Data/dev/nldcpp -B/Volumes/Data/dev/nldcpp/build -G "Unix Makefiles"
#!bin/zsh

cmake=/usr/local/bin/cmake
clang=/Volumes/Data/clang/clang+llvm-12.0.0-x86_64-apple-darwin/bin/clang
clangpp=/Volumes/Data/clang/clang+llvm-12.0.0-x86_64-apple-darwin/bin/clang++
vcpkg=/Volumes/Data/dev/vcpkg/scripts/buildsystems/vcpkg.cmake
build_dir=build
build_type=ReleaseWithDebInfo
enable_asan=FALSE

$cmake \
--no-warn-unused-cli \
-DCMAKE_BUILD_TYPE:STRING=$build_type \
-DCMAKE_TOOLCHAIN_FILE:STRING=$vcpkg \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
-DCMAKE_C_COMPILER:FILEPATH=$clang \
-DCMAKE_CXX_COMPILER:FILEPATH=$clangpp \
-DENABLE_ASAN:BOOL=$enable_asan \
-B $build_dir \
-G "Unix Makefiles"

$cmake --build $build_dir --config $build_type --target all

/usr/local/bin/cmake --build /Volumes/Data/dev/nldcpp/build --config RelWithDebInfo --target all -j 10 --

0 comments on commit 13906ab

Please sign in to comment.