PetscSolverGlobalBlockMatrix.cc 9.58 KB
Newer Older
1 2 3 4 5 6 7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9 10 11 12 13 14 15 16 17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
20 21 22


#include "parallel/PetscSolverGlobalBlockMatrix.h"
23
#include "parallel/PetscHelper.h"
24 25 26
#include "parallel/StdMpi.h"
#include "parallel/MpiHelper.h"

27 28
using namespace std;

29
namespace AMDiS { namespace Parallel {
30

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
31
  void PetscSolverGlobalBlockMatrix::fillPetscMatrix(Matrix<DOFMatrix*> *seqMat)
32 33
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::fillPetscMatrix()");
34

35
    TEST_EXIT_DBG(meshDistributor)("No mesh distributor object defined!\n");
36
    TEST_EXIT_DBG(interiorMap)("No parallel mapping object defined!\n");
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
37 38
    TEST_EXIT_DBG(seqMat)("No DOF matrix defined!\n");

39
    double wtime = MPI::Wtime();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
40

41
    prepare();
42

43
    const FiniteElemSpace *feSpace = componentSpaces[0];
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
44
    nComponents = seqMat->getNumRows();
45 46
    int nRankRows = (*interiorMap)[feSpace].nRankDofs;
    int nOverallRows = (*interiorMap)[feSpace].nOverallDofs;
47

48
#ifndef NDEBUG
49 50 51
    MSG("Fill petsc matrix 1 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    if (nBlocks == -1) {
      nBlocks = nComponents;
      for (int i = 0; i < nBlocks; i++)
	componentInBlock[i] = i;
    }

    vector<int> compNthInBlock(nComponents, 0);
    vector<int> blockSize(nBlocks, 0);

    for (int i = 0; i < nComponents; i++) {
      compNthInBlock[i] = blockSize[componentInBlock[i]];
      blockSize[componentInBlock[i]]++;
    }

    nestMat.resize(nBlocks * nBlocks);
67

68
    // === Transfer values from DOF matrices to the PETSc matrix. ===
69

70 71
    for (int i = 0; i < nBlocks; i++)
      for (int j = 0; j < nBlocks; j++)
72
	MatCreateAIJ(domainComm,
73 74
		     nRankRows * blockSize[i], nRankRows * blockSize[j],
		     nOverallRows * blockSize[i], nOverallRows * blockSize[j],
75
		     300 * blockSize[i], PETSC_NULL,
76
		     300 * blockSize[j], PETSC_NULL,
77
		     &(nestMat[i * nBlocks + j]));
78

79 80
    for (int i = 0; i < nComponents; i++)
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
81
	if ((*seqMat)[i][j]) {
82
	  int idx = componentInBlock[i] * nBlocks + componentInBlock[j];
83
	  setDofMatrix(nestMat[idx], (*seqMat)[i][j],
84
		       compNthInBlock[i], compNthInBlock[j]);
85 86
	}

87 88 89 90 91 92 93 94
    for (int i = 0; i < nBlocks; i++) {
      for (int j = 0; j < nBlocks; j++) {
	int idx = i * nBlocks + j;
	if (nestMat[idx]) {
	  MatAssemblyBegin(nestMat[idx], MAT_FINAL_ASSEMBLY);
	  MatAssemblyEnd(nestMat[idx], MAT_FINAL_ASSEMBLY);
	}
      }
95 96
    }

97

98
    MatCreateNest(domainComm, nBlocks, PETSC_NULL, nBlocks, PETSC_NULL,
99
		  &(nestMat[0]), &getMatInterior());
100

101
#ifndef NDEBUG
102 103 104
    MSG("Fill petsc matrix 2 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif

105
    matAssembly();
106

107

108
    /// initPreconditioner(...)
109 110 111 112 113 114

    // === Init PETSc solver and preconditioner objects. ===

    initSolver(kspInterior);
    KSPGetPC(kspInterior, &pcInterior);
    initPreconditioner(pcInterior);
115

116 117 118 119 120 121 122 123 124 125 126
    MSG("Fill petsc matrix needed %.5f seconds\n", MPI::Wtime() - wtime);
  }


  void PetscSolverGlobalBlockMatrix::fillPetscRhs(SystemVector *vec)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::fillPetscRhs()");

    TEST_EXIT_DBG(vec)("NO DOF vector defined!\n");

    nComponents = vec->getSize();
127
    const FiniteElemSpace *feSpace = componentSpaces[0];
128 129
    int nRankRows = (*interiorMap)[feSpace].nRankDofs;
    int nOverallRows = (*interiorMap)[feSpace].nOverallDofs;
130

131
    nestVec.resize(nComponents);
132

133
    for (int i = 0; i < nComponents; i++) {
134
      VecCreateMPI(domainComm, nRankRows, nOverallRows, &(nestVec[i]));
135

136
      setDofVector(nestVec[i], vec->getDOFVector(i));
137

138 139 140 141
      VecAssemblyBegin(nestVec[i]);
      VecAssemblyEnd(nestVec[i]);
    }

142
    VecCreateNest(domainComm, nComponents, PETSC_NULL,
143
		  &(nestVec[0]), &(getVecRhsInterior()));
144

145
    vecRhsAssembly();
146 147 148
  }


149 150 151 152
  void PetscSolverGlobalBlockMatrix::initSolver(KSP &ksp)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::initSolver()");

153
    KSPCreate(domainComm, &ksp);
154
    petsc::ksp_set_operators(ksp, getMatInterior(), getMatInterior());
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
    KSPSetOptionsPrefix(ksp, kspPrefix.c_str());
    KSPSetFromOptions(ksp);
  }


  void PetscSolverGlobalBlockMatrix::exitSolver(KSP ksp)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::exitSolver()");

    KSPDestroy(&ksp);
  }


  void PetscSolverGlobalBlockMatrix::initPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::initPreconditioner()");

    PCSetFromOptions(pc);
  }


  void PetscSolverGlobalBlockMatrix::exitPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::exitPreconditioner()");
  }


182
  void PetscSolverGlobalBlockMatrix::solvePetscMatrix(SystemVector &vec,
183 184 185 186
						      AdaptInfo *adaptInfo)
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::solvePetscMatrix()");

187
    const FiniteElemSpace *feSpace = componentSpaces[0];
188
    VecDuplicate(getVecRhsInterior(), &petscSolVec);
189

190 191 192 193 194
    for (int i = 0; i < vec.getSize(); i++)
    {
      Vec tmp;
      VecNestGetSubVec(petscSolVec, i, &tmp);
      setDofVector(tmp, vec.getDOFVector(i));
195
      VecAssemblyBegin(tmp);
196 197
      VecAssemblyEnd(tmp);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
198

199
    // PETSc.
200
    solve(getVecRhsInterior(), petscSolVec);
201 202 203 204

    // === Transfere values from PETSc's solution vectors to the DOF vectors. ===
    for (int i = 0; i < nComponents; i++) {
      DOFVector<double> &dofvec = *(vec.getDOFVector(i));
205 206

      Vec tmp;
Thomas Witkowski's avatar
Thomas Witkowski committed
207
      VecNestGetSubVec(petscSolVec, i, &tmp);
208

209
//       int nRankDofs = (*interiorMap)[feSpace].nRankDofs;
210 211 212
      PetscScalar *vecPointer;
      VecGetArray(tmp, &vecPointer);

213
      DofMap& d = (*interiorMap)[feSpace].getMap();
214 215 216
      for (DofMap::iterator it = d.begin(); it != d.end(); ++it)
	if (it->second.local != -1)
	  dofvec[it->first] = vecPointer[it->second.local];
217

218 219
      VecRestoreArray(tmp, &vecPointer);
    }
220

221
    VecDestroy(&petscSolVec);
222 223 224 225 226 227 228 229 230 231 232

    // === Synchronize DOFs at common DOFs, i.e., DOFs that correspond to ===
    // === more than one partition.                                       ===
    meshDistributor->synchVector(vec);
  }


  void PetscSolverGlobalBlockMatrix::destroyMatrixData()
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::destroyMatrixData()");

233 234 235 236
    for (unsigned int i = 0; i < nestMat.size(); i++)
      if (nestMat[i] != PETSC_NULL)
	MatDestroy(&(nestMat[i]));

237
    matDestroy();
238

239 240 241
    exitPreconditioner(pcInterior);

    exitSolver(kspInterior);
242 243 244
  }


245 246 247 248
  void PetscSolverGlobalBlockMatrix::destroyVectorData()
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::destroyVectorData()");

249 250
    for (unsigned int i = 0; i < nestVec.size(); i++)
      VecDestroy(&(nestVec[i]));
251

252
    vecDestroy();
253 254 255
  }


256
  void PetscSolverGlobalBlockMatrix::setDofMatrix(Mat& petscMat,
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
257
						  DOFMatrix* seqMat,
258
						  int dispRowBlock,
259
						  int dispColBlock)
260 261 262 263
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::setDofMatrix()");

    TEST_EXIT(petscMat)("No PETSc matrix!\n");
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
264
    TEST_EXIT(seqMat)("No DOFMatrix!\n");
265

266
    const FiniteElemSpace *feSpace = componentSpaces[0];
267

268 269 270 271
    using mtl::tag::row; using mtl::tag::nz; using mtl::begin; using mtl::end;
    namespace traits = mtl::traits;
    typedef DOFMatrix::base_matrix_type Matrix;

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
272 273
    traits::col<Matrix>::type col(seqMat->getBaseMatrix());
    traits::const_value<Matrix>::type value(seqMat->getBaseMatrix());
274 275 276 277

    typedef traits::range_generator<row, Matrix>::type cursor_type;
    typedef traits::range_generator<nz, cursor_type>::type icursor_type;

278 279
    int dispRowIndex = (*interiorMap)[feSpace].nRankDofs * dispRowBlock;
    int dispColIndex = (*interiorMap)[feSpace].nRankDofs * dispColBlock;
280

281 282
    vector<int> cols;
    vector<double> values;
283 284
    cols.reserve(3000);
    values.reserve(3000);
285

286 287 288
    // === Traverse all rows of the dof matrix and insert row wise the values ===
    // === to the PETSc matrix.                                               ===

289
    for (cursor_type cursor = begin<row>(seqMat->getBaseMatrix()),
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
290
	   cend = end<row>(seqMat->getBaseMatrix()); cursor != cend; ++cursor) {
291 292

      // Global index of the current row DOF.
293
      int rowIndex = (*interiorMap)[feSpace][cursor.value()].global + dispRowIndex;
294 295 296

      cols.clear();
      values.clear();
297 298 299

      for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor);
	   icursor != icend; ++icursor) {
300
	// Global index of the current column index.
301
	int colIndex = (*interiorMap)[feSpace][col(*icursor)].global + dispColIndex;
302

303 304 305
	// Ignore all zero entries, expect it is a diagonal entry.
	if (value(*icursor) == 0.0 && rowIndex != colIndex)
	  continue;
306

307 308 309 310
	// Calculate the exact position of the column index in the PETSc matrix.
	cols.push_back(colIndex);
	values.push_back(value(*icursor));
      }
311

312 313
      MatSetValues(petscMat, 1, &rowIndex, cols.size(),
 		   &(cols[0]), &(values[0]), ADD_VALUES);
314 315
    }
  }
316 317 318


  void PetscSolverGlobalBlockMatrix::setDofVector(Vec& petscVec,
319
						  DOFVector<double>* vec)
320 321 322
  {
    FUNCNAME("PetscSolverGlobalBlockMatrix::setDofVector()");

323
    const FiniteElemSpace *feSpace = componentSpaces[0];
324

325 326 327
    // Traverse all used DOFs in the dof vector.
    DOFVector<double>::Iterator dofIt(vec, USED_DOFS);
    for (dofIt.reset(); !dofIt.end(); ++dofIt) {
328
      int index = (*interiorMap)[feSpace][dofIt.getDOFIndex()].global;
329 330 331 332 333 334
      double value = *dofIt;

      VecSetValues(petscVec, 1, &index, &value, ADD_VALUES);
    }
  }

335
} } // end namespace Parallel, AMDiS