LinearSolver.hpp 2.16 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.
   **/
24
  template <class Traits, class Runner>
25
  class LinearSolver
26
      : public LinearSolverInterface<Traits>
27
  {
28
    using Self = LinearSolver;
29
30
31
    using Super = LinearSolverInterface<Traits>;

    using Mat = typename Traits::Mat;
32
    using Vec = typename Traits::Vec;
33
    using Comm = typename Traits::Comm;
34

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

45
  public:
46
    /// Constructor
47
48
49
    template <class... Args>
    explicit LinearSolver(std::string prefix, Args&&... args)
      : runner_(prefix, FWD(args)...)
50
    {}
51

52
53
54
    Runner&       runner()       { return runner_; }
    Runner const& runner() const { return runner_; }

55
56
  private:
    /// Implements \ref LinearSolverInterface::solveSystemImpl()
57
    void solveImpl(Mat const& A, Vec& x, Vec const& b, Comm const& comm, SolverInfo& solverInfo) override
58
    {
59
      Dune::Timer t;
60
      if (solverInfo.doCreateMatrixData()) {
61
        // init matrix or wrap block-matrix or ...
62
        runner_.init(A, comm);
63
      }
64

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

68
      int error = runner_.solve(A, x, b, solverInfo);
69
      solverInfo.setError(error);
70

71
      if (!solverInfo.doStoreMatrixData())
72
        runner_.exit();
73
    }
74

75
  private:
76
77
    /// redirect the implementation to a runner. Implementing a \ref RunnerInterface
    Runner runner_;
78
79
80
  };

} // end namespace AMDiS