opm-simulators
FlexibleSolver_impl.hpp
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>
26 #include <opm/simulators/linalg/matrixblock.hh>
27 #include <opm/simulators/linalg/ilufirstelement.hh>
28 #include <opm/simulators/linalg/FlexibleSolver.hpp>
29 #include <opm/simulators/linalg/PreconditionerFactory.hpp>
30 #include <opm/simulators/linalg/PropertyTree.hpp>
31 #include <opm/simulators/linalg/Preconditioner2InverseOperator.hpp>
32 #include <opm/simulators/linalg/WellOperators.hpp>
33 #include <opm/simulators/linalg/PreconditionerFactoryGPUIncludeWrapper.hpp>
34 #include <opm/simulators/linalg/is_gpu_operator.hpp>
35 
36 #if HAVE_AVX2_EXTENSION
37 #include <opm/simulators/linalg/mixed/wrapper.hpp>
38 #endif
39 
40 #include <dune/common/fmatrix.hh>
41 #include <dune/istl/bcrsmatrix.hh>
42 #include <dune/istl/multitypeblockvector.hh>
43 #include <dune/istl/solvers.hh>
44 #include <dune/istl/umfpack.hh>
45 #include <dune/istl/owneroverlapcopy.hh>
46 #include <dune/istl/paamg/pinfo.hh>
47 #include <type_traits>
48 
49 namespace Opm::detail {
50 template <typename T>
51 struct is_multi_type_block_vector : std::false_type {};
52 template <typename... Args>
53 struct is_multi_type_block_vector<Dune::MultiTypeBlockVector<Args...>> : std::true_type {};
54 template <typename T>
55 inline constexpr bool is_multi_type_block_vector_v = is_multi_type_block_vector<T>::value;
56 } // namespace Opm::detail
57 
58 #if HAVE_CUDA
59 #if USE_HIP
60 #include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
61 #else
62 #include <opm/simulators/linalg/gpuistl/SolverAdapter.hpp>
63 #endif
64 #endif
65 
66 namespace Dune
67 {
69  template <class Operator>
71  FlexibleSolver(Operator& op,
72  const Opm::PropertyTree& prm,
73  const std::function<VectorType()>& weightsCalculator,
74  std::size_t pressureIndex)
75  {
76  init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
77  pressureIndex);
78  }
79 
81  template <class Operator>
82  template <class Comm>
84  FlexibleSolver(Operator& op,
85  const Comm& comm,
86  const Opm::PropertyTree& prm,
87  const std::function<VectorType()>& weightsCalculator,
88  std::size_t pressureIndex)
89  {
90  init(op, comm, prm, weightsCalculator, pressureIndex);
91  }
92 
93  template <class Operator>
94  void
96  apply(VectorType& x, VectorType& rhs, Dune::InverseOperatorResult& res)
97  {
98  if (direct_solver_) {
99  auto* direct_precond = dynamic_cast<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>*>(preconditioner_.get());
100  if (direct_precond && direct_precond->needsRebuild()) {
101  recreateDirectSolver();
102  direct_precond->resetNeedsRebuild();
103  }
104  }
105  linsolver_->apply(x, rhs, res);
106  }
107 
108  template <class Operator>
109  void
110  FlexibleSolver<Operator>::
111  apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
112  {
113  if (direct_solver_) {
114  auto* direct_precond = dynamic_cast<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>*>(preconditioner_.get());
115  if (direct_precond && direct_precond->needsRebuild()) {
116  recreateDirectSolver();
117  direct_precond->resetNeedsRebuild();
118  }
119  }
120  linsolver_->apply(x, rhs, reduction, res);
121  }
122 
124  template <class Operator>
125  auto
128  {
129  return *preconditioner_;
130  }
131 
132  template <class Operator>
133  Dune::SolverCategory::Category
135  category() const
136  {
137  return linearoperator_for_solver_->category();
138  }
139 
140  // Machinery for making sequential or parallel operators/preconditioners/scalar products.
141  template <class Operator>
142  template <class Comm>
143  void
144  FlexibleSolver<Operator>::
145  initOpPrecSp(Operator& op,
146  const Opm::PropertyTree& prm,
147  const std::function<VectorType()> weightsCalculator,
148  const Comm& comm,
149  std::size_t pressureIndex)
150  {
151  // Parallel case.
152  linearoperator_for_solver_ = &op;
153  const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
154  auto child = prm.get_child_optional("preconditioner");
155  if (solver_type == "umfpack") {
156  preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
157  linearoperator_for_solver_->category());
158  } else {
160  child ? *child : Opm::PropertyTree(),
161  weightsCalculator,
162  comm,
163  pressureIndex);
164  }
165  scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
166  }
167 
168  template <class Operator>
169  void
170  FlexibleSolver<Operator>::
171  initOpPrecSp(Operator& op,
172  const Opm::PropertyTree& prm,
173  const std::function<VectorType()> weightsCalculator,
174  const Dune::Amg::SequentialInformation&,
175  std::size_t pressureIndex)
176  {
177  // Sequential case.
178  linearoperator_for_solver_ = &op;
179  const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
180  auto child = prm.get_child_optional("preconditioner");
181  if (solver_type == "umfpack") {
182  preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
183  linearoperator_for_solver_->category());
184  } else {
186  child ? *child : Opm::PropertyTree(),
187  weightsCalculator,
188  pressureIndex);
189  }
190  scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
191  }
192 
193 
194  template <class Operator>
195  template <class Comm>
196  void
197  FlexibleSolver<Operator>::
198  initSolver(const Opm::PropertyTree& prm, const Comm& comm)
199  {
200  const bool is_iorank = comm.communicator().rank() == 0;
201  const double tol = prm.get<double>("tol", 1e-2);
202  const int maxiter = prm.get<int>("maxiter", 200);
203  const int verbosity = is_iorank ? prm.get<int>("verbosity", 0) : 0;
204  const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
205 
206 
207  // make sure it is nullptr at the start (used for error checking in the end).
208  // while the linSolver_ is initalized as a nullptr, we want to make sure it is reset here,
209  // simply because we will check if it is at the end of this function and need to keep this invariant
210  // (that it is nullptr at the start of this function).
211  linsolver_.reset();
212  direct_solver_ = false;
213  if (solver_type == "bicgstab") {
214  linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
215  *scalarproduct_,
216  *preconditioner_,
217  tol, // desired residual reduction factor
218  maxiter, // maximum number of iterations
219  verbosity);
220 #if HAVE_AVX2_EXTENSION
221  } else if (solver_type == "mixed-bicgstab") {
222  if constexpr (Opm::is_gpu_operator_v<Operator>) {
223  OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for GPU operatorsg");
224  } else if constexpr (Opm::detail::is_multi_type_block_vector_v<VectorType>) {
225  OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for multi-type block vectors.");
226  } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
227  OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for single precision.");
228  } else {
229  const std::string prec_type = prm.get<std::string>("preconditioner.type", "error");
230  bool use_mixed_dilu= (prec_type=="mixed-dilu");
231  using MatrixType = decltype(linearoperator_for_solver_->getmat());
232  linsolver_ = std::make_shared<Dune::MixedSolver<VectorType,MatrixType>>(
233  linearoperator_for_solver_->getmat(),
234  tol,
235  maxiter,
236  use_mixed_dilu
237  );
238  }
239 #endif
240  } else if (solver_type == "loopsolver") {
241  linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
242  *scalarproduct_,
243  *preconditioner_,
244  tol, // desired residual reduction factor
245  maxiter, // maximum number of iterations
246  verbosity);
247  } else if (solver_type == "gmres") {
248  int restart = prm.get<int>("restart", 15);
249  linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
250  *scalarproduct_,
251  *preconditioner_,
252  tol,// desired residual reduction factor
253  restart,
254  maxiter, // maximum number of iterations
255  verbosity);
256  } else if (solver_type == "flexgmres") {
257  if constexpr (Opm::is_gpu_operator_v<Operator>) {
258  OPM_THROW(std::invalid_argument, "flexgmres solver not supported for GPU operators.");
259  } else {
260  int restart = prm.get<int>("restart", 15);
261  linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
262  *scalarproduct_,
263  *preconditioner_,
264  tol,// desired residual reduction factor
265  restart,
266  maxiter, // maximum number of iterations
267  verbosity);
268  }
269  } else if (solver_type == "preconditioner2inverseoperator") {
270  if (!preconditioner_) {
271  OPM_THROW(std::invalid_argument,
272  "Properties: Solver preconditioner2inverseoperator requires a preconditioner.");
273  }
274  linsolver_ = std::make_shared<Dune::Preconditioner2InverseOperator<VectorType>>(preconditioner_);
275  } else {
276  if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
277 #if HAVE_SUITESPARSE_UMFPACK
278  if (solver_type == "umfpack") {
279  if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
280  OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
281  } else {
282  using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
283  linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
284  direct_solver_ = true;
285  }
286 #endif
287 #if HAVE_CUDA
288  } else if (solver_type == "gpubicgstab") {
290  *linearoperator_for_solver_,
291  *scalarproduct_,
292  preconditioner_,
293  tol, // desired residual reduction factor
294  maxiter, // maximum number of iterations
295  verbosity,
296  comm));
297  #endif
298  }
299  }
300  }
301  if (!linsolver_) {
302  OPM_THROW(std::invalid_argument,
303  "Properties: Solver " + solver_type + " not known.");
304  }
305  }
306 
307 
308  // For now, the only direct solver we support is UMFPACK from SuiteSparse.
309  // When the matrix is updated (keeping sparsity pattern) it is possible to
310  // exploit separation of symbolic and numeric factorization, but we do not
311  // do so at this point. For complete generality, the solver abstract class
312  // Dune::InverseOperator<> should be extended with an update() function.
313  template <class Operator>
314  void
315  FlexibleSolver<Operator>::
316  recreateDirectSolver()
317  {
318 #if HAVE_SUITESPARSE_UMFPACK
319  if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
320  if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
321  OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
322  } else {
323  using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
324  linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
325  }
326  }
327 #else
328  OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
329 #endif
330  }
331 
332 
333  // Main initialization routine.
334  // Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
335  template <class Operator>
336  template <class Comm>
337  void
338  FlexibleSolver<Operator>::
339  init(Operator& op,
340  const Comm& comm,
341  const Opm::PropertyTree& prm,
342  const std::function<VectorType()> weightsCalculator,
343  std::size_t pressureIndex)
344  {
345  initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
346  initSolver(prm, comm);
347  }
348 
349 } // namespace Dune
350 
351 
352 // Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
353 
354 // Vectors and matrices.
355 template<class Scalar, int N>
356 using BV = Dune::BlockVector<Dune::FieldVector<Scalar, N>>;
357 template<class Scalar, int N>
358 using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<Scalar, N, N>>;
359 
360 // Sequential operators.
361 template<class Scalar, int N>
362 using SeqOpM = Dune::MatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>>;
363 template<class Scalar, int N>
364 using SeqOpW = Opm::WellModelMatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>>;
365 
366 #if HAVE_MPI
367 
368 // Parallel communicator and operators.
369 using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
370 template<class Scalar, int N>
371 using ParOpM = Opm::GhostLastMatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, Comm>;
372 template<class Scalar, int N>
373 using ParOpW = Opm::WellModelGhostLastMatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, true>;
374 template<class Scalar, int N>
375 using ParOpD = Dune::OverlappingSchwarzOperator<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, Comm>;
376 
377 // Note: we must instantiate the constructor that is a template.
378 // This is only needed in the parallel case, since otherwise the Comm type is
379 // not a template argument but always SequentialInformation.
380 
381 #define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
382  template class Dune::FlexibleSolver<__VA_ARGS__>; \
383  template Dune::FlexibleSolver<__VA_ARGS__>:: \
384  FlexibleSolver(__VA_ARGS__& op, \
385  const Comm& comm, \
386  const Opm::PropertyTree& prm, \
387  const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
388  std::size_t pressureIndex);
389 
390 #define INSTANTIATE_FLEXIBLESOLVER(T,N) \
391  INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
392  INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
393  INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
394  INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
395  INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
396 
397 #else // HAVE_MPI
398 
399 #define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
400  template class Dune::FlexibleSolver<__VA_ARGS__>;
401 
402 #define INSTANTIATE_FLEXIBLESOLVER(T,N) \
403  INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
404  INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
405 
406 #endif // HAVE_MPI
407 
408 #endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
Definition: alignedallocator.hh:32
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:298
T get(const std::string &key) const
Retrieve property value given hierarchical property key.
Definition: PropertyTree.cpp:59
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:224
Wraps a CUDA solver to work with CPU data.
Definition: SolverAdapter.hpp:51
Definition: fvbaseprimaryvariables.hh:161
FlexibleSolver(Operator &op, const Opm::PropertyTree &prm, const std::function< VectorType()> &weightsCalculator, std::size_t pressureIndex)
Create a sequential solver.
Definition: FlexibleSolver_impl.hpp:71
A solver class that encapsulates all needed objects for a linear solver (operator, scalar product, iterative solver and preconditioner) and sets them up based on runtime parameters, using the PreconditionerFactory for setting up preconditioners.
Definition: FlexibleSolver.hpp:43
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Retrieve copy of sub tree rooted at node.
Definition: PropertyTree.cpp:90
AbstractPrecondType & preconditioner()
Access the contained preconditioner.
Definition: FlexibleSolver_impl.hpp:127
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())
Create a new serial preconditioner and return a pointer to it.
Definition: PreconditionerFactory_impl.hpp:154
Definition: PreconditionerWithUpdate.hpp:97
Dune linear operator that assumes ghost rows are ordered after interior rows.
Definition: WellOperators.hpp:401
Definition: FlexibleSolver_impl.hpp:51
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38