Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GlobalLinSysPETSc.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File GlobalLinSys.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: GlobalLinSys definition
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 #include "petscis.h"
39 #include "petscversion.h"
40 
41 namespace Nektar
42 {
43  namespace MultiRegions
44  {
45  /**
46  * @class GlobalLinSysPETSc
47  *
48  * Solves a linear system using direct methods.
49  */
50 
51 
52  /// Constructor for full direct matrix solve.
54  const GlobalLinSysKey &pKey,
55  const boost::weak_ptr<ExpList> &pExp,
56  const boost::shared_ptr<AssemblyMap> &pLocToGloMap)
57  : GlobalLinSys(pKey, pExp, pLocToGloMap)
58  {
59  // Initialise PETSc
60  PetscInitialize(0, NULL, NULL, NULL);
61 
62  // Create matrix
63  MatCreate(PETSC_COMM_WORLD, &m_matrix);
64  }
65 
67  {
68  }
69 
71  const int pNumRows,
72  const Array<OneD,const NekDouble> &pInput,
73  Array<OneD, NekDouble> &pOutput,
74  const AssemblyMapSharedPtr &locToGloMap,
75  const int pNumDir)
76  {
77  const int nHomDofs = pNumRows - pNumDir;
78 
79  // Populate RHS vector from input
80  VecSetValues(m_b, nHomDofs, &m_reorderedMap[0], &pInput[pNumDir], INSERT_VALUES);
81 
82  // Assemble RHS vector
83  VecAssemblyBegin(m_b);
84  VecAssemblyEnd (m_b);
85 
86  // Do system solve
87  PetscErrorCode ierr = KSPSolve(m_ksp, m_b, m_x);
88 
89  // Grab number of iterations taken
90  PetscInt its;
91  KSPGetIterationNumber(m_ksp, &its);
92  cout << "iterations = " << its << endl;
93 
94  // Scatter results to local vector
95  VecScatterBegin(m_ctx, m_x, m_locVec, INSERT_VALUES, SCATTER_FORWARD);
96  VecScatterEnd (m_ctx, m_x, m_locVec, INSERT_VALUES, SCATTER_FORWARD);
97 
98  // Copy results into output vector
99  PetscScalar *avec;
100  VecGetArray (m_locVec, &avec);
101  Vmath::Vcopy (nHomDofs, avec, 1, &pOutput[pNumDir], 1);
102  VecRestoreArray(m_locVec, &avec);
103  }
104 
106  {
107  const int nHomDofs = m_reorderedMap.size();
108 
109  // Create local and global numbering systems for vector
110  IS isGlobal, isLocal;
111  ISCreateGeneral(PETSC_COMM_SELF, nHomDofs, &m_reorderedMap[0],
112  PETSC_COPY_VALUES, &isGlobal);
113  ISCreateStride (PETSC_COMM_SELF, nHomDofs, 0, 1, &isLocal);
114 
115  // Create local vector for output
116  VecCreate (PETSC_COMM_SELF, &m_locVec);
117  VecSetSizes (m_locVec, m_reorderedMap.size(), PETSC_DECIDE);
118  VecSetFromOptions(m_locVec);
119 
120  // Create scatter context
121  VecScatterCreate (m_x, isGlobal, m_locVec, isLocal, &m_ctx);
122 
123  // Clean up
124  ISDestroy(&isGlobal);
125  ISDestroy(&isLocal);
126  }
127 
129  const Array<OneD, const int> &glo2uniMap,
130  const Array<OneD, const int> &glo2unique,
131  const AssemblyMapSharedPtr &pLocToGloMap)
132  {
134  = m_expList.lock()->GetSession()->GetComm();
135 
136  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
137  const int nHomDofs = glo2uniMap.num_elements() - nDirDofs;
138  const int nProc = vComm->GetSize();
139  const int rank = vComm->GetRank();
140 
141  int n, cnt;
142 
143  m_nLocal = Vmath::Vsum(nHomDofs, glo2unique + nDirDofs, 1);
144 
145  m_reorderedMap.resize(nHomDofs);
146 
147  Array<OneD, int> localCounts(nProc, 0), localOffset(nProc, 0);
148  localCounts[rank] = nHomDofs;
149  vComm->AllReduce(localCounts, LibUtilities::ReduceSum);
150 
151  for (n = 1; n < nProc; ++n)
152  {
153  localOffset[n] = localOffset[n-1] + localCounts[n-1];
154  }
155 
156  int totHomDofs = Vmath::Vsum(nProc, localCounts, 1);
157  vector<unsigned int> allUniIds(totHomDofs, 0);
158 
159  for (n = 0; n < nHomDofs; ++n)
160  {
161  int gid = n + nDirDofs;
162  allUniIds[n + localOffset[rank]] = glo2uniMap[gid];
163  }
164 
165  vComm->AllReduce(allUniIds, LibUtilities::ReduceSum);
166  std::sort(allUniIds.begin(), allUniIds.end());
167  map<int,int> uniIdReorder;
168 
169  for (cnt = n = 0; n < allUniIds.size(); ++n)
170  {
171  if (uniIdReorder.count(allUniIds[n]) > 0)
172  {
173  continue;
174  }
175 
176  uniIdReorder[allUniIds[n]] = cnt++;
177  }
178 
179  for (n = 0; n < nHomDofs; ++n)
180  {
181  int gid = n + nDirDofs;
182  int uniId = glo2uniMap[gid];
183  ASSERTL0(uniIdReorder.count(uniId) > 0, "Error in ordering");
184  m_reorderedMap[n] = uniIdReorder[uniId];
185  }
186  }
187 
189  {
190  // CREATE VECTORS
191  VecCreate (PETSC_COMM_WORLD, &m_x);
192  VecSetSizes (m_x, m_nLocal, PETSC_DECIDE);
193  VecSetFromOptions(m_x);
194  VecDuplicate (m_x, &m_b);
195 
196  // CREATE MATRICES
197  MatCreate (PETSC_COMM_WORLD, &m_matrix);
198  MatSetType (m_matrix, MATMPIAIJ);
199  MatSetSizes (m_matrix, m_nLocal, m_nLocal,
200  PETSC_DETERMINE, PETSC_DETERMINE);
201  MatSetFromOptions(m_matrix);
202  MatSetUp (m_matrix);
203  }
204 
206  {
207  KSPCreate(PETSC_COMM_WORLD, &m_ksp);
208  KSPSetTolerances(
209  m_ksp, tolerance, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
210  KSPSetFromOptions(m_ksp);
211 #if PETSC_VERSION_GE(3,5,0)
212  KSPSetOperators(m_ksp, m_matrix, m_matrix);
213 #else
214  KSPSetOperators(m_ksp, m_matrix, m_matrix, SAME_NONZERO_PATTERN);
215 #endif
216  }
217  }
218 }