FlexibleSolver_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019, 2020 SINTEF Digital, Mathematics and Cybernetics.
3 Copyright 2020 Equinor.
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
22#define OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
23
24#include <opm/common/ErrorMacros.hpp>
25#include <opm/common/TimingMacros.hpp>
34
35#if HAVE_AVX2_EXTENSION
37#endif
38
39#include <dune/common/fmatrix.hh>
40#include <dune/istl/bcrsmatrix.hh>
41#include <dune/istl/solvers.hh>
42#include <dune/istl/umfpack.hh>
43#include <dune/istl/owneroverlapcopy.hh>
44#include <dune/istl/paamg/pinfo.hh>
45#include <type_traits>
46
47#if HAVE_CUDA
48#if USE_HIP
49#include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
50#else
52#endif
53#endif
54
55namespace Dune
56{
58 template <class Operator>
60 FlexibleSolver(Operator& op,
61 const Opm::PropertyTree& prm,
62 const std::function<VectorType()>& weightsCalculator,
63 std::size_t pressureIndex)
64 {
65 init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
66 pressureIndex);
67 }
68
70 template <class Operator>
71 template <class Comm>
73 FlexibleSolver(Operator& op,
74 const Comm& comm,
75 const Opm::PropertyTree& prm,
76 const std::function<VectorType()>& weightsCalculator,
77 std::size_t pressureIndex)
78 {
79 init(op, comm, prm, weightsCalculator, pressureIndex);
80 }
81
82 template <class Operator>
83 void
86 {
87 if (direct_solver_) {
88 recreateDirectSolver();
89 }
90 linsolver_->apply(x, rhs, res);
91 }
92
93 template <class Operator>
94 void
96 apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
97 {
98 if (direct_solver_) {
99 recreateDirectSolver();
100 }
101 linsolver_->apply(x, rhs, reduction, res);
102 }
103
105 template <class Operator>
106 auto
109 {
110 return *preconditioner_;
111 }
112
113 template <class Operator>
114 Dune::SolverCategory::Category
116 category() const
117 {
118 return linearoperator_for_solver_->category();
119 }
120
121 // Machinery for making sequential or parallel operators/preconditioners/scalar products.
122 template <class Operator>
123 template <class Comm>
124 void
126 initOpPrecSp(Operator& op,
127 const Opm::PropertyTree& prm,
128 const std::function<VectorType()> weightsCalculator,
129 const Comm& comm,
130 std::size_t pressureIndex)
131 {
132 // Parallel case.
133 linearoperator_for_solver_ = &op;
134 auto child = prm.get_child_optional("preconditioner");
136 child ? *child : Opm::PropertyTree(),
137 weightsCalculator,
138 comm,
139 pressureIndex);
140 scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
141 }
142
143 template <class Operator>
144 void
145 FlexibleSolver<Operator>::
146 initOpPrecSp(Operator& op,
147 const Opm::PropertyTree& prm,
148 const std::function<VectorType()> weightsCalculator,
149 const Dune::Amg::SequentialInformation&,
150 std::size_t pressureIndex)
151 {
152 // Sequential case.
153 linearoperator_for_solver_ = &op;
154 auto child = prm.get_child_optional("preconditioner");
156 child ? *child : Opm::PropertyTree(),
157 weightsCalculator,
158 pressureIndex);
159 scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
160 }
161
162
163 template <class Operator>
164 template <class Comm>
165 void
166 FlexibleSolver<Operator>::
167 initSolver(const Opm::PropertyTree& prm, const Comm& comm)
168 {
169 const bool is_iorank = comm.communicator().rank() == 0;
170 const double tol = prm.get<double>("tol", 1e-2);
171 const int maxiter = prm.get<int>("maxiter", 200);
172 const int verbosity = is_iorank ? prm.get<int>("verbosity", 0) : 0;
173 const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
174
175
176 // make sure it is nullptr at the start (used for error checking in the end).
177 // while the linSolver_ is initalized as a nullptr, we want to make sure it is reset here,
178 // simply because we will check if it is at the end of this function and need to keep this invariant
179 // (that it is nullptr at the start of this function).
180 linsolver_.reset();
181 if (solver_type == "bicgstab") {
182 linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
183 *scalarproduct_,
184 *preconditioner_,
185 tol, // desired residual reduction factor
186 maxiter, // maximum number of iterations
187 verbosity);
188#if HAVE_AVX2_EXTENSION
189 } else if (solver_type == "mixed-bicgstab") {
190 if constexpr (Opm::is_gpu_operator_v<Operator>) {
191 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for GPU operatorsg");
192 } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
193 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for single precision.");
194 } else {
195 const std::string prec_type = prm.get<std::string>("preconditioner.type", "error");
196 bool use_mixed_dilu= (prec_type=="mixed-dilu");
197 using MatrixType = decltype(linearoperator_for_solver_->getmat());
198 linsolver_ = std::make_shared<Dune::MixedSolver<VectorType,MatrixType>>(
199 linearoperator_for_solver_->getmat(),
200 tol,
201 maxiter,
202 use_mixed_dilu
203 );
204 }
205#endif
206 } else if (solver_type == "loopsolver") {
207 linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
208 *scalarproduct_,
209 *preconditioner_,
210 tol, // desired residual reduction factor
211 maxiter, // maximum number of iterations
212 verbosity);
213 } else if (solver_type == "gmres") {
214 int restart = prm.get<int>("restart", 15);
215 linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
216 *scalarproduct_,
217 *preconditioner_,
218 tol,// desired residual reduction factor
219 restart,
220 maxiter, // maximum number of iterations
221 verbosity);
222 } else {
223 if constexpr (!Opm::is_gpu_operator_v<Operator>) {
224 if (solver_type == "flexgmres") {
225 int restart = prm.get<int>("restart", 15);
226 linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
227 *scalarproduct_,
228 *preconditioner_,
229 tol,// desired residual reduction factor
230 restart,
231 maxiter, // maximum number of iterations
232 verbosity);
233#if HAVE_SUITESPARSE_UMFPACK
234 } else if (solver_type == "umfpack") {
235 if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
236 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
237 } else {
238 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
239 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
240 direct_solver_ = true;
241 }
242#endif
243#if HAVE_CUDA
244 } else if (solver_type == "gpubicgstab") {
246 *linearoperator_for_solver_,
247 *scalarproduct_,
248 preconditioner_,
249 tol, // desired residual reduction factor
250 maxiter, // maximum number of iterations
251 verbosity,
252 comm));
253 #endif
254 }
255 }
256 }
257 if (!linsolver_) {
258 OPM_THROW(std::invalid_argument,
259 "Properties: Solver " + solver_type + " not known.");
260 }
261 }
262
263
264 // For now, the only direct solver we support is UMFPACK from SuiteSparse.
265 // When the matrix is updated (keeping sparsity pattern) it is possible to
266 // exploit separation of symbolic and numeric factorization, but we do not
267 // do so at this point. For complete generality, the solver abstract class
268 // Dune::InverseOperator<> should be extended with an update() function.
269 template <class Operator>
270 void
271 FlexibleSolver<Operator>::
272 recreateDirectSolver()
273 {
274#if HAVE_SUITESPARSE_UMFPACK
275 if constexpr (!Opm::is_gpu_operator_v<Operator>) {
276 if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
277 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
278 } else {
279 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
280 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
281 }
282 }
283#else
284 OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
285#endif
286 }
287
288
289 // Main initialization routine.
290 // Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
291 template <class Operator>
292 template <class Comm>
293 void
294 FlexibleSolver<Operator>::
295 init(Operator& op,
296 const Comm& comm,
297 const Opm::PropertyTree& prm,
298 const std::function<VectorType()> weightsCalculator,
299 std::size_t pressureIndex)
300 {
301 initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
302 initSolver(prm, comm);
303 }
304
305} // namespace Dune
306
307
308// Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
309
310// Vectors and matrices.
311template<class Scalar, int N>
312using BV = Dune::BlockVector<Dune::FieldVector<Scalar, N>>;
313template<class Scalar, int N>
314using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<Scalar, N, N>>;
315
316// Sequential operators.
317template<class Scalar, int N>
318using SeqOpM = Dune::MatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>>;
319template<class Scalar, int N>
321
322#if HAVE_MPI
323
324// Parallel communicator and operators.
325using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
326template<class Scalar, int N>
328template<class Scalar, int N>
330template<class Scalar, int N>
331using ParOpD = Dune::OverlappingSchwarzOperator<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, Comm>;
332
333// Note: we must instantiate the constructor that is a template.
334// This is only needed in the parallel case, since otherwise the Comm type is
335// not a template argument but always SequentialInformation.
336
337#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
338 template class Dune::FlexibleSolver<__VA_ARGS__>; \
339 template Dune::FlexibleSolver<__VA_ARGS__>:: \
340 FlexibleSolver(__VA_ARGS__& op, \
341 const Comm& comm, \
342 const Opm::PropertyTree& prm, \
343 const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
344 std::size_t pressureIndex);
345
346#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
347 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
348 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
349 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
350 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
351 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
352
353#else // HAVE_MPI
354
355#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
356 template class Dune::FlexibleSolver<__VA_ARGS__>;
357
358#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
359 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
360 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
361
362#endif // HAVE_MPI
363
364#endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
Dune::OverlappingSchwarzOperator< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N >, Comm > ParOpD
Definition: FlexibleSolver_impl.hpp:331
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, N, N > > OBM
Definition: FlexibleSolver_impl.hpp:314
Dune::BlockVector< Dune::FieldVector< Scalar, N > > BV
Definition: FlexibleSolver_impl.hpp:312
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: FlexibleSolver_impl.hpp:325
Dune::MatrixAdapter< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N > > SeqOpM
Definition: FlexibleSolver_impl.hpp:318
Definition: FlexibleSolver.hpp:45
virtual Dune::SolverCategory::Category category() const override
Definition: FlexibleSolver_impl.hpp:116
virtual void apply(VectorType &x, VectorType &rhs, Dune::InverseOperatorResult &res) override
Definition: FlexibleSolver_impl.hpp:85
typename Operator::domain_type VectorType
Definition: FlexibleSolver.hpp:47
FlexibleSolver(Operator &op, const Opm::PropertyTree &prm, const std::function< VectorType()> &weightsCalculator, std::size_t pressureIndex)
Create a sequential solver.
Definition: FlexibleSolver_impl.hpp:60
AbstractPrecondType & preconditioner()
Access the contained preconditioner.
Definition: FlexibleSolver_impl.hpp:108
Dune linear operator that assumes ghost rows are ordered after interior rows. Avoids some computation...
Definition: WellOperators.hpp:402
static PrecPtr create(const Operator &op, const PropertyTree &prm, const std::function< Vector()> &weightsCalculator={}, std::size_t pressureIndex=std::numeric_limits< std::size_t >::max())
Definition: PreconditionerFactory_impl.hpp:154
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
T get(const std::string &key) const
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:299
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:225
Wraps a CUDA solver to work with CPU data.
Definition: SolverAdapter.hpp:52
Definition: fvbaseprimaryvariables.hh:161
Dune::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32