ParallelDomainProblem.cc 5.31 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
						       Mesh *m)
18
19
20
    : iterationIF(iIF),
      timeIF(tIF),
      mesh(m),
21
      initialPartitionMesh(true),
22
      nRankDOFs(0)
23
24
25
26
27
28
29
  {
    mpiRank = MPI::COMM_WORLD.Get_rank();
    mpiSize = MPI::COMM_WORLD.Get_size();
    mpiComm = MPI::COMM_WORLD;
    partitioner = new ParMetisPartitioner(mesh, &mpiComm);
  }

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

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

47
48
    std::vector<int> rankDofs;
    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);
    }

71
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);
	    }
	  }
	}
      }
    }

99
100
101
102
103
104
105
106
107
    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);
108
      }
109
110
111
112
    }

    mesh->removeMacroElements(macrosToRemove);

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    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);

    for (int i = 0; i < nRankDOFs; i++) {
      lOrder[i] = rstart - nRankDOFs + i;
    }

    AOCreateBasic(PETSC_COMM_WORLD, nRankDOFs, gOrder, lOrder, &applicationOrdering);

    free(gOrder);
    free(lOrder);
132
133
134
135
  }

  void ParallelDomainProblemBase::exitParallelization(AdaptInfo *adaptInfo)
  {
136
    AODestroy(applicationOrdering);
137
138
139
140
141
142
143
144
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
  }

  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)
    : ParallelDomainProblemBase(name, problem, problemInstat, problem->getMesh())
  {
  }


}