Loading...
Searching...
No Matches
PreconditionerAdapter.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_PRECONDITIONERADAPTER_HPP
#define OPM_PRECONDITIONERADAPTER_HPP
#include <cusparse.h>
#include <dune/istl/preconditioner.hh>
#include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
#include <opm/simulators/linalg/gpuistl/GpuVector.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 CudaPreconditionerType>
class PreconditionerAdapter
: public Dune::PreconditionerWithUpdate<X, Y>,
public PreconditionerHolder<GpuVector<typename X::field_type>, GpuVector<typename Y::field_type>>
{
public:
using domain_type = X;
using range_type = Y;
using field_type = typename X::field_type;
explicit PreconditionerAdapter(std::shared_ptr<CudaPreconditionerType> preconditioner)
: m_underlyingPreconditioner(preconditioner)
{
}
virtual void pre([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
{
static_assert(!detail::shouldCallPreconditionerPre<CudaPreconditionerType>(),
"We currently do not support Preconditioner::pre().");
}
virtual void apply(X& v, const Y& d) override
{
if (!m_inputBuffer) {
m_inputBuffer.reset(new GpuVector<field_type>(v.dim()));
m_outputBuffer.reset(new GpuVector<field_type>(v.dim()));
}
m_inputBuffer->copyFromHost(d);
m_underlyingPreconditioner->apply(*m_outputBuffer, *m_inputBuffer);
m_outputBuffer->copyToHost(v);
}
virtual void post([[maybe_unused]] X& x) override
{
static_assert(!detail::shouldCallPreconditionerPost<CudaPreconditionerType>(),
"We currently do not support Preconditioner::post().");
}
Dune::SolverCategory::Category category() const override
{
return m_underlyingPreconditioner->category();
}
virtual void update() override
{
m_underlyingPreconditioner->update();
}
static constexpr bool shouldCallPre()
{
return detail::shouldCallPreconditionerPost<CudaPreconditionerType>();
}
static constexpr bool shouldCallPost()
{
return detail::shouldCallPreconditionerPre<CudaPreconditionerType>();
}
virtual std::shared_ptr<Dune::PreconditionerWithUpdate<GpuVector<field_type>, GpuVector<field_type>>>
getUnderlyingPreconditioner() override
{
return m_underlyingPreconditioner;
}
virtual bool hasPerfectUpdate() const override {
return m_underlyingPreconditioner->hasPerfectUpdate();
}
private:
std::shared_ptr<CudaPreconditionerType> m_underlyingPreconditioner;
std::unique_ptr<GpuVector<field_type>> m_inputBuffer;
std::unique_ptr<GpuVector<field_type>> m_outputBuffer;
};
} // end namespace Opm::gpuistl
#endif
Dune::PreconditionerWithUpdate
Interface class adding the update() method to the preconditioner interface.
Definition PreconditionerWithUpdate.hpp:32
Opm::gpuistl::GpuVector
Definition gpu_type_detection.hpp:30
Opm::gpuistl::PreconditionerAdapter::range_type
Y range_type
The range type of the preconditioner.
Definition PreconditionerAdapter.hpp:48
Opm::gpuistl::PreconditionerAdapter::apply
virtual void apply(X &v, const Y &d) override
Apply the preconditoner.
Definition PreconditionerAdapter.hpp:76
Opm::gpuistl::PreconditionerAdapter::pre
virtual void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition PreconditionerAdapter.hpp:66
Opm::gpuistl::PreconditionerAdapter::update
virtual void update() override
Calls update on the underlying CUDA preconditioner.
Definition PreconditionerAdapter.hpp:105
Opm::gpuistl::PreconditionerAdapter::getUnderlyingPreconditioner
virtual std::shared_ptr< Dune::PreconditionerWithUpdate< GpuVector< field_type >, GpuVector< field_type > > > getUnderlyingPreconditioner() override
getUnderlyingPreconditioner gets the underlying preconditioner (preconditioner being held)
Definition PreconditionerAdapter.hpp:120
Opm::gpuistl::PreconditionerAdapter::field_type
typename X::field_type field_type
The field type of the preconditioner.
Definition PreconditionerAdapter.hpp:50
Opm::gpuistl::PreconditionerAdapter::post
virtual void post(X &x) override
Clean up.
Definition PreconditionerAdapter.hpp:91
Opm::gpuistl::PreconditionerAdapter::domain_type
X domain_type
The domain type of the preconditioner.
Definition PreconditionerAdapter.hpp:46
Opm::gpuistl::PreconditionerAdapter::category
Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category).
Definition PreconditionerAdapter.hpp:99
Opm::gpuistl::PreconditionerAdapter::PreconditionerAdapter
PreconditionerAdapter(std::shared_ptr< CudaPreconditionerType > preconditioner)
Constructor.
Definition PreconditionerAdapter.hpp:57
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