Loading...
Searching...
No Matches
GpuBlockPreconditioner.hpp
/*
Copyright 2022-2023 SINTEF AS
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_CUISTL_GPUBLOCKPRECONDITIONER_HPP
#define OPM_CUISTL_GPUBLOCKPRECONDITIONER_HPP
#include <dune/common/shared_ptr.hh>
#include <memory>
#include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
#include <opm/simulators/linalg/gpuistl/PreconditionerHolder.hpp>
#include <opm/simulators/linalg/gpuistl/detail/preconditioner_should_call_post_pre.hpp>
namespace Opm::gpuistl
{
template <class X, class Y, class C, class P = Dune::PreconditionerWithUpdate<X, Y>>
class GpuBlockPreconditioner : public Dune::PreconditionerWithUpdate<X, Y>, public PreconditionerHolder<X, Y>
{
public:
using domain_type = X;
using range_type = Y;
using field_type = typename X::field_type;
using communication_type = C;
GpuBlockPreconditioner(const std::shared_ptr<P>& p, const std::shared_ptr<const communication_type>& c)
: m_preconditioner(p)
, m_communication(c)
{
}
GpuBlockPreconditioner(const std::shared_ptr<P>& p, const communication_type& c)
: m_preconditioner(p)
, m_communication(Dune::stackobject_to_shared_ptr(c))
{
}
virtual void pre(X& x, Y& b) override
{
// TODO: [perf] Do we always need to copy, or only when we need the pre step?
m_communication->copyOwnerToAll(x, x); // make Dirichlet values consistent
if constexpr (detail::shouldCallPreconditionerPre<P>()) {
m_preconditioner->pre(x, b);
}
}
virtual void apply(X& v, const Y& d) override
{
m_preconditioner->apply(v, d);
m_communication->copyOwnerToAll(v, v);
}
virtual void update() override
{
m_preconditioner->update();
}
virtual void post(X& x) override
{
if constexpr (detail::shouldCallPreconditionerPost<P>()) {
m_preconditioner->post(x);
}
}
virtual Dune::SolverCategory::Category category() const override
{
return Dune::SolverCategory::overlapping;
}
static constexpr bool shouldCallPre()
{
return detail::shouldCallPreconditionerPost<P>();
}
static constexpr bool shouldCallPost()
{
return detail::shouldCallPreconditionerPre<P>();
}
virtual std::shared_ptr<Dune::PreconditionerWithUpdate<X, Y>> getUnderlyingPreconditioner() override
{
return m_preconditioner;
}
virtual bool hasPerfectUpdate() const override {
return true;
}
private:
std::shared_ptr<P> m_preconditioner;
std::shared_ptr<const communication_type> m_communication;
};
} // namespace Opm::gpuistl
#endif
Dune::PreconditionerWithUpdate
Interface class adding the update() method to the preconditioner interface.
Definition PreconditionerWithUpdate.hpp:32
Opm::gpuistl::GpuBlockPreconditioner::category
virtual Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category).
Definition GpuBlockPreconditioner.hpp:97
Opm::gpuistl::GpuBlockPreconditioner::apply
virtual void apply(X &v, const Y &d) override
Apply the preconditioner.
Definition GpuBlockPreconditioner.hpp:77
Opm::gpuistl::GpuBlockPreconditioner::pre
virtual void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition GpuBlockPreconditioner.hpp:65
Opm::gpuistl::GpuBlockPreconditioner::getUnderlyingPreconditioner
virtual std::shared_ptr< Dune::PreconditionerWithUpdate< X, Y > > getUnderlyingPreconditioner() override
getUnderlyingPreconditioner gets the underlying preconditioner (preconditioner being held)
Definition GpuBlockPreconditioner.hpp:111
Opm::gpuistl::GpuBlockPreconditioner::GpuBlockPreconditioner
GpuBlockPreconditioner(const std::shared_ptr< P > &p, const std::shared_ptr< const communication_type > &c)
Constructor.
Definition GpuBlockPreconditioner.hpp:50
Opm::gpuistl::PreconditionerHolder
Common interface for adapters that hold preconditioners.
Definition PreconditionerHolder.hpp:34
Opm::gpuistl::detail::shouldCallPreconditionerPost
constexpr bool shouldCallPreconditionerPost()
Tests (compile time) if the preconditioner type needs to call post() after a call to apply(....
Definition preconditioner_should_call_post_pre.hpp:52
Opm::gpuistl::detail::shouldCallPreconditionerPre
constexpr bool shouldCallPreconditionerPre()
Tests (compile time) if the preconditioner type needs to call pre() before a call to apply().
Definition preconditioner_should_call_post_pre.hpp:34