InteriorBoundary.cc 16.5 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/InteriorBoundary.h"
14
#include "parallel/ElementObjectDatabase.h"
15
16
#include "FiniteElemSpace.h"
#include "BasisFunction.h"
17
#include "Serializer.h"
18
#include "VertexVector.h"
19
20

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

22
23
24
  using namespace std;


25
26
  void InteriorBoundary::create(MeshLevelData &levelData,
				int level,
27
28
29
30
31
32
33
34
35
36
37
				ElementObjectDatabase &elObjDb)
  { 
    FUNCNAME("InteriorBoundary::clear()");

    own.clear();
    other.clear();
    periodic.clear();

    Mesh *mesh = elObjDb.getMesh();
    TEST_EXIT_DBG(mesh)("Should not happen!\n");

38
39
40
    int mpiRank = levelData.getMpiComm(0);
    MPI::Intracomm mpiComm = levelData.getMpiComm(level);
    std::set<int> levelRanks = levelData.getLevelRanks(level);
41

42
43
44
45
    // === Create interior boundary data structure. ===
    
    for (int geoPos = 0; geoPos < mesh->getDim(); geoPos++) {
      GeoIndex geoIndex = INDEX_OF_DIM(geoPos, mesh->getDim());
46

47
48
      while (elObjDb.iterate(geoIndex)) {
	map<int, ElementObjectData>& objData = elObjDb.getIterateData();
49
50

	// Test, if this is a boundary object of this rank.
51
52
53
	if (!(objData.count(mpiRank) && objData.size() > 1))
	  continue;

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#if 0
	// Test, if the boundary object defines an interior boundary within the
	// ranks of the MPI group. If not, go to next element.
	bool boundaryWithinMpiGroup = false;
	if (levelRanks.size() == 1 && *(levelRanks.begin()) == -1) {
	  boundaryWithinMpiGroup = true;
	} else {
	  for (map<int, ElementObjectData>::iterator it = objData.begin();
	       it != objData.end(); ++it) {
	    if (it->first != mpiRank && levelRanks.count(it->first)) {
	      boundaryWithinMpiGroup == true;
	      break;
	    }
	  }
	}
	if (!boundaryWithinMpiGroup)
	  continue;
#endif	

	int owner = elObjDb.getIterateOwner(level);
74
75
76
77
78
79
80
81
82
	ElementObjectData& rankBoundEl = objData[mpiRank];
	
	AtomicBoundary bound;
	bound.maxLevel = elObjDb.getIterateMaxLevel();
	bound.rankObj.el = elObjDb.getElementPtr(rankBoundEl.elIndex);
	bound.rankObj.elIndex = rankBoundEl.elIndex;
	bound.rankObj.elType = elObjDb.getElementType(rankBoundEl.elIndex);
	bound.rankObj.subObj = geoIndex;
	bound.rankObj.ithObj = rankBoundEl.ithObject;
83
	
84
85
86
87
88
89
90
91
	if (geoIndex == FACE) {
	  for (int edgeNo = 0; edgeNo < 3; edgeNo++) {
	    int edgeOfFace = 
	      bound.rankObj.el->getEdgeOfFace(bound.rankObj.ithObj, edgeNo);
	    
	    bound.rankObj.excludedSubstructures.push_back(make_pair(EDGE, edgeOfFace));
	  }
	}
92
	
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	
	if (owner == mpiRank) {
	  for (map<int, ElementObjectData>::iterator it2 = objData.begin();
	       it2 != objData.end(); ++it2) {
	    if (it2->first == mpiRank)
	      continue;
	    
	    bound.neighObj.el = elObjDb.getElementPtr(it2->second.elIndex);
	    bound.neighObj.elIndex = it2->second.elIndex;
	    bound.neighObj.elType = elObjDb.getElementType(it2->second.elIndex);
	    bound.neighObj.subObj = geoIndex;
	    bound.neighObj.ithObj = it2->second.ithObject;
	    
	    bound.type = INTERIOR;
	    
108
	    AtomicBoundary& b = getNewOwn(it2->first);
109
110
111
112
113
114
115
116
117
118
119
120
	    b = bound;
	    if (geoIndex == EDGE)
	      b.neighObj.reverseMode = 
		elObjDb.getEdgeReverseMode(rankBoundEl, it2->second);
	    if (geoIndex == FACE)
	      b.neighObj.reverseMode = 
		elObjDb.getFaceReverseMode(rankBoundEl, it2->second);
	  }
	  
	} else {
	  TEST_EXIT_DBG(objData.count(owner) == 1)
	    ("Should not happen!\n");
121
	  
122
123
124
125
126
127
128
129
130
131
	  ElementObjectData& ownerBoundEl = objData[owner];
	  
	  bound.neighObj.el = elObjDb.getElementPtr(ownerBoundEl.elIndex);
	  bound.neighObj.elIndex = ownerBoundEl.elIndex;
	  bound.neighObj.elType = -1;
	  bound.neighObj.subObj = geoIndex;
	  bound.neighObj.ithObj = ownerBoundEl.ithObject;
	  
	  bound.type = INTERIOR;
	  
132
	  AtomicBoundary& b = getNewOther(owner);
133
134
135
136
137
138
139
140
	  b = bound;	    
	  if (geoIndex == EDGE)
	    b.rankObj.reverseMode =
	      elObjDb.getEdgeReverseMode(rankBoundEl, ownerBoundEl);
	  if (geoIndex == FACE)
	    b.rankObj.reverseMode = 
	      elObjDb.getFaceReverseMode(rankBoundEl, ownerBoundEl);
	}
141
      }
142
143
144
    }


145
    // === Create periodic boundary data structure. ===
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
    for (PerBoundMap<DegreeOfFreedom>::iterator it = elObjDb.getPeriodicVertices().begin();
	 it != elObjDb.getPeriodicVertices().end(); ++it) {
      if (elObjDb.isInRank(it->first.first, mpiRank) == false)
	continue;

      ElementObjectData& perDofEl0 = 
	elObjDb.getElementsInRank(it->first.first)[mpiRank];

      for (map<int, ElementObjectData>::iterator elIt = elObjDb.getElementsInRank(it->first.second).begin();
	   elIt != elObjDb.getElementsInRank(it->first.second).end(); ++elIt) {

	int otherElementRank = elIt->first;
	ElementObjectData& perDofEl1 = elIt->second;

	AtomicBoundary bound;
	bound.rankObj.el = elObjDb.getElementPtr(perDofEl0.elIndex);
	bound.rankObj.elIndex = perDofEl0.elIndex;
	bound.rankObj.elType = elObjDb.getElementType(perDofEl0.elIndex);
	bound.rankObj.subObj = VERTEX;
	bound.rankObj.ithObj = perDofEl0.ithObject;

	bound.neighObj.el = elObjDb.getElementPtr(perDofEl1.elIndex);
	bound.neighObj.elIndex = perDofEl1.elIndex;
	bound.neighObj.elType = elObjDb.getElementType(perDofEl1.elIndex);
	bound.neighObj.subObj = VERTEX;
	bound.neighObj.ithObj = perDofEl1.ithObject;

	bound.type = it->second;

176
	AtomicBoundary& b = getNewPeriodic(otherElementRank);
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
	b = bound;	    
      }
    }


    for (PerBoundMap<DofEdge>::iterator it = elObjDb.getPeriodicEdges().begin();
	 it != elObjDb.getPeriodicEdges().end(); ++it) {
      if (elObjDb.isInRank(it->first.first, mpiRank) == false)
	continue;

      ElementObjectData& perEdgeEl0 = elObjDb.getElementsInRank(it->first.first)[mpiRank];

      for (map<int, ElementObjectData>::iterator elIt = elObjDb.getElementsInRank(it->first.second).begin();
 	   elIt != elObjDb.getElementsInRank(it->first.second).end(); ++elIt) {
      
	int otherElementRank = elIt->first;
	ElementObjectData& perEdgeEl1 = elIt->second;

	AtomicBoundary bound;	    	    
	bound.rankObj.el = elObjDb.getElementPtr(perEdgeEl0.elIndex);
	bound.rankObj.elIndex = perEdgeEl0.elIndex;
	bound.rankObj.elType = elObjDb.getElementType(perEdgeEl0.elIndex);
	bound.rankObj.subObj = EDGE;
	bound.rankObj.ithObj = perEdgeEl0.ithObject;
	
	bound.neighObj.el = elObjDb.getElementPtr(perEdgeEl1.elIndex);
	bound.neighObj.elIndex = perEdgeEl1.elIndex;
	bound.neighObj.elType = elObjDb.getElementType(perEdgeEl1.elIndex);
	bound.neighObj.subObj = EDGE;
	bound.neighObj.ithObj = perEdgeEl1.ithObject;
	
	bound.type = it->second;
	
210
	AtomicBoundary& b = getNewPeriodic(otherElementRank);
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
	b = bound;
     
	if (mpiRank > otherElementRank)
	  b.neighObj.reverseMode = 
	    elObjDb.getEdgeReverseMode(perEdgeEl0, perEdgeEl1);
	else
	  b.rankObj.reverseMode = 
	    elObjDb.getEdgeReverseMode(perEdgeEl0, perEdgeEl1);
      }
    }


    for (PerBoundMap<DofFace>::iterator it = elObjDb.getPeriodicFaces().begin();
	 it != elObjDb.getPeriodicFaces().end(); ++it) {
      if (elObjDb.isInRank(it->first.first, mpiRank) == false)
	continue;

      TEST_EXIT_DBG(elObjDb.getElements(it->first.first).size() == 1)
 	("Should not happen!\n");
      TEST_EXIT_DBG(elObjDb.getElements(it->first.second).size() == 1)
 	("Should not happen!\n");

      ElementObjectData& perFaceEl0 = elObjDb.getElementsInRank(it->first.first)[mpiRank];

      for (map<int, ElementObjectData>::iterator elIt = elObjDb.getElementsInRank(it->first.second).begin();
 	   elIt != elObjDb.getElementsInRank(it->first.second).end(); ++elIt) {
      
	int otherElementRank = elIt->first;
	ElementObjectData& perFaceEl1 = elIt->second;

	AtomicBoundary bound;	    	    
	bound.rankObj.el = elObjDb.getElementPtr(perFaceEl0.elIndex);
	bound.rankObj.elIndex = perFaceEl0.elIndex;
	bound.rankObj.elType = elObjDb.getElementType(perFaceEl0.elIndex);
	bound.rankObj.subObj = FACE;
	bound.rankObj.ithObj = perFaceEl0.ithObject;
	
	bound.neighObj.el = elObjDb.getElementPtr(perFaceEl1.elIndex);
	bound.neighObj.elIndex = perFaceEl1.elIndex;
	bound.neighObj.elType = elObjDb.getElementType(perFaceEl1.elIndex);
	bound.neighObj.subObj = FACE;
	bound.neighObj.ithObj = perFaceEl1.ithObject;
	
	bound.type = it->second;
	
256
	AtomicBoundary& b = getNewPeriodic(otherElementRank);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	b = bound;
     
	if (mpiRank > otherElementRank)
	  b.neighObj.reverseMode = 
	    elObjDb.getFaceReverseMode(perFaceEl0, perFaceEl1);
	else
	  b.rankObj.reverseMode = 
	    elObjDb.getFaceReverseMode(perFaceEl0, perFaceEl1);
      }
    }
    

    // === Once we have this information, we must care about the order of the ===
    // === atomic bounds in the three boundary handling object. Eventually    ===
    // === all the boundaries have to be in the same order on both ranks that ===
    // === share the bounday.                                                 ===

    StdMpi<vector<AtomicBoundary> > stdMpi(mpiComm);
    stdMpi.send(own);
    stdMpi.recv(other);
    stdMpi.startCommunication();


    // === The information about all neighbouring boundaries has been         ===
    // === received. So the rank tests if its own atomic boundaries are in    ===
    // === the same order. If not, the atomic boundaries are swaped to the    ===
    // === correct order.                                                     ===

    for (RankToBoundMap::iterator rankIt = other.begin();
	 rankIt != other.end(); ++rankIt) {

      // === We have received from rank "rankIt->first" the ordered list of   ===
      // === element indices. Now, we have to sort the corresponding list in  ===
      // === this rank to get the same order.                                 ===
     
      for (unsigned int j = 0; j < rankIt->second.size(); j++) {

	// If the expected object is not at place, search for it.

	BoundaryObject &recvedBound = stdMpi.getRecvData()[rankIt->first][j].rankObj;

	if ((rankIt->second)[j].neighObj != recvedBound) {
	  unsigned int k = j + 1;

	  for (; k < rankIt->second.size(); k++)
 	    if ((rankIt->second)[k].neighObj == recvedBound)
	      break;

	  // The element must always be found, because the list is just in
	  // another order.
	  TEST_EXIT_DBG(k < rankIt->second.size())("Should never happen!\n");

	  // Swap the current with the found element.
	  AtomicBoundary tmpBound = (rankIt->second)[k];
	  (rankIt->second)[k] = (rankIt->second)[j];
	  (rankIt->second)[j] = tmpBound;	
	}
      }
    }


    // === Do the same for the periodic boundaries. ===

    if (periodic.size() > 0) {
      stdMpi.clear();

      RankToBoundMap sendBounds, recvBounds;
      for (RankToBoundMap::iterator rankIt = periodic.begin();
	   rankIt != periodic.end(); ++rankIt) {

	if (rankIt->first == mpiRank)
	  continue;

	if (rankIt->first < mpiRank)
	  sendBounds[rankIt->first] = rankIt->second;
	else
	  recvBounds[rankIt->first] = rankIt->second;	
      }

      stdMpi.send(sendBounds);
      stdMpi.recv(recvBounds);
      stdMpi.startCommunication();

      for (RankToBoundMap::iterator rankIt = periodic.begin();
	   rankIt != periodic.end(); ++rankIt) {

 	if (rankIt->first <= mpiRank)
 	  continue;
  
	for (unsigned int j = 0; j < rankIt->second.size(); j++) {
	  BoundaryObject &recvRankObj = 
	    stdMpi.getRecvData()[rankIt->first][j].rankObj;
	  BoundaryObject &recvNeighObj = 
	    stdMpi.getRecvData()[rankIt->first][j].neighObj;

	  if (periodic[rankIt->first][j].neighObj != recvRankObj ||
	      periodic[rankIt->first][j].rankObj != recvNeighObj) {
	    unsigned int k = j + 1;	    
	    for (; k < rankIt->second.size(); k++)
	      if (periodic[rankIt->first][k].neighObj == recvRankObj &&
		  periodic[rankIt->first][k].rankObj == recvNeighObj)
		break;
	    
	    // The element must always be found, because the list is just in 
	    // another order.
	    TEST_EXIT_DBG(k < rankIt->second.size())("Should never happen!\n");
363

364
365
366
367
368
369
370
371
	    // Swap the current with the found element.
	    AtomicBoundary tmpBound = (rankIt->second)[k];
	    (rankIt->second)[k] = (rankIt->second)[j];
	    (rankIt->second)[j] = tmpBound;	
	  } 
	}
      }     
    } // periodicBoundary.boundary.size() > 0
372
373
374
  }


375
  void InteriorBoundary::serialize(ostream &out)
376
  {
377
378
379
380
    serialize(out, own);
    serialize(out, other);
    serialize(out, periodic);
  }
381

382

383
384
385
386
387
  void InteriorBoundary::serialize(ostream &out,
				   RankToBoundMap& boundary)
  {
    FUNCNAME("InteriorBoundary::serialize()");

388
    int mSize = boundary.size();
389
    SerUtil::serialize(out, mSize);
390
391
    for (RankToBoundMap::iterator it = boundary.begin(); 
	 it != boundary.end(); ++it) {
392
393
      int rank = it->first;
      int boundSize = it->second.size();
394
395
      SerUtil::serialize(out, rank);
      SerUtil::serialize(out, boundSize);
396
397
398
      for (int i = 0; i < boundSize; i++) {
	AtomicBoundary &bound = (it->second)[i];

399
	SerUtil::serialize(out, bound.rankObj.elIndex);
400
	SerUtil::serialize(out, bound.rankObj.elType);
401
402
	SerUtil::serialize(out, bound.rankObj.subObj);
	SerUtil::serialize(out, bound.rankObj.ithObj);
403
	SerUtil::serialize(out, bound.rankObj.reverseMode);
404
	serializeExcludeList(out, bound.rankObj.excludedSubstructures);
405

406
	SerUtil::serialize(out, bound.neighObj.elIndex);
407
	SerUtil::serialize(out, bound.neighObj.elType);
408
409
	SerUtil::serialize(out, bound.neighObj.subObj);
	SerUtil::serialize(out, bound.neighObj.ithObj);
410
	SerUtil::serialize(out, bound.neighObj.reverseMode);
411
	serializeExcludeList(out, bound.neighObj.excludedSubstructures);
412
413

	SerUtil::serialize(out, bound.type);
414
415
      }
    }
416
417
  }

418

419
  void InteriorBoundary::deserialize(istream &in, Mesh *mesh)				     
420
  {
421
422
423
424
425
426
427
    map<int, Element*> elIndexMap;
    mesh->getElementIndexMap(elIndexMap);

    deserialize(in, own, elIndexMap);
    deserialize(in, other, elIndexMap);
    deserialize(in, periodic, elIndexMap);
  }
428

429

430
431
432
433
434
435
  void InteriorBoundary::deserialize(istream &in, 
				     RankToBoundMap& boundary,
				     map<int, Element*> &elIndexMap)
  {
    FUNCNAME("InteriorBoundary::deserialize()");

436
    int mSize = 0;
437
    SerUtil::deserialize(in, mSize);
438
439
440
    for (int i = 0; i < mSize; i++) {
      int rank = 0;
      int boundSize = 0;
441
442
      SerUtil::deserialize(in, rank);
      SerUtil::deserialize(in, boundSize);
443
444
445
446
447

      boundary[rank].resize(boundSize);
      for (int i = 0; i < boundSize; i++) {
	AtomicBoundary &bound = boundary[rank][i];

448
	SerUtil::deserialize(in, bound.rankObj.elIndex);
449
	SerUtil::deserialize(in, bound.rankObj.elType);
450
451
	SerUtil::deserialize(in, bound.rankObj.subObj);
	SerUtil::deserialize(in, bound.rankObj.ithObj);
452
	SerUtil::deserialize(in, bound.rankObj.reverseMode);
453
	deserializeExcludeList(in, bound.rankObj.excludedSubstructures);
454

455
	SerUtil::deserialize(in, bound.neighObj.elIndex);
456
	SerUtil::deserialize(in, bound.neighObj.elType);
457
458
	SerUtil::deserialize(in, bound.neighObj.subObj);
	SerUtil::deserialize(in, bound.neighObj.ithObj);
459
	SerUtil::deserialize(in, bound.neighObj.reverseMode);
460
	deserializeExcludeList(in, bound.neighObj.excludedSubstructures);
461

462
463
	SerUtil::deserialize(in, bound.type);

464
465
466
467
	TEST_EXIT_DBG(elIndexMap.count(bound.rankObj.elIndex) == 1)
	  ("Cannot find element with index %d for deserialization!\n", 
	   bound.rankObj.elIndex);

468
469
470
	TEST_EXIT_DBG(elIndexMap[bound.rankObj.elIndex]->getIndex() == 
		      bound.rankObj.elIndex)("Should not happen!\n");

471
	bound.rankObj.el = elIndexMap[bound.rankObj.elIndex];
472

473
474
	// For the case of periodic interior boundaries, a rank may have an
	// boundary with itself. In this case, also the pointer to the neighbour
475
	// object must be set correctly.
476
477
478
479
	if (elIndexMap.count(bound.neighObj.elIndex))
	  bound.neighObj.el = elIndexMap[bound.neighObj.elIndex];
	else
	  bound.neighObj.el = NULL;
480
481
      }
    }
482
  }
483
484


485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  AtomicBoundary& InteriorBoundary::getNewOwn(int rank)
  {
    int size = own[rank].size();
    own[rank].resize(size + 1);
    return own[rank][size];
  }


  AtomicBoundary& InteriorBoundary::getNewOther(int rank)
  {
    int size = other[rank].size();
    other[rank].resize(size + 1);
    return other[rank][size];
  }


  AtomicBoundary& InteriorBoundary::getNewPeriodic(int rank)
  {
    int size = periodic[rank].size();
    periodic[rank].resize(size + 1);
    return periodic[rank][size];
  }


509
  void InteriorBoundary::serializeExcludeList(ostream &out, 
510
					      ExcludeList &list)
511
512
513
514
515
516
517
518
519
520
  {
    int size = list.size();
    SerUtil::serialize(out, size);
    for (int i = 0; i < size; i++) {
      SerUtil::serialize(out, list[i].first);
      SerUtil::serialize(out, list[i].second);
    }
  }


521
  void InteriorBoundary::deserializeExcludeList(istream &in, 
522
						ExcludeList &list)
523
524
525
526
527
528
529
530
531
532
533
534
  {
    int size = 0;
    SerUtil::deserialize(in, size);
    list.resize(0);
    list.reserve(size);

    for (int i = 0; i < size; i++) {
      GeoIndex a;
      int b;

      SerUtil::deserialize(in, a);
      SerUtil::deserialize(in, b);
535
      list.push_back(make_pair(a, b));
536
537
538
    }
  }

539
}