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
11
12namespace NeoN::la
13{
14
22template<typename ValueType, typename SparsityViewType>
24{
31 MatrixView(const View<ValueType> valueView, SparsityViewType sparsityView)
32 : values(valueView), sparsity(sparsityView) {};
33
37 ~MatrixView() = default;
38
45 KOKKOS_INLINE_FUNCTION
46 ValueType& entry(const localIdx i, const localIdx j) const
47 {
48 return values[sparsity.entry(i, j)];
49 }
50
56 KOKKOS_INLINE_FUNCTION
57 ValueType& entry(const localIdx offset) const { return values[offset]; }
58
60 SparsityViewType sparsity;
61};
62
69template<typename ValueType, typename SparsityType>
70class Matrix : public NeoN::SupportsCopyTo<Matrix<ValueType, SparsityType>>
71{
72
73 void validate()
74 {
75 NF_ASSERT(values_.exec() == sparsityPattern_->exec(), "Executors are not the same");
76 // TODO this is not necessarily true for matrix types with padding like ELL
77 NF_ASSERT(values_.size() == sparsityPattern_->nnz(), "Matrix values and columns mismatch");
78 }
79
80public:
81
82 using MatrixValueType = ValueType;
83 using MatrixSparsityType = SparsityType;
84
90 Matrix(const Vector<ValueType>& values, std::shared_ptr<const SparsityType> sp)
91 : values_(values), sparsityPattern_(sp)
92 {
93 validate();
94 }
95
106 Dimensions dimensions
107 )
108 : values_(values),
109 sparsityPattern_(
110 std::make_shared<const SparsityType>(Vector(colIdxs), Vector(rowOffs), dimensions)
111 )
112 {
113 validate();
114 }
115
128 std::shared_ptr<const SparsityType> sparsity,
129 std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress
130 )
131 requires std::is_same_v<typename SparsityType::SparsityIndexType, localIdx>
132 : values_(values), sparsityPattern_(sparsity), faceToMatrixAddress_(faceToMatrixAddress)
133 {
134 validate();
135 }
136
140 ~Matrix() = default;
141
146 [[nodiscard]] const Executor& exec() const { return values_.exec(); }
147
152 [[nodiscard]] localIdx nRows() const { return sparsityPattern_->rows(); }
153
158 [[nodiscard]] localIdx nNonZeros() const { return sparsityPattern_->nnz(); }
159
160 void reset() { values_ = ValueType {}; }
161
162
167 [[nodiscard]] Vector<ValueType>& values() { return values_; }
168
174 {
175 return sparsityPattern_->colIdxs();
176 }
177
183 {
184 return sparsityPattern_->rowOffs();
185 }
186
191 [[nodiscard]] const Vector<ValueType>& values() const { return values_; }
192
198 [[nodiscard]] Matrix<ValueType, SparsityType> copyToExecutor(Executor dstExec) const override;
199
204 [[nodiscard]] std::shared_ptr<const SparsityType> sparsity() const { return sparsityPattern_; }
205
209 [[nodiscard]] std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress() const
210 {
211 return faceToMatrixAddress_;
212 }
213
220 {
222 values_.view(), sparsityPattern_->view()
223 );
224 }
225
230 [[nodiscard]] MatrixView<
231 const ValueType,
233 view() const
234 {
236 View<const ValueType>(values_.view()), sparsityPattern_->view()
237 );
238 }
239
243 [[nodiscard]] Vector<ValueType> diag() const;
244
245
246private:
247
248 Vector<ValueType> values_;
249
250 std::shared_ptr<const SparsityType> sparsityPattern_;
251
252 std::shared_ptr<const FaceToMatrixAddress> faceToMatrixAddress_;
253};
254
255
256template<typename ValueType, typename IndexType>
258
259template<typename ValueType, typename IndexType>
261
265template<typename ValueType, typename IndexType>
267
272[[nodiscard]] Vector<scalar>
274
277);
278
283[[nodiscard]] Vector<scalar>
285
287 const CSRMatrix<Vec3, localIdx>& mtx,
288 const FaceToMatrixAddress& mi,
289 const Vector<scalar>& a,
290 Vector<scalar>& out
291);
292
297[[nodiscard]] Vector<scalar>
299
302 const FaceToMatrixAddress& mi,
303 const Vector<scalar>& a,
304 Vector<scalar>& out
305);
306
307/* @brief given Matrix<Vec3> this function returns a component Matrix<scalar>*/
308template<unsigned int I>
309[[nodiscard]] auto getComponent(const CSRMatrix<Vec3, localIdx>& in)
310{
311 auto sparsity = in.sparsity();
312 return CSRMatrix<scalar, localIdx>(getComponent<I>(in.values()), sparsity);
313}
314
320 const CSRMatrix<Vec3, localIdx>& mtx,
321 const Vector<Vec3>& a,
322 const Vector<Vec3>& b,
323 const Vector<scalar>& rAU,
324 const Vector<scalar>& vol,
325 Vector<Vec3>& out
326);
327
333 const CSRMatrix<Vec3, localIdx>& mtx,
334 const Vector<Vec3>& a,
335 const Vector<Vec3>& b,
336 const Vector<scalar>& vol,
337 Vector<scalar>& rAU,
338 Vector<Vec3>& out
339);
340
351 const Vector<Vec3>& a,
352 const Vector<Vec3>& b,
353 const Vector<scalar>& vol,
354 Vector<scalar>& rAU,
355 Vector<Vec3>& out
356);
357
358} // namespace NeoN
MixinClass signaling copyTo is supported.
Definition copyTo.hpp:20
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:27
Sparse matrix class with compact storage by row (CSR) format.
Definition matrix.hpp:71
std::shared_ptr< const FaceToMatrixAddress > faceToMatrixAddress() const
Get the FaceToMatrixAddress associated with this matrix (may be null).
Definition matrix.hpp:209
const Vector< typename SparsityType::SparsityIndexType > & rowOffs() const
Get a reference to row offset vector.
Definition matrix.hpp:182
const Vector< ValueType > & values() const
Get a const reference to values vector.
Definition matrix.hpp:191
localIdx nRows() const
Get the number of rows in the matrix.
Definition matrix.hpp:152
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:102
MatrixView< const ValueType, SparsityView< typename SparsityType::SparsityIndexType > > view() const
Get a const view representation of the matrix's data.
Definition matrix.hpp:233
SparsityType MatrixSparsityType
Definition matrix.hpp:83
const Executor & exec() const
Get the executor associated with this matrix.
Definition matrix.hpp:146
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:126
ValueType MatrixValueType
Definition matrix.hpp:82
MatrixView< ValueType, SparsityView< typename SparsityType::SparsityIndexType > > view()
Get a view representation of the matrix's data.
Definition matrix.hpp:219
Vector< ValueType > diag() const
extract the diagonal of the matrix
const Vector< typename SparsityType::SparsityIndexType > & colIdxs() const
Get a reference to column indices vector.
Definition matrix.hpp:173
Vector< ValueType > & values()
Get a reference to values vector.
Definition matrix.hpp:167
std::shared_ptr< const SparsityType > sparsity() const
Get a reference to column indices vector.
Definition matrix.hpp:204
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:158
~Matrix()=default
Default destructor.
Matrix(const Vector< ValueType > &values, std::shared_ptr< const SparsityType > sp)
Constructor for Matrix.
Definition matrix.hpp:90
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:118
auto getComponent(const CSRMatrix< Vec3, localIdx > &in)
Definition matrix.hpp:309
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
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
int32_t localIdx
Definition label.hpp:50
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:20
hold the number of rows and columns of a matrix
A view struct to allow easy read/write on all executors.
Definition matrix.hpp:24
View< ValueType > values
View to the values of the CSR matrix.
Definition matrix.hpp:59
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:46
KOKKOS_INLINE_FUNCTION ValueType & entry(const localIdx offset) const
Direct access to a value given the offset.
Definition matrix.hpp:57
MatrixView(const View< ValueType > valueView, SparsityViewType sparsityView)
Constructor for MatrixView.
Definition matrix.hpp:31
~MatrixView()=default
Default destructor.
SparsityViewType sparsity
Definition matrix.hpp:60
A view struct to allow easy read/write on all executors.