ParallelDomainBase.h 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

20
21
22
23
/** \file ParallelDomainBase.h */

#ifndef AMDIS_PARALLELDOMAINBASE_H
#define AMDIS_PARALLELDOMAINBASE_H
24
25
26


#include <map>
27
#include <set>
28
29
30
31
#include <vector>

#include "ProblemTimeInterface.h"
#include "ProblemIterationInterface.h"
32
#include "FiniteElemSpace.h"
33
#include "AdaptInfo.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
34
#include "InteriorBoundary.h"
35
36
#include "AMDiS_fwd.h"

37
38
#include "petsc.h"
#include "petscsys.h"
39
#include "petscao.h"
40
41
#include "mpi.h"

42
43
#include "Global.h"

44
45
46
47
namespace AMDiS {

  class ParMetisPartitioner;

48
  class ParallelDomainBase : public ProblemIterationInterface,
49
			     public ProblemTimeInterface
50
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  private:
    /// Defines type for a vector of DOFs.
    typedef std::vector<const DegreeOfFreedom*> DofContainer;

    /// Defines a mapping type from DOFs to rank numbers.
    typedef std::map<const DegreeOfFreedom*, int> DofToRank;

    /// Defines a mapping type from DOFs to a set of rank numbers.
    typedef std::map<const DegreeOfFreedom*, std::set<int> > DofToPartitions;

    /// Defines a mapping type from rank numbers to sets of DOFs.
    typedef std::map<int, DofContainer> RankToDofContainer;

    /// Defines a mapping type from DOF indices to DOF indices.
    typedef std::map<DegreeOfFreedom, DegreeOfFreedom> DofMapping;

67
68
69
    /// Defines a mapping type from DOFs to boolean values.
    typedef std::map<const DegreeOfFreedom*, bool> DofToBool;

Thomas Witkowski's avatar
Thomas Witkowski committed
70
    /// Defines a mapping type from DOF indices to boolean values.
71
    typedef std::map<DegreeOfFreedom, bool> DofIndexToBool;
Thomas Witkowski's avatar
Thomas Witkowski committed
72

Thomas Witkowski's avatar
Thomas Witkowski committed
73
74
75
76
77
78
    /// Defines a mapping type from rank numbers to sets of coordinates.
    typedef std::map<int, std::vector<WorldVector<double> > > RankToCoords;

    /// Forward type (it maps rank numbers to the interior boundary objects).
    typedef InteriorBoundary::RankToBoundMap RankToBoundMap;

Thomas Witkowski's avatar
Thomas Witkowski committed
79
80
    typedef std::map<int, DofContainer> ElementIdxToDofs;

Thomas Witkowski's avatar
Thomas Witkowski committed
81
82
    typedef std::map<const DegreeOfFreedom*, DegreeOfFreedom> DofIndexMap;

83
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
84
    ParallelDomainBase(std::string name,
Thomas Witkowski's avatar
Thomas Witkowski committed
85
86
87
88
		       ProblemIterationInterface *iterationIF,
		       ProblemTimeInterface *timeIF,
		       FiniteElemSpace *feSpace,
		       RefinementManager *refineManager);
89

90
    virtual ~ParallelDomainBase() {}
91

92
    virtual void initParallelization(AdaptInfo *adaptInfo);
93

94
95
    void exitParallelization(AdaptInfo *adaptInfo);

96
97
    void updateDofAdmins();

98
99
100
101
102
103
104
    /** \brief
     * Test, if the mesh consists of macro elements only. The mesh partitioning of
     * the parallelization works for macro meshes only and would fail, if the mesh
     * is already refined in some way. Therefore, this function will exit the program
     * if it finds a non macro element in the mesh.
     */
    void testForMacroMesh();
105
106
107
108
109
110

    /// Set for each element on the partitioning level the number of leaf elements.
    double setElemWeights(AdaptInfo *adaptInfo);

    void partitionMesh(AdaptInfo *adaptInfo);

111
112
113
114
115
    virtual void setTime(AdaptInfo *adaptInfo) 
    {
      if (timeIF) 
	timeIF->setTime(adaptInfo);      
    }
116

117
118
119
120
121
    virtual void initTimestep(AdaptInfo *adaptInfo) 
    {
      if (timeIF) 
	timeIF->initTimestep(adaptInfo);
    }
122

123
124
125
126
127
    virtual void closeTimestep(AdaptInfo *adaptInfo) 
    {
      if (timeIF) 
	timeIF->closeTimestep(adaptInfo);
    }
128

129
    virtual void solveInitialProblem(AdaptInfo *adaptInfo) 
130
    {     
131
132
133
      if (timeIF)
	timeIF->solveInitialProblem(adaptInfo);
    }
134
  
135
136
137
138
139
    virtual void transferInitialSolution(AdaptInfo *adaptInfo) 
    {
      if (timeIF) 
	timeIF->transferInitialSolution(adaptInfo);
    }
140

141
142
    virtual void beginIteration(AdaptInfo *adaptInfo) 
    {
143
144
145
      iterationIF->beginIteration(adaptInfo);
    }

146
    virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION);
147

148
149
    virtual void endIteration(AdaptInfo *adaptInfo) 
    {
150
151
      iterationIF->endIteration(adaptInfo);
    }
152

153
154
    virtual void solve() {}

155
156
157
158
    virtual int getNumProblems() 
    {
      return 0;
    }
159

Thomas Witkowski's avatar
Thomas Witkowski committed
160
    inline virtual std::string getName() 
161
    { 
162
163
164
      return name; 
    }

165
    /// Returns \ref nRankDOFs, the number of DOFs in the rank mesh.
166
167
    int getNumberRankDOFs() 
    {
168
169
170
      return nRankDOFs;
    }

171
172
    void fillPetscMatrix(DOFMatrix *mat, DOFVector<double> *vec);

173
174
    void fillPetscMatrix(Matrix<DOFMatrix*> *mat, SystemVector *vec);

175
176
    void solvePetscMatrix(DOFVector<double> *vec);

177
    void solvePetscMatrix(SystemVector &vec);
178

179
180
181
182
    virtual ProblemStatBase *getProblem(int number = 0) 
    {
      return NULL;
    }
183
184
185
186

    virtual void serialize(std::ostream&) {}

    virtual void deserialize(std::istream&) {}
187
188
189


  protected:
190
191
192
193
    /** \brief
     * Determine the interior boundaries, i.e. boundaries between ranks, and store
     * all information about them in \ref interiorBoundary.
     */
194
    void createInteriorBoundaryInfo(DofContainer& rankDOFs);
195
196
197
198

    /// Removes all macro elements from the mesh that are not part of ranks partition.
    void removeMacroElements();

199
200
201
202

    /** \brief
     * Creates from a macro mesh a correct local and global DOF index numbering.
     *
Thomas Witkowski's avatar
Thomas Witkowski committed
203
     * \param[out] rankDOFs      Returns all DOFs from the macro mesh, which are owned
204
     *                           by the rank after partitioning the macro mesh.
Thomas Witkowski's avatar
Thomas Witkowski committed
205
206
     * \param[out] nRankDOFs     Number of DOFs owned by rank.
     * \param[out] nOverallDOFs  Number of all DOFs in macro mesh.
207
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
208
    void createLocalGlobalNumbering(DofContainer& rankDOFs,
209
210
211
212
				    int& nRankDOFs, 
				    int& nOverallDOFs);

    void updateLocalGlobalNumbering(int& nRankDOFs, int& nOverallDOFs);
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    /** \brief
     * This function create new mappings from local to global indices, 
     * \ref mapLocalGlobalDOFs, and from local to dof indices, \ref mapLocalToDofIndex.
     * Furthermore, using the first argument the dof indices in ranks partition are
     * changed.
     * 
     * \param[in] rankDofsNewLocalIndex       Map from dof pointers of all dofs in rank
     *                                        to new dof indices.
     * \param[in] rankOwnedDofsNewLocalIndex  Map from dof pointers of dofs owned by
     *                                        the rank to the new local index.
     * \param[in] rankDofsNewGlobalIndex      Map from dof pointers of all dofs in rank
     *                                        to the new global index.
     */
    void createLocalMappings(DofIndexMap &rankDofsNewLocalIndex,
			     DofIndexMap &rankOwnedDofsNewLocalIndex,
			     DofIndexMap &rankDofsNewGlobalIndex);

Thomas Witkowski's avatar
Thomas Witkowski committed
231
232
233
    void addAllVertexDOFs(Element *el, int ithEdge, DofContainer& dofs);

    void addAllEdgeDOFs(Element *el, int ithEdge, DofContainer& dofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
234

235
236
    /** \brief
     * This function traverses the whole mesh, i.e. before it is really partitioned,
Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
239
     * and collects information about which DOF corresponds to which rank. Can only
     * be used, if \ref partitionVec is set correctly. This is only the case, when
     * the macro mesh is partitioned.
240
     *
Thomas Witkowski's avatar
Thomas Witkowski committed
241
     * \param[out] partionDOFs   Stores to each DOF pointer the set of ranks the DOF is
242
     *                           part of.
Thomas Witkowski's avatar
Thomas Witkowski committed
243
244
     * \param[out] rankDOFs      Stores all rank DOFs.
     * \param[out] boundaryDOFs  Stores all DOFs in ranks partition that are on an 
245
246
     *                           interior boundary but correspond to another rank.
     */
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
247
248
249
    void createDOFMemberInfo(DofToPartitions& partitionDofs,
			     DofContainer& rankOwnedDofs,
			     DofContainer& rankAllDofs,
250
251
			     DofToRank& boundaryDofs,
			     DofToBool& vertexDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
252

253
254
255
256
257
    void setDofMatrix(DOFMatrix* mat, int dispMult = 1, 
		      int dispAddRow = 0, int dispAddCol = 0);

    void setDofVector(DOFVector<double>* vec, int disMult = 1, int dispAdd = 0);

Thomas Witkowski's avatar
Thomas Witkowski committed
258
259
260
    void DbgCreateElementMap(ElementIdxToDofs &elMap);
    
    void DbgTestElementMap(ElementIdxToDofs &elMap);
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262

    void DbgTestInteriorBoundary();
263
     
Thomas Witkowski's avatar
Thomas Witkowski committed
264
265
266
267
268
    /** \brief
     * This function is used for debugging only. It traverses all interior boundaries
     * and compares the dof indices on them with the dof indices of the boundarys
     * neighbours. The function fails, when dof indices on an interior boundary does
     * not fit together.
269
270
271
     *
     * \param  printCoords   If true, the coords of all common dofs are printed to
     *                       the screen.
Thomas Witkowski's avatar
Thomas Witkowski committed
272
     */
273
    void DbgTestCommonDofs(bool printCoords = false);
274

275
276
277
278
279
280
281
    /** \brief
     * This functions create a Paraview file with the macro mesh where the elements
     * are colored by the partition they are part of. This function can be used for
     * debugging.
     */
    void writePartitioningMesh(std::string filename);

282
283
284
285
286
287
288
289
290
291
292
293
    /** \brief
     * This function must be used if the values of a SystemVector must be synchronised
     * over all ranks. That means, that each rank sends the values of the DOFs, which
     * are owned by the rank and lie on an interior bounday, to all other ranks also
     * having this DOF.
     *
     * This function must be used, for example, after the lineary system is solved, or
     * after the SystemVector is set by some user defined functions, e.g., initial
     * solution functions.
     */
    void synchVectors(SystemVector &vec);

Thomas Witkowski's avatar
Thomas Witkowski committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    inline void orderDOFs(const DegreeOfFreedom* dof1,
			  const DegreeOfFreedom* dof2,
			  const DegreeOfFreedom* dof3,
			  DofContainer &vec)
    {
      vec.resize(3);

      if (*dof1 < *dof2 && *dof1 < *dof3)
	vec[0] = dof1;
      else if (*dof2 < *dof1 && *dof2 < *dof3)
	vec[0] = dof2;
      else 
	vec[0] = dof3;

      if (*dof1 > *dof2 && *dof1 > *dof3)
	vec[2] = dof1;
      else if (*dof2 > *dof1 && *dof2 > *dof3)
	vec[2] = dof2;
      else 
	vec[2] = dof3;

      if (dof1 != vec[0] && dof1 != vec[2]) 
	vec[1] = dof1;
      else if (dof2 != vec[0] && dof2 != vec[2]) 
	vec[1] = dof2;
      else
	vec[1] = dof3;
    }

323
  protected:
324
325
326
327
328
329
    ///
    ProblemIterationInterface *iterationIF;

    ///
    ProblemTimeInterface *timeIF;

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    /// The rank of the current process.
    int mpiRank;

    /// Overall number of processes.
    int mpiSize;

    /** \brief
     * MPI communicator collected all processes, which should
     * be used for calculation. The Debug procces is not included
     * in this communicator.
     */
    MPI::Intracomm mpiComm;

    /// Name of the problem (as used in the init files)
    std::string name;

346
347
348
    /// Finite element space of the problem.
    FiniteElemSpace *feSpace;

349
350
351
    /// Mesh of the problem.
    Mesh *mesh;

352
353
354
    /// Info level.
    int info;

355
356
357
    /// Refinement manager for the mesh.
    RefinementManager *refinementManager;

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    /// Pointer to the paritioner which is used to devide a mesh into partitions.
    ParMetisPartitioner *partitioner;

    /// Weights for the elements, i.e., the number of leaf elements within this element.
    std::map<int, double> elemWeights;

    /// Is true, if the mesh was not partitioned before, otherwise it's false.
    bool initialPartitionMesh;

    /** \brief
     * Stores to every coarse element index the number of the partition it 
     * corresponds to.
     */
    std::map<int, int> partitionVec;

    /** \brief
     * Stores an old partitioning of elements. To every element index the number
     * of the parition it corresponds to is stored.
     */
    std::map<int, int> oldPartitionVec;    
378

379
380
381
382
    Mat petscMatrix;

    Vec petscRhsVec;
    
383
    Vec petscSolVec;
384

385
386
    Vec petscTmpVec;

387
388
    /// Number of DOFs in the rank mesh.
    int nRankDOFs;
Thomas Witkowski's avatar
Thomas Witkowski committed
389
390

    /** \brief 
Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
393
394
395
396
397
398
399
400
401
402
     * Defines the interior boundaries of the domain that result from partitioning
     * the whole mesh. Contains only the boundaries, which are owned by the rank, i.e.,
     * the object gives for every neighbour rank i the boundaries this rank owns and 
     * shares with rank i.
     */
    InteriorBoundary myIntBoundary;
    
    /** \brief
     * Defines the interior boundaries of the domain that result from partitioning
     * the whole mesh. Contains only the boundaries, which are not owned by the rank,
     * i.e., the object gives for every neighbour rank i the boundaries that are
     * owned by rank i and are shared with this rank.
Thomas Witkowski's avatar
Thomas Witkowski committed
403
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
404
    InteriorBoundary otherIntBoundary;
Thomas Witkowski's avatar
Thomas Witkowski committed
405

406
407
408
409
    /** \brief
     * This map contains for each rank the list of dofs the current rank must send
     * to exchange solution dofs at the interior boundaries.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
410
    RankToDofContainer sendDofs;
411
412
413
414
415

    /** \brief
     * This map contains for each rank the list of dofs from which the current rank 
     * must receive solution values of dofs at the interior boundaries.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
416
    RankToDofContainer recvDofs;
417
418

    /// Maps local to global dof indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
419
    DofMapping mapLocalGlobalDOFs;
420

Thomas Witkowski's avatar
Thomas Witkowski committed
421
    /// Maps local dof indices to real dof indices.
422
    DofMapping mapLocalToDofIndex;  
423
424
425
426
427
428

    /** \brief
     * Maps all DOFs in ranks partition to a bool value. If it is true, the DOF is
     * owned by the rank. Otherwise, its an interior boundary DOF that is owned by
     * another rank.
     */
429
430
431
    DofIndexToBool isRankDof;

    DofToBool vertexDof;
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
432
433

    int rstart;
434
435

    int nComponents;
436
437
438
439

    int nRankRows;

    int nOverallRows;
440
441
442
443
  };

}

444
#endif // AMDIS_PARALLELDOMAINBASE_H