ParallelDofMapping.cc 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// 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.


13
#include "parallel/ParallelDofMapping.h"
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
14
#include "parallel/StdMpi.h"
15
16
17
18
19

namespace AMDiS {

  using namespace std;

20
  void FeSpaceDofMap::clear()
21
  {
22
    int nLevel = levelData->getLevelNumber();
23
    dofMap.clear();
24
25
    dofMap.resize(nLevel);

26
    nonRankDofs.clear();
27
28
    nonRankDofs.resize(nLevel);

29
30
31
32
    nRankDofs.resize(nLevel, 0);
    nLocalDofs.resize(nLevel, 0);
    nOverallDofs.resize(nLevel, 0);
    rStartDofs.resize(nLevel, 0);
33
34
35
  }


36
  void FeSpaceDofMap::update()
37
  {
38
    FUNCNAME("FeSpaceDofMap::update()");
39

40
    int nLevel = levelData->getLevelNumber();
41
    for (int i = 0; i < nLevel; i++) {
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
42

43
      // === Compute local indices for all rank owned DOFs. ===
44
      
45
46
47
48
49
50
51
52
      for (DofMap::iterator it = dofMap[i].begin(); it != dofMap[i].end(); ++it)
	if (it->second.local == -1 && nonRankDofs[i].count(it->first) == 0)
	  it->second.local = nRankDofs[i]++;
      
      // === Compute number of local and global DOFs in the mapping. ===
      
      nOverallDofs[i] = 0;
      rStartDofs[i] = 0;
53
      mpi::getDofNumbering(levelData->getMpiComm(0), 
54
			   nRankDofs[i], rStartDofs[i], nOverallDofs[i]);
55
56
57
58
59
60
61
62
63
      
      // === If required, compute also the global indices. ===
      
      if (needGlobalMapping) {
	computeGlobalMapping(i);
	
	if (hasNonLocalDofs)
	  computeNonLocalIndices(i);
      }
64
    }
65
66
67
  }


68
  void FeSpaceDofMap::computeGlobalMapping(int level)
69
  {
70
71
72
73
74
    FUNCNAME("FeSpaceDofMap::computeGlobalMapping()");

    for (DofMap::iterator it = dofMap[level].begin(); 
	 it != dofMap[level].end(); ++it)
      it->second.global = it->second.local + rStartDofs[level];
75
76
  }

77

78
  void FeSpaceDofMap::computeNonLocalIndices(int level)
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
79
  {
80
    FUNCNAME("FeSpaceDofMap::computeNonLocalIndices()");
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
81
82
83

    typedef map<int, map<const FiniteElemSpace*, DofContainer> >::iterator it_type;

84
85
86
    // === Send all global indices of DOFs that are owned by the rank to all ===
    // === other ranks that also include this DOF.                           ===

87
    StdMpi<vector<int> > stdMpi(levelData->getMpiComm(0));
88

89
    for (DofComm::Iterator it(dofComm->getSendDofs(), level, feSpace); 
90
	 !it.end(); it.nextRank())
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
91
      for (; !it.endDofIter(); it.nextDof())
92
93
	if (dofMap[level].count(it.getDofIndex()) && !nonRankDofs[level].count(it.getDofIndex()))
	  stdMpi.getSendData(it.getRank()).push_back(dofMap[level][it.getDofIndex()].global);
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
94
95
96

    stdMpi.updateSendDataSize();

97
98
99

    // === Check from which ranks this rank must receive some data. ===

100
    for (DofComm::Iterator it(dofComm->getRecvDofs(), level, feSpace); 
101
	 !it.end(); it.nextRank()) {
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
102
103
      bool recvFromRank = false;
      for (; !it.endDofIter(); it.nextDof()) {
104
	if (nonRankDofs[level].count(it.getDofIndex())) {
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
105
106
107
108
109
	  recvFromRank = true;
	  break;
	}
      }

110
      if (recvFromRank)
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
111
112
113
	stdMpi.recv(it.getRank());
    }

114
115
116

    // === Start communication to exchange global indices. ===

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
117
118
    stdMpi.startCommunication();

119
120
121

    // === And set the global indices for all DOFs that are not owned by rank. ===
    
122
    for (DofComm::Iterator it(dofComm->getRecvDofs(), level, feSpace);
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
123
124
125
	 !it.end(); it.nextRank()) {
      int i = 0;
      for (; !it.endDofIter(); it.nextDof())
126
127
	if (nonRankDofs[level].count(it.getDofIndex()))
	  dofMap[level][it.getDofIndex()].global = stdMpi.getRecvData(it.getRank())[i++];
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
128
129
130
131
    }
  }


132
  void ParallelDofMapping::init(MeshLevelData &ldata,
133
				vector<const FiniteElemSpace*> &fe,
134
				vector<const FiniteElemSpace*> &uniqueFe,
135
136
				bool needGlobalMapping,
				bool bNonLocalDofs)
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
137
  {
138
139
    FUNCNAME("ParallelDofMapping::init()");

140
    levelData = &ldata;
141
    feSpaces = fe;
142
    feSpacesUnique = uniqueFe;
143
    hasNonLocalDofs = bNonLocalDofs;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
144
    
145
146
147

    // === Init the mapping for all different FE spaces. ===

148
    for (vector<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
149
150
151
152
153
	 it != feSpacesUnique.end(); ++it) {
      addFeSpace(*it);
      data[*it].setNeedGlobalMapping(needGlobalMapping);
      data[*it].setNonLocalDofs(hasNonLocalDofs);
    }
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
154
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
155
156


157
158
159
160
  void ParallelDofMapping::clear()
  {
    FUNCNAME("ParallelDofMapping::clear()");

161
162
    TEST_EXIT_DBG(levelData)("No mesh level data object defined!\n");

163
    for (vector<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
164
165
166
	 it != feSpacesUnique.end(); ++it)
      data[*it].clear();

167
168
169
170
171
    int nLevel = levelData->getLevelNumber();
    nRankDofs.resize(nLevel);
    nLocalDofs.resize(nLevel);
    nOverallDofs.resize(nLevel);
    rStartDofs.resize(nLevel);
172
    dofToMatIndex.resize(nLevel);
173

174
175
176
177
178
    for (int i = 0; i < nLevel; i++) {
      nRankDofs[i] = -1;
      nLocalDofs[i] = -1;
      nOverallDofs[i] = -1;
      rStartDofs[i] = -1;
179
      dofToMatIndex[i].clear();
180
    }
181
182
183
  }


184
  void ParallelDofMapping::setDofComm(DofComm &dc)
185
186
187
  {
    FUNCNAME("ParallelDofMapping::setDofComm()");

188
    dofComm = &dc;
189
190

    // Add the DOF communicator also to all FE space DOF mappings.
191
    for (vector<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
192
	 it != feSpacesUnique.end(); ++it)
193
      data[*it].setDofComm(dc);
194
  }
195

196

Thomas Witkowski's avatar
Thomas Witkowski committed
197
198
199
200
201
202
203
  void ParallelDofMapping::addFeSpace(const FiniteElemSpace* feSpace)
  {
    FUNCNAME("ParallelDofMapping::addFeSpace()");
    
    if (data.count(feSpace))
      data.find(feSpace)->second.clear();
    else
204
      data.insert(make_pair(feSpace, FeSpaceDofMap(levelData)));
Thomas Witkowski's avatar
Thomas Witkowski committed
205
206
207
208
209
    
    data.find(feSpace)->second.setFeSpace(feSpace);
  }    


210
  int ParallelDofMapping::computeRankDofs(int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
211
  {
212
    FUNCNAME("ParallelDofMapping::computeRankDofs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
213
214
    
    int result = 0;
215
216
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
217
      result += data[feSpaces[i]].nRankDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
218
219
220
221
222
223
    }
    
    return result;
  }


224
  int ParallelDofMapping::computeLocalDofs(int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
225
  {
226
    FUNCNAME("ParallelDofMapping::computeLocalDofs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
227
228
    
    int result = 0;
229
230
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
231
      result += data[feSpaces[i]].nLocalDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
232
233
234
235
236
237
    }
    
    return result;
  }


238
  int ParallelDofMapping::computeOverallDofs(int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
239
  {
240
    FUNCNAME("ParallelDofMapping::computeOverallDofs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
241
242
243
244

    int result = 0;
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
245
      result += data.find(feSpaces[i])->second.nOverallDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
246
247
248
249
250
251
    }
    
    return result;
  }


252
  int ParallelDofMapping::computeStartDofs(int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
253
  {
254
    FUNCNAME("ParallelDofMapping::computeStartDofs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
255
256
257
258

    int result = 0;
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
259
      result += data.find(feSpaces[i])->second.rStartDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
262
263
264
265
266
267
268
269
    }
    
    return result;
  }


  void ParallelDofMapping::update()
  {
    FUNCNAME("ParallelDofMapping::update()");

270
    // First, update all FE space DOF mappings.
271
    for (vector<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
272
273
274
	 it != feSpacesUnique.end(); ++it)
      data[*it].update();
    
275
    int nLevel = levelData->getLevelNumber();
276
277
278
279
280
281
282
283
284
285
286
287
    for (int i = 0; i < nLevel; i++) {
      // Compute all numbers from this mappings.

      nRankDofs[i] = computeRankDofs(i);
      nLocalDofs[i] = computeLocalDofs(i);
      nOverallDofs[i] = computeOverallDofs(i);
      rStartDofs[i] = computeStartDofs(i);
      
      // And finally, compute the matrix indices.
      if (needMatIndex)
	computeMatIndex(needMatIndexFromGlobal, i);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
288
289
  }
  
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

  void ParallelDofMapping::update(vector<const FiniteElemSpace*>& fe)
  {
    FUNCNAME("ParallelDofMapping::update()");

    for (vector<const FiniteElemSpace*>::iterator it = fe.begin();
	 it != fe.end(); ++it)
      if (find(feSpacesUnique.begin(), feSpacesUnique.end(), *it) == 
	  feSpacesUnique.end())
	ERROR_EXIT("Wrong FE space!\n");

    feSpaces = fe;
    update();
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
305
  
306
  void ParallelDofMapping::computeMatIndex(bool globalIndex, int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
307
308
  {
    FUNCNAME("ParallelDofMapping::computeMatIndex()");
309

310
    dofToMatIndex[level].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
311
    
312
313
314
    // The offset is always added to the local matrix index. The offset for the
    // DOFs in the first FE spaces is the smalled global index of a DOF that is
    // owned by the rank.
315
    int offset = rStartDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
316
    
317
318
319

    // === Create the matrix indices for all component FE spaces. ===

Thomas Witkowski's avatar
Thomas Witkowski committed
320
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
321
322
323

      // Traverse all DOFs of the FE space and create for all rank owned DOFs
      // a matrix index.
324
      DofMap& dofMap = data[feSpaces[i]].getMap(level);
325
      for (DofMap::iterator it = dofMap.begin(); it != dofMap.end(); ++it) {
326
	if (data[feSpaces[i]].isRankDof(it->first, level)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
	  int globalMatIndex = it->second.local + offset;
328
	  if (globalIndex)
329
	    dofToMatIndex[level].add(i, it->second.global, globalMatIndex);
330
	  else
331
	    dofToMatIndex[level].add(i, it->first, globalMatIndex);
Thomas Witkowski's avatar
Thomas Witkowski committed
332
333
334
	}
      }
      
335
336
      // Increase the offset for the next FE space by the number of DOFs owned 
      // by the rank in the current FE space.
337
      offset += data[feSpaces[i]].nRankDofs[level];
Thomas Witkowski's avatar
Thomas Witkowski committed
338
	
339
      // If there are no non local DOFs, continue with the next FE space.
Thomas Witkowski's avatar
Thomas Witkowski committed
340
341
342
      if (!hasNonLocalDofs)
	continue;
      
343
      TEST_EXIT_DBG(dofComm != NULL)("No communicator given!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
344
      
345
346
347
      // === Communicate the matrix indices for all DOFs that are on some ===
      // === interior boundaries.                                         ===

348
      StdMpi<vector<DegreeOfFreedom> > stdMpi(levelData->getMpiComm(0));
349
      for (DofComm::Iterator it(dofComm->getSendDofs(), level, feSpaces[i]); 
Thomas Witkowski's avatar
Thomas Witkowski committed
350
351
352
353
354
	   !it.end(); it.nextRank()) {
	vector<DegreeOfFreedom> sendGlobalDofs;
	
	for (; !it.endDofIter(); it.nextDof())
	  if (dofMap.count(it.getDofIndex()))
355
	    if (globalIndex)
356
	      sendGlobalDofs.push_back(dofToMatIndex[level].get(i, dofMap[it.getDofIndex()].global));
357
	    else
358
	      sendGlobalDofs.push_back(dofToMatIndex[level].get(i, it.getDofIndex()));
Thomas Witkowski's avatar
Thomas Witkowski committed
359
360
361
362
	
	stdMpi.send(it.getRank(), sendGlobalDofs);
      }
      
363
      for (DofComm::Iterator it(dofComm->getRecvDofs(), level, feSpaces[i]); 
Thomas Witkowski's avatar
Thomas Witkowski committed
364
365
366
367
368
369
	   !it.end(); it.nextRank())
	stdMpi.recv(it.getRank());
      
      stdMpi.startCommunication();
      
      {
370
	for (DofComm::Iterator it(dofComm->getRecvDofs(), level, feSpaces[i]); 
Thomas Witkowski's avatar
Thomas Witkowski committed
371
372
373
374
375
	     !it.end(); it.nextRank()) {
	  int counter = 0;
	  for (; !it.endDofIter(); it.nextDof()) {
	    if (dofMap.count(it.getDofIndex())) {
	      DegreeOfFreedom d = stdMpi.getRecvData(it.getRank())[counter++];
376
	      if (globalIndex)
377
		dofToMatIndex[level].add(i, dofMap[it.getDofIndex()].global, d);
378
	      else
379
		dofToMatIndex[level].add(i, it.getDofIndex(), d);
Thomas Witkowski's avatar
Thomas Witkowski committed
380
381
382
383
384
385
386
	    }
	  }
	}
      }
    }
  }
  
387
}