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> 35 #include <type_traits> 60 template <
typename T,
bool ForceLegacy = false>
61 class GpuSparseMatrixWrapper
73 #if USE_HIP || (!USE_HIP && CUDA_VERSION < 13000) 74 using matrix_type = GpuSparseMatrix<T>;
76 using matrix_type = std::conditional_t<ForceLegacy,
78 GpuSparseMatrixGeneric<T>>;
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);
160 GpuSparseMatrixWrapper& operator=(
const GpuSparseMatrixWrapper&) =
delete;
162 virtual ~GpuSparseMatrixWrapper() =
default;
164 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{};
virtual void mv(const GpuVector< T > &x, GpuVector< T > &y) const
mv performs matrix vector multiply y = Ax
Definition: GpuSparseMatrixWrapper.hpp:340
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
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition: gpu_type_detection.hpp:32
const GpuVector< int > & getColumnIndices() const
getColumnIndices returns the column indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:301
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
std::size_t N() const
N returns the number of rows (which is equal to the number of columns)
Definition: GpuSparseMatrixWrapper.hpp:232
const GpuVector< T > & getNonZeroValues() const
getNonZeroValues returns the GPU vector containing the non-zero values (ordered by block) ...
Definition: GpuSparseMatrixWrapper.hpp:261
GpuVector< int > & getRowIndices()
getRowIndices returns the row indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:271
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
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
auto dispatchOnBlocksize(FunctionType function) const
Dispatches a function based on the block size of the matrix.
Definition: GpuSparseMatrixWrapper.hpp:423
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
static constexpr int max_block_size
Maximum block size supported by this implementation.
Definition: GpuSparseMatrixWrapper.hpp:103
void setLowerTriangular()
setLowerTriangular sets the CuSparse flag that this is an lower diagonal (with non-unit diagonal) mat...
Definition: GpuSparseMatrixWrapper.hpp:204
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
const GpuVector< int > & getRowIndices() const
getRowIndices returns the row indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:281
virtual void umv(const GpuVector< T > &x, GpuVector< T > &y) const
umv computes y=Ax+y
Definition: GpuSparseMatrixWrapper.hpp:350
GpuVector< T > & getNonZeroValues()
getNonZeroValues returns the GPU vector containing the non-zero values (ordered by block) ...
Definition: GpuSparseMatrixWrapper.hpp:251
std::size_t dim() const
dim returns the dimension of the vector space on which this matrix acts
Definition: GpuSparseMatrixWrapper.hpp:312
virtual void usmv(T alpha, const GpuVector< T > &x, GpuVector< T > &y) const
umv computes y=alpha * Ax + y
Definition: GpuSparseMatrixWrapper.hpp:362
void setNonUnitDiagonal()
setNonUnitDiagonal sets the CuSparse flag that this has non-unit diagional.
Definition: GpuSparseMatrixWrapper.hpp:224
std::size_t blockSize() const
blockSize size of the blocks
Definition: GpuSparseMatrixWrapper.hpp:320
void setUnitDiagonal()
setUnitDiagonal sets the CuSparse flag that this has unit diagional.
Definition: GpuSparseMatrixWrapper.hpp:214
detail::GpuSparseMatrixDescription & getDescription()
getDescription the cusparse matrix description.
Definition: GpuSparseMatrixWrapper.hpp:330
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
GpuVector< int > & getColumnIndices()
getColumnIndices returns the column indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:291
The CuSparseResource class wraps a CuSparse resource in a proper RAII pattern.
Definition: CuSparseResource.hpp:54
void setToZero()
setToZero resets the matrix to zero values.
Definition: GpuSparseMatrixWrapper.hpp:397
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