ParallelCoarseSpaceMatVec.cc 8.19 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()
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
22
23
24
    : rStartInterior(0),
      nGlobalOverallInterior(0),
      lastMeshNnz(0),
25
      alwaysCreateNnzStructure(false)
26
  {
27
28
29
30
31
32
33
34
35
36
37
38
39
    Parameters::get("parallel->always create nnz structure", 
		    alwaysCreateNnzStructure);
  }


  void ParallelCoarseSpaceMatVec::init(ParallelDofMapping *iMap,
				       map<int, ParallelDofMapping*> cMap,
				       int sdLevel,
				       MPI::Intracomm mcLocal,
				       MPI::Intracomm mcGlobal,
				       MeshDistributor *meshDist)
  {
    FUNCNAME("ParallelCoarseSpaceMatVec:init()");
40
41
42

    interiorMap = iMap;
    coarseSpaceMap = cMap;
43
44
45
46
    subdomainLevel = sdLevel;
    mpiCommLocal = mcLocal;
    mpiCommGlobal = mcGlobal;
    meshDistributor = meshDist;
47
48
    

49
    uniqueCoarseMap.clear();
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    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);
	}
      }
    }

    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
66
67
68
    vecSol.resize(nCoarseMap + 1);
    vecRhs.resize(nCoarseMap + 1);

69
70
71
72
73
74
75
76
77
78
79
80
81
    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");
    }
82
83
84
85
86
87
88
  }


  void ParallelCoarseSpaceMatVec::create(Matrix<DOFMatrix*>& seqMat)
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::create()");

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
89
    updateSubdomainData();
90
91
92
93
94
95
96
97

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

    bool localMatrix = (coarseSpaceMap.size() && subdomainLevel == 0);
    if (checkMeshChange(seqMat, localMatrix)) {
      int nMat = uniqueCoarseMap.size() + 1;
      nnz.resize(nMat);
      for (int i = 0; i < nMat; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
98
	nnz[i].resize(nMat);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	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;

	  if (i == j) {
Thomas Witkowski's avatar
Thomas Witkowski committed
114
	    nnz[i][j].create(seqMat, mpiCommGlobal, *(coarseSpaceMap[i - 1]), NULL,
115
116
			     meshDistributor->getElementObjectDb());
	  } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
117
118
119
120
121
122
	    ParallelDofMapping &rowMap = 
	      (i == 0 ? *interiorMap : *(coarseSpaceMap[i - 1]));
	    ParallelDofMapping &colMap =
	      (j == 0 ? *interiorMap : *(coarseSpaceMap[j - 1]));

	    nnz[i][j].create(seqMat, mpiCommGlobal, rowMap, colMap, NULL,
123
124
125
126
127
128
129
			     meshDistributor->getElementObjectDb());
	  }
	}
      }
    }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
130
131
    // === Create PETSc matrices and PETSc vectors with the computed  ===
    // === nnz data structure.                                        ===
132
133
134
    
    int nRankRows = interiorMap->getRankDofs();
    int nOverallRows = interiorMap->getOverallDofs();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
135
    
136
    if (localMatrix) {
137
      MatCreateSeqAIJ(mpiCommLocal, nRankRows, nRankRows,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
138
		      0, nnz[0][0].dnnz,
139
140
141
142
143
		      &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    } else {
      MatCreateAIJ(mpiCommGlobal, nRankRows, nRankRows, 
		   nOverallRows, nOverallRows,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
144
		   0, nnz[0][0].dnnz, 0, nnz[0][0].onnz,
145
146
147
148
		   &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    }

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
149
150
    VecCreateMPI(mpiCommGlobal, nRankRows, nOverallRows, &vecSol[0]);
    VecCreateMPI(mpiCommGlobal, nRankRows, nOverallRows, &vecRhs[0]);
151
152


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

    int nCoarseMap = uniqueCoarseMap.size();
    for (int i = 0; i < nCoarseMap; i++) {
      ParallelDofMapping* cMap = uniqueCoarseMap[i];
      
      int nRowsRankCoarse = cMap->getRankDofs();
      int nRowsOverallCoarse = cMap->getOverallDofs();
      
      MatCreateAIJ(mpiCommGlobal,
		   nRowsRankCoarse, nRowsRankCoarse,
		   nRowsOverallCoarse, nRowsOverallCoarse,
		   0, nnz[i + 1][i + 1].dnnz, 0, nnz[i + 1][i + 1].onnz,
		   &mat[i + 1][i + 1]);

      VecCreateMPI(mpiCommGlobal, nRowsRankCoarse, nRowsOverallCoarse, 
		   &vecSol[i + 1]);
      VecCreateMPI(mpiCommGlobal, nRowsRankCoarse, nRowsOverallCoarse, 
		   &vecRhs[i + 1]);
      
      for (int j = 0; j < nCoarseMap + 1; j++) {
	int nRowsRankMat = (j == 0 ? nRankRows : uniqueCoarseMap[j - 1]->getRankDofs());
	int nRowsOverallMat = (j == 0 ? nOverallRows : uniqueCoarseMap[j - 1]->getOverallDofs());
175
176
	
	MatCreateAIJ(mpiCommGlobal,
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
177
178
179
180
181
182
183
184
185
186
		     nRowsRankCoarse, nRowsRankMat,
		     nRowsOverallCoarse, nRowsOverallMat,
		     0, nnz[i + 1][j].dnnz, 0, nnz[i + 1][j].onnz,
		     &mat[i + 1][j]);	  
	
	MatCreateAIJ(mpiCommGlobal,
		     nRowsRankMat, nRowsRankCoarse,
		     nRowsOverallMat, nRowsOverallCoarse,
		     0, nnz[j][i + 1].dnnz, 0, nnz[j][i + 1].onnz,
		     &mat[j][i + 1]);
187
      }
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
188
    }    
189
190
191
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
192
  void ParallelCoarseSpaceMatVec::matDestroy()
193
  {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
194
    FUNCNAME("ParallelCoarseSpaceMatVec::matDestroy()");
195
196
197
198
199
200
201
202

    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
203
  void ParallelCoarseSpaceMatVec::vecDestroy()
204
  {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
205
206
207
208
209
210
211
212
213
214
215
216
217
    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()");
218
219
220
221
222
223
224
225
226
227

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

228

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  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]);
    }
  }


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  bool ParallelCoarseSpaceMatVec::checkMeshChange(Matrix<DOFMatrix*> &seqMat,
						  bool localMatrix)
  {
    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) {
      vector<const FiniteElemSpace*> feSpaces = getFeSpaces(seqMat);
      
      interiorMap->setComputeMatIndex(!localMatrix);
      interiorMap->update(feSpaces);

      lastMeshNnz = meshDistributor->getLastMeshChangeIndex();

      return true;
    }

    return false;
  }

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

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

301
}