DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
ArrayEigenMatrix_DeviceView.hpp
Go to the documentation of this file.
1#pragma once
2/// @file ArrayEigenMatrix_DeviceView.hpp
3/// @brief Device-callable view for @ref DNDS::ArrayEigenMatrix "ArrayEigenMatrix". `operator[]` returns
4/// an `Eigen::Map<Matrix<real, Ni, Nj>>`.
5
6#include "../DeviceView.hpp"
7#include "../EigenUtil.hpp"
8
9namespace DNDS
10{
11 /**
12 * @brief Compute the underlying per-row element count for an `Ni x Nj`
13 * matrix cell.
14 *
15 * @details Resolves to `Ni*Nj` when both are compile-time fixed,
16 * @ref NonUniformSize when either uses per-row sizing, or @ref DynamicSize
17 * for the remaining runtime-determined cases.
18 */
19 template <rowsize _mat_ni, rowsize _mat_nj>
21 {
22 if constexpr (_mat_ni >= 0 && _mat_nj >= 0)
23 {
24 return _mat_ni * _mat_nj;
25 }
26 else if constexpr (_mat_ni == NonUniformSize || _mat_nj == NonUniformSize)
27 {
28 return NonUniformSize;
29 }
30 else
31 {
32 return DynamicSize;
33 }
34 }
35
36 /**
37 * @brief Device-callable view onto @ref DNDS::ArrayEigenMatrix "ArrayEigenMatrix" rows.
38 *
39 * @details Mirrors the host `operator[]` -> `Eigen::Map<Matrix>` but works
40 * inside CUDA kernels. The underlying @ref DNDS::ArrayDeviceView "ArrayDeviceView" stores the flat row
41 * of `Ni*Nj` reals; this class reinterprets that row as an Eigen matrix.
42 *
43 * @tparam real_T `DNDS::real` or `const DNDS::real`.
44 */
45 template <DeviceBackend B, class real_T, rowsize _mat_ni = 1, rowsize _mat_nj = 1,
46 rowsize _mat_ni_max = _mat_ni, rowsize _mat_nj_max = _mat_nj, rowsize _align = NoAlign>
48 __OneMatGetRowSize<_mat_ni, _mat_nj>(),
49 __OneMatGetRowSize<_mat_ni_max, _mat_nj_max>(),
50 _align>
51 {
52 public:
53 using t_base = ArrayDeviceView<B, real_T,
54 __OneMatGetRowSize<_mat_ni, _mat_nj>(),
55 __OneMatGetRowSize<_mat_ni_max, _mat_nj_max>(),
56 _align>;
57 // using t_base::t_base;
59
60 protected:
61 std::conditional_t<_mat_ni == DynamicSize, rowsize, EmptyNoDefault> _mat_nRow_dynamic = 0;
62 std::conditional_t<_mat_ni == NonUniformSize, const rowsize *, EmptyNoDefault> _mat_nRows = nullptr;
63
64 public:
66
68 const rowsize *n_mat_nRows, rowsize n_mat_nRow_dynamic)
69 : t_base(base_view), _mat_nRow_dynamic(n_mat_nRow_dynamic), _mat_nRows(n_mat_nRows)
70 {
71 if constexpr (_mat_ni != NonUniformSize)
72 DNDS_HD_assert(n_mat_nRows == nullptr);
73 if constexpr (_mat_ni != DynamicSize)
74 DNDS_HD_assert(n_mat_nRow_dynamic == 0);
75 }
76
77 using t_EigenMatrix = Eigen::Matrix<std::remove_cv_t<real_T>, RowSize_To_EigenSize(_mat_ni), RowSize_To_EigenSize(_mat_nj)>;
78 using t_EigenMap_const = Eigen::Map<const t_EigenMatrix, Eigen::Unaligned>; // default no buffer align and stride
79 using t_EigenMap = std::conditional_t<std::is_const_v<real_T>,
81 Eigen::Map<t_EigenMatrix, Eigen::Unaligned>>; // default no buffer align and stride
83 using t_EigenView_const = EigenMatrixView<B, const real_T, RowSize_To_EigenSize(_mat_ni), RowSize_To_EigenSize(_mat_nj)>;
84
85 DNDS_DEVICE_CALLABLE [[nodiscard]] rowsize MatRowSize(index iMat = 0) const
86 {
87 if constexpr (_mat_ni >= 0)
88 return _mat_ni;
89 if constexpr (_mat_ni == NonUniformSize)
90 {
91 DNDS_HD_assert(iMat >= 0 && iMat < this->Size());
92 return _mat_nRows[iMat];
93 }
94 if constexpr (_mat_ni == DynamicSize)
95 return _mat_nRow_dynamic;
96 return UnInitRowsize; // invalid branch
97 }
98
99 DNDS_DEVICE_CALLABLE [[nodiscard]] rowsize MatColSize(index iMat = 0) const
100 {
101 if constexpr (_mat_nj >= 0)
102 return _mat_nj;
103 if constexpr (_mat_nj == NonUniformSize)
104 return this->t_base::RowSize(iMat) / this->MatRowSize(iMat);
105 if constexpr (_mat_nj == DynamicSize)
106 return this->t_base::RowSize(iMat) / this->MatRowSize(iMat);
107 return UnInitRowsize; // invalid branch
108 }
109
110#define DNDS_ARRAYEIGENMATRIXVIEW_GETTER_PREREQ \
111 DNDS_HD_assert_infof(iRow >= 0 && iRow < this->Size(), "invalid index %lld / %lld", iRow, this->Size()); \
112 rowsize c_nRow; \
113 if constexpr (_mat_ni == NonUniformSize) \
114 c_nRow = _mat_nRows[iRow]; \
115 else if constexpr (_mat_ni == DynamicSize) \
116 c_nRow = _mat_nRow_dynamic; \
117 else \
118 c_nRow = _mat_ni;
119
121 {
123 return {t_base::operator[](iRow), c_nRow, t_base::RowSize(iRow) / c_nRow}; // need static dispatch?
124 }
125
127 {
129 return {t_base::operator[](iRow), c_nRow, t_base::RowSize(iRow) / c_nRow}; // need static dispatch?
130 }
131
132 DNDS_DEVICE_CALLABLE std::conditional_t<_mat_ni == 1 && _mat_nj == 1,
133 real &, void>
135 {
136 if constexpr (_mat_ni == 1 && _mat_nj == 1)
137 return *t_base::operator[](iRow);
138 }
139
140 DNDS_DEVICE_CALLABLE std::conditional_t<_mat_ni == 1 && _mat_nj == 1,
141 real, void>
142 operator()(index iRow) const
143 {
144 if constexpr (_mat_ni == 1 && _mat_nj == 1)
145 return *t_base::operator[](iRow);
146 }
147
148 DNDS_DEVICE_CALLABLE std::conditional_t<_mat_ni == 1 || _mat_nj == 1,
149 real &, void>
151 {
152 if constexpr (_mat_ni == 1 || _mat_nj == 1)
153 return t_base::operator()(iRow, j);
154 }
155
156 DNDS_DEVICE_CALLABLE std::conditional_t<_mat_ni == 1 || _mat_nj == 1,
157 real, void>
158 operator()(index iRow, rowsize j) const
159 {
160 if constexpr (_mat_ni == 1 || _mat_nj == 1)
161 return t_base::operator()(iRow, j);
162 }
163
165 {
167 return {t_base::operator[](iRow), c_nRow, t_base::RowSize(iRow) / c_nRow}; // need static dispatch?
168 }
169
171 {
173 return {t_base::operator[](iRow), c_nRow, t_base::RowSize(iRow) / c_nRow}; // need static dispatch?
174 }
175 };
176}
#define DNDS_ARRAYEIGENMATRIXVIEW_GETTER_PREREQ
#define DNDS_DEVICE_TRIVIAL_COPY_DEFINE(T, T_Self)
Definition Defines.hpp:83
#define DNDS_DEVICE_CALLABLE
Definition Defines.hpp:76
#define DNDS_HD_assert(cond)
Host-only expansion of DNDS_HD_assert (equivalent to DNDS_assert).
Definition Errors.hpp:189
Non-owning device-callable view of an Array, specialised per DeviceBackend.
Device-callable view onto ArrayEigenMatrix rows.
DNDS_DEVICE_CALLABLE std::conditional_t< _mat_ni==1||_mat_nj==1, real, void > operator()(index iRow, rowsize j) const
std::conditional_t< _mat_ni==NonUniformSize, const rowsize *, EmptyNoDefault > _mat_nRows
Eigen::Matrix< std::remove_cv_t< real_T >, RowSize_To_EigenSize(_mat_ni), RowSize_To_EigenSize(_mat_nj)> t_EigenMatrix
DNDS_DEVICE_CALLABLE std::conditional_t< _mat_ni==1||_mat_nj==1, real &, void > operator()(index iRow, rowsize j)
Eigen::Map< const t_EigenMatrix, Eigen::Unaligned > t_EigenMap_const
DNDS_DEVICE_CALLABLE rowsize MatRowSize(index iMat=0) const
DNDS_DEVICE_CALLABLE rowsize MatColSize(index iMat=0) const
std::conditional_t< std::is_const_v< real_T >, t_EigenMap_const, Eigen::Map< t_EigenMatrix, Eigen::Unaligned > > t_EigenMap
DNDS_DEVICE_CALLABLE std::conditional_t< _mat_ni==1 &&_mat_nj==1, real &, void > operator()(index iRow)
DNDS_DEVICE_CALLABLE t_EigenMap_const operator[](index iRow) const
DNDS_DEVICE_CALLABLE std::conditional_t< _mat_ni==1 &&_mat_nj==1, real, void > operator()(index iRow) const
DNDS_DEVICE_CALLABLE t_EigenView MatView(index iRow)
DNDS_DEVICE_CALLABLE t_EigenView MatView(index iRow) const
std::conditional_t< _mat_ni==DynamicSize, rowsize, EmptyNoDefault > _mat_nRow_dynamic
DNDS_DEVICE_CALLABLE t_EigenMap operator[](index iRow)
T * operator[](index iRow)
Raw row pointer. iRow == Size() is allowed for past-the-end queries (useful for computing buffer end ...
DNDS_DEVICE_CALLABLE index Size() const
Number of rows in the viewed array.
DNDS_DEVICE_CALLABLE rowsize RowSize() const
Uniform row width for fixed layouts (asserts otherwise).
the host side operators are provided as implemented
DeviceBackend
Enumerates the backends a DeviceStorage / Array can live on.
DNDS_CONSTANT const rowsize NoAlign
Alignment flag: no padding applied to rows (the only currently-supported value).
Definition Defines.hpp:282
constexpr int RowSize_To_EigenSize(rowsize rs)
Convert a rowsize constant to the corresponding Eigen compile-time size. Fixed >= 0 -> the value; Dyn...
Definition Defines.hpp:286
int32_t rowsize
Row-width / per-row element-count type (signed 32-bit).
Definition Defines.hpp:109
DNDS_CONSTANT const rowsize DynamicSize
Template parameter flag: "row width is set at runtime but uniform".
Definition Defines.hpp:277
constexpr rowsize __OneMatGetRowSize()
Compute the underlying per-row element count for an Ni x Nj matrix cell.
DNDS_CONSTANT const rowsize NonUniformSize
Template parameter flag: "each row has an independent width".
Definition Defines.hpp:279
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:107
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105
DNDS_CONSTANT const rowsize UnInitRowsize
Sentinel "not initialised" rowsize value (= INT32_MIN).
Definition Defines.hpp:179