Nektar++
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Member Functions | Static Private Attributes | List of all members
Nektar::MultiRegions::GlobalLinSysPETSc Class Referenceabstract

A PETSc global linear system. More...

#include <GlobalLinSysPETSc.h>

Inheritance diagram for Nektar::MultiRegions::GlobalLinSysPETSc:
[legend]

Classes

struct  ShellCtx
 Internal struct for MatShell and PCShell calls to store current context for callback. More...
 

Public Member Functions

 GlobalLinSysPETSc (const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExp, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
 Constructor for full direct matrix solve. More...
 
virtual ~GlobalLinSysPETSc ()
 Clean up PETSc objects. More...
 
- Public Member Functions inherited from Nektar::MultiRegions::GlobalLinSys
 GlobalLinSys (const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
 Constructor for full direct matrix solve. More...
 
virtual ~GlobalLinSys ()
 
const GlobalLinSysKeyGetKey (void) const
 Returns the key associated with the system. More...
 
const std::weak_ptr< ExpList > & GetLocMat (void) const
 
void InitObject ()
 
void Initialise (const std::shared_ptr< AssemblyMap > &pLocToGloMap)
 
void Solve (const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray)
 Solve the linear system for given input and output vectors using a specified local to global map. More...
 
std::shared_ptr< GlobalLinSysGetSharedThisPtr ()
 Returns a shared pointer to the current object. More...
 
int GetNumBlocks ()
 
DNekScalMatSharedPtr GetBlock (unsigned int n)
 
void DropBlock (unsigned int n)
 
DNekScalBlkMatSharedPtr GetStaticCondBlock (unsigned int n)
 
void DropStaticCondBlock (unsigned int n)
 
void SolveLinearSystem (const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir=0)
 Solve the linear system for given input and output vectors. More...
 

Protected Member Functions

void SetUpScatter ()
 Set up PETSc local (equivalent to Nektar++ global) and global (equivalent to universal) scatter maps. More...
 
void SetUpMatVec (int nGlobal, int nDir)
 Construct PETSc matrix and vector handles. More...
 
void SetUpSolver (NekDouble tolerance)
 Set up KSP solver object. More...
 
void CalculateReordering (const Array< OneD, const int > &glo2uniMap, const Array< OneD, const int > &glo2unique, const AssemblyMapSharedPtr &pLocToGloMap)
 Calculate a reordering of universal IDs for PETSc. More...
 
virtual void v_SolveLinearSystem (const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir) override
 Solve linear system using PETSc. More...
 
virtual void v_DoMatrixMultiply (const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)=0
 
- Protected Member Functions inherited from Nektar::MultiRegions::GlobalLinSys
virtual void v_Solve (const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray)=0
 Solve a linear system based on mapping. More...
 
virtual void v_InitObject ()
 
virtual void v_Initialise (const std::shared_ptr< AssemblyMap > &pLocToGloMap)
 
virtual int v_GetNumBlocks ()
 Get the number of blocks in this system. More...
 
virtual DNekScalMatSharedPtr v_GetBlock (unsigned int n)
 Retrieves the block matrix from n-th expansion using the matrix key provided by the m_linSysKey. More...
 
virtual void v_DropBlock (unsigned int n)
 Releases the local block matrix from NekManager of n-th expansion using the matrix key provided by the m_linSysKey. More...
 
virtual DNekScalBlkMatSharedPtr v_GetStaticCondBlock (unsigned int n)
 Retrieves a the static condensation block matrices from n-th expansion using the matrix key provided by the m_linSysKey. More...
 
virtual void v_DropStaticCondBlock (unsigned int n)
 Releases the static condensation block matrices from NekManager of n-th expansion using the matrix key provided by the m_linSysKey. More...
 
PreconditionerSharedPtr CreatePrecon (AssemblyMapSharedPtr asmMap)
 Create a preconditioner object from the parameters defined in the supplied assembly map. More...
 

Protected Attributes

Mat m_matrix
 PETSc matrix object. More...
 
Vec m_x
 PETSc vector objects used for local storage. More...
 
Vec m_b
 
Vec m_locVec
 
KSP m_ksp
 KSP object that represents solver system. More...
 
PC m_pc
 PCShell for preconditioner. More...
 
PETScMatMult m_matMult
 Enumerator to select matrix multiplication type. More...
 
std::vector< int > m_reorderedMap
 Reordering that takes universal IDs to a unique row in the PETSc matrix. More...
 
VecScatter m_ctx
 PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering. More...
 
int m_nLocal
 Number of unique degrees of freedom on this process. More...
 
PreconditionerSharedPtr m_precon
 
- Protected Attributes inherited from Nektar::MultiRegions::GlobalLinSys
const GlobalLinSysKey m_linSysKey
 Key associated with this linear system. More...
 
const std::weak_ptr< ExpListm_expList
 Local Matrix System. More...
 
const std::map< int, RobinBCInfoSharedPtrm_robinBCInfo
 Robin boundary info. More...
 
bool m_verbose
 

Static Private Member Functions

static PetscErrorCode DoMatrixMultiply (Mat M, Vec in, Vec out)
 Perform matrix multiplication using Nektar++ routines. More...
 
static PetscErrorCode DoPreconditioner (PC pc, Vec in, Vec out)
 Apply preconditioning using Nektar++ routines. More...
 
static void DoNekppOperation (Vec &in, Vec &out, ShellCtx *ctx, bool precon)
 Perform either matrix multiplication or preconditioning using Nektar++ routines. More...
 
static PetscErrorCode DoDestroyMatCtx (Mat M)
 Destroy matrix shell context object. More...
 
static PetscErrorCode DoDestroyPCCtx (PC pc)
 Destroy preconditioner context object. More...
 

Static Private Attributes

static std::string matMult
 
static std::string matMultIds []
 

Detailed Description

A PETSc global linear system.

Solves a linear system using PETSc.

Solves a linear system using single- or multi-level static condensation.

Definition at line 58 of file GlobalLinSysPETSc.h.

Constructor & Destructor Documentation

◆ GlobalLinSysPETSc()

Nektar::MultiRegions::GlobalLinSysPETSc::GlobalLinSysPETSc ( const GlobalLinSysKey pKey,
const std::weak_ptr< ExpList > &  pExp,
const std::shared_ptr< AssemblyMap > &  pLocToGloMap 
)

Constructor for full direct matrix solve.

Definition at line 61 of file GlobalLinSysPETSc.cpp.

64  : GlobalLinSys(pKey, pExp, pLocToGloMap)
65 {
66  // Determine whether to use standard sparse matrix approach or
67  // shell.
68  m_matMult = pExp.lock()->GetSession()->GetSolverInfoAsEnum<PETScMatMult>(
69  "PETScMatMult");
70 
71  // Check PETSc is initialized. For some reason, this is needed on
72  // OS X as logging is not initialized properly in the call within
73  // CommMpi.
74  PetscBool isInitialized;
75  PetscInitialized(&isInitialized);
76  if (!isInitialized)
77  {
78 #ifdef NEKTAR_USE_MPI
79  std::string commType =
80  m_expList.lock()->GetSession()->GetComm()->GetType();
81  if (commType.find("MPI") != std::string::npos)
82  {
84  std::static_pointer_cast<LibUtilities::CommMpi>(
85  m_expList.lock()->GetSession()->GetComm());
86  PETSC_COMM_WORLD = comm->GetComm();
87  }
88 #endif
89  PetscInitializeNoArguments();
90  }
91 
92  // Create matrix
93  MatCreate(PETSC_COMM_WORLD, &m_matrix);
94 }
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:124
GlobalLinSys(const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
std::shared_ptr< CommMpi > CommMpiSharedPtr
Pointer to a Communicator object.
Definition: CommMpi.h:55

References Nektar::MultiRegions::GlobalLinSys::m_expList, m_matMult, and m_matrix.

◆ ~GlobalLinSysPETSc()

Nektar::MultiRegions::GlobalLinSysPETSc::~GlobalLinSysPETSc ( )
virtual

Clean up PETSc objects.

Note that if SessionReader::Finalize is called before the end of the program, PETSc may have been finalized already, at which point we cannot deallocate our objects. If that's the case we do nothing and let the kernel clear up after us.

Definition at line 104 of file GlobalLinSysPETSc.cpp.

105 {
106  PetscBool isFinalized;
107  PetscFinalized(&isFinalized);
108 
109  // Sometimes, PetscFinalized returns false when (in fact) CommMpi's
110  // Finalise routine has been called. We therefore also need to check
111  // whether MPI has been finalised. This might arise from the
112  // additional call to PetscInitializeNoArguments in the constructor
113  // above.
114 #ifdef NEKTAR_USE_MPI
115  int mpiFinal = 0;
116  MPI_Finalized(&mpiFinal);
117  isFinalized = isFinalized || mpiFinal ? PETSC_TRUE : PETSC_FALSE;
118 #endif
119 
120  if (!isFinalized)
121  {
122  KSPDestroy(&m_ksp);
123  PCDestroy(&m_pc);
124  MatDestroy(&m_matrix);
125  VecDestroy(&m_x);
126  VecDestroy(&m_b);
127  VecDestroy(&m_locVec);
128  }
129 }
Vec m_x
PETSc vector objects used for local storage.
PC m_pc
PCShell for preconditioner.
KSP m_ksp
KSP object that represents solver system.

References m_b, m_ksp, m_locVec, m_matrix, m_pc, and m_x.

Member Function Documentation

◆ CalculateReordering()

void Nektar::MultiRegions::GlobalLinSysPETSc::CalculateReordering ( const Array< OneD, const int > &  glo2uniMap,
const Array< OneD, const int > &  glo2unique,
const AssemblyMapSharedPtr pLocToGloMap 
)
protected

Calculate a reordering of universal IDs for PETSc.

PETSc requires a unique, contiguous index of all global and universal degrees of freedom which represents its position inside the matrix. Presently Gs does not guarantee this, so this routine constructs a new universal mapping.

Parameters
glo2uniMapGlobal to universal map
glo2uniqueGlobal to unique map
pLocToGloMapAssembly map for this system

Definition at line 223 of file GlobalLinSysPETSc.cpp.

227 {
229  m_expList.lock()->GetSession()->GetComm();
230 
231  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
232  const int nHomDofs = glo2uniMap.size() - nDirDofs;
233  const int nProc = vComm->GetSize();
234  const int rank = vComm->GetRank();
235 
236  int n, cnt;
237 
238  // Count number of unique degrees of freedom on each process.
239  m_nLocal = Vmath::Vsum(nHomDofs, glo2unique + nDirDofs, 1);
240  m_reorderedMap.resize(nHomDofs);
241 
242  // Reduce coefficient counts across all processors.
243  Array<OneD, int> localCounts(nProc, 0), localOffset(nProc, 0);
244  localCounts[rank] = nHomDofs;
245  vComm->AllReduce(localCounts, LibUtilities::ReduceSum);
246 
247  for (n = 1; n < nProc; ++n)
248  {
249  localOffset[n] = localOffset[n - 1] + localCounts[n - 1];
250  }
251 
252  int totHomDofs = Vmath::Vsum(nProc, localCounts, 1);
253  vector<unsigned int> allUniIds(totHomDofs, 0);
254 
255  // Assemble list of universal IDs
256  for (n = 0; n < nHomDofs; ++n)
257  {
258  int gid = n + nDirDofs;
259  allUniIds[n + localOffset[rank]] = glo2uniMap[gid];
260  }
261 
262  // Reduce this across processors so that each process has a list of
263  // all universal IDs.
264  vComm->AllReduce(allUniIds, LibUtilities::ReduceSum);
265  std::sort(allUniIds.begin(), allUniIds.end());
266  map<int, int> uniIdReorder;
267 
268  // Renumber starting from 0.
269  for (cnt = n = 0; n < allUniIds.size(); ++n)
270  {
271  if (uniIdReorder.count(allUniIds[n]) > 0)
272  {
273  continue;
274  }
275 
276  uniIdReorder[allUniIds[n]] = cnt++;
277  }
278 
279  // Populate reordering map.
280  for (n = 0; n < nHomDofs; ++n)
281  {
282  int gid = n + nDirDofs;
283  int uniId = glo2uniMap[gid];
284  ASSERTL0(uniIdReorder.count(uniId) > 0, "Error in ordering");
285  m_reorderedMap[n] = uniIdReorder[uniId];
286  }
287 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
std::vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
int m_nLocal
Number of unique degrees of freedom on this process.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:895

References ASSERTL0, Nektar::MultiRegions::GlobalLinSys::m_expList, m_nLocal, m_reorderedMap, Nektar::LibUtilities::ReduceSum, and Vmath::Vsum().

Referenced by Nektar::MultiRegions::GlobalLinSysPETScFull::GlobalLinSysPETScFull(), and Nektar::MultiRegions::GlobalLinSysPETScStaticCond::v_AssembleSchurComplement().

◆ DoDestroyMatCtx()

PetscErrorCode Nektar::MultiRegions::GlobalLinSysPETSc::DoDestroyMatCtx ( Mat  M)
staticprivate

Destroy matrix shell context object.

Note the matrix shell and preconditioner share a common context so this might have already been deallocated below, in which case we do nothing.

Parameters
MMatrix shell object

Definition at line 471 of file GlobalLinSysPETSc.cpp.

472 {
473  void *ptr;
474  MatShellGetContext(M, &ptr);
475  ShellCtx *ctx = (ShellCtx *)ptr;
476  delete ctx;
477  return 0;
478 }

Referenced by SetUpMatVec().

◆ DoDestroyPCCtx()

PetscErrorCode Nektar::MultiRegions::GlobalLinSysPETSc::DoDestroyPCCtx ( PC  pc)
staticprivate

Destroy preconditioner context object.

Note the matrix shell and preconditioner share a common context so this might have already been deallocated above, in which case we do nothing.

Parameters
pcPreconditioner object

Definition at line 489 of file GlobalLinSysPETSc.cpp.

490 {
491  void *ptr;
492  PCShellGetContext(pc, &ptr);
493  ShellCtx *ctx = (ShellCtx *)ptr;
494  delete ctx;
495  return 0;
496 }

Referenced by SetUpMatVec().

◆ DoMatrixMultiply()

PetscErrorCode Nektar::MultiRegions::GlobalLinSysPETSc::DoMatrixMultiply ( Mat  M,
Vec  in,
Vec  out 
)
staticprivate

Perform matrix multiplication using Nektar++ routines.

This static function uses Nektar++ routines to calculate the matrix-vector product of M with in, storing the output in out.

Parameters
MOriginal MatShell matrix, which stores the ShellCtx object.
inInput vector.
outOutput vector.

Definition at line 425 of file GlobalLinSysPETSc.cpp.

426 {
427  // Grab our shell context from M.
428  void *ptr;
429  MatShellGetContext(M, &ptr);
430  ShellCtx *ctx = (ShellCtx *)ptr;
431 
432  DoNekppOperation(in, out, ctx, false);
433 
434  // Must return 0, otherwise PETSc complains.
435  return 0;
436 }
static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon)
Perform either matrix multiplication or preconditioning using Nektar++ routines.

References DoNekppOperation().

Referenced by SetUpMatVec().

◆ DoNekppOperation()

void Nektar::MultiRegions::GlobalLinSysPETSc::DoNekppOperation ( Vec &  in,
Vec &  out,
ShellCtx ctx,
bool  precon 
)
staticprivate

Perform either matrix multiplication or preconditioning using Nektar++ routines.

This static function uses Nektar++ routines to calculate the matrix-vector product of M with in, storing the output in out.

Todo:
There's a lot of scatters and copies that might possibly be eliminated to make this more efficient.
Parameters
inInput vector.
outOutput vector.
ctxShellCtx object that points to our instance of GlobalLinSysPETSc.
preconIf true, we apply a preconditioner, if false, we perform a matrix multiplication.

Definition at line 371 of file GlobalLinSysPETSc.cpp.

373 {
374  const int nGlobal = ctx->nGlobal;
375  const int nDir = ctx->nDir;
376  const int nHomDofs = nGlobal - nDir;
377  GlobalLinSysPETSc *linSys = ctx->linSys;
378 
379  // Scatter from PETSc ordering to our local ordering. It's actually
380  // unclear whether this step might also do some communication in
381  // parallel, which is probably not ideal.
382  VecScatterBegin(linSys->m_ctx, in, linSys->m_locVec, INSERT_VALUES,
383  SCATTER_FORWARD);
384  VecScatterEnd(linSys->m_ctx, in, linSys->m_locVec, INSERT_VALUES,
385  SCATTER_FORWARD);
386 
387  // Temporary storage to pass to Nektar++
388  Array<OneD, NekDouble> tmpIn(nHomDofs), tmpOut(nHomDofs);
389 
390  // Get values from input vector and copy to tmpIn.
391  PetscScalar *tmpLocIn;
392  VecGetArray(linSys->m_locVec, &tmpLocIn);
393  Vmath::Vcopy(nHomDofs, tmpLocIn, 1, &tmpIn[0], 1);
394  VecRestoreArray(linSys->m_locVec, &tmpLocIn);
395 
396  // Do matrix multiply in Nektar++, store in tmpOut.
397  if (precon)
398  {
399  linSys->m_precon->DoPreconditioner(tmpIn, tmpOut);
400  }
401  else
402  {
403  linSys->v_DoMatrixMultiply(tmpIn, tmpOut);
404  }
405 
406  // Scatter back to PETSc ordering and put in out.
407  VecSetValues(out, nHomDofs, &linSys->m_reorderedMap[0], &tmpOut[0],
408  INSERT_VALUES);
409  VecAssemblyBegin(out);
410  VecAssemblyEnd(out);
411 }
GlobalLinSysPETSc(const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExp, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255

References Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::linSys, m_ctx, m_locVec, m_precon, m_reorderedMap, Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::nDir, Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::nGlobal, v_DoMatrixMultiply(), and Vmath::Vcopy().

Referenced by DoMatrixMultiply(), and DoPreconditioner().

◆ DoPreconditioner()

PetscErrorCode Nektar::MultiRegions::GlobalLinSysPETSc::DoPreconditioner ( PC  pc,
Vec  in,
Vec  out 
)
staticprivate

Apply preconditioning using Nektar++ routines.

This static function uses Nektar++ routines to apply the preconditioner stored in GlobalLinSysPETSc::m_precon from the context of pc to the vector in, storing the output in out.

Parameters
pcPreconditioner object that stores the ShellCtx.
inInput vector.
outOutput vector.

Definition at line 449 of file GlobalLinSysPETSc.cpp.

450 {
451  // Grab our PCShell context from pc.
452  void *ptr;
453  PCShellGetContext(pc, &ptr);
454  ShellCtx *ctx = (ShellCtx *)ptr;
455 
456  DoNekppOperation(in, out, ctx, true);
457 
458  // Must return 0, otherwise PETSc complains.
459  return 0;
460 }

References DoNekppOperation().

Referenced by SetUpMatVec().

◆ SetUpMatVec()

void Nektar::MultiRegions::GlobalLinSysPETSc::SetUpMatVec ( int  nGlobal,
int  nDir 
)
protected

Construct PETSc matrix and vector handles.

Todo:
Preallocation should be done at this point, since presently matrix allocation takes a significant amount of time.
Parameters
nGlobalNumber of global degrees of freedom in the system (on this processor)
nDirNumber of Dirichlet degrees of freedom (on this processor).

Definition at line 300 of file GlobalLinSysPETSc.cpp.

301 {
302  // CREATE VECTORS
303  VecCreate(PETSC_COMM_WORLD, &m_x);
304  VecSetSizes(m_x, m_nLocal, PETSC_DECIDE);
305  VecSetFromOptions(m_x);
306  VecDuplicate(m_x, &m_b);
307 
308  // CREATE MATRICES
310  {
311  // Create ShellCtx context object which will store the matrix
312  // size and a pointer to the linear system. We do this so that
313  // we can call a member function to the matrix-vector and
314  // preconditioning multiplication in a subclass.
315  ShellCtx *ctx1 = new ShellCtx(), *ctx2 = new ShellCtx();
316  ctx1->nGlobal = ctx2->nGlobal = nGlobal;
317  ctx1->nDir = ctx2->nDir = nDir;
318  ctx1->linSys = ctx2->linSys = this;
319 
320  // Set up MatShell object.
321  MatCreateShell(PETSC_COMM_WORLD, m_nLocal, m_nLocal, PETSC_DETERMINE,
322  PETSC_DETERMINE, (void *)ctx1, &m_matrix);
323  MatShellSetOperation(m_matrix, MATOP_MULT,
324  (void (*)(void))DoMatrixMultiply);
325  MatShellSetOperation(m_matrix, MATOP_DESTROY,
326  (void (*)(void))DoDestroyMatCtx);
327 
328  // Create a PCShell to go alongside the MatShell.
329  PCCreate(PETSC_COMM_WORLD, &m_pc);
330 #if PETSC_VERSION_GE(3, 5, 0)
331  PCSetOperators(m_pc, m_matrix, m_matrix);
332 #else
333  PCSetOperators(m_pc, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
334 #endif
335  PCSetType(m_pc, PCSHELL);
336  PCShellSetApply(m_pc, DoPreconditioner);
337  PCShellSetDestroy(m_pc, DoDestroyPCCtx);
338  PCShellSetContext(m_pc, ctx2);
339  }
340  else
341  {
342  // Otherwise we create a PETSc matrix and use MatSetFromOptions
343  // so that we can set various options on the command line.
344  MatCreate(PETSC_COMM_WORLD, &m_matrix);
345  MatSetType(m_matrix, MATAIJ);
346  MatSetSizes(m_matrix, m_nLocal, m_nLocal, PETSC_DETERMINE,
347  PETSC_DETERMINE);
348  MatSetFromOptions(m_matrix);
349  MatSetUp(m_matrix);
350  }
351 }
static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out)
Apply preconditioning using Nektar++ routines.
static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out)
Perform matrix multiplication using Nektar++ routines.
static PetscErrorCode DoDestroyMatCtx(Mat M)
Destroy matrix shell context object.
static PetscErrorCode DoDestroyPCCtx(PC pc)
Destroy preconditioner context object.

References DoDestroyMatCtx(), DoDestroyPCCtx(), DoMatrixMultiply(), DoPreconditioner(), Nektar::MultiRegions::ePETScMatMultShell, Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::linSys, m_b, m_matMult, m_matrix, m_nLocal, m_pc, m_x, Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::nDir, and Nektar::MultiRegions::GlobalLinSysPETSc::ShellCtx::nGlobal.

Referenced by Nektar::MultiRegions::GlobalLinSysPETScFull::GlobalLinSysPETScFull(), and Nektar::MultiRegions::GlobalLinSysPETScStaticCond::v_AssembleSchurComplement().

◆ SetUpScatter()

void Nektar::MultiRegions::GlobalLinSysPETSc::SetUpScatter ( )
protected

Set up PETSc local (equivalent to Nektar++ global) and global (equivalent to universal) scatter maps.

These maps are used in GlobalLinSysPETSc::v_SolveLinearSystem to scatter the solution vector back to each process.

Definition at line 188 of file GlobalLinSysPETSc.cpp.

189 {
190  const int nHomDofs = m_reorderedMap.size();
191 
192  // Create local and global numbering systems for vector
193  IS isGlobal, isLocal;
194  ISCreateGeneral(PETSC_COMM_SELF, nHomDofs, &m_reorderedMap[0],
195  PETSC_COPY_VALUES, &isGlobal);
196  ISCreateStride(PETSC_COMM_SELF, nHomDofs, 0, 1, &isLocal);
197 
198  // Create local vector for output
199  VecCreate(PETSC_COMM_SELF, &m_locVec);
200  VecSetSizes(m_locVec, nHomDofs, PETSC_DECIDE);
201  VecSetFromOptions(m_locVec);
202 
203  // Create scatter context
204  VecScatterCreate(m_x, isGlobal, m_locVec, isLocal, &m_ctx);
205 
206  // Clean up
207  ISDestroy(&isGlobal);
208  ISDestroy(&isLocal);
209 }
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering.

References m_ctx, m_locVec, m_reorderedMap, and m_x.

Referenced by Nektar::MultiRegions::GlobalLinSysPETScFull::GlobalLinSysPETScFull(), and Nektar::MultiRegions::GlobalLinSysPETScStaticCond::v_AssembleSchurComplement().

◆ SetUpSolver()

void Nektar::MultiRegions::GlobalLinSysPETSc::SetUpSolver ( NekDouble  tolerance)
protected

Set up KSP solver object.

This is reasonably generic setup – most solver types can be changed using the .petscrc file.

Parameters
toleranceResidual tolerance to converge to.

Definition at line 506 of file GlobalLinSysPETSc.cpp.

507 {
508  KSPCreate(PETSC_COMM_WORLD, &m_ksp);
509  KSPSetTolerances(m_ksp, tolerance, PETSC_DEFAULT, PETSC_DEFAULT,
510  PETSC_DEFAULT);
511  KSPSetFromOptions(m_ksp);
512 #if PETSC_VERSION_GE(3, 5, 0)
513  KSPSetOperators(m_ksp, m_matrix, m_matrix);
514 #else
515  KSPSetOperators(m_ksp, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
516 #endif
517 
519  {
520  KSPSetPC(m_ksp, m_pc);
521  }
522 }

References Nektar::MultiRegions::ePETScMatMultShell, m_ksp, m_matMult, m_matrix, and m_pc.

Referenced by Nektar::MultiRegions::GlobalLinSysPETScFull::GlobalLinSysPETScFull(), and Nektar::MultiRegions::GlobalLinSysPETScStaticCond::v_AssembleSchurComplement().

◆ v_DoMatrixMultiply()

virtual void Nektar::MultiRegions::GlobalLinSysPETSc::v_DoMatrixMultiply ( const Array< OneD, const NekDouble > &  pInput,
Array< OneD, NekDouble > &  pOutput 
)
protectedpure virtual

◆ v_SolveLinearSystem()

void Nektar::MultiRegions::GlobalLinSysPETSc::v_SolveLinearSystem ( const int  pNumRows,
const Array< OneD, const NekDouble > &  pInput,
Array< OneD, NekDouble > &  pOutput,
const AssemblyMapSharedPtr locToGloMap,
const int  pNumDir 
)
overrideprotectedvirtual

Solve linear system using PETSc.

The general strategy being a PETSc solve is to:

  • Copy values into the PETSc vector m_b
  • Solve the system m_ksp and place result into m_x.
  • Scatter results back into m_locVec using m_ctx scatter object.
  • Copy from m_locVec to output array #pOutput.

Implements Nektar::MultiRegions::GlobalLinSys.

Definition at line 141 of file GlobalLinSysPETSc.cpp.

145 {
146  const int nHomDofs = pNumRows - pNumDir;
147 
149  {
150  m_precon = CreatePrecon(locToGloMap);
151  m_precon->BuildPreconditioner();
152  }
153 
154  // Populate RHS vector from input
155  VecSetValues(m_b, nHomDofs, &m_reorderedMap[0], &pInput[pNumDir],
156  INSERT_VALUES);
157 
158  // Assemble RHS vector
159  VecAssemblyBegin(m_b);
160  VecAssemblyEnd(m_b);
161 
162  // Do system solve
163  KSPSolve(m_ksp, m_b, m_x);
164 
165  KSPConvergedReason reason;
166  KSPGetConvergedReason(m_ksp, &reason);
167  ASSERTL0(reason > 0, "PETSc solver diverged, reason is: " +
168  std::string(KSPConvergedReasons[reason]));
169 
170  // Scatter results to local vector
171  VecScatterBegin(m_ctx, m_x, m_locVec, INSERT_VALUES, SCATTER_FORWARD);
172  VecScatterEnd(m_ctx, m_x, m_locVec, INSERT_VALUES, SCATTER_FORWARD);
173 
174  // Copy results into output vector
175  PetscScalar *tmp;
176  VecGetArray(m_locVec, &tmp);
177  Vmath::Vcopy(nHomDofs, tmp, 1, &pOutput[pNumDir], 1);
178  VecRestoreArray(m_locVec, &tmp);
179 }
PreconditionerSharedPtr CreatePrecon(AssemblyMapSharedPtr asmMap)
Create a preconditioner object from the parameters defined in the supplied assembly map.

References ASSERTL0, Nektar::MultiRegions::GlobalLinSys::CreatePrecon(), Nektar::MultiRegions::ePETScMatMultShell, m_b, m_ctx, m_ksp, m_locVec, m_matMult, m_precon, m_reorderedMap, m_x, and Vmath::Vcopy().

Member Data Documentation

◆ m_b

Vec Nektar::MultiRegions::GlobalLinSysPETSc::m_b
protected

Definition at line 72 of file GlobalLinSysPETSc.h.

Referenced by SetUpMatVec(), v_SolveLinearSystem(), and ~GlobalLinSysPETSc().

◆ m_ctx

VecScatter Nektar::MultiRegions::GlobalLinSysPETSc::m_ctx
protected

PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering.

Definition at line 84 of file GlobalLinSysPETSc.h.

Referenced by DoNekppOperation(), SetUpScatter(), and v_SolveLinearSystem().

◆ m_ksp

KSP Nektar::MultiRegions::GlobalLinSysPETSc::m_ksp
protected

KSP object that represents solver system.

Definition at line 74 of file GlobalLinSysPETSc.h.

Referenced by SetUpSolver(), v_SolveLinearSystem(), and ~GlobalLinSysPETSc().

◆ m_locVec

Vec Nektar::MultiRegions::GlobalLinSysPETSc::m_locVec
protected

◆ m_matMult

PETScMatMult Nektar::MultiRegions::GlobalLinSysPETSc::m_matMult
protected

◆ m_matrix

Mat Nektar::MultiRegions::GlobalLinSysPETSc::m_matrix
protected

◆ m_nLocal

int Nektar::MultiRegions::GlobalLinSysPETSc::m_nLocal
protected

Number of unique degrees of freedom on this process.

Definition at line 86 of file GlobalLinSysPETSc.h.

Referenced by CalculateReordering(), and SetUpMatVec().

◆ m_pc

PC Nektar::MultiRegions::GlobalLinSysPETSc::m_pc
protected

PCShell for preconditioner.

Definition at line 76 of file GlobalLinSysPETSc.h.

Referenced by SetUpMatVec(), SetUpSolver(), and ~GlobalLinSysPETSc().

◆ m_precon

PreconditionerSharedPtr Nektar::MultiRegions::GlobalLinSysPETSc::m_precon
protected

◆ m_reorderedMap

std::vector<int> Nektar::MultiRegions::GlobalLinSysPETSc::m_reorderedMap
protected

◆ m_x

Vec Nektar::MultiRegions::GlobalLinSysPETSc::m_x
protected

PETSc vector objects used for local storage.

Definition at line 72 of file GlobalLinSysPETSc.h.

Referenced by SetUpMatVec(), SetUpScatter(), v_SolveLinearSystem(), and ~GlobalLinSysPETSc().

◆ matMult

std::string Nektar::MultiRegions::GlobalLinSysPETSc::matMult
staticprivate
Initial value:
=
"Sparse")
static std::string RegisterDefaultSolverInfo(const std::string &pName, const std::string &pValue)
Registers the default string value of a solver info property.

Definition at line 130 of file GlobalLinSysPETSc.h.

◆ matMultIds

std::string Nektar::MultiRegions::GlobalLinSysPETSc::matMultIds
staticprivate
Initial value:
= {
"PETScMatMult", "Sparse", MultiRegions::ePETScMatMultSparse),
"PETScMatMult", "Shell", MultiRegions::ePETScMatMultShell)}
static std::string RegisterEnumValue(std::string pEnum, std::string pString, int pEnumValue)
Registers an enumeration value.

Definition at line 131 of file GlobalLinSysPETSc.h.