Skip to content

Commit

Permalink
[filter] declarative paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Jan 29, 2024
1 parent b696d31 commit d978b84
Show file tree
Hide file tree
Showing 34 changed files with 493 additions and 376 deletions.
11 changes: 5 additions & 6 deletions benchmark/predict_1x1x0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ For more information, please refer to <https://unlicense.org> */
namespace fcarouge::benchmark {
namespace {
//! @benchmark Measure predict, empty benchmark performance.
void bench(::benchmark::State &state) {
using kalman = kalman<float, float>;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{0.F}, output<float>};

kalman filter;

for (auto _ : state) {
for (auto _ : benchmark_state) {
::benchmark::ClobberMemory();
const auto start{clock::now()};

Expand All @@ -61,7 +59,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
13 changes: 7 additions & 6 deletions benchmark/predict_1x1x1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ For more information, please refer to <https://unlicense.org> */
namespace fcarouge::benchmark {
namespace {
//! @benchmark Measure predict, empty benchmark performance.
void bench(::benchmark::State &state) {
using kalman = kalman<float, float, float>;

kalman filter;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{0.F}, output<float>, input<float>};
std::random_device random_device;
std::mt19937 random_generator{random_device()};
std::uniform_real_distribution<float> uniformly_distributed;

for (auto _ : state) {
using kalman = decltype(filter);

for (auto _ : benchmark_state) {
const typename kalman::output u{uniformly_distributed(random_generator)};

::benchmark::ClobberMemory();
Expand All @@ -67,7 +67,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
14 changes: 8 additions & 6 deletions benchmark/predict_linalg_x1x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ template <auto Size> using vector = column_vector<float, Size>;
//! @benchmark Measure the prediction of the filter for different dimensions of
//! states and inputs with the Eigen linear algebra backend.
template <auto StateSize, auto InputSize>
void bench(::benchmark::State &state) {
using kalman = kalman<vector<StateSize>, float, vector<InputSize>>;

kalman filter;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{vector<StateSize>{}}, output<float>,
input<vector<InputSize>>};
std::random_device random_device;
std::mt19937 random_generator{random_device()};
std::uniform_real_distribution<float> uniformly_distributed;

for (auto _ : state) {
using kalman = decltype(filter);

for (auto _ : benchmark_state) {
float uv[InputSize];

internal::for_constexpr<0, InputSize, 1>(
Expand All @@ -84,7 +85,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
13 changes: 7 additions & 6 deletions benchmark/update_1x1x0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ For more information, please refer to <https://unlicense.org> */
namespace fcarouge::benchmark {
namespace {
//! @benchmark Measure update, empty benchmark performance.
void bench(::benchmark::State &state) {
using kalman = kalman<float, float>;

kalman filter;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{0.F}, output<float>};
std::random_device random_device;
std::mt19937 random_generator{random_device()};
std::uniform_real_distribution<float> uniformly_distributed;

for (auto _ : state) {
using kalman = decltype(filter);

for (auto _ : benchmark_state) {
const typename kalman::output z{uniformly_distributed(random_generator)};

::benchmark::ClobberMemory();
Expand All @@ -67,7 +67,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
13 changes: 7 additions & 6 deletions benchmark/update_1x1x1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ For more information, please refer to <https://unlicense.org> */
namespace fcarouge::benchmark {
namespace {
//! @benchmark Measure update, empty benchmark performance.
void bench(::benchmark::State &state) {
using kalman = kalman<float, float, float>;

kalman filter;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{0.F}, output<float>, input<float>};
std::random_device random_device;
std::mt19937 random_generator{random_device()};
std::uniform_real_distribution<float> uniformly_distributed;

for (auto _ : state) {
using kalman = decltype(filter);

for (auto _ : benchmark_state) {
const typename kalman::output z{uniformly_distributed(random_generator)};

::benchmark::ClobberMemory();
Expand All @@ -67,7 +67,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
13 changes: 7 additions & 6 deletions benchmark/update_linalg_xx0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ template <auto Size> using vector = column_vector<float, Size>;
//! @benchmark Measure the update of the filter for different dimensions of
//! states and outputs with the Eigen linear algebra backend.
template <auto StateSize, auto OutputSize>
void bench(::benchmark::State &state) {
using kalman = kalman<vector<StateSize>, vector<OutputSize>, void>;

kalman filter;
void bench(::benchmark::State &benchmark_state) {
kalman filter{state{vector<StateSize>{}}, output<vector<OutputSize>>};
std::random_device random_device;
std::mt19937 random_generator{random_device()};
std::uniform_real_distribution<float> uniformly_distributed;

for (auto _ : state) {
using kalman = decltype(filter);

for (auto _ : benchmark_state) {
float zv[OutputSize];

internal::for_constexpr<0, OutputSize, 1>(
Expand All @@ -93,7 +93,8 @@ void bench(::benchmark::State &state) {
::benchmark::ClobberMemory();
const auto end{clock::now()};

state.SetIterationTime(std::chrono::duration<double>{end - start}.count());
benchmark_state.SetIterationTime(
std::chrono::duration<double>{end - start}.count());
}
}

Expand Down
44 changes: 22 additions & 22 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,23 @@ For more information, please refer to <https://unlicense.org> */
#include <type_traits>

namespace fcarouge {
template <typename, typename, typename, typename, typename> class kalman;
template <typename> class kalman;
} // namespace fcarouge

template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes, typename Char>
template <typename Filter, typename Char>
// It is allowed to add template specializations for any standard library class
// template to the namespace std only if the declaration depends on at least one
// program-defined type and the specialization satisfies all requirements for
// the original template, except where such specializations are prohibited.
// NOLINTNEXTLINE(cert-dcl58-cpp)
struct std::formatter<
fcarouge::kalman<State, Output, Input, UpdateTypes, PredictionTypes>,
Char> {
struct std::formatter<fcarouge::kalman<Filter>, Char> {
constexpr auto parse(std::basic_format_parse_context<Char> &parse_context) {
return parse_context.begin();
}

//! @todo P2585 may be useful in simplifying and standardizing the support.
template <typename OutputIt>
auto format(const fcarouge::kalman<State, Output, Input, UpdateTypes,
PredictionTypes> &filter,
auto format(const fcarouge::kalman<Filter> &filter,
std::basic_format_context<OutputIt, Char> &format_context) const
-> OutputIt {
format_context.advance_to(
Expand All @@ -80,17 +76,19 @@ struct std::formatter<
R"("h": {}, "k": {}, "p": {}, )",
filter.h(), filter.k(), filter.p()));

{
constexpr auto end{fcarouge::internal::repack_s<PredictionTypes>};
[&]<template <typename> typename Type, typename InternalFilter>(
const Type<InternalFilter> &f) {
constexpr auto end{fcarouge::internal::repack_s<
typename InternalFilter::prediction_types>};
constexpr decltype(end) begin{0};
constexpr decltype(end) next{1};
fcarouge::internal::for_constexpr<begin, end, next>(
[&format_context, &filter](auto position) {
[&format_context, &f](auto position) {
format_context.advance_to(
format_to(format_context.out(), R"("prediction_{}": {}, )",
position(), filter.template predict<position>()));
position(), f.template predict<position>()));
});
}
}(filter);

format_context.advance_to(format_to(format_context.out(),
R"("q": {}, "r": {}, "s": {}, )",
Expand All @@ -101,17 +99,19 @@ struct std::formatter<
format_to(format_context.out(), R"("u": {}, )", filter.u()));
}

{
constexpr auto end{fcarouge::internal::repack_s<UpdateTypes>};
[&]<template <typename> typename Type, typename InternalFilter>(
const Type<InternalFilter> &f) {
constexpr auto end{
fcarouge::internal::repack_s<typename InternalFilter::update_types>};
constexpr decltype(end) begin{0};
constexpr decltype(end) next{1};
fcarouge::internal::for_constexpr<begin, end, next>(
[&format_context, &filter](auto position) {
format_context.advance_to(
format_to(format_context.out(), R"("update_{}": {}, )",
position(), filter.template update<position>()));
});
}
fcarouge::internal::for_constexpr<begin, end, next>([&format_context,
&f](auto position) {
format_context.advance_to(format_to(format_context.out(),
R"("update_{}": {}, )", position(),
f.template update<position>()));
});
}(filter);

format_context.advance_to(format_to(format_context.out(),
R"("x": {}, "y": {}, "z": {}}})",
Expand Down
Loading

0 comments on commit d978b84

Please sign in to comment.