gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
linearSystemPETSc.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef LINEAR_SYSTEM_PETSC_H
7 #define LINEAR_SYSTEM_PETSC_H
8 
9 // Interface to PETSc 3.x
10 
11 // Options for PETSc can be provided on the command line, or in the
12 // file ~/.petscrc. By default, we use the following options (GMRES
13 // with ILU(6) preconditioner and RCMK renumbering):
14 //
15 // -ksp_type gmres
16 // -pc_type ilu
17 // -pc_factor_levels 6
18 // -pc_factor_mat_ordering rcm
19 // -ksp_rtol 1.e-8
20 //
21 // Other useful options include:
22 //
23 // -ksp_gmres_restart 100
24 // -ksp_monitor
25 //
26 // To use a direct solver (a sparse lu) instead of an iterative
27 // solver, use
28 //
29 // -ksp_type preonly
30 // -pc_type lu
31 //
32 // When PETSc is compiled with external direct solvers (UMFPACK,
33 // SuperLU, etc.), they can be selected like this:
34 //
35 // -pc_factor_mat_solver_package umfpack
36 
37 #include "GmshConfig.h"
38 #include "GmshMessage.h"
39 #include "linearSystem.h"
40 #include "sparsityPattern.h"
41 #include "fullMatrix.h"
42 #include <string.h>
43 #include <vector>
44 
45 #if defined(HAVE_PETSC)
46 
47 #ifndef SWIG
48 #include "petsc.h"
49 #include "petscksp.h"
50 #else
51 typedef struct _p_Mat *Mat;
52 typedef struct _p_Vec *Vec;
53 typedef struct _p_KSP *KSP;
54 #endif
55 
56 // support old PETSc version, try to avoid using PETSC_VERSION in other places
57 #if PETSC_VERSION_RELEASE != 0 && \
58  (PETSC_VERSION_MAJOR < 3 || \
59  (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2))
60 #define KSPDestroy(k) KSPDestroy(*(k))
61 #define MatDestroy(m) MatDestroy(*(m))
62 #define VecDestroy(v) VecDestroy(*(v))
63 #define PetscViewerDestroy(v) PetscViewerDestroy(*(v))
64 #define PetscBool PetscTruth
65 #define PetscOptionsGetBool PetscOptionsGetTruth
66 #endif
67 
68 // The petsc3.5 change log says:
69 // "KSPSetOperators() no longer has the MatStructure argument. The Mat objects
70 // now track that information themselves. Use KPS/PCSetReusePreconditioner() to
71 // prevent the recomputation of the preconditioner if the operator changed in
72 // the way that SAME_PRECONDITIONER did with KSPSetOperators()" So I guess this
73 // should be called with PETSC_TRUE as second argument only for
74 // SAME_PRECONDITIONER and false otherwise (i.e. for SAME_NONZERO_PATTERN,
75 // DIFFRENT_NONZERO_PATTERN) but it is a guess...
76 #if(PETSC_VERSION_MAJOR < 3 || \
77  (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 5))
78 #define KSPSetOperators(_ksp, _a, _b, SAME_PRECONDITIONER) \
79  KSPSetOperators(_ksp, _a, _b, SAME_PRECONDITIONER)
80 #else
81 #define SAME_PRECONDITIONER 999
82 #define KSPSetOperators(_ksp, _a, _b, OPT_PRECON) \
83  (KSPSetOperators(_ksp, _a, _b), \
84  KSPSetReusePreconditioner(_ksp, \
85  PetscBool(OPT_PRECON == SAME_PRECONDITIONER)))
86 #endif
87 
88 // Deprecated method PetscViewerSetFormat starts 3.7 the only instance we use
89 // can be replaced by PetscViewerPushFormat as we do not change the format of
90 // the same viewer in such a case PetscViewerPopFormat should be used and the
91 // following will not work
92 #if(PETSC_VERSION_MAJOR < 3 || \
93  (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 7))
94 #define PetscViewerPushFormat(viewer, format) \
95  PetscViewerSetFormat(viewer, format)
96 #endif
97 
98 template <class scalar> class linearSystemPETSc : public linearSystem<scalar> {
99 protected:
100  bool _isAllocated, _kspAllocated, _entriesPreAllocated;
101  bool _matrixChangedSinceLastSolve;
102  bool _valuesNotAssembled; // cannot use MatAssembled since MatAssembled return
103  // false for an empty matrix
104  Mat _a;
105  Vec _b, _x;
106  KSP _ksp;
107  int _localRowStart, _localRowEnd, _localSize, _globalSize;
108  sparsityPattern _sparsity;
109  void _kspCreate();
110  void _assembleMatrixIfNeeded();
111 #ifndef SWIG
112  MPI_Comm _comm;
113 #endif
114  int _getBlockSizeFromParameters() const;
115 
116 public:
118  void insertInSparsityPattern(int i, int j);
119  bool isAllocated() const { return _isAllocated; }
120  void preAllocateEntries();
121  virtual void allocate(int nbRows);
122  void print();
123  void clear();
124  void getFromMatrix(int row, int col, scalar &val) const;
125  void addToRightHandSide(int row, const scalar &val, int ith = 0);
126  void getFromRightHandSide(int row, scalar &val) const;
127  double normInfRightHandSide() const;
128  int getNumKspIteration() const;
129  void addToMatrix(int row, int col, const scalar &val);
130  void addToSolution(int row, const scalar &val);
131  void getFromSolution(int row, scalar &val) const;
132  void zeroMatrix();
133  void zeroRightHandSide();
134  void zeroSolution();
135  void printMatlab(const char *filename) const;
136  virtual int systemSolve();
137  Mat getMatrix() { return _a; }
138  virtual int matMult();
139 // std::vector<scalar> getData();
140 // std::vector<int> getRowPointers();
141 // std::vector<int> getColumnsIndices();
142 #ifndef SWIG
143  linearSystemPETSc(MPI_Comm com);
144  MPI_Comm getComm() const { return _comm; }
145 #endif
147 };
148 #else
149 
150 template <class scalar> class linearSystemPETSc : public linearSystem<scalar> {
151 public:
153  {
154  Msg::Error("PETSc is not available in this version of Gmsh");
155  }
156  bool isAllocated() const { return false; }
157  void allocate(int nbRows) {}
158  void clear() {}
159  void addToMatrix(int row, int col, const scalar &val) {}
160  void getFromMatrix(int row, int col, scalar &val) const {}
161  void addToRightHandSide(int row, const scalar &val, int ith = 0) {}
162  void addToSolution(int row, const scalar &val) {}
163  void getFromRightHandSide(int row, scalar &val) const {}
164  void getFromSolution(int row, scalar &val) const {}
165  int getNumKspIteration() const { return 0; };
166  void zeroMatrix() {}
168  void zeroSolution() {}
169  void printMatlab(const char *filename) const {};
170  virtual int systemSolve() { return 0; }
171  double normInfRightHandSide() const { return 0; }
172  virtual int matMult() { return 0; }
173 };
174 #endif
175 #endif
sparsityPattern
Definition: sparsityPattern.h:13
linearSystemPETSc::getNumKspIteration
int getNumKspIteration() const
Definition: linearSystemPETSc.h:165
linearSystemPETSc::linearSystemPETSc
linearSystemPETSc()
Definition: linearSystemPETSc.h:152
linearSystemPETSc
Definition: linearSystemPETSc.h:150
linearSystemPETSc::zeroRightHandSide
void zeroRightHandSide()
Definition: linearSystemPETSc.h:167
linearSystemPETSc::getFromMatrix
void getFromMatrix(int row, int col, scalar &val) const
Definition: linearSystemPETSc.h:160
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
linearSystemPETSc::addToMatrix
void addToMatrix(int row, int col, const scalar &val)
Definition: linearSystemPETSc.h:159
linearSystemPETSc::addToRightHandSide
void addToRightHandSide(int row, const scalar &val, int ith=0)
Definition: linearSystemPETSc.h:161
GmshMessage.h
linearSystemPETSc::printMatlab
void printMatlab(const char *filename) const
Definition: linearSystemPETSc.h:169
linearSystemPETSc::getFromRightHandSide
void getFromRightHandSide(int row, scalar &val) const
Definition: linearSystemPETSc.h:163
linearSystemPETSc::allocate
void allocate(int nbRows)
Definition: linearSystemPETSc.h:157
linearSystemBase::preAllocateEntries
virtual void preAllocateEntries()
Definition: linearSystem.h:22
linearSystemPETSc::normInfRightHandSide
double normInfRightHandSide() const
Definition: linearSystemPETSc.h:171
linearSystemPETSc::zeroSolution
void zeroSolution()
Definition: linearSystemPETSc.h:168
linearSystemPETSc::clear
void clear()
Definition: linearSystemPETSc.h:158
linearSystemBase::insertInSparsityPattern
virtual void insertInSparsityPattern(int _row, int _col)
Definition: linearSystem.h:33
linearSystemPETSc::matMult
virtual int matMult()
Definition: linearSystemPETSc.h:172
linearSystemPETSc::isAllocated
bool isAllocated() const
Definition: linearSystemPETSc.h:156
linearSystemPETSc::systemSolve
virtual int systemSolve()
Definition: linearSystemPETSc.h:170
sparsityPattern.h
linearSystemPETSc::addToSolution
void addToSolution(int row, const scalar &val)
Definition: linearSystemPETSc.h:162
linearSystemPETSc::zeroMatrix
void zeroMatrix()
Definition: linearSystemPETSc.h:166
linearSystemPETSc::getFromSolution
void getFromSolution(int row, scalar &val) const
Definition: linearSystemPETSc.h:164
fullMatrix.h
linearSystem
Definition: linearSystem.h:38
linearSystem.h