ParallelDomainProblem.cc 5.69 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
    std::map<int, std::set<int> > partitionDofs;
49
50
51
52
53
54

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

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

      // 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()]);
          
68
69
70
      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
71
    std::vector<int> rankDofs;
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
    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);
	    }
	  }
	}
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
99
100
101
102
103
104
105
    if (mpiRank == 1) {
      std::cout << "RANKS dofs = ";
      for (int i = 0; i < rankDofs.size(); i++) 
	std::cout << rankDofs[i] << " ";
      std::cout << std::endl;
    }

106
107
    // === Remove all macro elements that are not part of the rank partition. ===

108
109
110
111
112
113
114
115
116
    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);
117
      }
118
119
120
121
    }

    mesh->removeMacroElements(macrosToRemove);

122
123
    // === Create local and global dofs ordering. ===

124
125
126
127
128
129
130
131
132
133
    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);
134
135
    rstart -= nRankDOFs;
    
136
    for (int i = 0; i < nRankDOFs; i++) {
137
      lOrder[i] = rstart + i;
138
139
140
    }

    AOCreateBasic(PETSC_COMM_WORLD, nRankDOFs, gOrder, lOrder, &applicationOrdering);
Thomas Witkowski's avatar
Thomas Witkowski committed
141
    
142
143
    free(gOrder);
    free(lOrder);
144
145
146
147
  }

  void ParallelDomainProblemBase::exitParallelization(AdaptInfo *adaptInfo)
  {
148
    AODestroy(applicationOrdering);
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
198
199
200
201
  }

  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)
202
    : ParallelDomainProblemBase(name, problem, problemInstat, problem->getFESpace())
203
204
205
206
207
  {
  }


}