Nektar++
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Nektar::MultiRegions::GlobalLinSysPETSc Class Reference

A 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]

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

Protected Attributes

Mat m_matrix
 
Vec m_x
 
Vec m_b
 
Vec m_locVec
 
KSP m_ksp
 
vector< int > m_reorderedMap
 
VecScatter m_ctx
 
int m_nLocal
 
- 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, RobinBCInfoSharedPtrm_robinBCInfo
 Robin boundary info. More...
 

Detailed Description

A global linear system.

Solves a linear system using PETSc.

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

Definition at line 52 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 52 of file GlobalLinSysPETSc.cpp.

References m_matrix.

56  : GlobalLinSys(pKey, pExp, pLocToGloMap)
57  {
58  // Check PETSc is initialized
59  // For some reason, this is needed on OS X as logging is not
60  // initialized properly in the call within CommMpi.
61  PetscInitializeNoArguments();
62 
63  // Create matrix
64  MatCreate(PETSC_COMM_WORLD, &m_matrix);
65  }
GlobalLinSys(const GlobalLinSysKey &pKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
Nektar::MultiRegions::GlobalLinSysPETSc::~GlobalLinSysPETSc ( )
virtual

Definition at line 67 of file GlobalLinSysPETSc.cpp.

68  {
69  }

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.

Definition at line 152 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().

156  {
158  = m_expList.lock()->GetSession()->GetComm();
159 
160  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
161  const int nHomDofs = glo2uniMap.num_elements() - nDirDofs;
162  const int nProc = vComm->GetSize();
163  const int rank = vComm->GetRank();
164 
165  int n, cnt;
166 
167  // Count number of unique degrees of freedom on each process.
168  m_nLocal = Vmath::Vsum(nHomDofs, glo2unique + nDirDofs, 1);
169  m_reorderedMap.resize(nHomDofs);
170 
171  // Reduce coefficient counts across all processors.
172  Array<OneD, int> localCounts(nProc, 0), localOffset(nProc, 0);
173  localCounts[rank] = nHomDofs;
174  vComm->AllReduce(localCounts, LibUtilities::ReduceSum);
175 
176  for (n = 1; n < nProc; ++n)
177  {
178  localOffset[n] = localOffset[n-1] + localCounts[n-1];
179  }
180 
181  int totHomDofs = Vmath::Vsum(nProc, localCounts, 1);
182  vector<unsigned int> allUniIds(totHomDofs, 0);
183 
184  // Assemble list of universal IDs
185  for (n = 0; n < nHomDofs; ++n)
186  {
187  int gid = n + nDirDofs;
188  allUniIds[n + localOffset[rank]] = glo2uniMap[gid];
189  }
190 
191  // Reduce this across processors so that each process has a list of
192  // all universal IDs.
193  vComm->AllReduce(allUniIds, LibUtilities::ReduceSum);
194  std::sort(allUniIds.begin(), allUniIds.end());
195  map<int,int> uniIdReorder;
196 
197  // Renumber starting from 0.
198  for (cnt = n = 0; n < allUniIds.size(); ++n)
199  {
200  if (uniIdReorder.count(allUniIds[n]) > 0)
201  {
202  continue;
203  }
204 
205  uniIdReorder[allUniIds[n]] = cnt++;
206  }
207 
208  // Populate reordering map.
209  for (n = 0; n < nHomDofs; ++n)
210  {
211  int gid = n + nDirDofs;
212  int uniId = glo2uniMap[gid];
213  ASSERTL0(uniIdReorder.count(uniId) > 0, "Error in ordering");
214  m_reorderedMap[n] = uniIdReorder[uniId];
215  }
216  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:53
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:714
const boost::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:125
void Nektar::MultiRegions::GlobalLinSysPETSc::SetUpMatVec ( )
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.

Definition at line 224 of file GlobalLinSysPETSc.cpp.

References m_b, m_matrix, m_nLocal, and m_x.

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

225  {
226  // CREATE VECTORS
227  VecCreate (PETSC_COMM_WORLD, &m_x);
228  VecSetSizes (m_x, m_nLocal, PETSC_DECIDE);
229  VecSetFromOptions(m_x);
230  VecDuplicate (m_x, &m_b);
231 
232  // CREATE MATRICES
233  MatCreate (PETSC_COMM_WORLD, &m_matrix);
234  MatSetType (m_matrix, MATAIJ);
235  MatSetSizes (m_matrix, m_nLocal, m_nLocal,
236  PETSC_DETERMINE, PETSC_DETERMINE);
237  MatSetFromOptions(m_matrix);
238  MatSetUp (m_matrix);
239  }
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 121 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().

122  {
123  const int nHomDofs = m_reorderedMap.size();
124 
125  // Create local and global numbering systems for vector
126  IS isGlobal, isLocal;
127  ISCreateGeneral(PETSC_COMM_SELF, nHomDofs, &m_reorderedMap[0],
128  PETSC_COPY_VALUES, &isGlobal);
129  ISCreateStride (PETSC_COMM_SELF, nHomDofs, 0, 1, &isLocal);
130 
131  // Create local vector for output
132  VecCreate (PETSC_COMM_SELF, &m_locVec);
133  VecSetSizes (m_locVec, nHomDofs, PETSC_DECIDE);
134  VecSetFromOptions(m_locVec);
135 
136  // Create scatter context
137  VecScatterCreate (m_x, isGlobal, m_locVec, isLocal, &m_ctx);
138 
139  // Clean up
140  ISDestroy(&isGlobal);
141  ISDestroy(&isLocal);
142  }
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.

Definition at line 247 of file GlobalLinSysPETSc.cpp.

References m_ksp, and m_matrix.

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

248  {
249  KSPCreate(PETSC_COMM_WORLD, &m_ksp);
250  KSPSetTolerances(
251  m_ksp, tolerance, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
252  KSPSetFromOptions(m_ksp);
253 #if PETSC_VERSION_GE(3,5,0)
254  KSPSetOperators(m_ksp, m_matrix, m_matrix);
255 #else
256  KSPSetOperators(m_ksp, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
257 #endif
258  }
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 81 of file GlobalLinSysPETSc.cpp.

References m_b, m_ctx, m_ksp, m_locVec, m_reorderedMap, m_x, and Vmath::Vcopy().

87  {
88  const int nHomDofs = pNumRows - pNumDir;
89 
90  // Populate RHS vector from input
91  VecSetValues(m_b, nHomDofs, &m_reorderedMap[0],
92  &pInput[pNumDir], INSERT_VALUES);
93 
94  // Assemble RHS vector
95  VecAssemblyBegin(m_b);
96  VecAssemblyEnd (m_b);
97 
98  // Do system solve
99  KSPSolve(m_ksp, m_b, m_x);
100 
101  // Scatter results to local vector
102  VecScatterBegin(m_ctx, m_x, m_locVec,
103  INSERT_VALUES, SCATTER_FORWARD);
104  VecScatterEnd (m_ctx, m_x, m_locVec,
105  INSERT_VALUES, SCATTER_FORWARD);
106 
107  // Copy results into output vector
108  PetscScalar *tmp;
109  VecGetArray (m_locVec, &tmp);
110  Vmath::Vcopy (nHomDofs, tmp, 1, &pOutput[pNumDir], 1);
111  VecRestoreArray(m_locVec, &tmp);
112  }
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1038

Member Data Documentation

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

Definition at line 72 of file GlobalLinSysPETSc.h.

Referenced by SetUpMatVec(), and v_SolveLinearSystem().

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

Definition at line 75 of file GlobalLinSysPETSc.h.

Referenced by SetUpScatter(), and v_SolveLinearSystem().

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

Definition at line 73 of file GlobalLinSysPETSc.h.

Referenced by SetUpSolver(), and v_SolveLinearSystem().

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

Definition at line 72 of file GlobalLinSysPETSc.h.

Referenced by SetUpScatter(), and v_SolveLinearSystem().

Mat Nektar::MultiRegions::GlobalLinSysPETSc::m_matrix
protected
int Nektar::MultiRegions::GlobalLinSysPETSc::m_nLocal
protected

Definition at line 76 of file GlobalLinSysPETSc.h.

Referenced by CalculateReordering(), and SetUpMatVec().

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

Definition at line 72 of file GlobalLinSysPETSc.h.

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