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/hypreinterface/HypreCpuTransfers.hpp>
24 #include <opm/simulators/linalg/hypreinterface/HypreDataStructures.hpp>
25 #include <opm/simulators/linalg/hypreinterface/HypreErrorHandling.hpp>
26 #include <opm/simulators/linalg/hypreinterface/HypreSetup.hpp>
27 #include <opm/simulators/linalg/hypreinterface/HypreUtils.hpp>
28 
29 // Fix conflict with Dune's matrixmarket.hh header - HYPRE defines MM_MAX_LINE_LENGTH as macro
30 // but Dune expects it as enum value
31 #ifdef MM_MAX_LINE_LENGTH
32 #undef MM_MAX_LINE_LENGTH
33 #endif
34 
35 #if HYPRE_USING_CUDA
36 #include <opm/simulators/linalg/gpuistl/detail/gpu_type_detection.hpp>
37 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreGpuTransfers.hpp>
38 #elif HYPRE_USING_HIP
39 #include <opm/simulators/linalg/gpuistl_hip/detail/gpu_type_detection.hpp>
40 #include <opm/simulators/linalg/gpuistl_hip/hypreinterface/HypreGpuTransfers.hpp>
41 #endif
42 
63 {
64 
68  void initialize(bool use_gpu_backend);
69 
73  HYPRE_Solver createAMGSolver();
74 
78  void setSolverParameters(HYPRE_Solver solver, const PropertyTree& prm, bool use_gpu_backend);
79 
83  template <typename CommType>
84  HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
85 
89  template <typename CommType>
90  HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType& comm);
91 
95  void destroySolver(HYPRE_Solver solver);
96 
100  void destroyMatrix(HYPRE_IJMatrix matrix);
101 
105  void destroyVector(HYPRE_IJVector vector);
106 
110  template <typename CommType, typename MatrixType>
111  ParallelInfo
112  setupHypreParallelInfo(const CommType& comm, const MatrixType& matrix);
113 
117  template <typename MatrixType>
118  SparsityPattern
119  setupSparsityPattern(const MatrixType& matrix,
120  const ParallelInfo& par_info,
121  bool owner_first);
122 
126  template <typename MatrixType>
127  std::vector<HYPRE_Int> computeRowIndexes(const MatrixType& matrix,
128  const std::vector<HYPRE_Int>& ncols,
129  const std::vector<int>& local_dune_to_local_hypre,
130  bool owner_first);
131 
135  template <typename VectorType>
136  void transferVectorToHypre(const VectorType& vec,
137  HYPRE_IJVector hypre_vec,
138  HostDataArrays& host_arrays,
139  const DeviceDataArrays& device_arrays,
140  const ParallelInfo& par_info,
141  bool use_gpu_backend);
142 
146  template <typename VectorType>
147  void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
148  VectorType& vec,
149  HostDataArrays& host_arrays,
150  const DeviceDataArrays& device_arrays,
151  const ParallelInfo& par_info,
152  bool use_gpu_backend);
153 
157  template <typename MatrixType>
158  void updateMatrixValues(const MatrixType& matrix,
159  HYPRE_IJMatrix hypre_matrix,
160  const SparsityPattern& sparsity_pattern,
161  const HostDataArrays& host_arrays,
162  const DeviceDataArrays& device_arrays,
163  bool use_gpu_backend);
164 
165  template <typename VectorType>
166  void transferVectorToHypre(const VectorType& vec,
167  HYPRE_IJVector hypre_vec,
168  HostDataArrays& host_arrays,
169  const DeviceDataArrays& device_arrays,
170  const ParallelInfo& par_info,
171  bool use_gpu_backend)
172  {
173 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
175  gpuistl::HypreInterface::transferGpuVectorToHypre(vec, hypre_vec, host_arrays,
176  device_arrays, par_info, use_gpu_backend);
177  } else
178 #endif
179  {
180  transferCpuVectorToHypre(vec, hypre_vec, host_arrays,
181  device_arrays, par_info, use_gpu_backend);
182  }
183  }
184 
185  template <typename VectorType>
186  void transferVectorFromHypre(HYPRE_IJVector hypre_vec,
187  VectorType& vec,
188  HostDataArrays& host_arrays,
189  const DeviceDataArrays& device_arrays,
190  const ParallelInfo& par_info,
191  bool use_gpu_backend)
192  {
193 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
195  gpuistl::HypreInterface::transferHypreToGpuVector(hypre_vec, vec, host_arrays,
196  device_arrays, par_info, use_gpu_backend);
197  } else
198 #endif
199  {
200  transferHypreToCpuVector(hypre_vec, vec, host_arrays, device_arrays,
201  par_info, use_gpu_backend);
202  }
203  }
204 
205  template <typename MatrixType>
206  void updateMatrixValues(const MatrixType& matrix,
207  HYPRE_IJMatrix hypre_matrix,
208  const SparsityPattern& sparsity_pattern,
209  const HostDataArrays& host_arrays,
210  const DeviceDataArrays& device_arrays,
211  bool use_gpu_backend)
212  {
213 #if HYPRE_USING_CUDA || HYPRE_USING_HIP
215  gpuistl::HypreInterface::updateMatrixFromGpuSparseMatrix(matrix,
216  hypre_matrix,
217  sparsity_pattern,
218  host_arrays,
219  device_arrays,
220  use_gpu_backend);
221  } else
222 #endif
223  {
224  updateMatrixFromCpuMatrix(matrix, hypre_matrix, sparsity_pattern,
225  host_arrays, device_arrays, use_gpu_backend);
226  }
227  }
228 
229 } // namespace Opm::linalg::HypreInterface
230 
231 #endif // OPM_HYPRE_INTERFACE_HPP
void transferVectorFromHypre(HYPRE_IJVector hypre_vec, VectorType &vec, HostDataArrays &host_arrays, const DeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer vector from Hypre to any vector type (CPU or GPU)
Definition: HypreInterface.hpp:186
Type trait to detect if a type is a GPU type.
Definition: gpu_type_detection.hpp:40
Parallel domain decomposition information for HYPRE-Dune interface.
Definition: HypreDataStructures.hpp:37
void transferHypreToCpuVector(HYPRE_IJVector hypre_vec, VectorType &cpu_vec, linalg::HypreInterface::HostDataArrays &host_arrays, [[maybe_unused]] const linalg::HypreInterface::DeviceDataArrays &device_arrays, const linalg::HypreInterface::ParallelInfo &par_info, bool use_gpu_backend)
Transfer Hypre vector to CPU vector.
Definition: HypreCpuTransfers.hpp:114
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:557
void destroySolver(HYPRE_Solver solver)
Destroy Hypre solver.
Definition: HypreSetup.hpp:214
Compressed Sparse Row (CSR) sparsity pattern for HYPRE matrix assembly.
Definition: HypreDataStructures.hpp:86
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:462
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre matrix.
Definition: HypreSetup.hpp:165
void destroyMatrix(HYPRE_IJMatrix matrix)
Destroy Hypre matrix.
Definition: HypreSetup.hpp:228
void transferVectorToHypre(const VectorType &vec, HYPRE_IJVector hypre_vec, HostDataArrays &host_arrays, const DeviceDataArrays &device_arrays, const ParallelInfo &par_info, bool use_gpu_backend)
Transfer vector to Hypre from any vector type (CPU or GPU)
Definition: HypreInterface.hpp:166
Host arrays for HYPRE matrix and vector data transfers.
Definition: HypreDataStructures.hpp:106
void initialize(bool use_gpu_backend)
Initialize the Hypre library and set memory/execution policy.
void updateMatrixFromCpuMatrix(const MatrixType &cpu_matrix, HYPRE_IJMatrix hypre_matrix, const linalg::HypreInterface::SparsityPattern &sparsity_pattern, const linalg::HypreInterface::HostDataArrays &host_arrays, [[maybe_unused]] const linalg::HypreInterface::DeviceDataArrays &device_arrays, bool use_gpu_backend)
Update Hypre matrix from CPU matrix Uses HYPRE_IJMatrixSetValues2 with pre-computed row_indexes...
Definition: HypreCpuTransfers.hpp:155
void destroyVector(HYPRE_IJVector vector)
Destroy Hypre vector.
Definition: HypreSetup.hpp:242
HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre vector.
Definition: HypreSetup.hpp:192
HYPRE_Solver createAMGSolver()
Create Hypre solver (BoomerAMG)
Definition: HypreSetup.hpp:112
ParallelInfo setupHypreParallelInfo(const CommType &comm, const MatrixType &matrix)
Setup parallel information for Hypre (automatically detects serial/parallel)
Definition: HypreSetup.hpp:258
void transferCpuVectorToHypre(const VectorType &cpu_vec, HYPRE_IJVector hypre_vec, linalg::HypreInterface::HostDataArrays &host_arrays, [[maybe_unused]] const linalg::HypreInterface::DeviceDataArrays &device_arrays, const linalg::HypreInterface::ParallelInfo &par_info, bool use_gpu_backend)
Transfer CPU vector to Hypre vector.
Definition: HypreCpuTransfers.hpp:73
void setSolverParameters(HYPRE_Solver solver, const PropertyTree &prm, bool use_gpu_backend)
Set solver parameters from property tree.
Definition: HypreSetup.hpp:128
Unified interface for Hypre operations with both CPU and GPU data structures.
Definition: HypreCpuTransfers.hpp:35
GPU device memory arrays for HYPRE operations with GPU backend.
Definition: HypreDataStructures.hpp:137
void updateMatrixValues(const MatrixType &matrix, HYPRE_IJMatrix hypre_matrix, const SparsityPattern &sparsity_pattern, const HostDataArrays &host_arrays, const DeviceDataArrays &device_arrays, bool use_gpu_backend)
Update matrix values in Hypre.
Definition: HypreInterface.hpp:206