hypre.hpp 5.86 KB
Newer Older
1
#pragma once
2

Praetorius, Simon's avatar
Praetorius, Simon committed
3
#if AMDIS_HAS_HYPRE && HAVE_MPI
4

5
6
7
#include <mpi.h>
#include <HYPRE.h>
#include <HYPRE_parcsr_ls.h>
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

namespace mtl
{
  class HypreMatrix
  {
  public:
    HypreMatrix() : initialized(false) {}

    template<typename Matrix>
    HypreMatrix(const Matrix& matrix)
    {
      init(matrix);
    }

    template<typename Matrix>
    void init(const Matrix& matrix)
    {
      MPI_Comm comm = MPI_COMM_WORLD;
      int ilower(0);
      int jlower(ilower);
      int nRows(num_rows(matrix));
      int nCols(num_cols(matrix));
      int iupper(ilower + nRows-1);
      int jupper(jlower + nCols-1);
      HYPRE_IJMatrixCreate(comm, ilower, iupper, jlower, jupper, &ij_matrix);
      HYPRE_IJMatrixCreate(comm, ilower, iupper, jlower, jupper, &ij_matrix);
      HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR);
      HYPRE_IJMatrixInitialize(ij_matrix);
      int* ncols = new int[nRows];
      int* rows = new int[nRows];
      int nnz(0);
      for(int i(0); i < nRows; ++i)
      {
        ncols[i] = matrix.nnz_local(i);
        rows[i] = i;
        nnz += ncols[i];
      }
      int* cols = new int[nnz];
      const typename Matrix::size_type* minors = matrix.address_minor();
      for(int i(0); i < nnz; ++i)
      {
        cols[i] = minors[i];
      }
      const double* values = matrix.address_data();
      HYPRE_IJMatrixSetValues(ij_matrix, nRows, ncols, rows, cols, values);
      HYPRE_IJMatrixAssemble(ij_matrix);
      delete [] ncols;
      delete [] rows;
      delete [] cols;

      initialized = true;
    }

    ~HypreMatrix()
    {
      if(initialized)
        HYPRE_IJMatrixDestroy(ij_matrix);
      initialized = false;
    }

    inline operator HYPRE_IJMatrix()
    {
      return ij_matrix;
    }

  private:
    HYPRE_IJMatrix ij_matrix;
    bool initialized;
  };

  class HypreVector
  {
  public:
    template<typename Vector>
    HypreVector(const Vector& mtlVec, int jlower = 0)
      : jlower(jlower)
    {
      int jupper(num_rows(mtlVec)-1 + jlower);
      HYPRE_IJVectorCreate(MPI_COMM_WORLD, jlower, jupper, &vec);
      HYPRE_IJVectorSetObjectType(vec, HYPRE_PARCSR);
      HYPRE_IJVectorInitialize(vec);
      int* vecIndices = new int[num_rows(mtlVec)];
90
      for (std::size_t i(0); i < num_rows(mtlVec); ++i)
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
        vecIndices[i] = i+jlower;
      HYPRE_IJVectorSetValues(vec, num_rows(mtlVec), vecIndices, mtlVec.address_data());
      HYPRE_IJVectorAssemble(vec);
      delete [] vecIndices;
      HYPRE_ParVector x;
      HYPRE_IJVectorGetObject(vec, (void**) &x);
    }

    ~HypreVector()
    {
      HYPRE_IJVectorDestroy(vec);
    }

    inline operator HYPRE_IJVector()
    {
      return vec;
    }

  public:
    int jlower;

  private:
    HYPRE_IJVector vec;
  };

  template<typename Vector>
  void convert(HypreVector& src, Vector& dest)
  {
    int* indices = new int[num_rows(dest)];
120
    for (std::size_t i(0); i < num_rows(dest); ++i)
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
      indices[i] = src.jlower + i;
    HYPRE_IJVectorGetValues(src, num_rows(dest), indices, dest.address_data());
    delete [] indices;
  }

  class HypreParVector
  {
  public:
    template<typename MtlVector>
    HypreParVector(const MtlVector& mtlVec)
      : vec2(mtlVec)
    {
      HYPRE_IJVectorGetObject(vec2, (void**) &vec);
    }

    inline operator HYPRE_ParVector()
    {
      return vec;
    }
    inline HypreVector& getHypreVector()
    {
      return vec2;
    }
    inline const HypreVector& getHypreVector() const
    {
      return vec2;
    }

  private:
    HYPRE_ParVector vec;
    HypreVector vec2;
  };

  class AMGConfigurator
  {
  public:
    enum CycleDirection { DOWN=1, UP=2, COARSEST=3 };

    AMGConfigurator()
      : _debugFlag(0), _printLevel(0), _logLevel(0),
        _tolerance(1.0e-7), _cycle(-1), _finestSweep(1),
        _interpolation(-1), _relaxation(-1), _maxIter(20)
    {
      for(int i(2); i>= 0; --i)
        _sweeps[i] = 1;
    }

    AMGConfigurator& debugFlag(int df)
    {
      _debugFlag = df;
      return *this;
    }
    AMGConfigurator& printLevel(int pl)
    {
      _printLevel = pl;
      return *this;
    }
    AMGConfigurator& logLevel(int ll)
    {
      _logLevel = ll;
      if (ll > 1) throw std::runtime_error("logging > 1 not supported\n2");
      return *this;
    }
    AMGConfigurator& tolerance(double tol)
    {
      _tolerance = tol;
      return *this;
    }
    AMGConfigurator& cycle(int c)
    {
      _cycle = c;
      return *this;
    }
    AMGConfigurator& finestSweep(int nr)
    {
      _finestSweep = nr;
      return *this;
    }

    AMGConfigurator& sweep(CycleDirection dir, int nr)
    {
      _sweeps[dir-1] = nr;
      return *this;
    }

    AMGConfigurator& interpolationType(int type)
    {
      _interpolation = type;
      return *this;
    }
    AMGConfigurator& relaxType(int type)
    {
      _relaxation = type;
      return *this;
    }
    AMGConfigurator& maxIter(int iter)
    {
      _maxIter = iter;
      return *this;
    }

    inline void operator()(HYPRE_Solver& solver) const
    {
      HYPRE_BoomerAMGSetDebugFlag(solver, _debugFlag);
      HYPRE_BoomerAMGSetPrintLevel(solver, _printLevel);
      HYPRE_BoomerAMGSetLogging(solver, _logLevel);
      HYPRE_BoomerAMGSetTol(solver, _tolerance);
      if (_cycle >= 0)
        HYPRE_BoomerAMGSetCycleType(solver, _cycle);
      HYPRE_BoomerAMGSetNumSweeps(solver, _finestSweep);
      for(int i(3); i >= 1; --i)
      {
        HYPRE_BoomerAMGSetCycleNumSweeps(solver, _sweeps[i-1], i);
      }
      if (_interpolation >= 0)
        HYPRE_BoomerAMGSetInterpType(solver, _interpolation);
      if (_relaxation >= 0)
        HYPRE_BoomerAMGSetRelaxType(solver, _relaxation);

      HYPRE_BoomerAMGSetMaxIter(solver, _maxIter);
    }

  protected:
    int _debugFlag;
    int _printLevel;
    int _logLevel;
    double _tolerance;
    int _cycle;
    int _finestSweep;
    int _sweeps[3];
    int _interpolation;
    int _relaxation;
    int _maxIter;
  };

256
} // end namespace mtl
257

Praetorius, Simon's avatar
Praetorius, Simon committed
258
#endif // AMDIS_HAS_HYPRE && HAVE_MPI