Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[filter] support lazy linear algebra #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ foreach(BENCHMARK "baseline.cpp" "predict_1x1x0.cpp" "predict_1x1x1.cpp"
"--benchmark_out=${NAME}.json")
endforeach()

foreach(BACKEND IN ITEMS "eigen" "naive")
foreach(STATE_SIZE RANGE 1 2)
foreach(
BACKEND IN
ITEMS # "eigen"
"lazy"
# "naive"
)
foreach(STATE_SIZE RANGE 1 1)
foreach(INPUT_SIZE RANGE 1 2)
configure_file(predict_linalg_x1x.cpp
predict_${BACKEND}_${STATE_SIZE}x1x${INPUT_SIZE}.cpp)
Expand All @@ -100,7 +105,8 @@ foreach(BACKEND IN ITEMS "eigen" "naive")
endforeach()
endforeach()

foreach(BACKEND IN ITEMS "eigen")
foreach(BACKEND IN ITEMS # "eigen"
)
foreach(STATE_SIZE RANGE 1 2)
foreach(OUTPUT_SIZE RANGE 1 2)
configure_file(update_linalg_xx0.cpp
Expand Down
1 change: 1 addition & 0 deletions include/fcarouge/internal/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct transpose final {
//! @todo Is there a better, simpler, canonical, standard way of doing this type
//! deduction? For example, by doing it directly from the operation itself?
//! There could be simplicity and performance benefits?
//! @todo Really need to fix this and either have it done automatically or moved out. Need to find a better way.
struct deducer final {
// Built-in, arithmetic, standard division support.
template <arithmetic Lhs, arithmetic Rhs>
Expand Down
85 changes: 66 additions & 19 deletions linalg/lazy/fcarouge/linalg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ For more information, please refer to <https://unlicense.org> */

#include "fcarouge/utility.hpp"

// #include <range/v3/range.hpp>

#include <algorithm>
#include <array>
#include <concepts>
#include <coroutine>
#include <format>
#include <generator>
#include <ranges>
#include <type_traits>
Expand Down Expand Up @@ -101,11 +104,13 @@ inline constexpr auto make_generator(Type element) -> std::generator<Type> {
//! @note A design decision for the composed generator to be mutable, traded
//! off for const member function API. Similar to the mutable mutex member
//! practice.
//! @note Why genie? Because genies generate on demand.
//! @note Why genie? Because genies generate on demand...
//!
//! @todo Explore and compare performance.
//! @todo Explore optimization of heap allocations?
//! @todo Explore constexpr support?
//! @todo Explore an implementation where each element is a generator?
//! @todo Explore cyclic generator to keep moving forward and not track results?
//! @todo Explore verification of lazy evaluation?
//! @todo Remove unecessary empty paramaters when MSVC supports lambda without
//! them.
Expand Down Expand Up @@ -155,7 +160,8 @@ struct matrix {
return *this;
}

inline constexpr explicit matrix(const std::same_as<Type> auto &...elements)
inline constexpr explicit(false)
matrix(const std::same_as<Type> auto &...elements)
requires(sizeof...(elements) == Row * Column)
: genie{[](auto... elements_copy) -> std::generator<Type> {
(co_yield elements_copy, ...);
Expand Down Expand Up @@ -322,27 +328,27 @@ auto make_zero_generator{[]() -> std::generator<Type> {
//! @name Algebraic Named Values
//! @{
//! @brief The identity matrix lazy specialization.
template <typename Type, auto Row, auto Column>
auto identity_v<matrix<Type, Row, Column>>{[](auto... args) {
template <typename Type, auto Row, auto Column, bool CopyableOrNot>
auto identity_v<matrix<Type, Row, Column, CopyableOrNot>>{[](auto... args) {
matrix<Type, Row, Column, true> m{
make_identity_generator<Type, Row, Column>()};
if constexpr (sizeof...(args)) {
return m(args...);
} else {
return m;
}
}};
}()};

//! @brief The zero matrix lazy specialization.
template <typename Type, auto Row, auto Column>
auto zero_v<matrix<Type, Row, Column>>{[](auto... args) {
template <typename Type, auto Row, auto Column, bool CopyableOrNot>
auto zero_v<matrix<Type, Row, Column, CopyableOrNot>>{[](auto... args) {
matrix<Type, Row, Column, true> m{make_zero_generator<Type, Row, Column>()};
if constexpr (sizeof...(args)) {
return m(args...);
} else {
return m;
}
}};
}()};

//! @}

Expand Down Expand Up @@ -370,9 +376,20 @@ operator==(matrix<Type, Row, Column, CopyableOrNot1> lhs,
return lhs_elements == rhs_elements;
}

template <typename Type, auto Row, auto Size>
[[nodiscard]] inline matrix<Type, Row, 1> operator*(matrix<Type, Row, Size> lhs,
matrix<Type, Size, 1> rhs) {
template <typename Type, auto Row, auto Column>
[[nodiscard]] inline matrix<Type, Row, Column>
operator*(matrix<Type, Row, Column> lhs, arithmetic auto rhs) {
auto next{lhs.begin()};
for (auto k{Row * Column}; k > 0; --k, ++next) {
co_yield *next *rhs;
}
}

template <typename Type, auto Row, auto Size, bool CopyableOrNot1,
bool CopyableOrNot2>
[[nodiscard]] inline matrix<Type, Row, 1>
operator*(matrix<Type, Row, Size, CopyableOrNot1> lhs,
matrix<Type, Size, 1, CopyableOrNot2> rhs) {
// fix me?
auto next1{lhs.begin()};
for (decltype(Row) i{0}; i < Row; ++i) { // chunk_by_rows
Expand All @@ -386,14 +403,16 @@ template <typename Type, auto Row, auto Size>
}
}

template <typename Type, auto Row, auto Column>
[[nodiscard]] inline matrix<Type, Row, Column>
operator*(matrix<Type, Row, Column> lhs, arithmetic auto rhs) {
auto next{lhs.begin()};
for (auto k{Row * Column}; k > 0; --k, ++next) {
co_yield *next *rhs;
}
}
// //! @todo Implement me.
// template <typename Type, auto Row, auto Size, auto Column>
// [[nodiscard]] inline matrix<Type, Row, Column>
// operator*(matrix<Type, Row, Size> lhs, matrix<Type, Size, Column> rhs) {
// static_cast<void>(lhs);
// static_cast<void>(rhs);
// for (auto k{Row * Column}; k > 0; --k) {
// co_yield Type{};
// }
// }

template <typename Type>
[[nodiscard]] inline matrix<Type, 1, 1> operator+(Type lhs,
Expand All @@ -410,6 +429,34 @@ operator+(matrix<Type, Row, Column> lhs, matrix<Type, Row, Column> rhs) {
co_yield *next1 + *next2;
}
}

// //! @todo Implement me.
// template <typename Type, auto Row, auto Column>
// [[nodiscard]] inline matrix<Type, Column, Row>
// transpose(matrix<Type, Row, Column> other) {
// static_cast<void>(other);
// for (auto k{Row * Column}; k > 0; --k) {
// co_yield Type{};
// }
// }
} // namespace fcarouge

// template <typename Type, auto Row, auto Column, typename Char>
// struct std::formatter<fcarouge::matrix<Type, Row, Column>, Char>
// : public std::formatter<Type, 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::matrix<Type, Row, Column> &other,
// std::basic_format_context<OutputIt, Char> &format_context)
// -> OutputIt {

// return format_context.out();
// }
// };

#endif // FCAROUGE_LINALG_HPP
23 changes: 23 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ foreach(BACKEND IN ITEMS "eigen" "lazy" "naive")
"linalg_constructor_nx1_array.cpp"
"linalg_constructor_nx1.cpp"
"linalg_copy.cpp"
"linalg_identity_1x1.cpp"
"linalg_identity_default.cpp"
"linalg_identity.cpp"
"linalg_multiplication_arithmetic.cpp"
"linalg_multiplication_rxc.cpp"
"linalg_multiplication_sxc.cpp"
# "linalg_multiplication_mxn.cpp"
"linalg_operator_bracket.cpp"
"linalg_operator_equality.cpp"
"linalg_zero_default.cpp"
Expand Down Expand Up @@ -150,3 +152,24 @@ foreach(BACKEND IN ITEMS "lazy")
$<TARGET_FILE:kalman_test_${BACKEND}_${NAME}_driver>)
endforeach()
endforeach()

foreach(BACKEND IN ITEMS "lazy")
foreach(TEST "linalg_constructor_default.cpp"
# "linalg_transpose.cpp"
)
get_filename_component(NAME ${TEST} NAME_WE)
add_executable(kalman_test_${BACKEND}_${NAME}_driver ${TEST})
set_target_properties(kalman_test_${BACKEND}_${NAME}_driver
PROPERTIES CXX_STANDARD 23)
set_target_properties(kalman_test_${BACKEND}_${NAME}_driver
PROPERTIES CXX_EXTENSIONS OFF)
target_link_libraries(kalman_test_${BACKEND}_${NAME}_driver
PRIVATE kalman kalman_main kalman_linalg_${BACKEND})
add_test(kalman_test_${BACKEND}_${NAME}
kalman_test_${BACKEND}_${NAME}_driver)
add_test(
NAME kalman_${BACKEND}_valgrind_${NAME}
COMMAND valgrind --error-exitcode=1 --leak-check=full --track-origins=yes
$<TARGET_FILE:kalman_test_${BACKEND}_${NAME}_driver>)
endforeach()
endforeach()
65 changes: 65 additions & 0 deletions test/linalg_constructor_default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|

Kalman Filter
Version 0.3.0
https://github.com/FrancoisCarouge/Kalman

SPDX-License-Identifier: Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org> */

#include "fcarouge/linalg.hpp"

#include <cassert>

namespace fcarouge::test {
namespace {
//! @test Verifies the initializer lists constructor.
//!
//! @todo Should this test exists? Or should default be all uninitialized a la
//! Eigen3?
[[maybe_unused]] auto test{[] {
matrix<double, 3, 3> m{};

assert(m(0, 0) == 0.0);
assert(m(0, 1) == 0.0);
assert(m(0, 2) == 0.0);
assert(m(1, 0) == 0.0);
assert(m(1, 1) == 0.0);
assert(m(1, 2) == 0.0);
assert(m(2, 0) == 0.0);
assert(m(2, 1) == 0.0);
assert(m(2, 2) == 0.0);

return 0;
}()};
} // namespace
} // namespace fcarouge::test
1 change: 1 addition & 0 deletions test/linalg_constructor_initializer_lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ For more information, please refer to <https://unlicense.org> */
#include "fcarouge/linalg.hpp"

#include <cassert>
#include <type_traits>

namespace fcarouge::test {
namespace {
Expand Down
58 changes: 58 additions & 0 deletions test/linalg_identity_1x1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|

Kalman Filter
Version 0.3.0
https://github.com/FrancoisCarouge/Kalman

SPDX-License-Identifier: Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org> */

#include "fcarouge/linalg.hpp"

#include <cassert>

namespace fcarouge::test {
namespace {
//! @test Verifies the identity matrices values are unit diagonals.
//!
//! @todo Rewrite this test as a property-based test.
[[maybe_unused]] auto test{[] {
matrix<double, 1, 1> i1{identity_v<matrix<double, 1, 1>>};
auto i2{identity_v<matrix<double, 1, 1>>};

// assert(i1(0, 0) == 1.0);
assert(i2(0, 0) == 1.0);

return 0;
}()};
} // namespace
} // namespace fcarouge::test
Loading
Loading