19#ifndef OPM_GPUSPARSEMATRIXWRAPPER_HPP
20#define OPM_GPUSPARSEMATRIXWRAPPER_HPP
22#include <opm/common/ErrorMacros.hpp>
23#include <opm/simulators/linalg/gpuistl/detail/CuMatrixDescription.hpp>
24#include <opm/simulators/linalg/gpuistl/detail/CuSparseHandle.hpp>
25#include <opm/simulators/linalg/gpuistl/detail/safe_conversion.hpp>
26#include <opm/simulators/linalg/gpuistl/GpuVector.hpp>
27#include <opm/simulators/linalg/gpuistl/GpuSparseMatrix.hpp>
28#include <opm/simulators/linalg/gpuistl/GpuSparseMatrixGeneric.hpp>
60template <
typename T,
bool ForceLegacy = false>
73#if USE_HIP || (!USE_HIP && CUDA_VERSION < 13000)
76 using matrix_type = std::conditional_t<ForceLegacy,
82 matrix_type* operator->() {
84 throw std::runtime_error(
"GpuSparseMatrixWrapper: underlying matrix is nullptr.");
86 return m_matrix.get();
88 const matrix_type* operator->()
const {
90 throw std::runtime_error(
"GpuSparseMatrixWrapper: underlying matrix is nullptr.");
92 return m_matrix.get();
119 const int* rowIndices,
120 const int* columnIndices,
121 std::size_t numberOfNonzeroBlocks,
123 std::size_t numberOfRows)
125 m_matrix = std::make_unique<matrix_type>(nonZeroElements,
128 numberOfNonzeroBlocks,
147 m_matrix = std::make_unique<matrix_type>(rowIndices, columnIndices,
blockSize);
152 if (!other.m_matrix) {
153 throw std::runtime_error(
"Internal error, other.m_matrix is a nullptr.");
155 m_matrix = std::make_unique<matrix_type>(*other.m_matrix);
162 virtual ~GpuSparseMatrixWrapper() =
default;
166 const matrix_type& get()
const {
168 throw std::runtime_error(
"GpuSparseMatrixWrapper: underlying matrix is nullptr.");
182 template <
class MatrixType>
186 gpuSparseMatrixWrapper.m_matrix = std::make_unique<matrix_type>(
188 return gpuSparseMatrixWrapper;
192 template <
class M = matrix_type,
193 typename = std::enable_if_t<std::is_same_v<M, GpuSparseMatrix<T>>>>
194 void setUpperTriangular()
196 m_matrix->setUpperTriangular();
202 template <
class M = matrix_type,
203 typename = std::enable_if_t<std::is_same_v<M, GpuSparseMatrix<T>>>>
206 m_matrix->setLowerTriangular();
212 template <
class M = matrix_type,
213 typename = std::enable_if_t<std::is_same_v<M, GpuSparseMatrix<T>>>>
216 m_matrix->setUnitDiagonal();
222 template <
class M = matrix_type,
223 typename = std::enable_if_t<std::is_same_v<M, GpuSparseMatrix<T>>>>
226 m_matrix->setNonUnitDiagonal();
232 std::size_t
N()
const
234 return m_matrix->N();
243 return m_matrix->nonzeroes();
253 return m_matrix->getNonZeroValues();
263 return m_matrix->getNonZeroValues();
273 return m_matrix->getRowIndices();
283 return m_matrix->getRowIndices();
293 return m_matrix->getColumnIndices();
303 return m_matrix->getColumnIndices();
314 return m_matrix->dim();
322 return m_matrix->blockSize();
332 return m_matrix->getDescription();
364 m_matrix->usmv(alpha, x, y);
377 template <
class MatrixType>
380 m_matrix->updateNonzeroValues(matrix, copyNonZeroElementsDirectly);
388 template <
bool OtherForceLegacy>
391 m_matrix->updateNonzeroValues(matrix.get());
399 m_matrix->setToZero();
422 template<
class FunctionType>
425 return m_matrix->dispatchOnBlocksize(function);
429 std::unique_ptr<matrix_type> m_matrix{};
The GpuSparseMatrixGeneric class uses cuSPARSE Generic API for sparse matrix operations.
Definition GpuSparseMatrixGeneric.hpp:48
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition GpuSparseMatrixWrapper.hpp:62
GpuVector< int > & getRowIndices()
getRowIndices returns the row indices used to represent the BSR structure.
Definition GpuSparseMatrixWrapper.hpp:271
void updateNonzeroValues(const GpuSparseMatrixWrapper< T, OtherForceLegacy > &matrix)
updateNonzeroValues updates the non-zero values by using the non-zero values of the supplied matrix
Definition GpuSparseMatrixWrapper.hpp:389
virtual void mv(const GpuVector< T > &x, GpuVector< T > &y) const
mv performs matrix vector multiply y = Ax
Definition GpuSparseMatrixWrapper.hpp:340
static constexpr int max_block_size
Definition GpuSparseMatrixWrapper.hpp:103
GpuVector< T > & getNonZeroValues()
getNonZeroValues returns the GPU vector containing the non-zero values (ordered by block)
Definition GpuSparseMatrixWrapper.hpp:251
const GpuVector< T > & getNonZeroValues() const
getNonZeroValues returns the GPU vector containing the non-zero values (ordered by block)
Definition GpuSparseMatrixWrapper.hpp:261
std::size_t N() const
N returns the number of rows (which is equal to the number of columns).
Definition GpuSparseMatrixWrapper.hpp:232
static GpuSparseMatrixWrapper< T, ForceLegacy > fromMatrix(const MatrixType &matrix, bool copyNonZeroElementsDirectly=false)
fromMatrix creates a new matrix with the same block size and values as the given matrix
Definition GpuSparseMatrixWrapper.hpp:183
void setNonUnitDiagonal()
setNonUnitDiagonal sets the CuSparse flag that this has non-unit diagional.
Definition GpuSparseMatrixWrapper.hpp:224
GpuSparseMatrixWrapper(const GpuVector< int > &rowIndices, const GpuVector< int > &columnIndices, std::size_t blockSize)
Create a sparse matrix by copying the sparsity structure of another matrix, not filling in the values...
Definition GpuSparseMatrixWrapper.hpp:143
void updateNonzeroValues(const MatrixType &matrix, bool copyNonZeroElementsDirectly=false)
updateNonzeroValues updates the non-zero values by using the non-zero values of the supplied matrix
Definition GpuSparseMatrixWrapper.hpp:378
detail::GpuSparseMatrixDescription & getDescription()
getDescription the cusparse matrix description.
Definition GpuSparseMatrixWrapper.hpp:330
std::size_t dim() const
dim returns the dimension of the vector space on which this matrix acts
Definition GpuSparseMatrixWrapper.hpp:312
std::size_t nonzeroes() const
nonzeroes behaves as the Dune::BCRSMatrix::nonzeros() function and returns the number of non zero blo...
Definition GpuSparseMatrixWrapper.hpp:241
GpuVector< int > & getColumnIndices()
getColumnIndices returns the column indices used to represent the BSR structure.
Definition GpuSparseMatrixWrapper.hpp:291
void setUnitDiagonal()
setUnitDiagonal sets the CuSparse flag that this has unit diagional.
Definition GpuSparseMatrixWrapper.hpp:214
GpuSparseMatrixWrapper(const T *nonZeroElements, const int *rowIndices, const int *columnIndices, std::size_t numberOfNonzeroBlocks, std::size_t blockSize, std::size_t numberOfRows)
Create the sparse matrix specified by the raw data.
Definition GpuSparseMatrixWrapper.hpp:118
void setLowerTriangular()
setLowerTriangular sets the CuSparse flag that this is an lower diagonal (with non-unit diagonal) mat...
Definition GpuSparseMatrixWrapper.hpp:204
virtual void usmv(T alpha, const GpuVector< T > &x, GpuVector< T > &y) const
umv computes y=alpha * Ax + y
Definition GpuSparseMatrixWrapper.hpp:362
void setToZero()
setToZero resets the matrix to zero values.
Definition GpuSparseMatrixWrapper.hpp:397
const GpuVector< int > & getColumnIndices() const
getColumnIndices returns the column indices used to represent the BSR structure.
Definition GpuSparseMatrixWrapper.hpp:301
std::size_t blockSize() const
Definition GpuSparseMatrixWrapper.hpp:320
const GpuVector< int > & getRowIndices() const
getRowIndices returns the row indices used to represent the BSR structure.
Definition GpuSparseMatrixWrapper.hpp:281
auto dispatchOnBlocksize(FunctionType function) const
Dispatches a function based on the block size of the matrix.
Definition GpuSparseMatrixWrapper.hpp:423
virtual void umv(const GpuVector< T > &x, GpuVector< T > &y) const
umv computes y=Ax+y
Definition GpuSparseMatrixWrapper.hpp:350
The GpuSparseMatrix class simple wrapper class for a CuSparse matrix.
Definition GpuSparseMatrix.hpp:61
static GpuSparseMatrix< T > fromMatrix(const MatrixType &matrix, bool copyNonZeroElementsDirectly=false)
fromMatrix creates a new matrix with the same block size and values as the given matrix
Definition GpuSparseMatrix.cpp:116
Definition gpu_type_detection.hpp:30
CuSparseResource< cusparseMatDescr_t > GpuSparseMatrixDescription
GpuSparseMatrixDescription holder.
Definition CuMatrixDescription.hpp:30
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition AmgxInterface.hpp:38