NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
CSRMatrix.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3#pragma once
4
6
7#include <type_traits>
8
9namespace NeoN::la
10{
11
19template<typename ValueType, typename IndexType>
21{
29 const View<ValueType>& valueView,
30 const View<IndexType>& colIdxsView,
31 const View<IndexType>& rowOffsView
32 )
33 : values(valueView), colIdxs(colIdxsView), rowOffs(rowOffsView) {};
34
38 ~CSRMatrixView() = default;
39
46 KOKKOS_INLINE_FUNCTION
47 ValueType& entry(const IndexType i, const IndexType j) const
48 {
49 const IndexType rowSize = rowOffs[i + 1] - rowOffs[i];
50 for (std::remove_const_t<IndexType> ic = 0; ic < rowSize; ++ic)
51 {
52 const IndexType localCol = rowOffs[i] + ic;
53 if (colIdxs[localCol] == j)
54 {
55 return values[localCol];
56 }
57 if (colIdxs[localCol] > j) break;
58 }
59 Kokkos::abort("Memory not allocated for CSR matrix component.");
60 return values[values.size()]; // compiler warning suppression.
61 }
62
68 KOKKOS_INLINE_FUNCTION
69 ValueType& entry(const IndexType offset) const { return values[offset]; }
70
74};
75
82template<typename ValueType, typename IndexType>
84{
85
86public:
87
97 const Vector<IndexType>& rowOffs
98 )
99 : values_(values), colIdxs_(colIdxs), rowOffs_(rowOffs)
100 {
101 NF_ASSERT(values.exec() == colIdxs_.exec(), "Executors are not the same");
102 NF_ASSERT(values.exec() == rowOffs_.exec(), "Executors are not the same");
103 }
104
105 CSRMatrix(const Executor exec) : values_(exec, 0), colIdxs_(exec, 0), rowOffs_(exec, 0) {}
106
110 ~CSRMatrix() = default;
111
116 [[nodiscard]] const Executor& exec() const { return values_.exec(); }
117
122 [[nodiscard]] IndexType nRows() const
123 {
124 return static_cast<IndexType>(rowOffs_.size())
125 - static_cast<IndexType>(static_cast<bool>(rowOffs_.size()));
126 }
127
132 [[nodiscard]] IndexType nNonZeros() const { return static_cast<IndexType>(values_.size()); }
133
138 [[nodiscard]] Vector<ValueType>& values() { return values_; }
139
144 [[nodiscard]] Vector<IndexType>& colIdxs() { return colIdxs_; }
145
150 [[nodiscard]] Vector<IndexType>& rowPtrs() { return rowOffs_; }
151
156 [[nodiscard]] const Vector<ValueType>& values() const { return values_; }
157
162 [[nodiscard]] const Vector<IndexType>& colIdxs() const { return colIdxs_; }
163
168 [[nodiscard]] const Vector<IndexType>& rowPtrs() const { return rowOffs_; }
169
176 {
177 if (dstExec == values_.exec())
178 {
179 return *this;
180 }
182 values_.copyToHost(), colIdxs_.copyToHost(), rowOffs_.copyToHost()
183 );
184 return other;
185 }
186
192 {
194 }
195
201 {
202 return CSRMatrixView(values_.view(), colIdxs_.view(), rowOffs_.view());
203 }
204
210 {
211 return CSRMatrixView(values_.view(), colIdxs_.view(), rowOffs_.view());
212 }
213
214private:
215
216 Vector<ValueType> values_;
217 Vector<IndexType> colIdxs_;
218 Vector<IndexType> rowOffs_;
219};
220
221// /* @brief given a csr matrix this function copies the matrix and converts to requested target
222// types
223// *
224// */
225// template<typename ValueTypeIn, typename IndexTypeIn, typename ValueTypeOut, typename
226// IndexTypeOut> la::CSRMatrix<ValueTypeOut, IndexTypeOut> convert(const Executor exec, const
227// la::CSRMatrixView<const ValueTypeIn, const IndexTypeIn> in)
228// {
229// Vector<IndexTypeOut> colIdxsTmp(exec, in.colIdxs.size());
230// Vector<IndexTypeOut> rowPtrsTmp(exec, in.rowOffs.size());
231// Vector<ValueTypeOut> valuesTmp(exec, in.values.data(), in.values.size());
232
233// parallelFor(
234// colIdxsTmp, KOKKOS_LAMBDA(const localIdx i) { return IndexTypeOut {in.colIdxs[i]}; }
235// );
236// parallelFor(
237// rowPtrsTmp, KOKKOS_LAMBDA(const localIdx i) { return IndexTypeOut {in.rowOffs[i]}; }
238// );
239// parallelFor(
240// valuesTmp, KOKKOS_LAMBDA(const localIdx i) { return ValueTypeOut {in.values[i]}; }
241// );
242
243// return la::CSRMatrix<ValueTypeOut, IndexTypeOut> {valuesTmp, colIdxsTmp, rowPtrsTmp};
244// }
245
246
247} // namespace NeoN
Reference executor for serial CPU execution.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
localIdx size() const
Gets the size of the field.
Definition vector.hpp:364
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:358
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Definition vector.hpp:192
Sparse matrix class with compact storage by row (CSR) format.
Definition CSRMatrix.hpp:84
IndexType nNonZeros() const
Get the number of non-zero values in the matrix.
const Vector< IndexType > & rowPtrs() const
Get a const span to the row pointers array.
const Vector< ValueType > & values() const
Get a const span to the values array.
Vector< IndexType > & colIdxs()
Get a span to the column indices array.
const Executor & exec() const
Get the executor associated with this matrix.
const CSRMatrixView< const ValueType, const IndexType > view() const
Get a const view representation of the matrix's data.
const Vector< IndexType > & colIdxs() const
Get a const span to the column indices array.
IndexType nRows() const
Get the number of rows in the matrix.
Vector< IndexType > & rowPtrs()
Get a span to the row pointers array.
Vector< ValueType > & values()
Get a span to the values array.
CSRMatrix< ValueType, IndexType > copyToHost() const
Copy the matrix to the host.
CSRMatrix(const Executor exec)
CSRMatrix(const Vector< ValueType > &values, const Vector< IndexType > &colIdxs, const Vector< IndexType > &rowOffs)
Constructor for CSRMatrix.
Definition CSRMatrix.hpp:94
CSRMatrix< ValueType, IndexType > copyToExecutor(Executor dstExec) const
Copy the matrix to another executor.
CSRMatrixView< ValueType, IndexType > view()
Get a view representation of the matrix's data.
~CSRMatrix()=default
Default destructor.
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
A view struct to allow easy read/write on all executors.
Definition CSRMatrix.hpp:21
View< ValueType > values
View to the values of the CSR matrix.
Definition CSRMatrix.hpp:71
View< IndexType > colIdxs
View to the column indices of the CSR matrix.
Definition CSRMatrix.hpp:72
CSRMatrixView(const View< ValueType > &valueView, const View< IndexType > &colIdxsView, const View< IndexType > &rowOffsView)
Constructor for CSRMatrixView.
Definition CSRMatrix.hpp:28
KOKKOS_INLINE_FUNCTION ValueType & entry(const IndexType i, const IndexType j) const
Retrieve a reference to the matrix element at position (i,j).
Definition CSRMatrix.hpp:47
~CSRMatrixView()=default
Default destructor.
View< IndexType > rowOffs
View to the row offsets for the CSR matrix.
Definition CSRMatrix.hpp:73
KOKKOS_INLINE_FUNCTION ValueType & entry(const IndexType offset) const
Direct access to a value given the offset.
Definition CSRMatrix.hpp:69