NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
linearSystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3#pragma once
4
7
8#include <string>
9
10namespace NeoN::la
11{
12
20template<typename ValueType, typename IndexType>
32
41template<typename ValueType, typename IndexType>
43{
44public:
45
47 : matrix_(matrix), rhs_(rhs)
48 {
49 NF_ASSERT(matrix.exec() == rhs.exec(), "Executors are not the same");
50 NF_ASSERT(matrix.nRows() == rhs.size(), "Matrix and RHS size mismatch");
51 };
52
53 LinearSystem(const LinearSystem& ls) : matrix_(ls.matrix_), rhs_(ls.rhs_) {};
54
55 LinearSystem(const Executor exec) : matrix_(exec), rhs_(exec, 0) {}
56
57 ~LinearSystem() = default;
58
59 [[nodiscard]] CSRMatrix<ValueType, IndexType>& matrix() { return matrix_; }
60
61 [[nodiscard]] Vector<ValueType>& rhs() { return rhs_; }
62
63 [[nodiscard]] const CSRMatrix<ValueType, IndexType>& matrix() const { return matrix_; }
64
65 [[nodiscard]] const Vector<ValueType>& rhs() const { return rhs_; }
66
67 [[nodiscard]] LinearSystem copyToHost() const
68 {
69 return LinearSystem(matrix_.copyToHost(), rhs_.copyToHost());
70 }
71
72 void reset()
73 {
74 fill(matrix_.values(), zero<ValueType>());
75 fill(rhs_, zero<ValueType>());
76 }
77
78 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() && = delete;
79
80 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() const&& = delete;
81
82 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() &
83 {
84 return LinearSystemView<ValueType, IndexType>(matrix_.view(), rhs_.view());
85 }
86
88 {
89 return LinearSystemView<const ValueType, const IndexType>(matrix_.view(), rhs_.view());
90 }
91
92 const Executor& exec() const { return matrix_.exec(); }
93
94private:
95
98};
99
100
101template<typename ValueType, typename IndexType>
103{
104 Vector<ValueType> resultVector(ls.exec(), ls.rhs().size(), 0.0);
105 auto [result, b, x] = spans(resultVector, ls.rhs(), xfield);
106
107 auto values = ls.matrix().values().view();
108 auto colIdxs = ls.matrix().colIdxs().view();
109 auto rowPtrs = ls.matrix().rowPtrs().view();
110
112 ls.exec(),
113 {0, ls.matrix().nRows()},
114 KOKKOS_LAMBDA(const localIdx rowi) {
115 IndexType rowStart = rowPtrs[rowi];
116 IndexType rowEnd = rowPtrs[rowi + 1];
117 ValueType sum = 0.0;
118 for (IndexType coli = rowStart; coli < rowEnd; coli++)
119 {
120 sum += values[coli] * x[colIdxs[coli]];
121 }
122 result[rowi] = sum - b[rowi];
123 }
124 );
125
126 return resultVector;
127};
128
129
130template<typename ValueTypeIn, typename IndexTypeIn, typename ValueTypeOut, typename IndexTypeOut>
131LinearSystem<ValueTypeOut, IndexTypeOut>
133{
134 auto exec = ls.exec();
135 Vector<ValueTypeOut> convertedRhs(exec, ls.rhs().data(), ls.rhs().size());
136 return {
137 convert<ValueTypeIn, IndexTypeIn, ValueTypeOut, IndexTypeOut>(exec, ls.view.matrix),
138 convertedRhs,
139 ls.sparsityPattern()
140 };
141}
142
143/*@brief helper function that creates a zero initialised linear system based on given sparsity
144 * pattern
145 */
146template<typename ValueType, typename IndexType, typename SparsityType>
148{
149 const auto& exec = sparsity.mesh().exec();
150
151 localIdx rows {sparsity.rows()};
152 localIdx nnzs {sparsity.nnz()};
153
154 return {
156 Vector<ValueType>(exec, nnzs, zero<ValueType>()), sparsity.colIdxs(), sparsity.rowPtrs()
157 },
158 Vector<ValueType> {exec, rows, zero<ValueType>()}
159 };
160}
161
162
163} // namespace NeoN::la
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
Sparse matrix class with compact storage by row (CSR) format.
Definition CSRMatrix.hpp:84
A class representing a linear system of equations.
const CSRMatrix< ValueType, IndexType > & matrix() const
LinearSystemView< ValueType, IndexType > view() const &&=delete
CSRMatrix< ValueType, IndexType > & matrix()
LinearSystem copyToHost() const
const Vector< ValueType > & rhs() const
LinearSystem(const LinearSystem &ls)
LinearSystem(const CSRMatrix< ValueType, IndexType > &matrix, const Vector< ValueType > &rhs)
LinearSystem(const Executor exec)
LinearSystemView< const ValueType, const IndexType > view() const &
const Executor & exec() const
Vector< ValueType > & rhs()
LinearSystemView< ValueType, IndexType > view() &&=delete
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142
LinearSystem< ValueTypeOut, IndexTypeOut > convertLinearSystem(const LinearSystem< ValueTypeIn, IndexTypeIn > &ls)
LinearSystem< ValueType, IndexType > createEmptyLinearSystem(const SparsityType &sparsity)
Vector< ValueType > spmv(LinearSystem< ValueType, IndexType > &ls, Vector< ValueType > &xfield)
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
int32_t localIdx
Definition label.hpp:30
void fill(Vector< ValueType > &a, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a scalar value using a specific executor.
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
auto spans(Args &... fields)
A view struct to allow easy read/write on all executors.
Definition CSRMatrix.hpp:21
A view linear into a linear system's data.
LinearSystemView(CSRMatrixView< ValueType, IndexType > matrixView, View< ValueType > rhsView)
CSRMatrixView< ValueType, IndexType > matrix