MTL4SolverBase.h 4.41 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
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


/** \file MTL4SolverBase.h */

#ifndef AMDIS_MTL4SOLVER_BASE_H
#define AMDIS_MTL4SOLVER_BASE_H

#include "MatrixStreams.h"
#include "Timer.h"
#include <iostream>
#include <boost/mpl/bool.hpp>
#include <boost/numeric/mtl/utility/is_distributed.hpp>

#ifdef HAVE_PARALLEL_MTL4
  #include <boost/numeric/mtl/par/distribution.hpp>
#endif

namespace AMDiS {

  template< typename MatrixType, typename VectorType, typename Runner >
  class MTL4SolverBase
  {
  public:
    /// Constructor
    MTL4SolverBase(LinearSolver* oem_)
    : runner(oem_),
46
47
      oem(*oem_)/*,
      matrix(0,0)*/
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
100
101
102
103
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
    {}
    
  protected:    

    /// init systemmatrix depending on Mapper parameters.
    template< typename MatrixT, typename Mapper >
    typename boost::disable_if< mtl::traits::is_distributed< MatrixT >, void >::type
    initMatrix(MatrixT& m, Mapper& mapper)
    {
      m.change_dim(mapper.getNumRows(), mapper.getNumCols());
      set_to_zero(m);
    }

    
#ifdef HAVE_PARALLEL_MTL4
    /// init systemmatrix depending on Mapper parameters,
    /// specialized for distributed matrices
    template< typename MatrixT >
    typename boost::enable_if< mtl::traits::is_distributed< MatrixT >, void >::type
    initMatrix(MatrixT& m, ParallelMapper& mapper)
    {
      mtl::par::block_distribution dist(mapper.getNumRows());
      dist.setup_from_local_size(mapper.getMap().getLocalDofs());
      m.change_dim(0, 0);
      m.init_distribution(dist, dist, mapper.getNumRows(), mapper.getNumRows());
      set_to_zero(m);
    }
#endif


    /// init MTL-vector depending on Mapper parameters.
    template< typename VectorT >
    typename boost::disable_if< mtl::traits::is_distributed< VectorT >, void >::type
    initVector(VectorT& v)
    {
      v.change_dim(num_rows(matrix));
      set_to_zero(v);
    }
    

    /// init MTL-vector depending on Mapper parameters,
    /// specialized for distributed matrices
    template< typename VectorT >
    typename boost::enable_if< mtl::traits::is_distributed< VectorT >, void >::type
    initVector(VectorT& v)
    {
      v.init_distribution(row_distribution(matrix), num_rows(matrix));
      set_to_zero(v);
    }
    

    /// Realization of solve(A, x, b) to solve the linear system \f$ Ax=b \f$.
    /// 1. transfer matrix and vectors to MTL datatypes
    /// 2. call Runner::solve(A, x, b)
    /// 3. transfer solution back to AMDiS \ref SystemVector
    template< typename MatrixT, typename VectorT, typename Mapper >
    int solve(const MatrixT& A, VectorT& x, VectorT& b, Mapper& mapper,
	      bool createMatrixData,
	      bool storeMatrixData) 
    { FUNCNAME("MTL4SolverBase::solve()");

      #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      MPI::COMM_WORLD.Barrier();
      #endif
      Timer t;
      if (createMatrixData) {
	initMatrix(matrix, mapper);

        MatMap< const MatrixT, Mapper > matMap(A, mapper);
        matrix << matMap;
  
	runner.init(A, matrix);
      }

      VectorType mtl_x;
      initVector(mtl_x);
      VecMap< VectorT, Mapper > xVecMap(x, mapper);
      mtl_x << xVecMap;

      VectorType mtl_b;
      initVector(mtl_b);
      VecMap< VectorT, Mapper > bVecMap(b, mapper);
      mtl_b << bVecMap;
   
      INFO(oem.getInfo(), 8)("fill MTL4 matrix needed %.5f seconds\n", t.elapsed());

      int error = runner.solve(matrix ,mtl_x, mtl_b);
      
      mtl_x >> xVecMap;

      runner.exit();

      return error;
    }
    
    OEMRunner* getRunner()
    {
      return &runner;
    }
    
    OEMPreconditioner* getLeftPrecon()
    {
      return runner.getLeftPrecon();
    }
    
    OEMPreconditioner* getRightPrecon()
    {
      return runner.getRightPrecon();
    }
    
  protected:
    Runner runner;
    LinearSolver& oem;
    
  private:
    MatrixType matrix;
  };
  
} // end namespace AMDiS

#endif // AMDIS_MTL4SOLVER_BASE_H