opm-simulators
Loading...
Searching...
No Matches
PreconditionerCPUMatrixToGPUMatrix.hpp
1/*
2 Copyright 2025 Equinor ASA
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_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
20#define OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
21#include "opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp"
22#include <dune/istl/preconditioner.hh>
23#include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
24#include <opm/simulators/linalg/gpuistl/GpuVector.hpp>
25#include <opm/simulators/linalg/gpuistl/PreconditionerHolder.hpp>
26#include <opm/simulators/linalg/gpuistl/detail/preconditioner_should_call_post_pre.hpp>
27
28
29namespace Opm::gpuistl
30{
40template <class X, class Y, class CudaPreconditionerType, class CPUMatrixType>
41class PreconditionerCPUMatrixToGPUMatrix : public Dune::PreconditionerWithUpdate<X, Y>
42{
43public:
45 using domain_type = X;
47 using range_type = Y;
49 using field_type = typename X::field_type;
50
51
52 template <typename... Args>
53 explicit PreconditionerCPUMatrixToGPUMatrix(const CPUMatrixType& A, Args&&... args)
54 : m_cpuMatrix(A)
55 , m_gpuMatrix(GpuSparseMatrixWrapper<field_type>::fromMatrix(A))
56 , m_underlyingPreconditioner(m_gpuMatrix, std::forward<Args>(args)...)
57 {
58 }
59
60
64 void pre([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
65 {
67 "We currently do not support Preconditioner::pre().");
68 }
69
70
74 void apply(X& v, const Y& d) override
75 {
76 m_underlyingPreconditioner.apply(v, d);
77 }
78
79
83 void post([[maybe_unused]] X& x) override
84 {
86 "We currently do not support Preconditioner::post().");
87 }
88
89
91 Dune::SolverCategory::Category category() const override
92 {
93 return m_underlyingPreconditioner.category();
94 }
95
98 void update() override
99 {
100 m_gpuMatrix.updateNonzeroValues(m_cpuMatrix, true);
101 m_underlyingPreconditioner.update();
102 }
103
104 static constexpr bool shouldCallPre()
105 {
107 }
108 static constexpr bool shouldCallPost()
109 {
111 }
112
113 virtual bool hasPerfectUpdate() const override
114 {
115 return m_underlyingPreconditioner.hasPerfectUpdate();
116 }
117
118private:
119 const CPUMatrixType& m_cpuMatrix;
120 GpuSparseMatrixWrapper<field_type> m_gpuMatrix;
121
123 CudaPreconditionerType m_underlyingPreconditioner;
124};
125} // end namespace Opm::gpuistl
126
127#endif // OPM_PRECONDITIONERCPUMATRIXTOGPUMATRIX_HPP
Interface class adding the update() method to the preconditioner interface.
Definition PreconditionerWithUpdate.hpp:32
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition GpuSparseMatrixWrapper.hpp:61
void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:64
void apply(X &v, const Y &d) override
Apply the preconditoner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:74
void post(X &x) override
Clean up.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:83
Dune::SolverCategory::Category category() const override
Category of the preconditioner (see SolverCategory::Category).
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:91
void update() override
Copies the CPU matrix to the GPU matrix then calls the GPU preconditioner update function.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:98
Y range_type
The range type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:47
X domain_type
The domain type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:45
typename X::field_type field_type
The field type of the preconditioner.
Definition PreconditionerCPUMatrixToGPUMatrix.hpp:49
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
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