opm-simulators
GpuBlockPreconditioner.hpp
1 /*
2  Copyright 2022-2023 SINTEF AS
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_CUISTL_GPUBLOCKPRECONDITIONER_HPP
20 #define OPM_CUISTL_GPUBLOCKPRECONDITIONER_HPP
21 
22 #include <dune/common/shared_ptr.hh>
23 #include <memory>
24 #include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
25 #include <opm/simulators/linalg/gpuistl/PreconditionerHolder.hpp>
26 #include <opm/simulators/linalg/gpuistl/detail/preconditioner_should_call_post_pre.hpp>
27 
28 namespace Opm::gpuistl
29 {
33 template <class X, class Y, class C, class P = Dune::PreconditionerWithUpdate<X, Y>>
35 {
36 public:
37  using domain_type = X;
38  using range_type = Y;
39  using field_type = typename X::field_type;
40  using communication_type = C;
41 
42 
50  GpuBlockPreconditioner(const std::shared_ptr<P>& p, const std::shared_ptr<const communication_type>& c)
51  : m_preconditioner(p)
52  , m_communication(c)
53  {
54  }
55 
56  GpuBlockPreconditioner(const std::shared_ptr<P>& p, const communication_type& c)
57  : m_preconditioner(p)
58  , m_communication(Dune::stackobject_to_shared_ptr(c))
59  {
60  }
61 
65  virtual void pre(X& x, Y& b) override
66  {
67  // TODO: [perf] Do we always need to copy, or only when we need the pre step?
68  m_communication->copyOwnerToAll(x, x); // make Dirichlet values consistent
69  if constexpr (detail::shouldCallPreconditionerPre<P>()) {
70  m_preconditioner->pre(x, b);
71  }
72  }
73 
77  virtual void apply(X& v, const Y& d) override
78  {
79  m_preconditioner->apply(v, d);
80  m_communication->copyOwnerToAll(v, v);
81  }
82 
83 
84  virtual void update() override
85  {
86  m_preconditioner->update();
87  }
88 
89  virtual void post(X& x) override
90  {
91  if constexpr (detail::shouldCallPreconditionerPost<P>()) {
92  m_preconditioner->post(x);
93  }
94  }
95 
97  virtual Dune::SolverCategory::Category category() const override
98  {
99  return Dune::SolverCategory::overlapping;
100  }
101 
102  static constexpr bool shouldCallPre()
103  {
104  return detail::shouldCallPreconditionerPost<P>();
105  }
106  static constexpr bool shouldCallPost()
107  {
108  return detail::shouldCallPreconditionerPre<P>();
109  }
110 
111  virtual std::shared_ptr<Dune::PreconditionerWithUpdate<X, Y>> getUnderlyingPreconditioner() override
112  {
113  return m_preconditioner;
114  }
115 
116  virtual bool hasPerfectUpdate() const override {
117  return true;
118  }
119 
120 
121 private:
123  std::shared_ptr<P> m_preconditioner;
124 
126  std::shared_ptr<const communication_type> m_communication;
127 };
128 } // namespace Opm::gpuistl
129 #endif
Definition: fvbaseprimaryvariables.hh:161
Common interface for adapters that hold preconditioners.
Definition: PreconditionerHolder.hpp:33
GpuBlockPreconditioner(const std::shared_ptr< P > &p, const std::shared_ptr< const communication_type > &c)
Constructor.
Definition: GpuBlockPreconditioner.hpp:50
virtual void apply(X &v, const Y &d) override
Apply the preconditioner.
Definition: GpuBlockPreconditioner.hpp:77
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:33
Is an adaptation of Dune::BlockPreconditioner that works within the CuISTL framework.
Definition: GpuBlockPreconditioner.hpp:34
virtual Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category)
Definition: GpuBlockPreconditioner.hpp:97
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
virtual void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition: GpuBlockPreconditioner.hpp:65
virtual std::shared_ptr< Dune::PreconditionerWithUpdate< X, Y > > getUnderlyingPreconditioner() override
getUnderlyingPreconditioner gets the underlying preconditioner (preconditioner being held) ...
Definition: GpuBlockPreconditioner.hpp:111