ParallelDofMapping.cc 7.47 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/ParallelDofMapping.h"
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
14
#include "parallel/StdMpi.h"
15
16
17
18
19
20
21
22
23
24
25
26
27
28

namespace AMDiS {

  using namespace std;

  void GlobalDofMap::clear()
  {
    dofMap.clear();
    nRankDofs = 0;
    nOverallDofs = 0;
    rStartDofs = 0;
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
29
  void GlobalDofMap::update()
30
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
31
32
33
    for (map<DegreeOfFreedom, MultiIndex>::iterator it = dofMap.begin(); it != dofMap.end(); ++it)
      if (it->second.local == -1 && nonRankDofs.count(it->first) == 0)
	it->second.local = nRankDofs++;
34
35
36
37
38
    
    nOverallDofs = 0;
    rStartDofs = 0;
    mpi::getDofNumbering(*mpiComm, nRankDofs, rStartDofs, nOverallDofs);
    
Thomas Witkowski's avatar
Thomas Witkowski committed
39
    if (needGlobalMapping)
40
      computeGlobalMapping(rStartDofs);
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
41

42
    if (hasNonLocalDofs)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
43
      computeNonLocalIndices();
44
45
46
  }


47
  void GlobalDofMap::computeGlobalMapping(int offset)
48
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
49
    for (map<DegreeOfFreedom, MultiIndex>::iterator it = dofMap.begin(); it != dofMap.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
50
      it->second.global = it->second.local + offset;
51
52
  }

53

Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
54
55
56
57
58
59
60
61
62
63
  void GlobalDofMap::computeNonLocalIndices()
  {
    FUNCNAME("GlobalDofMap::computeNonLocalIndices()");

    typedef map<int, map<const FiniteElemSpace*, DofContainer> >::iterator it_type;

    StdMpi<vector<int> > stdMpi(*mpiComm);
    for (DofComm::Iterator it(*sendDofs, feSpace); !it.end(); it.nextRank())
      for (; !it.endDofIter(); it.nextDof())
	if (dofMap.count(it.getDofIndex()) && !nonRankDofs.count(it.getDofIndex()))
Thomas Witkowski's avatar
Thomas Witkowski committed
64
	  stdMpi.getSendData(it.getRank()).push_back(dofMap[it.getDofIndex()].global);
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    stdMpi.updateSendDataSize();

    for (DofComm::Iterator it(*recvDofs, feSpace); !it.end(); it.nextRank()) {
      bool recvFromRank = false;
      for (; !it.endDofIter(); it.nextDof()) {
	if (nonRankDofs.count(it.getDofIndex())) {
	  recvFromRank = true;
	  break;
	}
      }

      if (recvFromRank) 
	stdMpi.recv(it.getRank());
    }

    stdMpi.startCommunication();

    for (DofComm::Iterator it(*recvDofs, feSpace);
	 !it.end(); it.nextRank()) {
      int i = 0;
      for (; !it.endDofIter(); it.nextDof())
	if (nonRankDofs.count(it.getDofIndex()))
Thomas Witkowski's avatar
Thomas Witkowski committed
88
	  dofMap[it.getDofIndex()].global = stdMpi.getRecvData(it.getRank())[i++];
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
89
90
91
92
93
94
95
96
97
98
    }
  }


  void GlobalDofMap::print()
  {
    FUNCNAME("GlobalDofMap::print()");
    
    MSG("Local to global mapping on this rank: \n");

Thomas Witkowski's avatar
Thomas Witkowski committed
99
    for (map<DegreeOfFreedom, MultiIndex>::iterator it = dofMap.begin(); it != dofMap.end(); ++it)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
100
      if (nonRankDofs.count(it->first) == 0)	
Thomas Witkowski's avatar
Thomas Witkowski committed
101
	MSG("  %d -> %d  (rank-dof)\n", it->first, it->second.local);
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
102
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
103
	MSG("  %d -> %d  \n", it->first, it->second.local);
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
104
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285


  void ParallelDofMapping::addFeSpace(const FiniteElemSpace* feSpace)
  {
    FUNCNAME("ParallelDofMapping::addFeSpace()");
    
    if (data.count(feSpace))
      data.find(feSpace)->second.clear();
    else
      data.insert(make_pair(feSpace, GlobalDofMap(mpiComm)));
    
    data.find(feSpace)->second.setFeSpace(feSpace);
  }    


  int ParallelDofMapping::getRankDofs(vector<const FiniteElemSpace*> &fe)
  {
    FUNCNAME("ParallelDofMapping::getRankDofs()");
    
    int result = 0;
    for (unsigned int i = 0; i < fe.size(); i++) {
      TEST_EXIT_DBG(data.count(fe[i]))("Cannot find FE space: %p\n", fe[i]);
      result += data[fe[i]].nRankDofs;
    }
    
    return result;
  }


  int ParallelDofMapping::getLocalDofs(vector<const FiniteElemSpace*> &fe)
  {
    FUNCNAME("ParallelDofMapping::getLocalDofs()");
    
    int result = 0;
    for (unsigned int i = 0; i < fe.size(); i++) {
      TEST_EXIT_DBG(data.count(fe[i]))("Cannot find FE space: %p\n", fe[i]);
      result += data[fe[i]].nLocalDofs;
    }
    
    return result;
  }


  int ParallelDofMapping::getOverallDofs(vector<const FiniteElemSpace*> &feSpaces)
  {
    FUNCNAME("ParallelDofMapping::getOverallDofs()");

    int result = 0;
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
      result += data.find(feSpaces[i])->second.nOverallDofs;
    }
    
    return result;
  }


  int ParallelDofMapping::getStartDofs(vector<const FiniteElemSpace*> &feSpaces)
  {
    FUNCNAME("ParallelDofMapping::getStartDofs()");

    int result = 0;
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
      result += data.find(feSpaces[i])->second.rStartDofs;
    }
    
    return result;
  }


  void ParallelDofMapping::init(MPI::Intracomm *m,
				vector<const FiniteElemSpace*> &fe,
				bool needGlobalMapping,
				bool bNonLocalDofs)
  {
    FUNCNAME("ParallelDofMapping::init()");

    mpiComm = m;
    feSpaces = fe;
    hasNonLocalDofs = bNonLocalDofs;
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      feSpacesUnique.insert(feSpaces[i]);
      
      addFeSpace(feSpaces[i]);
      data[feSpaces[i]].setNeedGlobalMapping(needGlobalMapping);
      data[feSpaces[i]].setNonLocalDofs(hasNonLocalDofs);
    }
  }
  

  void ParallelDofMapping::update()
  {
    FUNCNAME("ParallelDofMapping::update()");

    for (std::set<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
	 it != feSpacesUnique.end(); ++it)
      data[*it].update();
    
    nRankDofs = getRankDofs(feSpaces);
    nLocalDofs = getLocalDofs(feSpaces);
    nOverallDofs = getOverallDofs(feSpaces);
    rStartDofs = getStartDofs(feSpaces);
    
    computeMatIndex();
  }
  
  
  void ParallelDofMapping::computeMatIndex()
  {
    FUNCNAME("ParallelDofMapping::computeMatIndex()");
    
    dofToMatIndex.clear();
    
    int offset = rStartDofs;
    
    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      
      map<DegreeOfFreedom, MultiIndex>& dofMap = data[feSpaces[i]].getMap();
      typedef map<DegreeOfFreedom, MultiIndex>::iterator ItType;
      for (ItType it = dofMap.begin(); it != dofMap.end(); ++it) {
	if (data[feSpaces[i]].isRankDof(it->first)) {
	  int globalMatIndex = it->second.local + offset;
	  dofToMatIndex.add(i, it->first, globalMatIndex);
	}
      }
      
      offset += data[feSpaces[i]].nRankDofs;
	
      if (!hasNonLocalDofs)
	continue;
      
      TEST_EXIT_DBG(sendDofs != NULL && recvDofs != NULL)
	("No communicator given!\n");
      
      StdMpi<vector<DegreeOfFreedom> > stdMpi(*mpiComm);
      for (DofComm::Iterator it(*sendDofs, feSpaces[i]); 
	   !it.end(); it.nextRank()) {
	vector<DegreeOfFreedom> sendGlobalDofs;
	
	for (; !it.endDofIter(); it.nextDof())
	  if (dofMap.count(it.getDofIndex()))
	    sendGlobalDofs.push_back(dofToMatIndex.get(i, it.getDofIndex()));
	
	stdMpi.send(it.getRank(), sendGlobalDofs);
      }
      
      for (DofComm::Iterator it(*recvDofs, feSpaces[i]); 
	   !it.end(); it.nextRank())
	stdMpi.recv(it.getRank());
      
      stdMpi.startCommunication();
      
      {
	for (DofComm::Iterator it(*recvDofs, feSpaces[i]); 
	     !it.end(); it.nextRank()) {
	  int counter = 0;
	  for (; !it.endDofIter(); it.nextDof()) {
	    if (dofMap.count(it.getDofIndex())) {
	      DegreeOfFreedom d = stdMpi.getRecvData(it.getRank())[counter++];
	      dofToMatIndex.add(i, it.getDofIndex(), d);
	    }
	  }
	}
      }
    }
  }
  

  void ParallelDofMapping::setDofComm(DofComm &pSend, DofComm &pRecv)
  {
    FUNCNAME("ParallelDofMapping::setDofComm()");

    sendDofs = &pSend;
    recvDofs = &pRecv;

    for (std::set<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
	 it != feSpacesUnique.end(); ++it)
      data[*it].setDofComm(pSend, pRecv);
  }
  
286
}