Documentation 6b54b78464
Loading...
Searching...
No Matches
container_decorators.hpp
Go to the documentation of this file.
1/*!
2 * \file container_decorators.hpp
3 * \brief Collection of small classes that decorate C2DContainer to
4 * augment its functionality, e.g. give it extra dimensions.
5 * \author P. Gomes
6 * \version 8.5.0 "Harrier"
7 *
8 * SU2 Project Website: https://su2code.github.io
9 *
10 * The SU2 Project is maintained by the SU2 Foundation
11 * (http://su2foundation.org)
12 *
13 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
14 *
15 * SU2 is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * SU2 is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29#pragma once
30
31#include "C2DContainer.hpp"
32
33/// \addtogroup Containers
34/// @{
35
36/*!
37 * \brief Class to represent a matrix (without owning the data, this just wraps a pointer).
38 */
39template <class T>
41 public:
42 using Scalar = typename std::remove_const<T>::type;
43 using Index = unsigned long;
44
45 private:
46 T* m_ptr;
47 Index m_cols;
48
49 public:
50 CMatrixView(T* ptr = nullptr, Index cols = 0) : m_ptr(ptr), m_cols(cols) {}
51
52 template <class U>
53 friend class CMatrixView;
54 template <class U>
55 CMatrixView(const CMatrixView<U>& other) : m_ptr(other.m_ptr), m_cols(other.m_cols) {}
56
57 explicit CMatrixView(su2matrix<Scalar>& mat) : m_ptr(mat.data()), m_cols(mat.cols()) {}
58
59 template <class U = T, su2enable_if<std::is_const<U>::value> = 0>
60 explicit CMatrixView(const su2matrix<Scalar>& mat) : m_ptr(mat.data()), m_cols(mat.cols()) {}
61
62 const Scalar* operator[](Index i) const noexcept { return &m_ptr[i * m_cols]; }
63 const Scalar& operator()(Index i, Index j) const noexcept { return m_ptr[i * m_cols + j]; }
64
65 /*!
66 * \brief Return copy of data in a static size container (see C2DContainer::get).
67 * \param[in] i - Row of the view (e.g. point index, whole-mesh usage).
68 * \param[in] start - Starting column to copy the data (amount determined by container size).
69 */
70 template <class StaticContainer>
71 StaticContainer get(Index i, Index start = 0) const noexcept {
72 constexpr size_t Size = StaticContainer::StaticSize;
73 static_assert(Size, "This method requires a static output type.");
74 StaticContainer ret;
75 for (size_t k = 0; k < Size; ++k) ret.data()[k] = m_ptr[i * m_cols + start + k];
76 return ret;
77 }
78
79 /*!
80 * \brief SIMD gather version of get, one row per lane.
81 */
82 template <class StaticContainer, class U, size_t N>
83 StaticContainer get(simd::Array<U, N> i, Index start = 0) const noexcept {
84 constexpr size_t Size = StaticContainer::StaticSize;
85 static_assert(Size, "This method requires a static output type.");
86 StaticContainer ret;
87 for (size_t lane = 0; lane < N; ++lane)
88 for (size_t k = 0; k < Size; ++k) ret.data()[k][lane] = m_ptr[i[lane] * m_cols + start + k];
89 return ret;
90 }
91
92 template <class U = T, su2enable_if<!std::is_const<U>::value> = 0>
93 Scalar* operator[](Index i) noexcept {
94 return &m_ptr[i * m_cols];
95 }
96
97 template <class U = T, su2enable_if<!std::is_const<U>::value> = 0>
98 Scalar& operator()(Index i, Index j) noexcept {
99 return m_ptr[i * m_cols + j];
100 }
101
102 friend CMatrixView operator+(CMatrixView mv, Index incr) { return CMatrixView(mv[incr], mv.m_cols); }
103};
104
105/*!
106 * \class C3DContainerDecorator
107 * \brief Decorate a matrix type (Storage) with 3 dimensions.
108 */
109template <class Storage>
111 static_assert(!Storage::IsVector, "Storage type must be a matrix.");
112 static_assert(Storage::IsRowMajor, "Storage type must be row major.");
113
114 public:
115 using Scalar = typename Storage::Scalar;
116 using Index = typename Storage::Index;
117 static constexpr bool IsRowMajor = true;
118 static constexpr bool IsColumnMajor = false;
119
122
123 using CInnerIter = typename Storage::CInnerIter;
124 template <class T, size_t N>
125 using CInnerIterGather = typename Storage::template CInnerIterGather<simd::Array<T, N> >;
126
127 private:
128 Storage m_storage;
129 Index m_innerSz;
130
131 public:
133
135 resize(length, rows, cols, value);
136 }
137
138 void resize(Index length, Index rows, Index cols, Scalar value = 0) noexcept {
139 m_innerSz = cols;
140 m_storage.resize(length, rows * cols) = value;
141 }
142
143 /*!
144 * \brief Container sizes.
145 */
146 Index size() const noexcept { return m_storage.size(); }
147 Index length() const noexcept { return m_storage.rows(); }
148 Index rows() const noexcept { return m_storage.cols() / m_innerSz; }
149 Index cols() const noexcept { return m_innerSz; }
150
151 /*!
152 * \brief Element-wise access.
153 */
154 Scalar& operator()(Index i, Index j, Index k) noexcept { return m_storage(i, j * m_innerSz + k); }
155 const Scalar& operator()(Index i, Index j, Index k) const noexcept { return m_storage(i, j * m_innerSz + k); }
156
157 /*!
158 * \brief Matrix access.
159 */
160 Matrix operator[](Index i) noexcept { return Matrix(m_storage[i], m_innerSz); }
161 ConstMatrix operator[](Index i) const noexcept { return ConstMatrix(m_storage[i], m_innerSz); }
162
163 /*!
164 * \brief Matrix access with an offset.
165 */
166 Matrix operator()(Index i, Index j) noexcept { return Matrix(m_storage[i] + j * m_innerSz, m_innerSz); }
167 ConstMatrix operator()(Index i, Index j) const noexcept {
168 return ConstMatrix(m_storage[i] + j * m_innerSz, m_innerSz);
169 }
170
171 /*!
172 * \brief Get a scalar iterator to the inner-most dimension of the container.
173 */
175 return CInnerIter(&m_storage(i, j * m_innerSz), 1);
176 }
177
178 /*!
179 * \brief Get a SIMD gather iterator to the inner-most dimension of the container.
180 */
181 template <class T, size_t N>
183 return CInnerIterGather<T, N>(m_storage.data(), 1, i * m_storage.cols() + j * m_innerSz);
184 }
185
186 /*!
187 * \brief Return copy of data in a static size container (see C2DContainer::get).
188 * \param[in] i - Outer index.
189 * \param[in] j - Starting middle index for the copy (amount determined by container size).
190 */
191 template <class StaticContainer, class Int>
192 FORCEINLINE StaticContainer get(Int i, Index j = 0) const noexcept {
193 return m_storage.template get<StaticContainer>(i, j * m_innerSz);
194 }
195
196 /*!
197 * \brief Raw data access, for Python wrapper.
198 */
199 FORCEINLINE Scalar* data() { return m_storage.data(); }
200};
201
202/*!
203 * \brief Some typedefs for 3D containers
204 */
208
209/*!
210 * \class C2DDummyLastView
211 * \brief Helper class, adds dummy trailing dimension to a reference of a
212 * vector object making it a dummy matrix.
213 * \note The constness of the object is derived from the template type, but
214 * we allways keep a reference, never a copy of the associated vector.
215 */
216template <class T>
218 static_assert(T::IsVector, "This class decorates vectors.");
219 using Index = typename T::Index;
220 using Scalar = typename T::Scalar;
221
223
225
226 C2DDummyLastView(T& ref) : data(ref) {}
227
228 template <class U = T, su2enable_if<!std::is_const<U>::value> = 0>
229 Scalar& operator()(Index i, Index) noexcept {
230 return data(i);
231 }
232
233 const Scalar& operator()(Index i, Index) const noexcept { return data(i); }
234};
235
236/*!
237 * \class C3DDummyMiddleView
238 * \brief Helper class, adds dummy middle dimension to a reference of a
239 * matrix object making it a dummy 3D array.
240 * \note The constness of the object is derived from the template type, but
241 * we allways keep a reference, never a copy of the associated matrix.
242 */
243template <class T>
245 static_assert(!T::IsVector, "This class decorates matrices.");
246 using Index = typename T::Index;
247 using Scalar = typename T::Scalar;
248
250
252
253 C3DDummyMiddleView(T& ref) : data(ref) {}
254
255 template <class U = T, su2enable_if<!std::is_const<U>::value> = 0>
256 Scalar& operator()(Index i, Index, Index k) noexcept {
257 return data(i, k);
258 }
259
260 const Scalar& operator()(Index i, Index, Index k) const noexcept { return data(i, k); }
261};
262
263/*--- Helper functions to allocate containers of containers. ---*/
264
265/*!
266 * \brief Allocate a vector of varying-size vectors and initialize with some value.
267 * \param[in] M - the first index is >=0 and < M
268 * \param[in] N - the second index is >=0 and < N[first index]
269 * \param[in,out] X - the vector of vectors
270 * \param[in] val - the value for initialization, default is 0
271 * \tparam IndexVector - type of N
272 * \tparam VectorOfVector - type of X
273 */
274template <class IndexVector, class VectorOfVector, class Scalar = int>
275inline void AllocVectorOfVectors(size_t M, const IndexVector& N, VectorOfVector& X, Scalar val = 0) {
276 X.resize(M);
277 for (size_t i = 0; i < M; ++i) {
278 X[i].resize(N[i]);
279 for (auto& x : X[i]) x = val;
280 }
281}
282
283/*!
284 * \overload Deduce outer size from index vector.
285 */
286template <class IndexVector, class VectorOfVector, class Scalar = int>
287inline void AllocVectorOfVectors(const IndexVector& N, VectorOfVector& X, Scalar val = 0) {
288 auto M = N.size();
289 AllocVectorOfVectors(M, N, X, val);
290}
291
292/*!
293 * \brief Allocate a vector of matrices with varying row count, and initialize with some value.
294 * \param[in] M - the first index is >=0 and < M
295 * \param[in] N - the second index is >=0 and < N[first index]
296 * \param[in] P - the third index is >=0 and < P
297 * \param[in,out] X - the vector of matrices
298 * \param[in] val - the value for initialization, default is 0
299 * \tparam IndexVector - type of N
300 * \tparam VectorOfMatrix - type of X
301 */
302template <class IndexVector, class VectorOfMatrix, class Scalar = int>
303inline void AllocVectorOfMatrices(size_t M, const IndexVector& N, size_t P, VectorOfMatrix& X, Scalar val = 0) {
304 X.resize(M);
305 for (size_t i = 0; i < M; ++i) {
306 X[i].resize(N[i], P);
307 for (auto& x : X[i]) x = val;
308 }
309}
310
311/*!
312 * \overload Deduce outer size from index vector.
313 */
314template <class IndexVector, class VectorOfMatrix, class Scalar = int>
315inline void AllocVectorOfMatrices(const IndexVector& N, size_t P, VectorOfMatrix& X, Scalar val = 0) {
316 auto M = N.size();
317 AllocVectorOfMatrices(M, N, P, X, val);
318}
319
320/// @}
A templated vector/matrix object.
#define M(I, J)
CNewtonIntegration::Scalar Scalar
Definition CNewtonIntegration.cpp:31
A templated matrix/vector-like object.
Definition C2DContainer.hpp:374
Decorate a matrix type (Storage) with 3 dimensions.
Definition container_decorators.hpp:110
CInnerIter innerIter(Index i, Index j) const noexcept
Get a scalar iterator to the inner-most dimension of the container.
Definition container_decorators.hpp:174
ConstMatrix operator()(Index i, Index j) const noexcept
Definition container_decorators.hpp:167
static constexpr bool IsColumnMajor
Definition container_decorators.hpp:118
Index cols() const noexcept
Definition container_decorators.hpp:149
StaticContainer get(Int i, Index j=0) const noexcept
Return copy of data in a static size container (see C2DContainer::get).
Definition container_decorators.hpp:192
Index length() const noexcept
Definition container_decorators.hpp:147
Index rows() const noexcept
Definition container_decorators.hpp:148
CMatrixView< const Scalar > ConstMatrix
Definition container_decorators.hpp:121
typename Storage::Scalar Scalar
Definition container_decorators.hpp:115
ConstMatrix operator[](Index i) const noexcept
Definition container_decorators.hpp:161
CInnerIterGather< T, N > innerIter(simd::Array< T, N > i, Index j) const noexcept
Get a SIMD gather iterator to the inner-most dimension of the container.
Definition container_decorators.hpp:182
Index size() const noexcept
Container sizes.
Definition container_decorators.hpp:146
void resize(Index length, Index rows, Index cols, Scalar value=0) noexcept
Definition container_decorators.hpp:138
static constexpr bool IsRowMajor
Definition container_decorators.hpp:117
CMatrixView< Scalar > Matrix
Definition container_decorators.hpp:120
typename Storage::template CInnerIterGather< simd::Array< T, N > > CInnerIterGather
Definition container_decorators.hpp:125
Scalar * data()
Raw data access, for Python wrapper.
Definition container_decorators.hpp:199
typename Storage::Index Index
Definition container_decorators.hpp:116
C3DContainerDecorator(Index length, Index rows, Index cols, Scalar value=0) noexcept
Definition container_decorators.hpp:134
const Scalar & operator()(Index i, Index j, Index k) const noexcept
Definition container_decorators.hpp:155
Scalar & operator()(Index i, Index j, Index k) noexcept
Element-wise access.
Definition container_decorators.hpp:154
C3DContainerDecorator()=default
Matrix operator()(Index i, Index j) noexcept
Matrix access with an offset.
Definition container_decorators.hpp:166
Matrix operator[](Index i) noexcept
Matrix access.
Definition container_decorators.hpp:160
typename Storage::CInnerIter CInnerIter
Definition container_decorators.hpp:123
Class to represent a matrix (without owning the data, this just wraps a pointer).
Definition container_decorators.hpp:40
CMatrixView(const CMatrixView< U > &other)
Definition container_decorators.hpp:55
typename std::remove_const< T >::type Scalar
Definition container_decorators.hpp:42
friend CMatrixView operator+(CMatrixView mv, Index incr)
Definition container_decorators.hpp:102
unsigned long Index
Definition container_decorators.hpp:43
CMatrixView(T *ptr=nullptr, Index cols=0)
Definition container_decorators.hpp:50
Scalar & operator()(Index i, Index j) noexcept
Definition container_decorators.hpp:98
StaticContainer get(Index i, Index start=0) const noexcept
Return copy of data in a static size container (see C2DContainer::get).
Definition container_decorators.hpp:71
Scalar * operator[](Index i) noexcept
Definition container_decorators.hpp:93
CMatrixView(const su2matrix< Scalar > &mat)
Definition container_decorators.hpp:60
friend class CMatrixView
Definition container_decorators.hpp:53
CMatrixView(su2matrix< Scalar > &mat)
Definition container_decorators.hpp:57
const Scalar & operator()(Index i, Index j) const noexcept
Definition container_decorators.hpp:63
const Scalar * operator[](Index i) const noexcept
Definition container_decorators.hpp:62
StaticContainer get(simd::Array< U, N > i, Index start=0) const noexcept
SIMD gather version of get, one row per lane.
Definition container_decorators.hpp:83
A simple SIMD type relying on implicit vectorization, i.e. done by the compiler, explicitly vectorize...
Definition vectorization.hpp:89
#define FORCEINLINE
Definition code_config.hpp:44
void AllocVectorOfVectors(size_t M, const IndexVector &N, VectorOfVector &X, Scalar val=0)
Allocate a vector of varying-size vectors and initialize with some value.
Definition container_decorators.hpp:275
C3DContainerDecorator< su2activematrix > C3DDoubleMatrix
Definition container_decorators.hpp:206
void AllocVectorOfMatrices(size_t M, const IndexVector &N, size_t P, VectorOfMatrix &X, Scalar val=0)
Allocate a vector of matrices with varying row count, and initialize with some value.
Definition container_decorators.hpp:303
Helper class, adds dummy trailing dimension to a reference of a vector object making it a dummy matri...
Definition container_decorators.hpp:217
typename T::Index Index
Definition container_decorators.hpp:219
T & data
Definition container_decorators.hpp:222
C2DDummyLastView(T &ref)
Definition container_decorators.hpp:226
const Scalar & operator()(Index i, Index) const noexcept
Definition container_decorators.hpp:233
Scalar & operator()(Index i, Index) noexcept
Definition container_decorators.hpp:229
C2DDummyLastView()=delete
typename T::Scalar Scalar
Definition container_decorators.hpp:220
Helper class, adds dummy middle dimension to a reference of a matrix object making it a dummy 3D arra...
Definition container_decorators.hpp:244
Scalar & operator()(Index i, Index, Index k) noexcept
Definition container_decorators.hpp:256
typename T::Scalar Scalar
Definition container_decorators.hpp:247
T & data
Definition container_decorators.hpp:249
C3DDummyMiddleView(T &ref)
Definition container_decorators.hpp:253
typename T::Index Index
Definition container_decorators.hpp:246
const Scalar & operator()(Index i, Index, Index k) const noexcept
Definition container_decorators.hpp:260
C3DDummyMiddleView()=delete