opm-simulators
Loading...
Searching...
No Matches
ISTLSolverRuntimeOptionProxy.hpp
1/*
2 Copyright 2025 Equinor ASA
3
4 This file is part of the Open Porous Media project (OPM).
5 OPM is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 OPM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with OPM. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#ifndef OPM_ISTLSOLVERRUNTIMEOPTIONPROXY_HEADER_INCLUDED
18#define OPM_ISTLSOLVERRUNTIMEOPTIONPROXY_HEADER_INCLUDED
19
20#include "opm/simulators/linalg/FlowLinearSolverParameters.hpp"
21#include <opm/simulators/linalg/setupPropertyTree.hpp>
22#include <opm/simulators/linalg/AbstractISTLSolver.hpp>
23#include <opm/simulators/linalg/ISTLSolver.hpp>
24#if COMPILE_GPU_BRIDGE
25#include <opm/simulators/linalg/ISTLSolverGpuBridge.hpp>
26#endif
27
28#if USE_HIP
29#include <opm/simulators/linalg/gpuistl_hip/ISTLSolverGPUISTL.hpp>
30#elif HAVE_CUDA
31#include <opm/simulators/linalg/gpuistl/ISTLSolverGPUISTL.hpp>
32#endif
33
34#include <fmt/format.h>
35
36namespace Opm
37{
38
42template <class TypeTag>
44{
45public:
49 using Matrix = typename SparseMatrixAdapter::IstlMatrix;
50
51#if HAVE_MPI
52 using CommunicationType = Dune::OwnerOverlapCopyCommunication<int, int>;
53#else
54 using CommunicationType = Dune::Communication<int>;
55#endif
56
57
58 static void registerParameters()
59 {
60 FlowLinearSolverParameters::registerParameters();
61 }
62
70 ISTLSolverRuntimeOptionProxy(const Simulator& simulator,
71 const FlowLinearSolverParameters& parameters,
72 bool forceSerial = false)
73 {
74 createSolver(simulator, parameters, forceSerial);
75 }
76
79 explicit ISTLSolverRuntimeOptionProxy(const Simulator& simulator)
80 {
81 createSolver(simulator);
82 }
83
84
85 void eraseMatrix() override
86 {
87 istlSolver_->eraseMatrix();
88 }
89
90 void setActiveSolver(int num) override
91 {
92 istlSolver_->setActiveSolver(num);
93 }
94
95 int numAvailableSolvers() const override
96 {
97 return istlSolver_->numAvailableSolvers();
98 }
99
100 void prepare(const SparseMatrixAdapter& M, Vector& b) override
101 {
102 istlSolver_->prepare(M, b);
103 }
104
105 void prepare(const Matrix& M, Vector& b) override
106 {
107 istlSolver_->prepare(M, b);
108 }
109
110 void setResidual(Vector& b) override
111 {
112 istlSolver_->setResidual(b);
113 }
114
115 void getResidual(Vector& b) const override
116 {
117 istlSolver_->getResidual(b);
118 }
119
120 void setMatrix(const SparseMatrixAdapter& M) override
121 {
122 istlSolver_->setMatrix(M);
123 }
124
125 bool solve(Vector& x) override
126 {
127 return istlSolver_->solve(x);
128 }
129
130 int iterations() const override
131 {
132 return istlSolver_->iterations();
133 }
134
135 const CommunicationType* comm() const override
136 {
137 return istlSolver_->comm();
138 }
139
140 int getSolveCount() const override
141 {
142 return istlSolver_->getSolveCount();
143 }
144
145private:
146 std::unique_ptr<AbstractISTLSolver<TypeTag>> istlSolver_;
147
148
149 template <class... Args>
150 void createSolver(const Simulator& simulator, Args&&... args)
151 {
152 const auto backend = Parameters::linearSolverAcceleratorTypeFromCLI();
153 if (backend == Parameters::LinearSolverAcceleratorType::CPU) {
154 // Note that for now we keep the old behavior of using the bridge solver if it is available.
155#if COMPILE_GPU_BRIDGE
156 istlSolver_ = std::make_unique<ISTLSolverGpuBridge<TypeTag>>(simulator, std::forward<Args>(args)...);
157#else
158 istlSolver_ = std::make_unique<ISTLSolver<TypeTag>>(simulator, std::forward<Args>(args)...);
159#endif
160 }
161#if HAVE_CUDA
162 else if (backend == Parameters::LinearSolverAcceleratorType::GPU) {
163 istlSolver_ = std::make_unique<gpuistl::ISTLSolverGPUISTL<TypeTag>>(simulator, std::forward<Args>(args)...);
164 }
165#endif
166 else {
167 // If we reach here, it means the backend is not supported. This could be because we have added a third backend
168 // that we need to handle. A user error would be handled in the linearSolverAcceleratorTypeFromString function called above.
169 OPM_THROW(std::invalid_argument, fmt::format(fmt::runtime("Unknown backend: {}"), Parameters::toString(backend)));
170 }
171 }
172};
173} // namespace Opm
174
175#endif
Abstract interface for ISTL solvers.
Definition AbstractISTLSolver.hpp:45
ISTLSolverRuntimeOptionProxy(const Simulator &simulator, const FlowLinearSolverParameters &parameters, bool forceSerial=false)
Construct a system solver.
Definition ISTLSolverRuntimeOptionProxy.hpp:70
int numAvailableSolvers() const override
Get the number of available solvers.
Definition ISTLSolverRuntimeOptionProxy.hpp:95
void setResidual(Vector &b) override
Set the residual vector.
Definition ISTLSolverRuntimeOptionProxy.hpp:110
void getResidual(Vector &b) const override
Get the residual vector.
Definition ISTLSolverRuntimeOptionProxy.hpp:115
ISTLSolverRuntimeOptionProxy(const Simulator &simulator)
Construct a system solver.
Definition ISTLSolverRuntimeOptionProxy.hpp:79
void setActiveSolver(int num) override
Set the active solver by its index.
Definition ISTLSolverRuntimeOptionProxy.hpp:90
int getSolveCount() const override
Get the count of how many times the solver has been called.
Definition ISTLSolverRuntimeOptionProxy.hpp:140
const CommunicationType * comm() const override
Get the communication object used by the solver.
Definition ISTLSolverRuntimeOptionProxy.hpp:135
bool solve(Vector &x) override
Solve the system of equations Ax = b.
Definition ISTLSolverRuntimeOptionProxy.hpp:125
void setMatrix(const SparseMatrixAdapter &M) override
Set the matrix for the solver.
Definition ISTLSolverRuntimeOptionProxy.hpp:120
void prepare(const SparseMatrixAdapter &M, Vector &b) override
Prepare the solver with the given sparse matrix and right-hand side vector.
Definition ISTLSolverRuntimeOptionProxy.hpp:100
int iterations() const override
Get the number of iterations used in the last solve.
Definition ISTLSolverRuntimeOptionProxy.hpp:130
void eraseMatrix() override
Signals that the memory for the matrix internally in the solver could be erased.
Definition ISTLSolverRuntimeOptionProxy.hpp:85
void prepare(const Matrix &M, Vector &b) override
Prepare the solver with the given matrix and right-hand side vector.
Definition ISTLSolverRuntimeOptionProxy.hpp:105
Manages the initializing and running of time dependent problems.
Definition simulator.hh:84
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:45
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition propertysystem.hh:233
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition FlowLinearSolverParameters.hpp:98