ParallelCoarseSpaceMatVec.cc 9.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
// 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.


#include "AMDiS.h"
#include "parallel/ParallelCoarseSpaceMatVec.h"
15
#include "parallel/MatrixNnzStructure.h"
16
17
18
19
20

namespace AMDiS {

  using namespace std;

21
  ParallelCoarseSpaceMatVec::ParallelCoarseSpaceMatVec()
22
    : interiorMap(NULL),
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
23
      lastMeshNnz(0),
24
25
26
27
28
      alwaysCreateNnzStructure(false),
      meshDistributor(NULL),
      subdomainLevel(0),
      rStartInterior(0),
      nGlobalOverallInterior(0)
29
  {
30
31
32
33
34
    Parameters::get("parallel->always create nnz structure", 
		    alwaysCreateNnzStructure);
  }


35
36
  void ParallelCoarseSpaceMatVec::setCoarseSpaceDofMapping(ParallelDofMapping *coarseDofs, 
							   int component)
37
  {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    FUNCNAME("ParallelCoarseSpaceMatVec::setCoarseSpaceDofMapping()");

    TEST_EXIT_DBG(coarseDofs)("Should not happen!\n");

    if (component == -1) {
      // === Set coarse space for all components. ===

      coarseSpaceMap.clear();

      int nComponents = coarseDofs->getNumberOfComponents();
      for (int i = 0; i < nComponents; i++)
	coarseSpaceMap[i] = coarseDofs;
    } else {
      // === Set coarse space for just one component. ===

      coarseSpaceMap[component] = coarseDofs;
    }
  }


  void ParallelCoarseSpaceMatVec::prepare()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec:prepare()");

    // === Create vector of unique pointers to all coarse space maps. ===
63

64
    uniqueCoarseMap.clear();
65
66
67
68
69
70
71
72
73
74
75
    if (coarseSpaceMap.size()) {
      std::set<ParallelDofMapping*> tmp;
      for (map<int, ParallelDofMapping*>::iterator it = coarseSpaceMap.begin();
	   it != coarseSpaceMap.end(); ++it) {
	if (tmp.count(it->second) == 0) {	  
	  tmp.insert(it->second);
	  uniqueCoarseMap.push_back(it->second);
	}
      }
    }

76
77
78

    // === Create pointers to PETSc matrix and vector objects. ===

79
80
81
82
83
    int nCoarseMap = uniqueCoarseMap.size();
    mat.resize(nCoarseMap + 1);
    for (int i = 0; i < nCoarseMap + 1; i++)
      mat[i].resize(nCoarseMap + 1);

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
84
85
86
    vecSol.resize(nCoarseMap + 1);
    vecRhs.resize(nCoarseMap + 1);

87
88
89
    
    // === Create map from component number to its coarse space map. ===

90
91
92
93
94
95
96
97
98
99
100
101
102
    componentIthCoarseMap.resize(coarseSpaceMap.size());
    for (unsigned int i = 0; i < componentIthCoarseMap.size(); i++) {
      bool found = false;
      for (int j = 0; j < nCoarseMap; j++) {
	if (coarseSpaceMap[i] == uniqueCoarseMap[j]) {
	  componentIthCoarseMap[i] = j;
	  found = true;
	  break;
	}
      }
      
      TEST_EXIT_DBG(found)("Should not happen!\n");
    }
103
104
105
  }


106
  void ParallelCoarseSpaceMatVec::createMatVec(Matrix<DOFMatrix*>& seqMat)
107
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    FUNCNAME("ParallelCoarseSpaceMatVec::createMatVec()");
109

110
111
112
113
114
115
116
117
    // === Prepare coarse space information and generate the correct number ===
    // === of empty PETSc matrix and vector objects.                        ===

    prepare();

    
    // === Update subdomain data (mostly required for multi-level methods) ===

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
118
    updateSubdomainData();
119

120

121
122
123
    // === If required, recompute non zero structure of the matrix. ===

    bool localMatrix = (coarseSpaceMap.size() && subdomainLevel == 0);
124
125
126
127
128
129
    if (checkMeshChange()) {
      // Mesh has been changed, recompute interior DOF mapping.
      vector<const FiniteElemSpace*> feSpaces = getComponentFeSpaces(seqMat);     
      interiorMap->setComputeMatIndex(!localMatrix);
      interiorMap->update(feSpaces);

130
131
132
      int nMat = uniqueCoarseMap.size() + 1;
      nnz.resize(nMat);
      for (int i = 0; i < nMat; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
133
	nnz[i].resize(nMat);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
	for (int j = 0; j < nMat; j++)
	  nnz[i][j].clear();
      }

      nnz[0][0].create(seqMat, mpiCommGlobal, *interiorMap,
		       (coarseSpaceMap.size() == 0 ? &(meshDistributor->getPeriodicMap()) : NULL),
		       meshDistributor->getElementObjectDb(),
		       localMatrix);

      for (int i = 0; i < nMat; i++) {
	for (int j = 0; j < nMat; j++) {
	  if (i == 0 && j == 0)
	    continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
148
149
150
151
152
153
154
	  ParallelDofMapping &rowMap = 
	    (i == 0 ? *interiorMap : *(uniqueCoarseMap[i - 1]));
	  ParallelDofMapping &colMap =
	    (j == 0 ? *interiorMap : *(uniqueCoarseMap[j - 1]));

	  nnz[i][j].create(seqMat, mpiCommGlobal, rowMap, colMap, NULL,
			   meshDistributor->getElementObjectDb());
155
156
157
158
159
	}
      }
    }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
160
161
    // === Create PETSc matrices and PETSc vectors with the computed  ===
    // === nnz data structure.                                        ===
162
    
Thomas Witkowski's avatar
Thomas Witkowski committed
163
164
    int nRankInteriorRows = interiorMap->getRankDofs();
    int nOverallInteriorRows = interiorMap->getOverallDofs();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
165
    
166
    if (localMatrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
167
      MatCreateSeqAIJ(mpiCommLocal, nRankInteriorRows, nRankInteriorRows,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
168
		      0, nnz[0][0].dnnz,
169
170
171
		      &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
172
173
      MatCreateAIJ(mpiCommGlobal, nRankInteriorRows, nRankInteriorRows, 
		   nOverallInteriorRows, nOverallInteriorRows,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
174
		   0, nnz[0][0].dnnz, 0, nnz[0][0].onnz,
175
176
177
178
		   &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
179
180
    VecCreateMPI(mpiCommGlobal, nRankInteriorRows, nOverallInteriorRows, &vecSol[0]);
    VecCreateMPI(mpiCommGlobal, nRankInteriorRows, nOverallInteriorRows, &vecRhs[0]);
181

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
182
183
184
185
    int nCoarseMap = uniqueCoarseMap.size();
    for (int i = 0; i < nCoarseMap; i++) {
      ParallelDofMapping* cMap = uniqueCoarseMap[i];
      
Thomas Witkowski's avatar
Thomas Witkowski committed
186
187
      int nRankCoarseRows = cMap->getRankDofs();
      int nOverallCoarseRows = cMap->getOverallDofs();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
188
189
      
      MatCreateAIJ(mpiCommGlobal,
Thomas Witkowski's avatar
Thomas Witkowski committed
190
191
		   nRankCoarseRows, nRankCoarseRows,
		   nOverallCoarseRows, nOverallCoarseRows,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
192
193
		   0, nnz[i + 1][i + 1].dnnz, 0, nnz[i + 1][i + 1].onnz,
		   &mat[i + 1][i + 1]);
194
195
      cMap->createVec(vecSol[i + 1]);
      cMap->createVec(vecRhs[i + 1]);
Thomas Witkowski's avatar
Thomas Witkowski committed
196
197
198
    }

    for (int i = 0; i < nCoarseMap + 1; i++) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
199
      for (int j = 0; j < nCoarseMap + 1; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
200
201
202
203
204
205
206
207
	if (i == j)
	  continue;

	int nRowsRankMat = (i == 0 ? nRankInteriorRows : uniqueCoarseMap[i - 1]->getRankDofs());
	int nRowsOverallMat = (i == 0 ? nOverallInteriorRows : uniqueCoarseMap[i - 1]->getOverallDofs());

	int nColsRankMat = (j == 0 ? nRankInteriorRows : uniqueCoarseMap[j - 1]->getRankDofs());
	int nColsOverallMat = (j == 0 ? nOverallInteriorRows : uniqueCoarseMap[j - 1]->getOverallDofs());
208
209
	
	MatCreateAIJ(mpiCommGlobal,
Thomas Witkowski's avatar
Thomas Witkowski committed
210
211
212
		     nRowsRankMat, nColsRankMat,
		     nRowsOverallMat, nColsOverallMat,
		     0, nnz[i][j].dnnz, 0, nnz[i][j].onnz,
213
		     &mat[i][j]);	  
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
214
	MatCreateAIJ(mpiCommGlobal,
Thomas Witkowski's avatar
Thomas Witkowski committed
215
216
217
218
		     nColsRankMat, nRowsRankMat,
		     nColsOverallMat, nRowsOverallMat,
		     0, nnz[j][i].dnnz, 0, nnz[j][i].onnz,
		     &mat[j][i]);
219
      }
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
220
    }    
221
222
223
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
224
  void ParallelCoarseSpaceMatVec::matDestroy()
225
  {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
226
    FUNCNAME("ParallelCoarseSpaceMatVec::matDestroy()");
227
228
229
230
231
232
233
234

    int nMatrix = mat.size();
    for (int i = 0; i < nMatrix; i++)
      for (int j = 0; j < nMatrix; j++)
	MatDestroy(&mat[i][j]);
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
235
  void ParallelCoarseSpaceMatVec::vecDestroy()
236
  {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
237
238
239
240
241
242
243
244
245
246
247
248
249
    FUNCNAME("ParallelCoarseSpaceMatVec::vecDestroy()");

    int nVec = vecSol.size();
    for (int i = 0; i < nVec; i++) {
      VecDestroy(&vecSol[i]);
      VecDestroy(&vecRhs[i]);
    }
  }


  void ParallelCoarseSpaceMatVec::matAssembly()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::matAssembly()");
250
251
252
253
254
255
256
257
258
259

    int nMatrix = mat.size();
    for (int i = 0; i < nMatrix; i++) {
      for (int j = 0; j < nMatrix; j++) {
	MatAssemblyBegin(mat[i][j], MAT_FINAL_ASSEMBLY);
	MatAssemblyEnd(mat[i][j], MAT_FINAL_ASSEMBLY);  
      }
    }
  }

260

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  void ParallelCoarseSpaceMatVec::vecRhsAssembly()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::vecRhsAssembly()");

    int nVec = vecRhs.size();
    for (int i = 0; i < nVec; i++) {
      VecAssemblyBegin(vecRhs[i]);
      VecAssemblyEnd(vecRhs[i]);
    }
  }


  void ParallelCoarseSpaceMatVec::vecSolAssembly()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::vecSolAssembly()");

    int nVec = vecRhs.size();
    for (int i = 0; i < nVec; i++) {
      VecAssemblyBegin(vecSol[i]);
      VecAssemblyEnd(vecSol[i]);
    }
  }


285
  bool ParallelCoarseSpaceMatVec::checkMeshChange()
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::checkMeshChange()");

    int recvAllValues = 0;
    int sendValue = 
      static_cast<int>(meshDistributor->getLastMeshChangeIndex() != lastMeshNnz);
    mpiCommGlobal.Allreduce(&sendValue, &recvAllValues, 1, MPI_INT, MPI_SUM);

    if (recvAllValues != 0 || alwaysCreateNnzStructure) {
      lastMeshNnz = meshDistributor->getLastMeshChangeIndex();
      return true;
    }

    return false;
  }

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

  void ParallelCoarseSpaceMatVec::updateSubdomainData()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::updateSubdomainData()");

    if (mpiCommLocal.Get_size() == 1) {
      rStartInterior = 0;
      nGlobalOverallInterior = interiorMap->getOverallDofs();
    } else {
      int groupRowsInterior = 0;
      if (mpiCommLocal.Get_rank() == 0)
	groupRowsInterior = interiorMap->getOverallDofs();

      mpi::getDofNumbering(mpiCommGlobal, groupRowsInterior,
			   rStartInterior, nGlobalOverallInterior);

      int tmp = 0;
      if (mpiCommLocal.Get_rank() == 0)
	tmp = rStartInterior;

      mpiCommLocal.Allreduce(&tmp, &rStartInterior, 1, MPI_INT, MPI_SUM);
    }
  }

326
}