ParallelDomainProblem.cc 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "ParallelDomainProblem.h"
#include "ProblemScal.h"
#include "ProblemInstat.h"
#include "ParMetisPartitioner.h"
#include "Mesh.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "Element.h"
#include "MacroElement.h"
#include "PartitionElementData.h"
11
12
#include "DOFMatrix.h"
#include "DOFVector.h"
13
14
15
16

namespace AMDiS {

  ParallelDomainProblemBase::ParallelDomainProblemBase(const std::string& name,
17
18
						       ProblemIterationInterface *iIF,
						       ProblemTimeInterface *tIF,
19
						       FiniteElemSpace *fe)
20
21
    : iterationIF(iIF),
      timeIF(tIF),
22
23
      feSpace(fe),
      mesh(fe->getMesh()),
24
      initialPartitionMesh(true),
25
      nRankDOFs(0)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  {
    mpiRank = MPI::COMM_WORLD.Get_rank();
    mpiSize = MPI::COMM_WORLD.Get_size();
    mpiComm = MPI::COMM_WORLD;
    partitioner = new ParMetisPartitioner(mesh, &mpiComm);
  }

  void ParallelDomainProblemBase::initParallelization(AdaptInfo *adaptInfo)
  {
    if (mpiSize <= 1)
      return;

    // create an initial partitioning of the mesh
    partitioner->createPartitionData();
    // set the element weights, which are 1 at the very first begin
    setElemWeights(adaptInfo);
    // and now partition the mesh
    partitionMesh(adaptInfo);   


Thomas Witkowski's avatar
Thomas Witkowski committed
46
47
48
    /// === Determine to each dof the set of partitions the dof belongs to. ===

    std::map<const DegreeOfFreedom*, std::set<int> > partitionDofs;
49
50
51
52
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *element = elInfo->getElement();
53
54
55

      // Determine to each dof the partition(s) it corresponds to.
      for (int i = 0; i < 3; i++) 
Thomas Witkowski's avatar
Thomas Witkowski committed
56
	partitionDofs[element->getDOF(i)].insert(partitionVec[element->getIndex()]);
57
          
58
59
60
      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
61
62
63
64
    /// === Determine the set of ranks dofs and the dofs ownership at the boundary. ===

    std::vector<const DegreeOfFreedom*> rankDofs;
    for (std::map<const DegreeOfFreedom*, std::set<int> >::iterator it = partitionDofs.begin();
65
66
67
68
69
70
71
72
73
	 it != partitionDofs.end();
	 ++it) {
      for (std::set<int>::iterator itpart1 = it->second.begin();
	   itpart1 != it->second.end();
	   ++itpart1) {
	if (*itpart1 == mpiRank) {
	  if (it->second.size() == 1) {
	    rankDofs.push_back(it->first);
	  } else {	    
Thomas Witkowski's avatar
Thomas Witkowski committed
74
75
76
	    // This dof is at the ranks boundary. It is owned by the rank only if
	    // the rank number is the highest of all ranks containing this dof.

77
	    bool insert = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
78
	    int highestRank = mpiRank;
79
80
81
	    for (std::set<int>::iterator itpart2 = it->second.begin();
		 itpart2 != it->second.end();
		 ++itpart2) {
Thomas Witkowski's avatar
Thomas Witkowski committed
82
	      if (*itpart2 > mpiRank)
83
		insert = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
84
85
86

	      if (*itpart2 > highestRank)
		highestRank = *itpart2;
87
	    }
Thomas Witkowski's avatar
Thomas Witkowski committed
88
89

	    if (insert)
90
	      rankDofs.push_back(it->first);
Thomas Witkowski's avatar
Thomas Witkowski committed
91
92

	    boundaryDofs[it->first] = highestRank;
93
94
95
96
97
	  }
	}
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    // === Create interior boundary information ===

    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL | Mesh::FILL_NEIGH);
    while (elInfo) {
      Element *element = elInfo->getElement();

      // Hidde elements which are not part of ranks partition.
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>(element->getElementData(PARTITION_ED));   
      if (partitionData->getPartitionStatus() == IN) {
	for (int i = 0; i < 3; i++) {
	  if (!elInfo->getNeighbour(i))
	    continue;

	  PartitionElementData *neighbourPartitionData =
	    dynamic_cast<PartitionElementData*>(elInfo->getNeighbour(i)->getElementData(PARTITION_ED));
 	  if (neighbourPartitionData->getPartitionStatus() == OUT) {
 	    AtomicBoundary& bound = interiorBoundary.
	      getNewAtomicBoundary(partitionVec[elInfo->getNeighbour(i)->getIndex()]);
 	    bound.rankObject.el = element;
 	    bound.rankObject.subObjAtBoundary = EDGE;
 	    bound.rankObject.ithObjAtBoundary = i;
 	    bound.neighbourObject.el = elInfo->getNeighbour(i);
 	    bound.neighbourObject.subObjAtBoundary = EDGE;
 	    bound.neighbourObject.ithObjAtBoundary = -1;
 	  }
	}
      }

      elInfo = stack.traverseNext(elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
129
130
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
131

132
133
    // === Remove all macro elements that are not part of the rank partition. ===

134
135
136
137
138
139
140
141
142
    std::vector<MacroElement*> macrosToRemove;
    for (std::deque<MacroElement*>::iterator it = mesh->firstMacroElement();
	 it != mesh->endOfMacroElements();
	 ++it) {
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>
	((*it)->getElement()->getElementData(PARTITION_ED));
      if (partitionData->getPartitionStatus() != IN) {
	macrosToRemove.push_back(*it);
143
      }
144
145
146
147
    }

    mesh->removeMacroElements(macrosToRemove);

148
149
    // === Create local and global dofs ordering. ===

150
151
152
    int *gOrder = (int*)(malloc(sizeof(int) * rankDofs.size()));
    int *lOrder = (int*)(malloc(sizeof(int) * rankDofs.size()));

Thomas Witkowski's avatar
Thomas Witkowski committed
153
    for (std::vector<const DegreeOfFreedom*>::iterator it = rankDofs.begin();
154
	 it != rankDofs.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
155
      gOrder[nRankDOFs++] = (*it)[0];
156
157
158
159
    }

    int rstart = 0;
    MPI_Scan(&nRankDOFs, &rstart, 1, MPI_INT, MPI_SUM, PETSC_COMM_WORLD);
160
    rstart -= nRankDOFs;
Thomas Witkowski's avatar
Thomas Witkowski committed
161
   
162
    for (int i = 0; i < nRankDOFs; i++) {
163
      lOrder[i] = rstart + i;
164
165
166
    }

    AOCreateBasic(PETSC_COMM_WORLD, nRankDOFs, gOrder, lOrder, &applicationOrdering);
Thomas Witkowski's avatar
Thomas Witkowski committed
167
    
168
169
    free(gOrder);
    free(lOrder);
Thomas Witkowski's avatar
Thomas Witkowski committed
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
256
257
258
259
260
261
262
263
264
265
266

    /// === Create information which dof indices must be send and which must be received. ===

    std::map<int, std::map<DegreeOfFreedom, DegreeOfFreedom> > sendNewDofs;
    std::map<int, std::vector<DegreeOfFreedom> > recvNewDofs;

    for (std::map<const DegreeOfFreedom*, int>::iterator it = boundaryDofs.begin();
	 it != boundaryDofs.end();
	 ++it) {
      if (it->second == mpiRank) {
	int oldDofIndex = (it->first)[0];
	int newDofIndex = 0;
	for (int i = 0; i < static_cast<int>(rankDofs.size()); i++) {
	  if (rankDofs[i] == it->first) {
	    newDofIndex = rstart + i;
	    break;
	  }
	}

	for (std::set<int>::iterator itRanks = partitionDofs[it->first].begin();
	     itRanks != partitionDofs[it->first].end();
	     ++itRanks) {
	  if (*itRanks != mpiRank) {
	    sendNewDofs[*itRanks][oldDofIndex] = newDofIndex;
	  }
	}
      } else {
	recvNewDofs[it->second].push_back((it->first)[0]);
      }
    }

    /// === Send and receive the dof indices at boundary. ===

    std::vector<int*> sendBuffers(sendNewDofs.size());
    std::vector<int*> recvBuffers(recvNewDofs.size());
    
    int i = 0;
    for (std::map<int, std::map<DegreeOfFreedom, DegreeOfFreedom> >::iterator sendIt = sendNewDofs.begin();
	 sendIt != sendNewDofs.end();
	 ++sendIt, i++) {
      sendBuffers[i] = new int[sendIt->second.size() * 2];
      int c = 0;
      for (std::map<DegreeOfFreedom, DegreeOfFreedom>::iterator dofIt = sendIt->second.begin();
	   dofIt != sendIt->second.end();
	   ++dofIt, c += 2) {
	sendBuffers[i][c] = dofIt->first;
	sendBuffers[i][c + 1] = dofIt->second;
      }

      mpiComm.Isend(sendBuffers[i], sendIt->second.size() * 2, MPI_INT, sendIt->first, 0);
    }

    i = 0;
    for (std::map<int, std::vector<DegreeOfFreedom> >::iterator recvIt = recvNewDofs.begin();
	 recvIt != recvNewDofs.end();
	 ++recvIt, i++) {
      recvBuffers[i] = new int[recvIt->second.size() * 2];
      
      mpiComm.Irecv(recvBuffers[i], recvIt->second.size() * 2, MPI_INT, recvIt->first, 0);
    }


    mpiComm.Barrier();

    /// === Change dof indices at boundary from other ranks. ===

    i = 0;
    for (std::map<int, std::vector<DegreeOfFreedom> >::iterator recvIt = recvNewDofs.begin();
	 recvIt != recvNewDofs.end();
	 ++recvIt, i++) {

      for (int j = 0; j < static_cast<int>(recvIt->second.size()); j++) {
	for (std::map<const DegreeOfFreedom*, int>::iterator dofIt = boundaryDofs.begin();
	     dofIt != boundaryDofs.end();
	     ++dofIt) {
	  if ((dofIt->first)[0] == recvBuffers[i][j * 2]) {
	    const_cast<DegreeOfFreedom*>(dofIt->first)[0] = recvBuffers[i][j * 2 + 1];
	    break;
	  }
	}
      }

      delete [] recvBuffers[i];
    }

    i = 0;
    for (std::map<int, std::map<DegreeOfFreedom, DegreeOfFreedom> >::iterator sendIt = sendNewDofs.begin();
	 sendIt != sendNewDofs.end();
	 ++sendIt, i++) {
      delete [] sendBuffers[i];
    }

    /// === Change dof indices for rank partition. ===

    for (int i = 0; i < static_cast<int>(rankDofs.size()); i++) {
      const_cast<DegreeOfFreedom*>(rankDofs[i])[0] = rstart + i;
    }
267
268
269
270
271
272
273
274
275
276
277

    /// === Create petsc matrix. ===
    int ierr;
    ierr = MatCreate(PETSC_COMM_WORLD, &petscMatrix);
    ierr = MatSetSizes(petscMatrix, rankDofs.size(), rankDofs.size(),
		       partitionDofs.size(), partitionDofs.size());
    ierr = MatSetType(petscMatrix, MATAIJ);

    ierr = VecCreate(PETSC_COMM_WORLD, &petscRhsVec);
    ierr = VecSetSizes(petscRhsVec, rankDofs.size(), partitionDofs.size());    
    ierr = VecSetType(petscRhsVec, VECMPI);
278
279
280
281
  }

  void ParallelDomainProblemBase::exitParallelization(AdaptInfo *adaptInfo)
  {
282
    AODestroy(applicationOrdering);
283
284
  }

285
286
287
  void ParallelDomainProblemBase::fillPetscMatrix(DOFMatrix *mat, 
						  DOFVector<double> *vec)
  {
288
    /*   DOFMatrix::Iterator rowIt(mat, USED_DOFS);
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    for (rowIt.reset(); !rowIt.end(); ++rowIt) {
      for (int i = 0; i < static_cast<int>((*rowIt).size()); i++) {
	if ((*rowIt)[i].col >= 0) {
	  MatSetValues(petscMatrix, 1, &i, 1, &((*rowIt)[i].col), &((*rowIt)[i].entry), ADD_VALUES);
	}
      }
    }

    MatAssemblyBegin(petscMatrix, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(petscMatrix, MAT_FINAL_ASSEMBLY);

    DOFVector<double>::Iterator dofIt(vec, USED_DOFS);
    for (dofIt.reset(); !dofIt.end(); ++dofIt) {
      int index = dofIt.getDOFIndex();
      double value = *dofIt;

      VecSetValues(petscRhsVec, 1, &index, &value, ADD_VALUES);
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
  double ParallelDomainProblemBase::setElemWeights(AdaptInfo *adaptInfo) 
  {
    double localWeightSum = 0.0;
    int elNum = -1;

    elemWeights.clear();

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1,
					 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      Element *element = elInfo->getElement();

      // get partition data
      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));

      if (partitionData && partitionData->getPartitionStatus() == IN) {
	if (partitionData->getLevel() == 0) {
	  elNum = element->getIndex();
	}
	TEST_EXIT(elNum != -1)("invalid element number\n");
	if (element->isLeaf()) {
	  elemWeights[elNum] += 1.0;
	  localWeightSum += 1.0;
	}
      }

      elInfo = stack.traverseNext(elInfo);
    }

    return localWeightSum;
  }

  void ParallelDomainProblemBase::partitionMesh(AdaptInfo *adaptInfo)
  {
    if (initialPartitionMesh) {
      initialPartitionMesh = false;
      partitioner->fillCoarsePartitionVec(&oldPartitionVec);
      partitioner->partition(&elemWeights, INITIAL);
    } else {
      oldPartitionVec = partitionVec;
      partitioner->partition(&elemWeights, ADAPTIVE_REPART, 100.0 /*0.000001*/);
    }    

    partitioner->fillCoarsePartitionVec(&partitionVec);
  }

  ParallelDomainProblemScal::ParallelDomainProblemScal(const std::string& name,
						       ProblemScal *problem,
						       ProblemInstatScal *problemInstat)
360
361
    : ParallelDomainProblemBase(name, problem, problemInstat, problem->getFESpace()),
      probScal(problem)
362
363
364
  {
  }

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  Flag ParallelDomainProblemScal::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    //    return iterationIF->oneIteration(adaptInfo, toDo);

    Flag flag = dynamic_cast<StandardProblemIteration*>(iterationIF)->buildAndAdapt(adaptInfo, toDo);

    fillPetscMatrix(probScal->getSystemMatrix(), probScal->getRHS());

//     if (toDo.isSet(SOLVE))
//       iterationIF->getProblem()->solve(adaptInfo, false);

//     if (toDo.isSet(SOLVE_RHS))
//       iterationIF->getProblem()->solve(adaptInfo, true);

//     if (toDo.isSet(ESTIMATE)) 
//       iterationIF->getProblem()->estimate(adaptInfo);

    return flag;

  }
385
386

}