20#ifndef OPM_AMGX_INTERFACE_HPP
21#define OPM_AMGX_INTERFACE_HPP
23#include <opm/common/ErrorMacros.hpp>
24#include <opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp>
25#include <opm/simulators/linalg/gpuistl/GpuVector.hpp>
26#include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>
27#include <opm/simulators/linalg/gpuistl/detail/gpu_type_detection.hpp>
30#include <cuda_runtime.h>
47 : std::runtime_error(
msg)
70 "AMGX error in expression: {}\nError code: {}\nError message: {}\nLocation: {}:{} in function {}",
72 static_cast<int>(err),
107#define OPM_AMGX_SAFE_CALL(expr) ::Opm::gpuistl::amgxSafeCall((expr), #expr, __FILE__, __func__, __LINE__)
271 template <
typename MatrixType>
276 using T =
typename MatrixType::field_type;
277 const T* values = &(matrix[0][0][0][0]);
309 template <
typename T>
317 throw AmgxError(fmt::format(
"Vector size mismatch in updateAmgxFromGpuVector: "
318 "AMGX vector size {} vs. GpuVector size {}",
339 template <
typename T>
347 throw AmgxError(fmt::format(
"Vector size mismatch in updateGpuVectorFromAmgx: "
348 "AMGX vector size {} vs. GpuVector size {}",
369 template <
typename VectorType>
378 const int N =
vec.size();
379 const int block_size = 1;
395 template <
typename VectorType>
416 template <
typename T>
445 template <
typename T>
473 template <
typename T>
514 template <
typename MatrixType>
524 const int block_size = 1;
532 for (
auto row = matrix.begin();
row != matrix.end(); ++
row) {
540 using T =
typename MatrixType::field_type;
541 const T* values = &(matrix[0][0][0][0]);
549 OPM_AMGX_SAFE_CALL(
AMGX_pin_memory(
const_cast<T*
>(values),
sizeof(T) * nnz * block_size * block_size));
559 const_cast<T*
>(values),
589 template <
typename MatrixType>
597 using T =
typename MatrixType::field_type;
598 const T* values = &(matrix[0][0][0][0]);
600 amgx_matrix, matrix.N(), matrix.nonzeroes(),
const_cast<T*
>(values),
nullptr));
612 template <
typename MatrixFieldType,
typename VectorFieldType>
615 if constexpr (std::is_same_v<MatrixFieldType, double> && std::is_same_v<VectorFieldType, double>) {
617 }
else if constexpr (std::is_same_v<MatrixFieldType, float> && std::is_same_v<VectorFieldType, double>) {
619 }
else if constexpr (std::is_same_v<MatrixFieldType, float> && std::is_same_v<VectorFieldType, float>) {
623 "Unsupported combination of matrix and vector types for AMGX: "
Exception class for AMGX errors.
Definition AmgxInterface.hpp:44
Unified interface for AMGX operations with both CPU and GPU data structures.
Definition AmgxInterface.hpp:122
static void updateAmgxMatrixCoefficientsFromGpuSparseMatrix(const GpuSparseMatrixWrapper< T > &gpuSparseMatrix, AMGX_matrix_handle amgxMatrix)
Update only the coefficient values of an AMGX matrix from a GpuSparseMatrix.
Definition AmgxInterface.hpp:446
static AMGX_Mode determineAmgxMode()
Determine the appropriate AMGX mode based on matrix and vector field types.
Definition AmgxInterface.hpp:613
static void updateGpuVectorFromAmgx(AMGX_vector_handle amgx_vec, GpuVector< T > &gpu_vec)
Update a GpuVector from an AMGX vector (device-to-device transfer)
Definition AmgxInterface.hpp:340
static void destroyResources(AMGX_resources_handle resources)
Destroy an AMGX resources handle.
Definition AmgxInterface.hpp:244
static AMGX_resources_handle createResources(AMGX_config_handle config)
Create AMGX resources from a config.
Definition AmgxInterface.hpp:167
static void initialize()
Initialize the AMGX library.
Definition AmgxInterface.hpp:130
static void destroyVector(AMGX_vector_handle vector)
Destroy an AMGX vector handle.
Definition AmgxInterface.hpp:292
static AMGX_solver_handle createSolver(AMGX_resources_handle resources, AMGX_Mode mode, AMGX_config_handle config)
Create an AMGX solver.
Definition AmgxInterface.hpp:183
static void updateMatrixValues(const MatrixType &matrix, AMGX_matrix_handle amgx_matrix)
Update matrix values in AMGX.
Definition AmgxInterface.hpp:590
static void updateAmgxFromGpuVector(const GpuVector< T > &gpu_vec, AMGX_vector_handle amgx_vec)
Update an AMGX vector from a GpuVector (device-to-device transfer)
Definition AmgxInterface.hpp:310
static void initializeVector(int N, int block_size, AMGX_vector_handle amgx_vector)
Initialize an AMGX vector with zeros.
Definition AmgxInterface.hpp:574
static void destroyConfig(AMGX_config_handle config)
Destroy an AMGX config handle.
Definition AmgxInterface.hpp:226
static AMGX_config_handle createConfig(const std::string &config_string)
Create an AMGX config handle from a configuration string.
Definition AmgxInterface.hpp:153
static void destroySolver(AMGX_solver_handle solver)
Destroy an AMGX solver handle.
Definition AmgxInterface.hpp:257
static AMGX_matrix_handle createMatrix(AMGX_resources_handle resources, AMGX_Mode mode)
Create an AMGX matrix.
Definition AmgxInterface.hpp:198
static void initializeMatrix(const MatrixType &matrix, AMGX_matrix_handle amgx_matrix)
Initialize an AMGX matrix from any matrix type (CPU or GPU)
Definition AmgxInterface.hpp:515
static void updateAmgxMatrixFromGpuSparseMatrix(const GpuSparseMatrixWrapper< T > &gpuSparseMatrix, AMGX_matrix_handle amgxMatrix)
Update an AMGX matrix from a GpuSparseMatrixWrapper (device-to-device transfer)
Definition AmgxInterface.hpp:417
static AMGX_vector_handle createVector(AMGX_resources_handle resources, AMGX_Mode mode)
Create an AMGX vector.
Definition AmgxInterface.hpp:213
static void finalize()
Finalize the AMGX library.
Definition AmgxInterface.hpp:141
static void destroyMatrix(AMGX_matrix_handle amgx_matrix, const MatrixType &matrix)
Destroy an AMGX matrix handle.
Definition AmgxInterface.hpp:272
static void updateGpuSparseMatrixFromAmgxMatrix(AMGX_matrix_handle amgxMatrix, GpuSparseMatrixWrapper< T > &gpuSparseMatrix)
Update a GpuSparseMatrixWrapper from an AMGX matrix (device-to-device transfer)
Definition AmgxInterface.hpp:474
static void transferVectorToAmgx(const VectorType &vec, AMGX_vector_handle amgx_vec)
Transfer vector to AMGX from any vector type (CPU or GPU)
Definition AmgxInterface.hpp:370
static void transferVectorFromAmgx(AMGX_vector_handle amgx_vec, VectorType &vec)
Transfer vector from AMGX to any vector type (CPU or GPU)
Definition AmgxInterface.hpp:396
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition GpuSparseMatrixWrapper.hpp:61
Definition gpu_type_detection.hpp:30
int to_int(std::size_t s)
to_int converts a (on most relevant platforms) 64 bits unsigned size_t to a signed 32 bits signed int
Definition safe_conversion.hpp:52
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:240
Type trait to detect if a type is a GPU type.
Definition gpu_type_detection.hpp:40