LinearSolver.hpp 2.02 KB
Newer Older
1
#pragma once
2

3
4
#include <string>

5
6
#include <dune/common/timer.hh>

7
// AMDiS includes
8
9
#include <amdis/CreatorInterface.hpp>
#include <amdis/Output.hpp>
10
#include <amdis/linearalgebra/LinearSolverInterface.hpp>
11
#include <amdis/linearalgebra/SolverInfo.hpp>
12

13
14
namespace AMDiS
{
15
16
17
  /** \ingroup Solver
   *
   * \brief
18
   * Wrapper class for various solvers. These solvers
19
20
   * are parametrized by MatrixType and VectorType. The different
   * algorithms, like krylov subspace methods or other external
21
   * solvers where the backend provides an interface, can be assigned
22
23
   * by different Runner objects.
   **/
Praetorius, Simon's avatar
Praetorius, Simon committed
24
  template <class Mat, class Vec, class Runner>
25
  class LinearSolver
Praetorius, Simon's avatar
Praetorius, Simon committed
26
      : public LinearSolverInterface<Mat,Vec>
27
  {
28
    using Self = LinearSolver;
Praetorius, Simon's avatar
Praetorius, Simon committed
29
    using Super = LinearSolverInterface<Mat,Vec>;
30

31
32
33
34
  public:
    /// A creator to be used instead of the constructor.
    struct Creator : CreatorInterfaceName<Super>
    {
35
      std::unique_ptr<Super> createWithString(std::string prefix) override
36
      {
37
        return std::make_unique<Self>(prefix);
38
39
      }
    };
40

41
  public:
42
    /// Constructor
43
44
45
    template <class... Args>
    explicit LinearSolver(std::string prefix, Args&&... args)
      : runner_(prefix, FWD(args)...)
46
    {}
47

Praetorius, Simon's avatar
Praetorius, Simon committed
48
49
50
51
    Runner* runner() override
    {
      return &runner_;
    }
52

Praetorius, Simon's avatar
Praetorius, Simon committed
53
54
55
  protected:
    /// Implements \ref LinearSolverInterface::solveImpl()
    void solveImpl(Mat const& A, Vec& x, Vec const& b, SolverInfo& solverInfo) override
56
    {
57
      Dune::Timer t;
58
      if (solverInfo.doCreateMatrixData()) {
59
        // init matrix or wrap block-matrix or ...
Praetorius, Simon's avatar
Praetorius, Simon committed
60
        runner_.init(A.matrix());
61
      }
62

Praetorius, Simon's avatar
Praetorius, Simon committed
63
      if (solverInfo.info() > 0)
64
        msg("prepare solver needed {} seconds", t.elapsed());
65

Praetorius, Simon's avatar
Praetorius, Simon committed
66
      int error = runner_.solve(A.matrix(), x.vector(), b.vector(), solverInfo);
67
      solverInfo.setError(error);
68

69
      if (!solverInfo.doStoreMatrixData())
70
        runner_.exit();
71
    }
72

73
  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
74
    /// redirect the implementation to a runner implementing a \ref RunnerInterface
75
    Runner runner_;
76
77
78
  };

} // end namespace AMDiS