ISTLSolverCreator.hpp 8.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include <memory>

#include <dune/common/version.hh>

#include <dune/common/ftraits.hh>
#include <dune/istl/solvers.hh>
#include <dune/istl/solvertype.hh>

#if HAVE_SUITESPARSE_UMFPACK
#include <dune/istl/umfpack.hh>
#endif
#if HAVE_SUITESPARSE_LDL
#include <dune/istl/ldl.hh>
#endif
#if HAVE_SUITESPARSE_SPQR
#include <dune/istl/spqr.hh>
#endif
#if HAVE_SUPERLU
#include <dune/istl/superlu.hh>
#endif

#include <amdis/CreatorMap.hpp>
#include <amdis/Environment.hpp>
#include <amdis/Initfile.hpp>
#include <amdis/Output.hpp>

#include <amdis/linearalgebra/istl/ISTLPreconCreator.hpp>
#include <amdis/linearalgebra/istl/SolverWrapper.hpp>
#include <amdis/linearalgebra/istl/Traits.hpp>
#include <amdis/linearalgebra/istl/precompiled/Solvers.hpp>

namespace AMDiS
{
  namespace tag
  {
    template <class Traits>
    struct solver {};

    // forward declaration
    template <class Traits>
    struct preconditioner;
  }

  /// Base class for solver creators, \see IterativeSolverCreator, \see DirectSolverCreator.
  /**
   * Constructor for solvers.
   *
   * Initfile parameters:
   * - `[SOLVER]->info`:  Information level [0]
   **/
  template <class Traits>
  class ISTLSolverCreatorBase
      : public CreatorInterface<tag::solver<Traits>>
  {
    using X = typename Traits::X;
    using Y = typename Traits::Y;

    std::unique_ptr<tag::solver<Traits>> create() final { return {}; };

  public:
    /// Prepare the solver for the creation
    virtual void init(std::string const& prefix)
    {
      if (Environment::mpiRank() == 0)
        Parameters::get(prefix + "->info", info_);
    }

    virtual std::unique_ptr<Dune::InverseOperator<X,Y>>
    create(typename Traits::M const& A, typename Traits::Comm const& comm) const = 0;

  protected:
    int info_ = 0;
  };


  /// Base solver creator for iterative solvers.
  /**
   * Provides and interface to constructs a linear solver.
   *
   * Initfile parameters:
   * - `[SOLVER]->max iteration`:       Maximal number of solver iterations [500]
   * - `[SOLVER]->relative tolerance`:  Relative break tolerance [1.e-6]
   * - `[SOLVER]->precon`:              Name of the preconditioner
   **/
  template <class Traits>
  class ISTLIterativeSolverCreatorBase
      : public ISTLSolverCreatorBase<Traits>
  {
    using Super = ISTLSolverCreatorBase<Traits>;

    using real_type = typename Dune::FieldTraits<typename Traits::M::field_type>::real_type;

  public:
    void init(std::string const& prefix) override
    {
      Super::init(prefix);

100
101
102
103
      maxIter_ = Parameters::get<int>(prefix + "->maxit").value_or(
                 Parameters::get<int>(prefix + "->max iteration").value_or(maxIter_));
      rTol_ = Parameters::get<real_type>(prefix + "->reduction").value_or(
              Parameters::get<real_type>(prefix + "->relative tolerance").value_or(rTol_));
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

      std::string precon = "default";
      Parameters::get(prefix + "->precon", precon);

      using CreatorMap = CreatorMap<tag::preconditioner<Traits>>;
      auto* creator = CreatorMap::get(precon, prefix + "->precon");
      preconCreator_ = dynamic_cast<ISTLPreconCreatorBase<Traits>*>(creator);
      assert(preconCreator_ != nullptr);
      preconCreator_->init(prefix + "->precon");
    }

  protected:
    template <class Solver, class... Args>
    auto create_impl(typename Traits::M const& mat, typename Traits::Comm const& comm, Args&&... args) const
    {
      auto cat = Dune::SolverCategory::category(comm);
      auto sp = Traits::ScalProdCreator::create(cat, comm);
      auto linOp = Traits::LinOpCreator::create(cat, mat, comm);

      assert(preconCreator_ != nullptr);
      auto precon = preconCreator_->create(mat, comm);
      return std::make_unique<IterativeSolverWrapper<Solver>>(
        std::move(linOp), std::move(sp), std::move(precon), FWD(args)...);
    }

  protected:
    int maxIter_ = 500;
    real_type rTol_ = 1.e-6;
    ISTLPreconCreatorBase<Traits>* preconCreator_ = nullptr;
  };


  /// Default solver creator for iterative solvers
  /**
   * Constructs a linear solver, using the constructor signature
   * `Solver(LinOp, ScalarProd, Precon, rTol, maxIter, info)`
   **/
  template <class Solver, class Traits>
  class IterativeSolverCreator
      : public ISTLIterativeSolverCreatorBase<Traits>
  {
    using Super = ISTLIterativeSolverCreatorBase<Traits>;
    using Interface = typename Traits::Solver;

  public:
    std::unique_ptr<Interface>
    create(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
    {
      return this->template create_impl<Solver>(mat, comm, this->rTol_, this->maxIter_, this->info_);
    }
  };


  /// Solver creator for iterative GMRes-like solvers
  /**
   * Constructs a linear solver, using the constructor signature
   * `Solver(LinOp, ScalarProd, Precon, rTol, restart, maxIter, info)`
   *
   * Initfile parameters:
   * - `[SOLVER]->restart`:  Restart parameter for restarted GMRes solvers [30]
   **/
  template <class Solver, class Traits>
  struct GMResSolverCreator
      : public ISTLIterativeSolverCreatorBase<Traits>
  {
    using Super = ISTLIterativeSolverCreatorBase<Traits>;
    using Interface = typename Traits::Solver;

  public:
    void init(std::string const& prefix) override
    {
      Super::init(prefix);
      Parameters::get(prefix + "->restart", restart_);
    }

    std::unique_ptr<Interface>
    create(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
    {
      return this->template create_impl<Solver>(mat, comm, this->rTol_, restart_, this->maxIter_, this->info_);
    }

  private:
    int restart_ = 30;
  };

  template <class X, class Y, class Traits>
  struct IterativeSolverCreator<Dune::RestartedGMResSolver<X,Y>, Traits>
      : public GMResSolverCreator<Dune::RestartedGMResSolver<X,Y>, Traits>
  {};

#if DUNE_VERSION_GTE(DUNE_ISTL,2,7)
  template <class X, class Y, class Traits>
  struct IterativeSolverCreator<Dune::RestartedFlexibleGMResSolver<X,Y>, Traits>
      : public GMResSolverCreator<Dune::RestartedFlexibleGMResSolver<X,Y>, Traits>
  {};
#endif


  /// Solver creator for iterative CG-like solvers
  /**
   * Constructs a linear solver, using the constructor signature
   * `Solver(LinOp, ScalarProd, Precon, rTol, maxIter, info, restart)`
   *
   * Initfile parameters:
   * - `[SOLVER]->restart`:  Restart parameter for restarted CG solvers [30]
   **/
  template <class Solver, class Traits>
  struct PCGSolverCreator
      : public ISTLIterativeSolverCreatorBase<Traits>
  {
    using Super = ISTLIterativeSolverCreatorBase<Traits>;
    using Interface = typename Traits::Solver;

  public:
    void init(std::string const& prefix) override
    {
      Super::init(prefix);
      Parameters::get(prefix + "->restart", restart_);
    }

    std::unique_ptr<Interface>
    create(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
    {
      return this->template create_impl<Solver>(mat, comm, this->rTol_, this->maxIter_, this->info_, restart_);
    }

  private:
    int restart_ = 30;
  };

  template <class X, class Traits>
  struct IterativeSolverCreator<Dune::GeneralizedPCGSolver<X>, Traits>
      : public PCGSolverCreator<Dune::GeneralizedPCGSolver<X>, Traits>
  {};

#if DUNE_VERSION_GTE(DUNE_ISTL,2,7)
  template <class X, class Traits>
  struct IterativeSolverCreator<Dune::RestartedFCGSolver<X>, Traits>
      : public PCGSolverCreator<Dune::RestartedFCGSolver<X>, Traits>
  {};

  template <class X, class Traits>
  struct IterativeSolverCreator<Dune::CompleteFCGSolver<X>, Traits>
      : public PCGSolverCreator<Dune::CompleteFCGSolver<X>, Traits>
  {};
#endif


  /// Default creator for direct solvers
  /**
   * Constructs a linear solver, using the constructor signature
   * `Solver(Mat, info, reuseVector)`
   *
   * Initfile parameters:
   * - `[SOLVER]->reuse vector`:  Reuse vectors in subsequent calls to apply [true]
   *
   * Note: The reuse parameter is used by SuperLU only, and should be set to false in
   * case of multi-threaded applications using the same solver object in multiple threads.
   **/
  template <class Solver, class Traits>
  struct DirectSolverCreator
      : public ISTLSolverCreatorBase<Traits>
  {
    using Super = ISTLSolverCreatorBase<Traits>;

    void init(std::string const& prefix) override
    {
      Super::init(prefix);
      Parameters::get(prefix + "->reuse vector", reuseVector_);
    }

    std::unique_ptr<typename Traits::Solver>
    create(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
    {
      test_exit(Dune::SolverCategory::category(comm) == Dune::SolverCategory::sequential,
        "Direct solver can be used as sequential solver only.");
      return std::make_unique<Solver>(mat, this->info_, reuseVector_);
    }

  protected:
    bool reuseVector_ = true;
  };


  template <class Solver, class Traits>
  using ISTLSolverCreator = std::conditional_t<Dune::IsDirectSolver<Solver>::value,
    DirectSolverCreator<Solver,Traits>,
    IterativeSolverCreator<Solver,Traits>>;

} // end namespace AMDiS