opm-simulators
HypreInterface.hpp
1 /*
2  Copyright 2025 Equinor ASA
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_HYPRE_INTERFACE_HPP
21 #define OPM_HYPRE_INTERFACE_HPP
22 
23 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreCpuTransfers.hpp>
24 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreDataStructures.hpp>
25 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreErrorHandling.hpp>
26 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreGpuTransfers.hpp>
27 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreSetup.hpp>
28 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreUtils.hpp>
29 
30 // Fix conflict with Dune's matrixmarket.hh header - HYPRE defines MM_MAX_LINE_LENGTH as macro
31 // but Dune expects it as enum value
32 #ifdef MM_MAX_LINE_LENGTH
33 #undef MM_MAX_LINE_LENGTH
34 #endif
35 
36 #include <opm/simulators/linalg/gpuistl/detail/gpu_type_detection.hpp>
37 
38 namespace Opm::gpuistl
39 {
40 
60 namespace HypreInterface
61 {
62  using HostArrays = ::Opm::gpuistl::HypreHostDataArrays;
63  using DeviceArrays = ::Opm::gpuistl::HypreDeviceDataArrays;
64  using ParallelInfo = ::Opm::gpuistl::ParallelInfo;
65  using SparsityPattern = ::Opm::gpuistl::SparsityPattern;
66 
70  void initialize(bool use_gpu_backend);
71 
75  HYPRE_Solver createAMGSolver();
76 
80  void setSolverParameters(HYPRE_Solver solver, const PropertyTree& prm, bool use_gpu_backend);
81 
85  template <typename CommType>
86  HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
87 
91  template <typename CommType>
92  HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
93 
97  void destroySolver(HYPRE_Solver solver);
98 
102  void destroyMatrix(HYPRE_IJMatrix matrix);
103 
107  void destroyVector(HYPRE_IJVector vector);
108 
112  template <typename CommType, typename MatrixType>
113  ParallelInfo setupHypreParallelInfo(const CommType& comm, const MatrixType& matrix);
114 
118  template <typename MatrixType>
119  SparsityPattern setupSparsityPattern(const MatrixType& matrix, const ParallelInfo& par_info, bool owner_first);
120 
124  template <typename MatrixType>
125  std::vector<HYPRE_Int> computeRowIndexes(const MatrixType& matrix,
126  const std::vector<HYPRE_Int>& ncols,
127  const std::vector<int>& local_dune_to_local_hypre,
128  bool owner_first);
129 
133  template <typename VectorType>
134  void transferVectorToHypre(const VectorType& vec,
135  HYPRE_IJVector hypre_vec,
136  HostArrays& host_arrays,
137  const DeviceArrays& device_arrays,
138  const ParallelInfo& par_info,
139  bool use_gpu_backend);
140 
144  template <typename VectorType>
145  void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
146  VectorType& vec,
147  HostArrays& host_arrays,
148  const DeviceArrays& device_arrays,
149  const ParallelInfo& par_info,
150  bool use_gpu_backend);
151 
155  template <typename MatrixType>
156  void updateMatrixValues(const MatrixType& matrix,
157  HYPRE_IJMatrix hypre_matrix,
158  const SparsityPattern& sparsity_pattern,
159  const HostArrays& host_arrays,
160  const DeviceArrays& device_arrays,
161  bool use_gpu_backend);
162 
163  template <typename VectorType>
164  void transferVectorToHypre(const VectorType& vec,
165  HYPRE_IJVector hypre_vec,
166  HostArrays& host_arrays,
167  const DeviceArrays& device_arrays,
168  const ParallelInfo& par_info,
169  bool use_gpu_backend)
170  {
171 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
172  if constexpr (is_gpu_type<VectorType>::value) {
173  transferGpuVectorToHypre(vec, hypre_vec, host_arrays, device_arrays, par_info, use_gpu_backend);
174  } else
175 #endif
176  {
177  transferCpuVectorToHypre(vec, hypre_vec, host_arrays, device_arrays, par_info, use_gpu_backend);
178  }
179  }
180 
181  template <typename VectorType>
182  void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
183  VectorType& vec,
184  HostArrays& host_arrays,
185  const DeviceArrays& device_arrays,
186  const ParallelInfo& par_info,
187  bool use_gpu_backend)
188  {
189 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
190  if constexpr (is_gpu_type<VectorType>::value) {
191  transferHypreToGpuVector(hypre_vec, vec, host_arrays, device_arrays, par_info, use_gpu_backend);
192  } else
193 #endif
194  {
195  transferHypreToCpuVector(hypre_vec, vec, host_arrays, device_arrays, par_info, use_gpu_backend);
196  }
197  }
198 
199  template <typename MatrixType>
200  void updateMatrixValues(const MatrixType& matrix,
201  HYPRE_IJMatrix hypre_matrix,
202  const SparsityPattern& sparsity_pattern,
203  const HostArrays& host_arrays,
204  const DeviceArrays& device_arrays,
205  bool use_gpu_backend)
206  {
207 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
208  if constexpr (is_gpu_type<MatrixType>::value) {
209  updateMatrixFromGpuSparseMatrix(
210  matrix, hypre_matrix, sparsity_pattern, host_arrays, device_arrays, use_gpu_backend);
211  } else
212 #endif
213  {
215  matrix, hypre_matrix, sparsity_pattern, host_arrays, device_arrays, use_gpu_backend);
216  }
217  }
218 
219 } // namespace HypreInterface
220 
221 } // namespace Opm::gpuistl
222 
223 #endif // OPM_HYPRE_INTERFACE_HPP
Type trait to detect if a type is a GPU type.
Definition: gpu_type_detection.hpp:40
Unified interface for Hypre operations with both CPU and GPU data structures.
Definition: HypreCpuTransfers.hpp:29
HYPRE_Solver createAMGSolver()
Create Hypre BoomerAMG solver.
Definition: HypreSetup.hpp:117
Host arrays for HYPRE matrix and vector data transfers.
Definition: HypreDataStructures.hpp:106
void destroyMatrix(HYPRE_IJMatrix matrix)
Destroy Hypre matrix.
Definition: HypreSetup.hpp:233
void initialize([[maybe_unused]] bool use_gpu_backend)
Initialize the Hypre library and set memory/execution policy.
Definition: HypreSetup.hpp:89
GPU device memory arrays for HYPRE operations with GPU backend.
Definition: HypreDataStructures.hpp:137
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre matrix.
Definition: HypreSetup.hpp:170
void updateMatrixFromCpuMatrix(const MatrixType &cpu_matrix, HYPRE_IJMatrix hypre_matrix, const SparsityPattern &sparsity_pattern, const HypreHostDataArrays &host_arrays, [[maybe_unused]] 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
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
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
HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre vector.
Definition: HypreSetup.hpp:197
void transferCpuVectorToHypre(const VectorType &cpu_vec, HYPRE_IJVector hypre_vec, HypreHostDataArrays &host_arrays, [[maybe_unused]] const HypreDeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer CPU vector to Hypre vector.
Definition: HypreCpuTransfers.hpp:67
ParallelInfo setupHypreParallelInfo(const CommType &comm, const MatrixType &matrix)
Setup parallel information for Hypre (automatically detects serial/parallel)
Definition: HypreSetup.hpp:263
void transferHypreToCpuVector(HYPRE_IJVector hypre_vec, VectorType &cpu_vec, HypreHostDataArrays &host_arrays, [[maybe_unused]] const HypreDeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer Hypre vector to CPU vector.
Definition: HypreCpuTransfers.hpp:127
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:467
void destroyVector(HYPRE_IJVector vector)
Destroy Hypre vector.
Definition: HypreSetup.hpp:247
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
void setSolverParameters(HYPRE_Solver solver, const PropertyTree &prm, bool use_gpu_backend)
Set solver parameters from property tree.
Definition: HypreSetup.hpp:133
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
Compressed Sparse Row (CSR) sparsity pattern for HYPRE matrix assembly.
Definition: HypreDataStructures.hpp:86
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:636
Parallel domain decomposition information for HYPRE-Dune interface.
Definition: HypreDataStructures.hpp:37
void destroySolver(HYPRE_Solver solver)
Destroy Hypre solver.
Definition: HypreSetup.hpp:219