Loading...
Searching...
No Matches
PreconditionerCPUMatrixToGPUMatrix.hpp
/*
Copyright 2025 Equinor ASA
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_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
#define OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
#include "opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp"
#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 CPUMatrixType>
class PreconditionerCPUMatrixToGPUMatrix : public Dune::PreconditionerWithUpdate<X, Y>
{
public:
using domain_type = X;
using range_type = Y;
using field_type = typename X::field_type;
template <typename... Args>
explicit PreconditionerCPUMatrixToGPUMatrix(const CPUMatrixType& A, Args&&... args)
: m_cpuMatrix(A)
, m_gpuMatrix(GpuSparseMatrixWrapper<field_type>::fromMatrix(A))
, m_underlyingPreconditioner(m_gpuMatrix, std::forward<Args>(args)...)
{
}
void pre([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
{
static_assert(!detail::shouldCallPreconditionerPre<CudaPreconditionerType>(),
"We currently do not support Preconditioner::pre().");
}
void apply(X& v, const Y& d) override
{
m_underlyingPreconditioner.apply(v, d);
}
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();
}
void update() override
{
m_gpuMatrix.updateNonzeroValues(m_cpuMatrix, true);
m_underlyingPreconditioner.update();
}
static constexpr bool shouldCallPre()
{
return detail::shouldCallPreconditionerPost<CudaPreconditionerType>();
}
static constexpr bool shouldCallPost()
{
return detail::shouldCallPreconditionerPre<CudaPreconditionerType>();
}
virtual bool hasPerfectUpdate() const override
{
return m_underlyingPreconditioner.hasPerfectUpdate();
}
private:
const CPUMatrixType& m_cpuMatrix;
GpuSparseMatrixWrapper<field_type> m_gpuMatrix;
CudaPreconditionerType m_underlyingPreconditioner;
};
} // end namespace Opm::gpuistl
#endif // OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
Dune::PreconditionerWithUpdate
Interface class adding the update() method to the preconditioner interface.
Definition PreconditionerWithUpdate.hpp:32
Opm::gpuistl::GpuSparseMatrixWrapper
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition GpuSparseMatrixWrapper.hpp:61
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::pre
void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:64
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::apply
void apply(X &v, const Y &d) override
Apply the preconditoner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:74
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::post
void post(X &x) override
Clean up.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:83
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::category
Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category).
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:91
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::update
void update() override
Copies the CPU matrix to the GPU matrix then calls the GPU preconditioner update function.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:98
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::range_type
Y range_type
The range type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:47
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::domain_type
X domain_type
The domain type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:45
Opm::gpuistl::PreconditionerCPUMatrixToGPUMatrix::field_type
typename X::field_type field_type
The field type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:49
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