-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a linear algebra library dependency
Add `mdspan` and `stdBLAS` as dependencies in order to allow use of a linear algebra library. Change-Id: I9d2e1f43f6df315a922753d1d88e5904888b3d16
- Loading branch information
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,7 @@ cc_test( | |
deps = [ | ||
"@boost_ut", | ||
"@fmt", | ||
"@linalg", | ||
"@mdspan", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,87 @@ | ||
#include <boost/ut.hpp> | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <cstddef> | ||
#include <experimental/linalg> | ||
#include <experimental/mdspan> | ||
#include <fmt/core.h> | ||
|
||
namespace stx = std::experimental; | ||
|
||
template <class T, std::size_t R, std::size_t C> | ||
struct matrix | ||
{ | ||
static_assert(R != std::dynamic_extent); | ||
static_assert(C != std::dynamic_extent); | ||
|
||
using extents_type = stx::extents<std::size_t, R, C>; | ||
using mdspan_type = stx::mdspan<T, extents_type>; | ||
using const_mdspan_type = stx::mdspan<const T, extents_type>; | ||
|
||
std::array<T, R * C> data{}; | ||
|
||
matrix() = default; | ||
|
||
matrix(std::initializer_list<std::initializer_list<T>> init) | ||
{ | ||
assert(R == init.size()); | ||
|
||
auto i = std::size_t{}; | ||
for (const auto& row : init) { | ||
assert(C == row.size()); | ||
|
||
auto j = std::size_t{}; | ||
for (const auto& elem : row) { | ||
span()[i, j] = elem; | ||
|
||
++j; | ||
} | ||
|
||
++i; | ||
} | ||
} | ||
|
||
[[nodiscard]] | ||
constexpr auto span() -> mdspan_type { return mdspan_type{data.data()}; } | ||
[[nodiscard]] | ||
constexpr auto span() const -> const_mdspan_type | ||
{ | ||
return const_mdspan_type{data.data()}; | ||
} | ||
|
||
[[nodiscard]] | ||
constexpr auto | ||
operator[](std::size_t i, std::size_t j) -> mdspan_type::reference | ||
{ | ||
return span()[i, j]; | ||
} | ||
[[nodiscard]] | ||
constexpr auto | ||
operator[](std::size_t i, std::size_t j) const -> const_mdspan_type::reference | ||
{ | ||
return span()[i, j]; | ||
} | ||
}; | ||
|
||
auto main() -> int | ||
{ | ||
using ::boost::ut::expect; | ||
using ::boost::ut::test; | ||
|
||
// NOLINTBEGIN(readability-magic-numbers) | ||
test("true is true") = [] { | ||
const auto s = fmt::format("The answer is {}.", 42); | ||
using Mat = matrix<int, 2, 2>; | ||
|
||
auto A = Mat{{1, 2}, {3, 4}}; | ||
auto B = Mat{{5, 6}, {7, 8}}; | ||
auto C = Mat{}; | ||
|
||
stx::linalg::matrix_product(A.span(), B.span(), C.span()); | ||
|
||
const auto s = fmt::format("The answer is {}.", C[0, 0]); | ||
|
||
expect("The answer is 42." == s); | ||
expect("The answer is 19." == s); | ||
}; | ||
// NOLINTEND(readability-magic-numbers) | ||
} |