Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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...
 
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 map< int,
RobinBCInfoSharedPtr
m_robinBCInfo
 Robin boundary info. More...
 

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

References m_matMult, and m_matrix.

67  : GlobalLinSys(pKey, pExp, pLocToGloMap)
68  {
69  // Determine whether to use standard sparse matrix approach or
70  // shell.
71  m_matMult = pExp.lock()->GetSession()
72  ->GetSolverInfoAsEnum<PETScMatMult>(
73  "PETScMatMult");
74 
75  // Check PETSc is initialized. For some reason, this is needed on
76  // OS X as logging is not initialized properly in the call within
77  // CommMpi.
78  PetscBool isInitialized;
79  PetscInitialized(&isInitialized);
80  if (!isInitialized)
81  {
82  PetscInitializeNoArguments();
83  }
84 
85  // Create matrix
86  MatCreate(PETSC_COMM_WORLD, &m_matrix);
87  }
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.
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 97 of file GlobalLinSysPETSc.cpp.

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

98  {
99  PetscBool isFinalized;
100  PetscFinalized(&isFinalized);
101 
102  // Sometimes, PetscFinalized returns false when (in fact) CommMpi's
103  // Finalise routine has been called. We therefore also need to check
104  // whether MPI has been finalised. This might arise from the
105  // additional call to PetscInitializeNoArguments in the constructor
106  // above.
107 #ifdef NEKTAR_USE_MPI
108  int mpiFinal = 0;
109  MPI_Finalized(&mpiFinal);
110  isFinalized = isFinalized || mpiFinal ? PETSC_TRUE : PETSC_FALSE;
111 #endif
112 
113  if (!isFinalized)
114  {
115  KSPDestroy(&m_ksp);
116  PCDestroy (&m_pc);
117  MatDestroy(&m_matrix);
118  VecDestroy(&m_x);
119  VecDestroy(&m_b);
120  VecDestroy(&m_locVec);
121  }
122  }
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 215 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().

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

Referenced by SetUpMatVec().

467  {
468  void *ptr;
469  MatShellGetContext(M, &ptr);
470  ShellCtx *ctx = (ShellCtx *)ptr;
471  delete ctx;
472  return 0;
473  }
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 484 of file GlobalLinSysPETSc.cpp.

Referenced by SetUpMatVec().

485  {
486  void *ptr;
487  PCShellGetContext(pc, &ptr);
488  ShellCtx *ctx = (ShellCtx *)ptr;
489  delete ctx;
490  return 0;
491  }
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 418 of file GlobalLinSysPETSc.cpp.

References DoNekppOperation().

Referenced by SetUpMatVec().

420  {
421  // Grab our shell context from M.
422  void *ptr;
423  MatShellGetContext(M, &ptr);
424  ShellCtx *ctx = (ShellCtx *)ptr;
425 
426  DoNekppOperation(in, out, ctx, false);
427 
428  // Must return 0, otherwise PETSc complains.
429  return 0;
430  }
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 364 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().

366  {
367  const int nGlobal = ctx->nGlobal;
368  const int nDir = ctx->nDir;
369  const int nHomDofs = nGlobal - nDir;
370  GlobalLinSysPETSc *linSys = ctx->linSys;
371 
372  // Scatter from PETSc ordering to our local ordering. It's actually
373  // unclear whether this step might also do some communication in
374  // parallel, which is probably not ideal.
375  VecScatterBegin(linSys->m_ctx, in, linSys->m_locVec,
376  INSERT_VALUES, SCATTER_FORWARD);
377  VecScatterEnd (linSys->m_ctx, in, linSys->m_locVec,
378  INSERT_VALUES, SCATTER_FORWARD);
379 
380  // Temporary storage to pass to Nektar++
381  Array<OneD, NekDouble> tmpIn(nHomDofs), tmpOut(nHomDofs);
382 
383  // Get values from input vector and copy to tmpIn.
384  PetscScalar *tmpLocIn;
385  VecGetArray (linSys->m_locVec, &tmpLocIn);
386  Vmath::Vcopy (nHomDofs, tmpLocIn, 1, &tmpIn[0], 1);
387  VecRestoreArray(linSys->m_locVec, &tmpLocIn);
388 
389  // Do matrix multiply in Nektar++, store in tmpOut.
390  if (precon)
391  {
392  linSys->m_precon->DoPreconditioner(tmpIn, tmpOut);
393  }
394  else
395  {
396  linSys->v_DoMatrixMultiply(tmpIn, tmpOut);
397  }
398 
399  // Scatter back to PETSc ordering and put in out.
400  VecSetValues(out, nHomDofs, &linSys->m_reorderedMap[0],
401  &tmpOut[0], INSERT_VALUES);
402  VecAssemblyBegin(out);
403  VecAssemblyEnd (out);
404  }
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:1047
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 443 of file GlobalLinSysPETSc.cpp.

References DoNekppOperation().

Referenced by SetUpMatVec().

445  {
446  // Grab our PCShell context from pc.
447  void *ptr;
448  PCShellGetContext(pc, &ptr);
449  ShellCtx *ctx = (ShellCtx *)ptr;
450 
451  DoNekppOperation(in, out, ctx, true);
452 
453  // Must return 0, otherwise PETSc complains.
454  return 0;
455  }
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 292 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().

293  {
294  // CREATE VECTORS
295  VecCreate (PETSC_COMM_WORLD, &m_x);
296  VecSetSizes (m_x, m_nLocal, PETSC_DECIDE);
297  VecSetFromOptions(m_x);
298  VecDuplicate (m_x, &m_b);
299 
300  // CREATE MATRICES
302  {
303  // Create ShellCtx context object which will store the matrix
304  // size and a pointer to the linear system. We do this so that
305  // we can call a member function to the matrix-vector and
306  // preconditioning multiplication in a subclass.
307  ShellCtx *ctx1 = new ShellCtx(), *ctx2 = new ShellCtx();
308  ctx1->nGlobal = ctx2->nGlobal = nGlobal;
309  ctx1->nDir = ctx2->nDir = nDir;
310  ctx1->linSys = ctx2->linSys = this;
311 
312  // Set up MatShell object.
313  MatCreateShell (PETSC_COMM_WORLD, m_nLocal, m_nLocal,
314  PETSC_DETERMINE, PETSC_DETERMINE,
315  (void *)ctx1, &m_matrix);
316  MatShellSetOperation(m_matrix, MATOP_MULT,
317  (void(*)(void))DoMatrixMultiply);
318  MatShellSetOperation(m_matrix, MATOP_DESTROY,
319  (void(*)(void))DoDestroyMatCtx);
320 
321  // Create a PCShell to go alongside the MatShell.
322  PCCreate (PETSC_COMM_WORLD, &m_pc);
323 #if PETSC_VERSION_GE(3,5,0)
324  PCSetOperators (m_pc, m_matrix, m_matrix);
325 #else
326  PCSetOperators (m_pc, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
327 #endif
328  PCSetType (m_pc, PCSHELL);
329  PCShellSetApply (m_pc, DoPreconditioner);
330  PCShellSetDestroy(m_pc, DoDestroyPCCtx);
331  PCShellSetContext(m_pc, ctx2);
332  }
333  else
334  {
335  // Otherwise we create a PETSc matrix and use MatSetFromOptions
336  // so that we can set various options on the command line.
337  MatCreate (PETSC_COMM_WORLD, &m_matrix);
338  MatSetType (m_matrix, MATAIJ);
339  MatSetSizes (m_matrix, m_nLocal, m_nLocal,
340  PETSC_DETERMINE, PETSC_DETERMINE);
341  MatSetFromOptions(m_matrix);
342  MatSetUp (m_matrix);
343  }
344  }
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 180 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().

181  {
182  const int nHomDofs = m_reorderedMap.size();
183 
184  // Create local and global numbering systems for vector
185  IS isGlobal, isLocal;
186  ISCreateGeneral (PETSC_COMM_SELF, nHomDofs, &m_reorderedMap[0],
187  PETSC_COPY_VALUES, &isGlobal);
188  ISCreateStride (PETSC_COMM_SELF, nHomDofs, 0, 1, &isLocal);
189 
190  // Create local vector for output
191  VecCreate (PETSC_COMM_SELF, &m_locVec);
192  VecSetSizes (m_locVec, nHomDofs, PETSC_DECIDE);
193  VecSetFromOptions(m_locVec);
194 
195  // Create scatter context
196  VecScatterCreate (m_x, isGlobal, m_locVec, isLocal, &m_ctx);
197 
198  // Clean up
199  ISDestroy(&isGlobal);
200  ISDestroy(&isLocal);
201  }
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering...
vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
Vec m_x
PETSc vector objects used for local storage.
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 501 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().

502  {
503  KSPCreate(PETSC_COMM_WORLD, &m_ksp);
504  KSPSetTolerances(
505  m_ksp, tolerance, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
506  KSPSetFromOptions(m_ksp);
507 #if PETSC_VERSION_GE(3,5,0)
508  KSPSetOperators(m_ksp, m_matrix, m_matrix);
509 #else
510  KSPSetOperators(m_ksp, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
511 #endif
512 
514  {
515  KSPSetPC(m_ksp, m_pc);
516  }
517  }
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 134 of file GlobalLinSysPETSc.cpp.

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

140  {
141  const int nHomDofs = pNumRows - pNumDir;
142 
144  {
145  m_precon = CreatePrecon(locToGloMap);
146  m_precon->BuildPreconditioner();
147  }
148 
149  // Populate RHS vector from input
150  VecSetValues(m_b, nHomDofs, &m_reorderedMap[0],
151  &pInput[pNumDir], INSERT_VALUES);
152 
153  // Assemble RHS vector
154  VecAssemblyBegin(m_b);
155  VecAssemblyEnd (m_b);
156 
157  // Do system solve
158  KSPSolve(m_ksp, m_b, m_x);
159 
160  // Scatter results to local vector
161  VecScatterBegin(m_ctx, m_x, m_locVec,
162  INSERT_VALUES, SCATTER_FORWARD);
163  VecScatterEnd (m_ctx, m_x, m_locVec,
164  INSERT_VALUES, SCATTER_FORWARD);
165 
166  // Copy results into output vector
167  PetscScalar *tmp;
168  VecGetArray (m_locVec, &tmp);
169  Vmath::Vcopy (nHomDofs, tmp, 1, &pOutput[pNumDir], 1);
170  VecRestoreArray(m_locVec, &tmp);
171  }
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering...
vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
Vec m_x
PETSc vector objects used for local storage.
KSP m_ksp
KSP object that represents solver system.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1047
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
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