Loading...
Searching...
No Matches
GpuSeqILU0.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_GPUSEQILU0_HPP
#define OPM_GPUSEQILU0_HPP
#include <dune/istl/preconditioner.hh>
#include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
#include <opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp>
#include <opm/simulators/linalg/gpuistl/detail/CuMatrixDescription.hpp>
#include <opm/simulators/linalg/gpuistl/detail/CuSparseHandle.hpp>
#include <opm/simulators/linalg/gpuistl/detail/CuSparseResource.hpp>
#include <opm/simulators/linalg/gpuistl/detail/fix_zero_diagonal.hpp>
#include <opm/simulators/linalg/is_gpu_operator.hpp>
namespace Opm::gpuistl
{
template <class M, class X, class Y, int l = 1>
class GpuSeqILU0 : public Dune::PreconditionerWithUpdate<X, Y>
{
public:
using matrix_type = typename std::remove_const<M>::type ;
using domain_type = X;
using range_type = Y;
using field_type = typename X::field_type;
template <typename T = M, typename std::enable_if_t<is_gpu_matrix_v<T>, int> = 0>
GpuSeqILU0(const M& A, field_type w);
template <typename T = M, typename std::enable_if_t<!is_gpu_matrix_v<T>, int> = 0>
GpuSeqILU0(const M& A, field_type w);
virtual void pre(X& x, Y& b) override;
virtual void apply(X& v, const Y& d) override;
virtual void post(X& x) override;
virtual Dune::SolverCategory::Category category() const override;
virtual void update() override;
static constexpr bool shouldCallPre()
{
return false;
}
static constexpr bool shouldCallPost()
{
return false;
}
virtual bool hasPerfectUpdate() const override {
return true;
}
private:
const M& m_underlyingMatrix;
field_type m_w;
GpuSparseMatrixWrapper<field_type> m_LU;
GpuVector<field_type> m_temporaryStorage;
detail::GpuSparseMatrixDescriptionPtr m_descriptionL;
detail::GpuSparseMatrixDescriptionPtr m_descriptionU;
detail::CuSparseResource<bsrsv2Info_t> m_infoL;
detail::CuSparseResource<bsrsv2Info_t> m_infoU;
detail::CuSparseResource<bsrilu02Info_t> m_infoM;
std::unique_ptr<GpuVector<field_type>> m_buffer;
detail::CuSparseHandle& m_cuSparseHandle;
bool m_analysisDone = false;
void analyzeMatrix();
size_t findBufferSize();
void createILU();
void updateILUConfiguration();
};
// Case where matrix is a CPU matrix as input
template <class M, class X, class Y, int l>
template <typename T, typename std::enable_if_t<!is_gpu_matrix_v<T>, int>>
GpuSeqILU0<M, X, Y, l>::GpuSeqILU0(const M& A, field_type w)
: m_underlyingMatrix(A)
, m_w(w)
, m_LU(GpuSparseMatrixWrapper<field_type>::fromMatrix(detail::makeMatrixWithNonzeroDiagonal(A)))
, m_temporaryStorage(m_LU.N() * m_LU.blockSize())
, m_descriptionL(detail::createLowerDiagonalDescription())
, m_descriptionU(detail::createUpperDiagonalDescription())
, m_cuSparseHandle(detail::CuSparseHandle::getInstance())
{
// Some sanity check
OPM_ERROR_IF(A.N() != m_LU.N(),
fmt::format("CuSparse matrix not same size as DUNE matrix. {} vs {}.", m_LU.N(), A.N()));
OPM_ERROR_IF(
A[0][0].N() != m_LU.blockSize(),
fmt::format("CuSparse matrix not same blocksize as DUNE matrix. {} vs {}.", m_LU.blockSize(), A[0][0].N()));
OPM_ERROR_IF(
A.N() * A[0][0].N() != m_LU.dim(),
fmt::format("CuSparse matrix not same dimension as DUNE matrix. {} vs {}.", m_LU.dim(), A.N() * A[0][0].N()));
OPM_ERROR_IF(A.nonzeroes() != m_LU.nonzeroes(),
fmt::format("CuSparse matrix not same number of non zeroes as DUNE matrix. {} vs {}. ",
m_LU.nonzeroes(),
A.nonzeroes()));
updateILUConfiguration();
}
// Case where matrix is a GPU matrix as input
template <class M, class X, class Y, int l>
template <typename T, typename std::enable_if_t<is_gpu_matrix_v<T>, int>>
GpuSeqILU0<M, X, Y, l>::GpuSeqILU0(const M& A, field_type w)
: m_underlyingMatrix(A)
, m_w(w)
, m_LU(A)
, m_temporaryStorage(m_LU.N() * m_LU.blockSize())
, m_descriptionL(detail::createLowerDiagonalDescription())
, m_descriptionU(detail::createUpperDiagonalDescription())
, m_cuSparseHandle(detail::CuSparseHandle::getInstance())
{
// Some sanity check
OPM_ERROR_IF(A.N() != m_LU.N(),
fmt::format("CuSparse matrix not same size as DUNE matrix. {} vs {}.", m_LU.N(), A.N()));
OPM_ERROR_IF(A.nonzeroes() != m_LU.nonzeroes(),
fmt::format("CuSparse matrix not same number of non zeroes as DUNE matrix. {} vs {}. ",
m_LU.nonzeroes(),
A.nonzeroes()));
updateILUConfiguration();
}
} // end namespace Opm::gpuistl
#endif
Dune::PreconditionerWithUpdate
Interface class adding the update() method to the preconditioner interface.
Definition PreconditionerWithUpdate.hpp:32
Opm::gpuistl::GpuSeqILU0::matrix_type
typename std::remove_const< M >::type matrix_type
The matrix type the preconditioner is for.
Definition GpuSeqILU0.hpp:55
Opm::gpuistl::GpuSeqILU0::update
virtual void update() override
Updates the matrix data.
Definition GpuSeqILU0.cpp:125
Opm::gpuistl::GpuSeqILU0::range_type
Y range_type
The range type of the preconditioner.
Definition GpuSeqILU0.hpp:59
Opm::gpuistl::GpuSeqILU0::pre
virtual void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition GpuSeqILU0.cpp:49
Opm::gpuistl::GpuSeqILU0::field_type
typename X::field_type field_type
The field type of the preconditioner.
Definition GpuSeqILU0.hpp:61
Opm::gpuistl::GpuSeqILU0::post
virtual void post(X &x) override
Post processing.
Definition GpuSeqILU0.cpp:112
Opm::gpuistl::GpuSeqILU0::apply
virtual void apply(X &v, const Y &d) override
Apply the preconditoner.
Definition GpuSeqILU0.cpp:55
Opm::gpuistl::GpuSeqILU0::domain_type
X domain_type
The domain type of the preconditioner.
Definition GpuSeqILU0.hpp:57
Opm::gpuistl::GpuSeqILU0::shouldCallPost
static constexpr bool shouldCallPost()
Definition GpuSeqILU0.hpp:108
Opm::gpuistl::GpuSeqILU0::shouldCallPre
static constexpr bool shouldCallPre()
Definition GpuSeqILU0.hpp:102
Opm::gpuistl::GpuSeqILU0::category
virtual Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category).
Definition GpuSeqILU0.cpp:118
Opm::gpuistl::GpuSeqILU0::GpuSeqILU0
GpuSeqILU0(const M &A, field_type w)
Constructor.
Definition GpuSeqILU0.hpp:155
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::GpuSparseMatrixWrapper::nonzeroes
size_t nonzeroes() const
nonzeroes behaves as the Dune::BCRSMatrix::nonzeros() function and returns the number of non zero blo...
Definition GpuSparseMatrixWrapper.hpp:237
Opm::gpuistl::GpuSparseMatrixWrapper::N
size_t N() const
N returns the number of rows (which is equal to the number of columns).
Definition GpuSparseMatrixWrapper.hpp:228
Opm::gpuistl::detail
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading.
Definition autotuner.hpp:30
Opm::gpuistl::detail::GpuSparseMatrixDescriptionPtr
std::shared_ptr< CuSparseResource< cusparseMatDescr_t > > GpuSparseMatrixDescriptionPtr
Pointer to GpuSparseMatrixDescription holder.
Definition CuMatrixDescription.hpp:35