Loading...
Searching...
No Matches
HypreInterface.hpp
/*
Copyright 2025 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_HYPRE_INTERFACE_HPP
#define OPM_HYPRE_INTERFACE_HPP
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreCpuTransfers.hpp>
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreDataStructures.hpp>
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreErrorHandling.hpp>
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreGpuTransfers.hpp>
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreSetup.hpp>
#include <opm/simulators/linalg/gpuistl/hypreinterface/HypreUtils.hpp>
// Fix conflict with Dune's matrixmarket.hh header - HYPRE defines MM_MAX_LINE_LENGTH as macro
// but Dune expects it as enum value
#ifdef MM_MAX_LINE_LENGTH
#undef MM_MAX_LINE_LENGTH
#endif
#include <opm/simulators/linalg/gpuistl/detail/gpu_type_detection.hpp>
namespace Opm::gpuistl
{
namespace HypreInterface
{
using HostArrays = ::Opm::gpuistl::HypreHostDataArrays;
using DeviceArrays = ::Opm::gpuistl::HypreDeviceDataArrays;
using ParallelInfo = ::Opm::gpuistl::ParallelInfo;
using SparsityPattern = ::Opm::gpuistl::SparsityPattern;
void initialize(bool use_gpu_backend);
HYPRE_Solver createAMGSolver();
void setSolverParameters(HYPRE_Solver solver, const PropertyTree& prm, bool use_gpu_backend);
template <typename CommType>
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
template <typename CommType>
HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
void destroySolver(HYPRE_Solver solver);
void destroyMatrix(HYPRE_IJMatrix matrix);
void destroyVector(HYPRE_IJVector vector);
template <typename CommType, typename MatrixType>
ParallelInfo setupHypreParallelInfo(const CommType& comm, const MatrixType& matrix);
template <typename MatrixType>
SparsityPattern setupSparsityPattern(const MatrixType& matrix, const ParallelInfo& par_info, bool owner_first);
template <typename MatrixType>
std::vector<HYPRE_Int> computeRowIndexes(const MatrixType& matrix,
const std::vector<HYPRE_Int>& ncols,
const std::vector<int>& local_dune_to_local_hypre,
bool owner_first);
template <typename VectorType>
void transferVectorToHypre(const VectorType& vec,
HYPRE_IJVector hypre_vec,
HostArrays& host_arrays,
const DeviceArrays& device_arrays,
const ParallelInfo& par_info,
bool use_gpu_backend);
template <typename VectorType>
void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
VectorType& vec,
HostArrays& host_arrays,
const DeviceArrays& device_arrays,
const ParallelInfo& par_info,
bool use_gpu_backend);
template <typename MatrixType>
void updateMatrixValues(const MatrixType& matrix,
HYPRE_IJMatrix hypre_matrix,
const SparsityPattern& sparsity_pattern,
const HostArrays& host_arrays,
const DeviceArrays& device_arrays,
bool use_gpu_backend);
template <typename VectorType>
void transferVectorToHypre(const VectorType& vec,
HYPRE_IJVector hypre_vec,
HostArrays& host_arrays,
const DeviceArrays& device_arrays,
const ParallelInfo& par_info,
bool use_gpu_backend)
{
#if HYPRE_USING_CUDA || HYPRE_USING_HIP
if constexpr (is_gpu_type<VectorType>::value) {
transferGpuVectorToHypre(vec, hypre_vec, host_arrays, device_arrays, par_info, use_gpu_backend);
} else
#endif
{
transferCpuVectorToHypre(vec, hypre_vec, host_arrays, device_arrays, par_info, use_gpu_backend);
}
}
template <typename VectorType>
void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
VectorType& vec,
HostArrays& host_arrays,
const DeviceArrays& device_arrays,
const ParallelInfo& par_info,
bool use_gpu_backend)
{
#if HYPRE_USING_CUDA || HYPRE_USING_HIP
if constexpr (is_gpu_type<VectorType>::value) {
transferHypreToGpuVector(hypre_vec, vec, host_arrays, device_arrays, par_info, use_gpu_backend);
} else
#endif
{
transferHypreToCpuVector(hypre_vec, vec, host_arrays, device_arrays, par_info, use_gpu_backend);
}
}
template <typename MatrixType>
void updateMatrixValues(const MatrixType& matrix,
HYPRE_IJMatrix hypre_matrix,
const SparsityPattern& sparsity_pattern,
const HostArrays& host_arrays,
const DeviceArrays& device_arrays,
bool use_gpu_backend)
{
#if HYPRE_USING_CUDA || HYPRE_USING_HIP
if constexpr (is_gpu_type<MatrixType>::value) {
updateMatrixFromGpuSparseMatrix(
matrix, hypre_matrix, sparsity_pattern, host_arrays, device_arrays, use_gpu_backend);
} else
#endif
{
updateMatrixFromCpuMatrix(
matrix, hypre_matrix, sparsity_pattern, host_arrays, device_arrays, use_gpu_backend);
}
}
} // namespace HypreInterface
} // namespace Opm::gpuistl
#endif // OPM_HYPRE_INTERFACE_HPP
Opm::PropertyTree
Hierarchical collection of key/value pairs.
Definition PropertyTree.hpp:39
Opm::gpuistl::HypreInterface
Unified interface for Hypre operations with both CPU and GPU data structures.
Definition HypreInterface.hpp:61
Opm::gpuistl::HypreInterface::destroySolver
void destroySolver(HYPRE_Solver solver)
Destroy Hypre solver.
Definition HypreSetup.hpp:219
Opm::gpuistl::HypreInterface::transferVectorToHypre
void transferVectorToHypre(const VectorType &vec, HYPRE_IJVector hypre_vec, HostArrays &host_arrays, const DeviceArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer vector to Hypre from any vector type (CPU or GPU).
Definition HypreInterface.hpp:164
Opm::gpuistl::HypreInterface::transferCpuVectorToHypre
void transferCpuVectorToHypre(const VectorType &cpu_vec, HYPRE_IJVector hypre_vec, HypreHostDataArrays &host_arrays, const HypreDeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer CPU vector to Hypre vector.
Definition HypreCpuTransfers.hpp:67
Opm::gpuistl::HypreInterface::setupHypreParallelInfo
ParallelInfo setupHypreParallelInfo(const CommType &comm, const MatrixType &matrix)
Setup parallel information for Hypre (automatically detects serial/parallel).
Definition HypreSetup.hpp:263
Opm::gpuistl::HypreInterface::destroyMatrix
void destroyMatrix(HYPRE_IJMatrix matrix)
Destroy Hypre matrix.
Definition HypreSetup.hpp:233
Opm::gpuistl::HypreInterface::createAMGSolver
HYPRE_Solver createAMGSolver()
Create Hypre solver (BoomerAMG).
Definition HypreSetup.hpp:117
Opm::gpuistl::HypreInterface::transferHypreToCpuVector
void transferHypreToCpuVector(HYPRE_IJVector hypre_vec, VectorType &cpu_vec, HypreHostDataArrays &host_arrays, const HypreDeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer Hypre vector to CPU vector.
Definition HypreCpuTransfers.hpp:127
Opm::gpuistl::HypreInterface::updateMatrixFromCpuMatrix
void updateMatrixFromCpuMatrix(const MatrixType &cpu_matrix, HYPRE_IJMatrix hypre_matrix, const SparsityPattern &sparsity_pattern, const HypreHostDataArrays &host_arrays, const HypreDeviceDataArrays &device_arrays, bool use_gpu_backend)
Update Hypre matrix from CPU matrix Uses HYPRE_IJMatrixSetValues2 with pre-computed row_indexes,...
Definition HypreCpuTransfers.hpp:187
Opm::gpuistl::HypreInterface::computeRowIndexes
std::vector< HYPRE_Int > computeRowIndexes(const MatrixType &matrix, const std::vector< HYPRE_Int > &ncols, const std::vector< int > &local_dune_to_local_hypre, bool owner_first)
Compute row indexes for HYPRE_IJMatrixSetValues2.
Definition HypreSetup.hpp:634
Opm::gpuistl::HypreInterface::createMatrix
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre matrix.
Definition HypreSetup.hpp:170
Opm::gpuistl::HypreInterface::updateMatrixValues
void updateMatrixValues(const MatrixType &matrix, HYPRE_IJMatrix hypre_matrix, const SparsityPattern &sparsity_pattern, const HostArrays &host_arrays, const DeviceArrays &device_arrays, bool use_gpu_backend)
Update matrix values in Hypre.
Definition HypreInterface.hpp:200
Opm::gpuistl::HypreInterface::setupSparsityPattern
SparsityPattern setupSparsityPattern(const MatrixType &matrix, const ParallelInfo &par_info, bool owner_first)
Setup sparsity pattern from matrix (automatically detects CPU/GPU type).
Definition HypreSetup.hpp:465
Opm::gpuistl::HypreInterface::initialize
void initialize(bool use_gpu_backend)
Initialize the Hypre library and set memory/execution policy.
Definition HypreSetup.hpp:89
Opm::gpuistl::HypreInterface::createVector
HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre vector.
Definition HypreSetup.hpp:197
Opm::gpuistl::HypreInterface::destroyVector
void destroyVector(HYPRE_IJVector vector)
Destroy Hypre vector.
Definition HypreSetup.hpp:247
Opm::gpuistl::HypreInterface::setSolverParameters
void setSolverParameters(HYPRE_Solver solver, const PropertyTree &prm, bool use_gpu_backend)
Set solver parameters from property tree.
Definition HypreSetup.hpp:133
Opm::gpuistl::HypreInterface::transferVectorFromHypre
void transferVectorFromHypre(HYPRE_IJVector hypre_vec, VectorType &vec, HostArrays &host_arrays, const DeviceArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer vector from Hypre to any vector type (CPU or GPU).
Definition HypreInterface.hpp:182
Opm::gpuistl::HypreDeviceDataArrays
GPU device memory arrays for HYPRE operations with GPU backend.
Definition HypreDataStructures.hpp:137
Opm::gpuistl::HypreHostDataArrays
Host arrays for HYPRE matrix and vector data transfers.
Definition HypreDataStructures.hpp:106
Opm::gpuistl::ParallelInfo
Parallel domain decomposition information for HYPRE-Dune interface.
Definition HypreDataStructures.hpp:37
Opm::gpuistl::SparsityPattern
Compressed Sparse Row (CSR) sparsity pattern for HYPRE matrix assembly.
Definition HypreDataStructures.hpp:86
Opm::gpuistl::is_gpu_type
Type trait to detect if a type is a GPU type.
Definition gpu_type_detection.hpp:40