ParallelDofMapping.h 16.2 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file FeSpaceMapping.h */

23
24
25
26
#ifndef AMDIS_FE_SPACE_MAPPING_H
#define AMDIS_FE_SPACE_MAPPING_H

#include <mpi.h>
27
#include <vector>
28
#include <map>
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
29
#include <set>
30
31
32
33
#include <petsc.h>
#include <petscis.h>

#include "AMDiS_fwd.h"
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
34
#include "parallel/DofComm.h"
35
36
#include "parallel/MpiHelper.h"
#include "parallel/ParallelTypes.h"
37
#include "parallel/StdMpi.h"
38
39

namespace AMDiS {
Thomas Witkowski's avatar
Thomas Witkowski committed
40

41
42
  using namespace std;

43
  /** \brief
44
45
46
   * Is used to store matrix indices to all DOFs in rank's subdomain. Thus, the
   * class defines a mapping from component number and DOF index to a global
   * matrix index. This class does not calculate the indices by itself!
47
48
49
50
51
52
53
54
55
56
57
58
   */
  class DofToMatIndex
  {
  public:
    DofToMatIndex() {}

    /// Reset the data structure.
    inline void clear()
    {
      data.clear();
    }

59
60
61
62
63
64
65
    /** Add a new mapping for a given DOF.
     * 
     * \param[in]   component       Component number for which the mapping 
     *                              is defined.
     * \param[in]   dof             DOF index
     * \param[in]   globalMatIndex  Global matrix index.
     */
66
67
68
69
70
    inline void add(int component, DegreeOfFreedom dof, int globalMatIndex)
    {
      data[component][dof] = globalMatIndex;
    }

71
    /// Maps a global DOF index to the global matrix index for a specific 
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    /// system component number.
    inline int get(int component, DegreeOfFreedom dof)
    {
      FUNCNAME("DofToMatIndex::get()");

      TEST_EXIT_DBG(data.count(component))
	("No mapping data for component %d available!\n", component);

      TEST_EXIT_DBG(data[component].count(dof))
	("Mapping for DOF %d in component %d does not exists!\n",
	 dof, component);

      return data[component][dof];
    }

87
88
89
90
91
    /// Returns for a given matrix index the component and (local or global) DOF
    /// index. As the data structure is not made for this kind of reverse
    /// search, this is very slow and should be only used for debugging.
    void getReverse(int rowIndex, int &component, int &dofIndex);

92
93
94
95
96
97
98
  private:
    /// The mapping data. For each system component there is a specific map that
    /// maps global DOF indices to global matrix indices.
    map<int, map<DegreeOfFreedom, int> > data;
  };


99
100
101
102
103
104
  /**
   * This class defines the parallel mapping of DOFs for one FE space. It is used
   * by the class \ref ParallelDofMapping to specifiy the mapping for a set of 
   * FE spaces.
   */
  class FeSpaceDofMap
105
106
  {
  public:
107
108
    /// This constructor exists only to create std::map of this class and make
    /// use of the operator [] for read access. Should never be called.
109
    FeSpaceDofMap() 
110
111
112
    {
      ERROR_EXIT("Should not be called!\n");
    }
113
114
     
    /// This is the only valid constructur to be used. 
115
    FeSpaceDofMap(MeshLevelData* ld);
116
117

    /// Clears all data of the mapping.
118
    void clear();
119

120
121
    /// Maps a DOF index to both, the local and global index of the mapping. The
    /// global index must not be set.
Thomas Witkowski's avatar
Thomas Witkowski committed
122
    MultiIndex& operator[](DegreeOfFreedom d)
123
    {
124
      TEST_EXIT_DBG(dofMap.count(d))("DOF %d is not in map!\n", d);
125

126
      return dofMap[d];
127
    }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

    /** \brief
     * Searchs the map for a given DOF. It does not fail, if the DOF is not 
     * mapped by this mapping. In this case, it returns false. If the DOF is 
     * mapped, the result is stored and the function returns true.
     *
     * \param[in]    dof     DOF index for which a mapping is searched.
     * \param[out]   index   In the case that the DOF is mapped, the result
     *                       is stored here.
     */
    inline bool find(DegreeOfFreedom dof, MultiIndex& index)
    {
      DofMap::iterator it = dofMap.find(dof);
      if (it == dofMap.end())
	return false;

      index = it->second;
      return true;
    }
147
    
148
149
    /// Inserts a new DOF to rank's mapping. The DOF is assumed to be owend by
    /// the rank.
150
    void insertRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
151
    {
152
      FUNCNAME("FeSpaceDofMap::insertRankDof()");
153
      
154
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
155
      
156
157
      dofMap[dof0].local = dof1;
      nLocalDofs++;
Thomas Witkowski's avatar
Thomas Witkowski committed
158
      if (dof1 != -1)
159
	nRankDofs++;
160
161
    }
    
162
163
    /// Inserts a new DOF to rank's mapping. The DOF exists in rank's subdomain
    /// but is owned by a different rank, thus it is part of an interior boundary.
164
    void insertNonRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
165
    {
166
      FUNCNAME("FeSpaceDofMap::insertNonRankDof()");
167
      
168
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
169
      
170
171
172
      dofMap[dof0].local = dof1;
      nLocalDofs++;
      nonRankDofs.insert(dof0);
173
174
    }
    
175
    /// Checks if a given DOF is in the DOF mapping.
176
    bool isSet(DegreeOfFreedom dof)
177
    {
178
      return static_cast<bool>(dofMap.count(dof));
179
    }
180

Thomas Witkowski's avatar
Blbu  
Thomas Witkowski committed
181
182
183
    /// Checks if a given DOF is a rank owned DOF of the DOF mapping. The DOF
    /// must be a DOF of the mapping (this is not checked here), otherwise the
    /// result is meaningsless.
184
    bool isRankDof(DegreeOfFreedom dof)
185
    {
186
      return !(static_cast<bool>(nonRankDofs.count(dof)));
187
    }
Thomas Witkowski's avatar
Blbu  
Thomas Witkowski committed
188
189
190
191
192

    bool isRankGlobalDof(int dof)
    {
      return (dof >= rStartDofs && dof < rStartDofs + nRankDofs);
    }
193
    
194
    /// Returns number of DOFs in the mapping.
195
    unsigned int size()
196
    {
197
      return dofMap.size();
198
199
    }
    
200
    /// Returns the raw data of the mapping.
201
    DofMap& getMap()
202
    {
203
      return dofMap;
204
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
205

206
207
208
209
210
211
212
213
214
215
    DofMap::iterator begin()
    {
      return dofMap.begin();
    }

    DofMap::iterator end()
    {
      return dofMap.end();
    }

216
217
    /// Recomputes the mapping.
    void update();
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
218

219
    /// Sets the FE space this mapping corresponds to.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
220
221
222
223
224
    void setFeSpace(const FiniteElemSpace *fe)
    {
      feSpace = fe;
    }

225
226
    /// Informs the mapping whether the mapping will include DOFs that are not
    /// owned by the rank.
227
    void setNonLocal(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
228
    {
229
      isNonLocal = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
230
231
    }

232
    /// Informs the mapping whether a global index must be computed.
233
    void setNeedGlobalMapping(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
234
    {
235
      needGlobalMapping = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
236
237
    }

238
    /// Sets the DOF communicator.
239
    void setDofComm(DofComm &dc)
Thomas Witkowski's avatar
Thomas Witkowski committed
240
    {
241
      dofComm = &dc;
Thomas Witkowski's avatar
Thomas Witkowski committed
242
243
    }

244
245
246
247
248
249
    void setMpiComm(MPI::Intracomm &m, int l)
    {
      mpiComm = m;
      meshLevel = l;
    }

250
  private:
251
    /// Computes a global mapping from the local one.
252
    void computeGlobalMapping();
253
254
255

    /// Computes the global indices of all DOFs in the mapping that are not owned
    /// by the rank.
256
    void computeNonLocalIndices();
257
258

  private:
259
    MeshLevelData *levelData;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
260

261
262
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
263

264
265
266
267
    MPI::Intracomm mpiComm;

    int meshLevel;

268
    /// The FE space this mapping belongs to. This is used only the get the
269
    /// correct DOF communicator in \ref dofComm.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
270
271
    const FiniteElemSpace *feSpace;

272
    /// Mapping data from DOF indices to local and global indices.
273
    DofMap dofMap;
274

275
    /// Set of all DOFs that are in mapping but are not owned by the rank.
276
    std::set<DegreeOfFreedom> nonRankDofs;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
277

278
    /// If true, a global index mapping will be computed for all DOFs.
Thomas Witkowski's avatar
Thomas Witkowski committed
279
    bool needGlobalMapping;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
280

281
282
283
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
284
    bool isNonLocal;
285

286
287
  public:
    /// 
288
    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
289
290
  };
  
291
292
293
294
295
296
  
  /**
   * Implements the mapping from sets of distributed DOF indices to local and
   * global indices. The mapping works for a given set of FE spaces. Furthermore,
   * this class may compute the matrix indices of the set of DOF indices.
   */
Thomas Witkowski's avatar
Thomas Witkowski committed
297
  class ParallelDofMapping
298
299
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
300
    ParallelDofMapping() 
301
      : levelData(NULL),
302
	dofComm(NULL),
303
	isNonLocal(true),
304
	needMatIndexFromGlobal(false),
305
	feSpaces(0),
306
307
308
309
310
	nRankDofs(1),
	nLocalDofs(1),
	nOverallDofs(1),
	rStartDofs(1)
    {
311
312
313
314
      nRankDofs = -1;
      nLocalDofs = -1;
      nOverallDofs = -1;
      rStartDofs = -1;
315
    } 
316

317
318
319
320
    /** \brief Initialize the parallel DOF mapping.
     *
     * \param[in]  m                  MPI communicator.
     * \param[in]  fe                 The FE spaces of all components of the 
321
322
323
324
     *                                PDE to be solved.
     * \param[in]  uniqueFe           Unique list of FE spaces. Thus, two
     *                                arbitrary elements of this list are always
     *                                different.
325
     * \param[in]  isNonLocal         If true, at least one rank's mapping con-
326
327
     *                                taines DOFs that are not owend by the rank.
     */
328
    void init(MeshLevelData& mld,
329
	      vector<const FiniteElemSpace*> &fe,
330
	      vector<const FiniteElemSpace*> &uniqueFe,
331
332
333
334
335
336
337
338
339
340
341
342
	      bool isNonLocal = true);

    /// In the case of having only one FE space, this init function can be used.
    void init(MeshLevelData& mld,
	      const FiniteElemSpace *feSpace,
	      bool isNonLocal = true)
    {
      vector<const FiniteElemSpace*> feSpaces;
      feSpaces.push_back(feSpace);
      init(mld, feSpaces, feSpaces, isNonLocal);
    }

343
    void setMpiComm(MPI::Intracomm &m, int l);
344
345
346
    
    /// Clear all data.
    void clear();
347
348
349

    /// Set the DOF communicator objects that are required to exchange information
    /// about DOFs that are on interior boundaries.
350
    void setDofComm(DofComm &dofComm);
351

352
353
354
    /// Returns the DOF communicator.
    DofComm& getDofComm()
    {
355
356
357
      FUNCNAME("ParallelDofMapping::getDofComm()");

      TEST_EXIT_DBG(dofComm)("No DOF communicator object defined!\n");
358
359
360
361

      return *dofComm;
    }

362
363
364
    /// Changes the computation of matrix indices based of either local or
    /// global DOF indices, see \ref needMatIndexFromGlobal
    void setComputeMatIndex(bool global)
365
366
367
368
    {
      needMatIndexFromGlobal = global;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
369
370
371
372
373
    inline bool isMatIndexFromGlobal()
    {
      return needMatIndexFromGlobal;
    }

374
375
    /// Access the DOF mapping for a given FE space.
    inline FeSpaceDofMap& operator[](const FiniteElemSpace* feSpace)
376
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
377
      FUNCNAME("ParallelDofMapping::operator[]()");
378

379
380
      TEST_EXIT_DBG(data.count(feSpace))
	("No data for FE space at address %p!\n", feSpace);
381
382
383
384

      return data.find(feSpace)->second;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
385
386
387
388
389
    inline bool isDefinedFor(const FiniteElemSpace* feSpace) const
    {
      return static_cast<bool>(data.count(feSpace));
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
390
391
392
393
394
395
    /// Returns the number of solution components the mapping is defined on.
    inline int getNumberOfComponents() const
    {
      return static_cast<int>(feSpaces.size());
    }

396
    /// Returns \ref nRankDofs, thus the number of DOFs owned by the rank.
397
    inline int getRankDofs()
398
    {
399
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");
400

401
      return nRankDofs;
402
403
    }

404
    /// Returns \ref nLocalDofs, thus the number of DOFs in ranks subdomain.
405
    inline int getLocalDofs()
Thomas Witkowski's avatar
Thomas Witkowski committed
406
    {
407
      TEST_EXIT_DBG(nLocalDofs >= 0)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
408

409
      return nLocalDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
    }

412
    /// Returns \ref nOverallDofs, thus the number of all DOFs in this mapping.
413
    inline int getOverallDofs()
414
    {
415
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");
416

417
      return nOverallDofs;
418
419
    }

420
421
    /// Returns \ref rStartDofs, thus the smallest global index of a DOF that is
    /// owned by the rank.
422
    inline int getStartDofs()
423
    {
424
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");
425

426
      return rStartDofs;
427
428
    }

429
    /// Update the mapping.
Thomas Witkowski's avatar
Thomas Witkowski committed
430
    void update();
431

432
433
434
    /// Update the mapping.
    void update(vector<const FiniteElemSpace*>& feSpaces);

435
436
    /// Returns the global matrix index of a given DOF for a given 
    /// component number.
437
    inline int getMatIndex(int ithComponent, DegreeOfFreedom d)
Thomas Witkowski's avatar
Thomas Witkowski committed
438
    {
439
      return dofToMatIndex.get(ithComponent, d);
Thomas Witkowski's avatar
Thomas Witkowski committed
440
441
    }

442
443
444
445
446
447
448
    /// Returns the component number and local/global DOF index for a given
    /// matrix row index. Should be used for debugging only!
    inline void getReverseMatIndex(int index, int &component, int &dofIndex)
    {
      dofToMatIndex.getReverse(index, component, dofIndex);
    }

449
450
    /// Returns the local matrix index of a given DOF for a given 
    /// component number.
451
    inline int getLocalMatIndex(int ithComponent, DegreeOfFreedom d)
452
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
453
      FUNCNAME("ParallelDofMapping::getLocalMatIndex()");
454

455
      TEST_EXIT_DBG(data[feSpaces[ithComponent]].isRankDof(d))
456
457
	("Should not happen!\n");

458
      return dofToMatIndex.get(ithComponent, d) - rStartDofs;
459
460
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
461
462
463
464
465
466
467
468
469
470
471
472
    // Writes all data of this object to an output stream.
    void serialize(ostream &out)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

    // Reads the object data from an input stream.
    void deserialize(istream &in)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

473
    /// Compute local and global matrix indices.
474
    void computeMatIndex(bool globalIndex);
475

476
477
478
479
    void createIndexSet(IS &is, 
			int firstComponent, 
			int nComponents);

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
    /// Create a parallel distributed PETSc vector based on this mapping.
    inline void createVec(Vec &vec)
    {
      VecCreateMPI(mpiComm, getRankDofs(), getOverallDofs(), &vec);
    }

    /// Create a parallel distributed PETsc vector based on this mapping but
    /// with a different (larger) global size. This is used in multi-level 
    /// method to embed a local vector into a subdomain spaned by several
    /// ranks.
    inline void createVec(Vec &vec, int nGlobalRows)
    {
      VecCreateMPI(mpiComm, getRankDofs(), nGlobalRows, &vec);
    }

495
496
497
498
499
  protected:
    /// Insert a new FE space DOF mapping for a given FE space.
    void addFeSpace(const FiniteElemSpace* feSpace);

    /// Compute \ref nRankDofs.
500
    int computeRankDofs();
501
502

    /// Compute \ref nLocalDofs.
503
    int computeLocalDofs();
504
505

    /// Compute \ref nOverallDofs.
506
    int computeOverallDofs();
507
508

    /// Compute \ref rStartDofs.
509
    int computeStartDofs();
510

511
  private:
512
513
514
515
    MPI::Intracomm mpiComm;

    int meshLevel;

516
    MeshLevelData *levelData;
517

518
519
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
520

521
522
523
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
524
    bool isNonLocal;
525
526
527
528
529

    /// If matrix indices should be computed, this variable defines if the
    /// mapping from DOF indices to matrix row indices is defined on local
    /// or global DOF indices. If true, the mapping is to specify and to use
    /// on global ones, otherwise on local DOF indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
530
531
532
533
534
    /// In most scenarios the mapping stored on local DOF indices is what we
    /// want to have. Only when periodic boundary conditions are used together
    /// with some global matrix approache, the matrix indices must be stored
    /// also for DOFs that are not part of the local subdomain. Thus, the
    /// mapping will be stored on global DOF indices.
535
536
    bool needMatIndexFromGlobal;

537
538
539
540
    /// Maps from FE space pointers to DOF mappings.
    map<const FiniteElemSpace*, FeSpaceDofMap> data;

    /// The FE spaces for all components.
541
542
    vector<const FiniteElemSpace*> feSpaces;

543
544
    /// The set of all FE spaces. It uniquly contains all different FE spaces
    /// from \ref feSpaces.
545
    vector<const FiniteElemSpace*> feSpacesUnique;
Thomas Witkowski's avatar
Thomas Witkowski committed
546

547
    /// Number of DOFs owned by rank.
548
    int nRankDofs;
549
550

    /// Number of DOFs in rank's subdomain.
551
    int nLocalDofs;
552
553

    /// Number of global DOFs (this value is thus the same on all ranks).
554
    int nOverallDofs;
555
556

    /// Smallest global index of a DOF owned by the rank.
557
    int rStartDofs;
558
559
560

    /// Mapping from global DOF indices to global matrix indices under 
    /// consideration of possibly multiple components.
561
    DofToMatIndex dofToMatIndex;
562
563
564
565
566
  };

}

#endif