NeoN
A framework for CFD software
Loading...
Searching...
No Matches
CSRMatrix.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
9#include <type_traits>
10
11namespace NeoN::la
12{
13
21template<typename ValueType, typename IndexType>
23{
31 const View<ValueType>& valueView,
32 const View<IndexType>& colIdxsView,
33 const View<IndexType>& rowOffsView
34 )
35 : values(valueView), colIdxs(colIdxsView), rowOffs(rowOffsView) {};
36
40 ~CSRMatrixView() = default;
41
48 KOKKOS_INLINE_FUNCTION
49 ValueType& entry(const IndexType i, const IndexType j) const
50 {
51 const IndexType rowSize = rowOffs[i + 1] - rowOffs[i];
52 for (std::remove_const_t<IndexType> ic = 0; ic < rowSize; ++ic)
53 {
54 const IndexType localCol = rowOffs[i] + ic;
55 if (colIdxs[localCol] == j)
56 {
57 return values[localCol];
58 }
59 if (colIdxs[localCol] > j) break;
60 }
61 Kokkos::abort("Memory not allocated for CSR matrix component.");
62 return values[0]; // compiler warning suppression.
63 }
64
70 KOKKOS_INLINE_FUNCTION
71 ValueType& entry(const IndexType offset) const { return values[offset]; }
72
76};
77
84template<typename ValueType, typename IndexType>
86{
87
88public:
89
100 )
101 : values_(values), colIdxs_(colIdxs), rowOffs_(rowOffs)
102 {
103 NF_ASSERT(values.exec() == colIdxs_.exec(), "Executors are not the same");
104 NF_ASSERT(values.exec() == rowOffs_.exec(), "Executors are not the same");
105 }
106
107 CSRMatrix(const Executor exec) : values_(exec, 0), colIdxs_(exec, 0), rowOffs_(exec, 0) {}
108
112 ~CSRMatrix() = default;
113
118 [[nodiscard]] const Executor& exec() const { return values_.exec(); }
119
124 [[nodiscard]] IndexType nRows() const
125 {
126 return static_cast<IndexType>(rowOffs_.size())
127 - static_cast<IndexType>(static_cast<bool>(rowOffs_.size()));
128 }
129
134 [[nodiscard]] IndexType nNonZeros() const { return static_cast<IndexType>(values_.size()); }
135
140 [[nodiscard]] Vector<ValueType>& values() { return values_; }
141
146 [[nodiscard]] Vector<IndexType>& colIdxs() { return colIdxs_; }
147
152 [[nodiscard]] Vector<IndexType>& rowOffs() { return rowOffs_; }
153
158 [[nodiscard]] const Vector<ValueType>& values() const { return values_; }
159
164 [[nodiscard]] const Vector<IndexType>& colIdxs() const { return colIdxs_; }
165
170 [[nodiscard]] const Vector<IndexType>& rowOffs() const { return rowOffs_; }
171
178 {
179 if (dstExec == values_.exec())
180 {
181 return *this;
182 }
184 values_.copyToHost(), colIdxs_.copyToHost(), rowOffs_.copyToHost()
185 );
186 return other;
187 }
188
194 {
196 }
197
203 {
204 return CSRMatrixView(values_.view(), colIdxs_.view(), rowOffs_.view());
205 }
206
212 {
213 return CSRMatrixView(values_.view(), colIdxs_.view(), rowOffs_.view());
214 }
215
216private:
217
218 Vector<ValueType> values_;
219 Vector<IndexType> colIdxs_;
220 Vector<IndexType> rowOffs_;
221};
222
223// /* @brief given a csr matrix this function copies the matrix and converts to requested target
224// types
225// *
226// */
227// template<typename ValueTypeIn, typename IndexTypeIn, typename ValueTypeOut, typename
228// IndexTypeOut> la::CSRMatrix<ValueTypeOut, IndexTypeOut> convert(const Executor exec, const
229// la::CSRMatrixView<const ValueTypeIn, const IndexTypeIn> in)
230// {
231// Vector<IndexTypeOut> colIdxsTmp(exec, in.colIdxs.size());
232// Vector<IndexTypeOut> rowOffsTmp(exec, in.rowOffs.size());
233// Vector<ValueTypeOut> valuesTmp(exec, in.values.data(), in.values.size());
234
235// parallelFor(
236// colIdxsTmp, KOKKOS_LAMBDA(const localIdx i) { return IndexTypeOut {in.colIdxs[i]}; }
237// );
238// parallelFor(
239// rowOffsTmp, KOKKOS_LAMBDA(const localIdx i) { return IndexTypeOut {in.rowOffs[i]}; }
240// );
241// parallelFor(
242// valuesTmp, KOKKOS_LAMBDA(const localIdx i) { return ValueTypeOut {in.values[i]}; }
243// );
244
245// return la::CSRMatrix<ValueTypeOut, IndexTypeOut> {valuesTmp, colIdxsTmp, rowOffsTmp};
246// }
247
248
249} // 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:30
localIdx size() const
Gets the size of the field.
Definition vector.hpp:235
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:229
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Sparse matrix class with compact storage by row (CSR) format.
Definition CSRMatrix.hpp:86
IndexType nNonZeros() const
Get the number of non-zero values in the matrix.
const Vector< ValueType > & values() const
Get a const reference to values vector.
Vector< IndexType > & colIdxs()
Get a reference to column indices vector.
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 reference to column indices vector.
IndexType nRows() const
Get the number of rows in the matrix.
Vector< ValueType > & values()
Get a reference to values vector.
CSRMatrix< ValueType, IndexType > copyToHost() const
Copy the matrix to the host.
const Vector< IndexType > & rowOffs() const
Get a const reference to row offset vector.
CSRMatrix(const Executor exec)
CSRMatrix(const Vector< ValueType > &values, const Vector< IndexType > &colIdxs, const Vector< IndexType > &rowOffs)
Constructor for CSRMatrix.
Definition CSRMatrix.hpp:96
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.
Vector< IndexType > & rowOffs()
Get a reference to row offset vector.
~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:144
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:18
A view struct to allow easy read/write on all executors.
Definition CSRMatrix.hpp:23
View< ValueType > values
View to the values of the CSR matrix.
Definition CSRMatrix.hpp:73
View< IndexType > colIdxs
View to the column indices of the CSR matrix.
Definition CSRMatrix.hpp:74
CSRMatrixView(const View< ValueType > &valueView, const View< IndexType > &colIdxsView, const View< IndexType > &rowOffsView)
Constructor for CSRMatrixView.
Definition CSRMatrix.hpp:30
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:49
~CSRMatrixView()=default
Default destructor.
View< IndexType > rowOffs
View to the row offsets for the CSR matrix.
Definition CSRMatrix.hpp:75
KOKKOS_INLINE_FUNCTION ValueType & entry(const IndexType offset) const
Direct access to a value given the offset.
Definition CSRMatrix.hpp:71