opm-simulators
PreconditionerCPUMatrixToGPUMatrix.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 #ifndef OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
20 #define OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
21 #include "opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp"
22 #include <dune/istl/preconditioner.hh>
23 #include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
24 #include <opm/simulators/linalg/gpuistl/GpuVector.hpp>
25 #include <opm/simulators/linalg/gpuistl/PreconditionerHolder.hpp>
26 #include <opm/simulators/linalg/gpuistl/detail/preconditioner_should_call_post_pre.hpp>
27 
28 
29 namespace Opm::gpuistl
30 {
40 template <class X, class Y, class CudaPreconditionerType, class CPUMatrixType>
42 {
43 public:
45  using domain_type = X;
47  using range_type = Y;
49  using field_type = typename X::field_type;
50 
51 
52  template <typename... Args>
53  explicit PreconditionerCPUMatrixToGPUMatrix(const CPUMatrixType& A, Args&&... args)
54  : m_cpuMatrix(A)
55  , m_gpuMatrix(GpuSparseMatrixWrapper<field_type>::fromMatrix(A))
56  , m_underlyingPreconditioner(m_gpuMatrix, std::forward<Args>(args)...)
57  {
58  }
59 
60 
64  void pre([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
65  {
66  static_assert(!detail::shouldCallPreconditionerPre<CudaPreconditionerType>(),
67  "We currently do not support Preconditioner::pre().");
68  }
69 
70 
74  void apply(X& v, const Y& d) override
75  {
76  m_underlyingPreconditioner.apply(v, d);
77  }
78 
79 
83  void post([[maybe_unused]] X& x) override
84  {
85  static_assert(!detail::shouldCallPreconditionerPost<CudaPreconditionerType>(),
86  "We currently do not support Preconditioner::post().");
87  }
88 
89 
91  Dune::SolverCategory::Category category() const override
92  {
93  return m_underlyingPreconditioner.category();
94  }
95 
98  void update() override
99  {
100  m_gpuMatrix.updateNonzeroValues(m_cpuMatrix, true);
101  m_underlyingPreconditioner.update();
102  }
103 
104  static constexpr bool shouldCallPre()
105  {
106  return detail::shouldCallPreconditionerPost<CudaPreconditionerType>();
107  }
108  static constexpr bool shouldCallPost()
109  {
110  return detail::shouldCallPreconditionerPre<CudaPreconditionerType>();
111  }
112 
113  virtual bool hasPerfectUpdate() const override
114  {
115  return m_underlyingPreconditioner.hasPerfectUpdate();
116  }
117 
118 private:
119  const CPUMatrixType& m_cpuMatrix;
120  GpuSparseMatrixWrapper<field_type> m_gpuMatrix;
121 
123  CudaPreconditionerType m_underlyingPreconditioner;
124 };
125 } // end namespace Opm::gpuistl
126 
127 #endif // OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition: gpu_type_detection.hpp:32
X domain_type
The domain type of the preconditioner.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:45
Convert a CPU matrix to a GPU matrix and use a CUDA preconditioner on the GPU.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:41
void pre([[maybe_unused]] X &x, [[maybe_unused]] Y &b) override
Prepare the preconditioner.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:64
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:33
void update() override
Copies the CPU matrix to the GPU matrix then calls the GPU preconditioner update function.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:98
Y range_type
The range type of the preconditioner.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:47
void apply(X &v, const Y &d) override
Apply the preconditoner.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:74
void post([[maybe_unused]] X &x) override
Clean up.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:83
typename X::field_type field_type
The field type of the preconditioner.
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:49
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
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
Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category)
Definition: PreconditionerCPUMatrixToGPUMatrix.hpp:91