NeoN
A framework for CFD software
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
12
13namespace NeoN::la
14{
15
23template<typename ValueType, typename SparsityViewType>
25{
32 MatrixView(const View<ValueType> valueView, SparsityViewType sparsityView)
33 : values(valueView), sparsity(sparsityView) {};
34
38 ~MatrixView() = default;
39
46 KOKKOS_INLINE_FUNCTION
47 ValueType& entry(const localIdx i, const localIdx j) const
48 {
49 return values[sparsity.entry(i, j)];
50 }
51
57 KOKKOS_INLINE_FUNCTION
58 ValueType& entry(const localIdx offset) const { return values[offset]; }
59
61 SparsityViewType sparsity;
62};
63
70template<typename ValueType, typename SparsityType>
71class Matrix : public NeoN::SupportsCopyTo<Matrix<ValueType, SparsityType>>
72{
73
74 void validate()
75 {
76 NF_ASSERT(values_.exec() == sparsityPattern_->exec(), "Executors are not the same");
77 // TODO this is not necessarily true for matrix types with padding like ELL
79 toLocalIdx(values_.size()) == sparsityPattern_->nnz(),
80 "Matrix values and columns mismatch"
81 );
82 }
83
84public:
85
86 using MatrixValueType = ValueType;
87 using MatrixSparsityType = SparsityType;
88
94 Matrix(const Vector<ValueType>& values, std::shared_ptr<const SparsityType> sp)
95 : values_(values), sparsityPattern_(sp)
96 {
97 validate();
98 }
99
110 Dimensions dimensions
111 )
112 : values_(values),
113 sparsityPattern_(
114 std::make_shared<const SparsityType>(Vector(colIdxs), Vector(rowOffs), dimensions)
115 )
116 {
117 validate();
118 }
119
132 std::shared_ptr<const SparsityType> sparsity,
133 std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress
134 )
135 requires std::is_same_v<typename SparsityType::SparsityIndexType, localIdx>
136 : values_(values), sparsityPattern_(sparsity), faceToMatrixAddress_(faceToMatrixAddress)
137 {
138 validate();
139 }
140
144 ~Matrix() = default;
145
150 [[nodiscard]] const Executor& exec() const { return values_.exec(); }
151
156 [[nodiscard]] localIdx nRows() const { return sparsityPattern_->rows(); }
157
162 [[nodiscard]] localIdx nNonZeros() const { return sparsityPattern_->nnz(); }
163
164 void reset() { values_ = ValueType {}; }
165
166
171 [[nodiscard]] Vector<ValueType>& values() { return values_; }
172
178 {
179 return sparsityPattern_->colIdxs();
180 }
181
187 {
188 return sparsityPattern_->rowOffs();
189 }
190
195 [[nodiscard]] const Vector<ValueType>& values() const { return values_; }
196
202 [[nodiscard]] Matrix<ValueType, SparsityType> copyToExecutor(Executor dstExec) const override;
203
208 [[nodiscard]] std::shared_ptr<const SparsityType> sparsity() const { return sparsityPattern_; }
209
213 [[nodiscard]] std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress() const
214 {
215 return faceToMatrixAddress_;
216 }
217
224 {
226 values_.view(), sparsityPattern_->view()
227 );
228 }
229
234 [[nodiscard]] MatrixView<
235 const ValueType,
237 view() const
238 {
240 View<const ValueType>(values_.view()), sparsityPattern_->view()
241 );
242 }
243
247 [[nodiscard]] Vector<ValueType> diag() const;
248
249
250private:
251
252 Vector<ValueType> values_;
253
254 std::shared_ptr<const SparsityType> sparsityPattern_;
255
256 std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress_;
257};
258
259
260template<typename ValueType, typename IndexType>
262
263template<typename ValueType, typename IndexType>
265
269template<typename ValueType, typename IndexType>
271
276[[nodiscard]] Vector<scalar>
278
281);
282
287[[nodiscard]] Vector<scalar>
289
291 const CSRMatrix<Vec3, localIdx>& mtx,
292 const FaceToMatrixAddress& mi,
293 const Vector<scalar>& a,
294 Vector<scalar>& out
295);
296
301[[nodiscard]] Vector<scalar>
303
306 const FaceToMatrixAddress& mi,
307 const Vector<scalar>& a,
308 Vector<scalar>& out
309);
310
311/* @brief given Matrix<Vec3> this function returns a component Matrix<scalar>*/
312template<unsigned int I>
313[[nodiscard]] auto getComponent(const CSRMatrix<Vec3, localIdx>& in)
314{
315 auto sparsity = in.sparsity();
316 return CSRMatrix<scalar, localIdx>(getComponent<I>(in.values()), sparsity);
317}
318
324 const CSRMatrix<Vec3, localIdx>& mtx,
325 const Vector<Vec3>& a,
326 const Vector<Vec3>& b,
327 const Vector<scalar>& rAU,
328 const Vector<scalar>& vol,
329 Vector<Vec3>& out
330);
331
337 const CSRMatrix<Vec3, localIdx>& mtx,
338 const Vector<Vec3>& a,
339 const Vector<Vec3>& b,
340 const Vector<scalar>& vol,
341 Vector<scalar>& rAU,
342 Vector<Vec3>& out
343);
344
355 const Vector<Vec3>& a,
356 const Vector<Vec3>& b,
357 const Vector<scalar>& vol,
358 Vector<scalar>& rAU,
359 Vector<Vec3>& out
360);
361
362} // namespace NeoN
MixinClass signaling copyTo is supported.
Definition copyTo.hpp:20
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Definition vector.hpp:40
Sparse matrix class with compact storage by row (CSR) format.
Definition matrix.hpp:72
std::shared_ptr< const FaceToMatrixAddress > faceToMatrixAddress() const
Definition matrix.hpp:213
const Vector< typename la::CsrSparsityPattern< IndexType >::SparsityIndexType > & rowOffs() const
Definition matrix.hpp:186
const Vector< ValueType > & values() const
Get a const reference to values vector.
Definition matrix.hpp:195
localIdx nRows() const
Get the number of rows in the matrix.
Definition matrix.hpp:156
Matrix(const Vector< ValueType > &values, const Vector< typename SparsityType::SparsityIndexType > &colIdxs, const Vector< typename SparsityType::SparsityIndexType > &rowOffs, Dimensions dimensions)
Constructor for Matrix.
Definition matrix.hpp:106
MatrixView< const ValueType, SparsityView< typename SparsityType::SparsityIndexType > > view() const
Get a const view representation of the matrix's data.
Definition matrix.hpp:237
SparsityType MatrixSparsityType
Definition matrix.hpp:87
const Executor & exec() const
Get the executor associated with this matrix.
Definition matrix.hpp:150
Matrix(const Vector< ValueType > &values, std::shared_ptr< const SparsityType > sparsity, std::shared_ptr< const FaceToMatrixAddress > faceToMatrixAddress)
Constructor for Matrix with a FaceToMatrixAddress.
Definition matrix.hpp:130
ValueType MatrixValueType
Definition matrix.hpp:86
MatrixView< ValueType, SparsityView< typename SparsityType::SparsityIndexType > > view()
Get a view representation of the matrix's data.
Definition matrix.hpp:223
Vector< ValueType > diag() const
extract the diagonal of the matrix
const Vector< typename la::CsrSparsityPattern< IndexType >::SparsityIndexType > & colIdxs() const
Definition matrix.hpp:177
std::shared_ptr< const la::CsrSparsityPattern< IndexType > > sparsity() const
Definition matrix.hpp:208
Matrix< ValueType, SparsityType > copyToExecutor(Executor dstExec) const override
Copy the matrix to another executor.
localIdx nNonZeros() const
Get the number of non-zero values in the matrix.
Definition matrix.hpp:162
~Matrix()=default
Default destructor.
Matrix(const Vector< ValueType > &values, std::shared_ptr< const SparsityType > sp)
Constructor for Matrix.
Definition matrix.hpp:94
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:118
Matrix< ValueType, la::CooSparsityPattern< IndexType > > COOMatrix
Definition matrix.hpp:264
auto getComponent(const CSRMatrix< Vec3, localIdx > &in)
Definition matrix.hpp:313
Vector< scalar > scaledInverseDiag(const CSRMatrix< Vec3, localIdx > &, const Vector< scalar > &)
computes the inverted diagonal of a matrix and scales it by a, ie. a*D^-1
void scaledInvDiagNegLUx(const CSRMatrix< Vec3, localIdx > &mtx, const Vector< Vec3 > &a, const Vector< Vec3 > &b, const Vector< scalar > &vol, Vector< scalar > &rAU, Vector< Vec3 > &out)
computes out = -(L+U) x
Matrix< ValueType, la::CsrSparsityPattern< IndexType > > CSRMatrix
Definition matrix.hpp:261
void negLUx(const CSRMatrix< Vec3, localIdx > &mtx, const Vector< Vec3 > &a, const Vector< Vec3 > &b, const Vector< scalar > &rAU, const Vector< scalar > &vol, Vector< Vec3 > &out)
computes out = -(L+U) x
Vector< ValueType > upper(const CSRMatrix< ValueType, IndexType > &)
extract the upper triangular of the matrix
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
localIdx toLocalIdx(sizeType value)
int32_t localIdx
Definition label.hpp:50
hold the number of rows and columns of a matrix
A view struct to allow easy read/write on all executors.
Definition matrix.hpp:25
View< ValueType > values
View to the values of the CSR matrix.
Definition matrix.hpp:60
KOKKOS_INLINE_FUNCTION ValueType & entry(const localIdx i, const localIdx j) const
Retrieve a reference to the matrix element at position (i,j).
Definition matrix.hpp:47
KOKKOS_INLINE_FUNCTION ValueType & entry(const localIdx offset) const
Direct access to a value given the offset.
Definition matrix.hpp:58
MatrixView(const View< ValueType > valueView, SparsityViewType sparsityView)
Constructor for MatrixView.
Definition matrix.hpp:32
~MatrixView()=default
Default destructor.
SparsityViewType sparsity
Definition matrix.hpp:61
A view struct to allow easy read/write on all executors.