NeoFOAM
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 NeoFOAM authors
3#pragma once
4
5#include <tuple>
6
8
9
10namespace NeoFOAM::la
11{
12
19template<typename ValueType, typename IndexType>
21{
22public:
23
31 const std::span<ValueType>& values,
32 const std::span<IndexType>& colIdxs,
33 const std::span<IndexType>& rowPtrs
34 )
35 : values_(values), colIdxs_(colIdxs), rowPtrs_(rowPtrs) {};
36
40 ~CSRMatrixSpan() = default;
41
48 KOKKOS_INLINE_FUNCTION
49 ValueType& entry(const IndexType i, const IndexType j) const
50 {
51 const IndexType rowSize = rowPtrs_[i + 1] - rowPtrs_[i];
52 for (std::remove_const_t<IndexType> ic = 0; ic < rowSize; ++ic)
53 {
54 const IndexType localCol = rowPtrs_[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_[values_.size()]; // compiler warning suppression.
63 }
64
70 KOKKOS_INLINE_FUNCTION
71 ValueType& entry(const IndexType offset) const { return values_[offset]; }
72
77 std::tuple<std::span<ValueType>, std::span<IndexType>, std::span<IndexType>> span()
78 {
79 return {values_, colIdxs_, rowPtrs_};
80 }
81
82private:
83
84 std::span<ValueType> values_;
85 std::span<IndexType> colIdxs_;
86 std::span<IndexType> rowPtrs_;
87};
88
89template<typename ValueType, typename IndexType>
91{
92
93public:
94
105 )
106 : values_(values), colIdxs_(colIdxs), rowPtrs_(rowPtrs)
107 {
108 NF_ASSERT(values.exec() == colIdxs.exec(), "Executors are not the same");
109 NF_ASSERT(values.exec() == rowPtrs.exec(), "Executors are not the same");
110 };
111
115 ~CSRMatrix() = default;
116
121 [[nodiscard]] const Executor& exec() const { return values_.exec(); }
122
127 [[nodiscard]] IndexType nRows() const { return static_cast<IndexType>(rowPtrs_.size()) - 1; }
128
133 [[nodiscard]] IndexType nNonZeros() const { return static_cast<IndexType>(values_.size()); }
134
139 [[nodiscard]] std::span<ValueType> values() { return values_.span(); }
140
145 [[nodiscard]] std::span<IndexType> colIdxs() { return colIdxs_.span(); }
146
151 [[nodiscard]] std::span<IndexType> rowPtrs() { return rowPtrs_.span(); }
152
157 [[nodiscard]] const std::span<const ValueType> values() const { return values_.span(); }
158
163 [[nodiscard]] const std::span<const IndexType> colIdxs() const { return colIdxs_.span(); }
164
169 [[nodiscard]] const std::span<const IndexType> rowPtrs() const { return rowPtrs_.span(); }
170
177 {
178 if (dstExec == values_.exec())
179 {
180 return *this;
181 }
183 values_.copyToHost(), colIdxs_.copyToHost(), rowPtrs_.copyToHost()
184 );
185 return other;
186 }
187
193 {
195 }
196
202 {
203 return CSRMatrixSpan(values_.span(), colIdxs_.span(), rowPtrs_.span());
204 }
205
211 {
212 return CSRMatrixSpan(values_.span(), colIdxs_.span(), rowPtrs_.span());
213 }
214
215private:
216
217 Field<ValueType> values_;
218 Field<IndexType> colIdxs_;
219 Field<IndexType> rowPtrs_;
220};
221
222} // namespace NeoFOAM
A class to contain the data and executors for a field and define some basic operations.
Definition field.hpp:49
size_t size() const
Gets the size of the field.
Definition field.hpp:355
Field< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Definition field.hpp:183
std::span< ValueType > span() &&=delete
Reference executor for serial CPU execution.
A helper class to allow easy read/write on all executors.
Definition CSRMatrix.hpp:21
CSRMatrixSpan(const std::span< ValueType > &values, const std::span< IndexType > &colIdxs, const std::span< IndexType > &rowPtrs)
Constructor for CSRMatrixSpan.
Definition CSRMatrix.hpp:30
KOKKOS_INLINE_FUNCTION ValueType & entry(const IndexType offset) const
Direct access to a value given the offset.
Definition CSRMatrix.hpp:71
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
std::tuple< std::span< ValueType >, std::span< IndexType >, std::span< IndexType > > span()
Returns a structured binding of all containers.
Definition CSRMatrix.hpp:77
~CSRMatrixSpan()=default
Default destructor.
CSRMatrixSpan< ValueType, IndexType > span()
Get a span representation of the matrix.
const Executor & exec() const
Get the executor associated with this matrix.
std::span< IndexType > colIdxs()
Get a span to the column indices array.
const std::span< const IndexType > colIdxs() const
Get a const span to the column indices array.
const std::span< const ValueType > values() const
Get a const span to the values array.
const std::span< const IndexType > rowPtrs() const
Get a const span to the row pointers array.
const CSRMatrixSpan< const ValueType, const IndexType > span() const
Get a const span representation of the matrix.
CSRMatrix< ValueType, IndexType > copyToHost() const
Copy the matrix to the host.
CSRMatrix(const Field< ValueType > &values, const Field< IndexType > &colIdxs, const Field< IndexType > &rowPtrs)
Constructor for CSRMatrix.
std::span< IndexType > rowPtrs()
Get a span to the row pointers array.
~CSRMatrix()=default
Default destructor.
IndexType nNonZeros() const
Get the number of non-zero values in the matrix.
CSRMatrix< ValueType, IndexType > copyToExecutor(Executor dstExec) const
Copy the matrix to another executor.
IndexType nRows() const
Get the number of rows in the matrix.
std::span< ValueType > values()
Get a span to the values array.
#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