Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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:
Inheritance graph
[legend]
Collaboration diagram for Nektar::MultiRegions::GlobalLinSysPETSc:
Collaboration graph
[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 boost::weak_ptr< ExpList > &pExp, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
 Constructor for full direct matrix solve. More...
 
virtual ~GlobalLinSysPETSc ()
 Clean up PETSc objects. More...
 
virtual void v_SolveLinearSystem (const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir)
 Solve linear system using PETSc. More...
 
- Public Member Functions inherited from Nektar::MultiRegions::GlobalLinSys
 GlobalLinSys (const GlobalLinSysKey &pKey, const boost::weak_ptr< ExpList > &pExpList, const boost::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 boost::weak_ptr< ExpList > & GetLocMat (void) const
 
void InitObject ()
 
void Initialise (const boost::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...
 
boost::shared_ptr< GlobalLinSysGetSharedThisPtr ()
 Returns a shared pointer to the current object. More...
 
int GetNumBlocks ()
 
DNekScalMatSharedPtr GetBlock (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_DoMatrixMultiply (const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)=0
 
- Protected Member Functions inherited from Nektar::MultiRegions::GlobalLinSys
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 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 boost::weak_ptr< ExpListm_expList
 Local Matrix System. More...
 
const std::map< int,
RobinBCInfoSharedPtr
m_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 59 of file GlobalLinSysPETSc.h.

Constructor & Destructor Documentation

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

Constructor for full direct matrix solve.

Definition at line 65 of file GlobalLinSysPETSc.cpp.

References Nektar::LibUtilities::CommMpi::GetComm(), Nektar::MultiRegions::GlobalLinSys::m_expList, m_matMult, and m_matrix.

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

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

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

Member Function Documentation

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 235 of file GlobalLinSysPETSc.cpp.

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().

239  {
241  = m_expList.lock()->GetSession()->GetComm();
242 
243  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
244  const int nHomDofs = glo2uniMap.num_elements() - nDirDofs;
245  const int nProc = vComm->GetSize();
246  const int rank = vComm->GetRank();
247 
248  int n, cnt;
249 
250  // Count number of unique degrees of freedom on each process.
251  m_nLocal = Vmath::Vsum(nHomDofs, glo2unique + nDirDofs, 1);
252  m_reorderedMap.resize(nHomDofs);
253 
254  // Reduce coefficient counts across all processors.
255  Array<OneD, int> localCounts(nProc, 0), localOffset(nProc, 0);
256  localCounts[rank] = nHomDofs;
257  vComm->AllReduce(localCounts, LibUtilities::ReduceSum);
258 
259  for (n = 1; n < nProc; ++n)
260  {
261  localOffset[n] = localOffset[n-1] + localCounts[n-1];
262  }
263 
264  int totHomDofs = Vmath::Vsum(nProc, localCounts, 1);
265  vector<unsigned int> allUniIds(totHomDofs, 0);
266 
267  // Assemble list of universal IDs
268  for (n = 0; n < nHomDofs; ++n)
269  {
270  int gid = n + nDirDofs;
271  allUniIds[n + localOffset[rank]] = glo2uniMap[gid];
272  }
273 
274  // Reduce this across processors so that each process has a list of
275  // all universal IDs.
276  vComm->AllReduce(allUniIds, LibUtilities::ReduceSum);
277  std::sort(allUniIds.begin(), allUniIds.end());
278  map<int,int> uniIdReorder;
279 
280  // Renumber starting from 0.
281  for (cnt = n = 0; n < allUniIds.size(); ++n)
282  {
283  if (uniIdReorder.count(allUniIds[n]) > 0)
284  {
285  continue;
286  }
287 
288  uniIdReorder[allUniIds[n]] = cnt++;
289  }
290 
291  // Populate reordering map.
292  for (n = 0; n < nHomDofs; ++n)
293  {
294  int gid = n + nDirDofs;
295  int uniId = glo2uniMap[gid];
296  ASSERTL0(uniIdReorder.count(uniId) > 0, "Error in ordering");
297  m_reorderedMap[n] = uniIdReorder[uniId];
298  }
299  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
int m_nLocal
Number of unique degrees of freedom on this process.
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:737
std::vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
const boost::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:129
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 486 of file GlobalLinSysPETSc.cpp.

Referenced by SetUpMatVec().

487  {
488  void *ptr;
489  MatShellGetContext(M, &ptr);
490  ShellCtx *ctx = (ShellCtx *)ptr;
491  delete ctx;
492  return 0;
493  }
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 504 of file GlobalLinSysPETSc.cpp.

Referenced by SetUpMatVec().

505  {
506  void *ptr;
507  PCShellGetContext(pc, &ptr);
508  ShellCtx *ctx = (ShellCtx *)ptr;
509  delete ctx;
510  return 0;
511  }
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 438 of file GlobalLinSysPETSc.cpp.

References DoNekppOperation().

Referenced by SetUpMatVec().

440  {
441  // Grab our shell context from M.
442  void *ptr;
443  MatShellGetContext(M, &ptr);
444  ShellCtx *ctx = (ShellCtx *)ptr;
445 
446  DoNekppOperation(in, out, ctx, false);
447 
448  // Must return 0, otherwise PETSc complains.
449  return 0;
450  }
static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon)
Perform either matrix multiplication or preconditioning using Nektar++ routines.
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 384 of file GlobalLinSysPETSc.cpp.

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().

386  {
387  const int nGlobal = ctx->nGlobal;
388  const int nDir = ctx->nDir;
389  const int nHomDofs = nGlobal - nDir;
390  GlobalLinSysPETSc *linSys = ctx->linSys;
391 
392  // Scatter from PETSc ordering to our local ordering. It's actually
393  // unclear whether this step might also do some communication in
394  // parallel, which is probably not ideal.
395  VecScatterBegin(linSys->m_ctx, in, linSys->m_locVec,
396  INSERT_VALUES, SCATTER_FORWARD);
397  VecScatterEnd (linSys->m_ctx, in, linSys->m_locVec,
398  INSERT_VALUES, SCATTER_FORWARD);
399 
400  // Temporary storage to pass to Nektar++
401  Array<OneD, NekDouble> tmpIn(nHomDofs), tmpOut(nHomDofs);
402 
403  // Get values from input vector and copy to tmpIn.
404  PetscScalar *tmpLocIn;
405  VecGetArray (linSys->m_locVec, &tmpLocIn);
406  Vmath::Vcopy (nHomDofs, tmpLocIn, 1, &tmpIn[0], 1);
407  VecRestoreArray(linSys->m_locVec, &tmpLocIn);
408 
409  // Do matrix multiply in Nektar++, store in tmpOut.
410  if (precon)
411  {
412  linSys->m_precon->DoPreconditioner(tmpIn, tmpOut);
413  }
414  else
415  {
416  linSys->v_DoMatrixMultiply(tmpIn, tmpOut);
417  }
418 
419  // Scatter back to PETSc ordering and put in out.
420  VecSetValues(out, nHomDofs, &linSys->m_reorderedMap[0],
421  &tmpOut[0], INSERT_VALUES);
422  VecAssemblyBegin(out);
423  VecAssemblyEnd (out);
424  }
GlobalLinSysPETSc(const GlobalLinSysKey &pKey, const boost::weak_ptr< ExpList > &pExp, const boost::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:1061
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 463 of file GlobalLinSysPETSc.cpp.

References DoNekppOperation().

Referenced by SetUpMatVec().

465  {
466  // Grab our PCShell context from pc.
467  void *ptr;
468  PCShellGetContext(pc, &ptr);
469  ShellCtx *ctx = (ShellCtx *)ptr;
470 
471  DoNekppOperation(in, out, ctx, true);
472 
473  // Must return 0, otherwise PETSc complains.
474  return 0;
475  }
static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon)
Perform either matrix multiplication or preconditioning using Nektar++ routines.
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 312 of file GlobalLinSysPETSc.cpp.

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().

313  {
314  // CREATE VECTORS
315  VecCreate (PETSC_COMM_WORLD, &m_x);
316  VecSetSizes (m_x, m_nLocal, PETSC_DECIDE);
317  VecSetFromOptions(m_x);
318  VecDuplicate (m_x, &m_b);
319 
320  // CREATE MATRICES
322  {
323  // Create ShellCtx context object which will store the matrix
324  // size and a pointer to the linear system. We do this so that
325  // we can call a member function to the matrix-vector and
326  // preconditioning multiplication in a subclass.
327  ShellCtx *ctx1 = new ShellCtx(), *ctx2 = new ShellCtx();
328  ctx1->nGlobal = ctx2->nGlobal = nGlobal;
329  ctx1->nDir = ctx2->nDir = nDir;
330  ctx1->linSys = ctx2->linSys = this;
331 
332  // Set up MatShell object.
333  MatCreateShell (PETSC_COMM_WORLD, m_nLocal, m_nLocal,
334  PETSC_DETERMINE, PETSC_DETERMINE,
335  (void *)ctx1, &m_matrix);
336  MatShellSetOperation(m_matrix, MATOP_MULT,
337  (void(*)(void))DoMatrixMultiply);
338  MatShellSetOperation(m_matrix, MATOP_DESTROY,
339  (void(*)(void))DoDestroyMatCtx);
340 
341  // Create a PCShell to go alongside the MatShell.
342  PCCreate (PETSC_COMM_WORLD, &m_pc);
343 #if PETSC_VERSION_GE(3,5,0)
344  PCSetOperators (m_pc, m_matrix, m_matrix);
345 #else
346  PCSetOperators (m_pc, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
347 #endif
348  PCSetType (m_pc, PCSHELL);
349  PCShellSetApply (m_pc, DoPreconditioner);
350  PCShellSetDestroy(m_pc, DoDestroyPCCtx);
351  PCShellSetContext(m_pc, ctx2);
352  }
353  else
354  {
355  // Otherwise we create a PETSc matrix and use MatSetFromOptions
356  // so that we can set various options on the command line.
357  MatCreate (PETSC_COMM_WORLD, &m_matrix);
358  MatSetType (m_matrix, MATAIJ);
359  MatSetSizes (m_matrix, m_nLocal, m_nLocal,
360  PETSC_DETERMINE, PETSC_DETERMINE);
361  MatSetFromOptions(m_matrix);
362  MatSetUp (m_matrix);
363  }
364  }
PC m_pc
PCShell for preconditioner.
Vec m_x
PETSc vector objects used for local storage.
static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out)
Apply preconditioning using Nektar++ routines.
int m_nLocal
Number of unique degrees of freedom on this process.
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.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
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 200 of file GlobalLinSysPETSc.cpp.

References m_ctx, m_locVec, m_reorderedMap, and m_x.

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

201  {
202  const int nHomDofs = m_reorderedMap.size();
203 
204  // Create local and global numbering systems for vector
205  IS isGlobal, isLocal;
206  ISCreateGeneral (PETSC_COMM_SELF, nHomDofs, &m_reorderedMap[0],
207  PETSC_COPY_VALUES, &isGlobal);
208  ISCreateStride (PETSC_COMM_SELF, nHomDofs, 0, 1, &isLocal);
209 
210  // Create local vector for output
211  VecCreate (PETSC_COMM_SELF, &m_locVec);
212  VecSetSizes (m_locVec, nHomDofs, PETSC_DECIDE);
213  VecSetFromOptions(m_locVec);
214 
215  // Create scatter context
216  VecScatterCreate (m_x, isGlobal, m_locVec, isLocal, &m_ctx);
217 
218  // Clean up
219  ISDestroy(&isGlobal);
220  ISDestroy(&isLocal);
221  }
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering...
Vec m_x
PETSc vector objects used for local storage.
std::vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
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 521 of file GlobalLinSysPETSc.cpp.

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().

522  {
523  KSPCreate(PETSC_COMM_WORLD, &m_ksp);
524  KSPSetTolerances(
525  m_ksp, tolerance, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
526  KSPSetFromOptions(m_ksp);
527 #if PETSC_VERSION_GE(3,5,0)
528  KSPSetOperators(m_ksp, m_matrix, m_matrix);
529 #else
530  KSPSetOperators(m_ksp, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
531 #endif
532 
534  {
535  KSPSetPC(m_ksp, m_pc);
536  }
537  }
PC m_pc
PCShell for preconditioner.
KSP m_ksp
KSP object that represents solver system.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
virtual void Nektar::MultiRegions::GlobalLinSysPETSc::v_DoMatrixMultiply ( const Array< OneD, const NekDouble > &  pInput,
Array< OneD, NekDouble > &  pOutput 
)
protectedpure virtual
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 
)
virtual

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 147 of file GlobalLinSysPETSc.cpp.

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().

153  {
154  const int nHomDofs = pNumRows - pNumDir;
155 
157  {
158  m_precon = CreatePrecon(locToGloMap);
159  m_precon->BuildPreconditioner();
160  }
161 
162  // Populate RHS vector from input
163  VecSetValues(m_b, nHomDofs, &m_reorderedMap[0],
164  &pInput[pNumDir], INSERT_VALUES);
165 
166  // Assemble RHS vector
167  VecAssemblyBegin(m_b);
168  VecAssemblyEnd (m_b);
169 
170  // Do system solve
171  KSPSolve(m_ksp, m_b, m_x);
172 
173  KSPConvergedReason reason;
174  KSPGetConvergedReason(m_ksp, &reason);
175  ASSERTL0(reason > 0,
176  "PETSc solver diverged, reason is: " +
177  std::string(KSPConvergedReasons[reason]));
178 
179 
180  // Scatter results to local vector
181  VecScatterBegin(m_ctx, m_x, m_locVec,
182  INSERT_VALUES, SCATTER_FORWARD);
183  VecScatterEnd (m_ctx, m_x, m_locVec,
184  INSERT_VALUES, SCATTER_FORWARD);
185 
186  // Copy results into output vector
187  PetscScalar *tmp;
188  VecGetArray (m_locVec, &tmp);
189  Vmath::Vcopy (nHomDofs, tmp, 1, &pOutput[pNumDir], 1);
190  VecRestoreArray(m_locVec, &tmp);
191  }
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering...
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
Vec m_x
PETSc vector objects used for local storage.
KSP m_ksp
KSP object that represents solver system.
std::vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1061
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
PreconditionerSharedPtr CreatePrecon(AssemblyMapSharedPtr asmMap)
Create a preconditioner object from the parameters defined in the supplied assembly map...

Member Data Documentation

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

Definition at line 81 of file GlobalLinSysPETSc.h.

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

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

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

Definition at line 93 of file GlobalLinSysPETSc.h.

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

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

KSP object that represents solver system.

Definition at line 83 of file GlobalLinSysPETSc.h.

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

Vec Nektar::MultiRegions::GlobalLinSysPETSc::m_locVec
protected
PETScMatMult Nektar::MultiRegions::GlobalLinSysPETSc::m_matMult
protected
Mat Nektar::MultiRegions::GlobalLinSysPETSc::m_matrix
protected
int Nektar::MultiRegions::GlobalLinSysPETSc::m_nLocal
protected

Number of unique degrees of freedom on this process.

Definition at line 95 of file GlobalLinSysPETSc.h.

Referenced by CalculateReordering(), and SetUpMatVec().

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

PCShell for preconditioner.

Definition at line 85 of file GlobalLinSysPETSc.h.

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

PreconditionerSharedPtr Nektar::MultiRegions::GlobalLinSysPETSc::m_precon
protected
std::vector<int> Nektar::MultiRegions::GlobalLinSysPETSc::m_reorderedMap
protected
Vec Nektar::MultiRegions::GlobalLinSysPETSc::m_x
protected

PETSc vector objects used for local storage.

Definition at line 81 of file GlobalLinSysPETSc.h.

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

std::string Nektar::MultiRegions::GlobalLinSysPETSc::matMult
staticprivate
Initial value:

Definition at line 134 of file GlobalLinSysPETSc.h.

std::string Nektar::MultiRegions::GlobalLinSysPETSc::matMultIds
staticprivate