ParallelDomainProblem.cc 5.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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"

namespace AMDiS {

  ParallelDomainProblemBase::ParallelDomainProblemBase(const std::string& name,
15
16
						       ProblemIterationInterface *iIF,
						       ProblemTimeInterface *tIF,
17
						       FiniteElemSpace *fe)
18
19
    : iterationIF(iIF),
      timeIF(tIF),
20
21
      feSpace(fe),
      mesh(fe->getMesh()),
22
      initialPartitionMesh(true),
23
      nRankDOFs(0)
24
25
26
27
28
29
30
  {
    mpiRank = MPI::COMM_WORLD.Get_rank();
    mpiSize = MPI::COMM_WORLD.Get_size();
    mpiComm = MPI::COMM_WORLD;
    partitioner = new ParMetisPartitioner(mesh, &mpiComm);
  }

31
32
33
34
35
  Flag ParallelDomainProblemBase::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    return iterationIF->oneIteration(adaptInfo, toDo);
  }

36
37
38
39
40
41
42
43
44
45
46
47
  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);   

48
49
    std::vector<int> rankDofs;
    std::map<int, std::set<int> > partitionDofs;
50
51
52
53
54
55

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    int nLeaves = 0;
    while (elInfo) {
      Element *element = elInfo->getElement();
56
57

      // Hidde elements which are not part of ranks partition.
58
59
60
61
62
63
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>
	(element->getElementData(PARTITION_ED));
      if (partitionData->getPartitionStatus() != IN) {
      } else {
      }
64
65
66
67
68

      // Determine to each dof the partition(s) it corresponds to.
      for (int i = 0; i < 3; i++) 
	partitionDofs[element->getDOF(i, 0)].insert(partitionVec[element->getIndex()]);
          
69
70
71
      elInfo = stack.traverseNext(elInfo);
    }

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    for (std::map<int, std::set<int> >::iterator it = partitionDofs.begin();
	 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 {	    
	    bool insert = true;
	    for (std::set<int>::iterator itpart2 = it->second.begin();
		 itpart2 != it->second.end();
		 ++itpart2) {
	      if (*itpart2 > mpiRank) {
		insert = false;
		break;
	      }
	    }
	    if (insert) {
	      rankDofs.push_back(it->first);
	    }
	  }
	}
      }
    }

100
101
    // === Remove all macro elements that are not part of the rank partition. ===

102
103
104
105
106
107
108
109
110
    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);
111
      }
112
113
114
115
    }

    mesh->removeMacroElements(macrosToRemove);

116
117
    // === Create local and global dofs ordering. ===

118
119
120
121
122
123
124
125
126
127
    int *gOrder = (int*)(malloc(sizeof(int) * rankDofs.size()));
    int *lOrder = (int*)(malloc(sizeof(int) * rankDofs.size()));

    for (std::vector<int>::iterator it = rankDofs.begin();
	 it != rankDofs.end(); ++it) {
      gOrder[nRankDOFs++] = *it;
    }

    int rstart = 0;
    MPI_Scan(&nRankDOFs, &rstart, 1, MPI_INT, MPI_SUM, PETSC_COMM_WORLD);
128
129
    rstart -= nRankDOFs;
    
130
    for (int i = 0; i < nRankDOFs; i++) {
131
      lOrder[i] = rstart + i;
132
133
    }

134
135
    mesh->createContinuousDofOrdering(feSpace, rstart);

136
137
138
139
    AOCreateBasic(PETSC_COMM_WORLD, nRankDOFs, gOrder, lOrder, &applicationOrdering);

    free(gOrder);
    free(lOrder);
140
141
142
143
  }

  void ParallelDomainProblemBase::exitParallelization(AdaptInfo *adaptInfo)
  {
144
    AODestroy(applicationOrdering);
145
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  }

  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)
198
    : ParallelDomainProblemBase(name, problem, problemInstat, problem->getFESpace())
199
200
201
202
203
  {
  }


}