gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
linearSystemCSR.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_CSR_H
7 #define LINEAR_SYSTEM_CSR_H
8 
9 #include <vector>
10 #include <string>
11 #include "GmshConfig.h"
12 #include "GmshMessage.h"
13 #include "linearSystem.h"
14 #include "sparsityPattern.h"
15 
16 typedef int INDEX_TYPE;
17 typedef struct {
18  int nmax;
19  int size;
20  int incr;
21  int n;
22  int isorder;
23  char *array;
24 } CSRList_T;
25 
26 void CSRList_Add(CSRList_T *liste, const void *data);
27 int CSRList_Nbr(CSRList_T *liste);
28 
29 template <class scalar> class linearSystemCSR : public linearSystem<scalar> {
30 protected:
31  bool sorted;
33  char *something;
35  std::vector<scalar> *_b, *_x;
36  sparsityPattern _sparsity; // only used for pre-allocation, does not store the
37  // sparsity once allocated
38 public:
40  int getNbUnk() { return linearSystemCSR<scalar>::_b->size(); }
42  : sorted(false), _entriesPreAllocated(false), _a(nullptr), _b(0), _x(0)
43  {
44  }
45  virtual bool isAllocated() const { return _a != nullptr; }
46  virtual void allocate(int);
47  virtual void clear() { allocate(0); }
48  virtual ~linearSystemCSR() { allocate(0); }
49  virtual void insertInSparsityPattern(int i, int j)
50  {
51  _sparsity.insertEntry(i, j);
52  }
53  virtual void preAllocateEntries();
54  virtual void addToMatrix(int il, int ic, const scalar &val)
55  {
57  INDEX_TYPE *jptr = (INDEX_TYPE *)_jptr->array;
58  INDEX_TYPE *ptr = (INDEX_TYPE *)_ptr->array;
59  INDEX_TYPE *ai = (INDEX_TYPE *)_ai->array;
60  scalar *a = (scalar *)_a->array;
61 
62  INDEX_TYPE position = jptr[il];
63 
64  if(sorted) { // use bisection and direct adressing if sorted
65  int p0 = jptr[il];
66  int p1 = jptr[il + 1];
67  while(p1 - p0 > 20) {
68  position = ((p0 + p1) / 2);
69  if(ai[position] > ic)
70  p1 = position;
71  else if(ai[position] < ic)
72  p0 = position + 1;
73  else {
74  a[position] += val;
75  return;
76  }
77  }
78  for(position = p0; position < p1; position++) {
79  if(ai[position] >= ic) {
80  if(ai[position] == ic) {
81  a[position] += val;
82  return;
83  }
84  break;
85  }
86  }
87  }
88  else if(something[il]) {
89  while(1) {
90  if(ai[position] == ic) {
91  a[position] += val;
92  return;
93  }
94  if(ptr[position] == 0) break;
95  position = ptr[position];
96  }
97  }
98 
99  INDEX_TYPE zero = 0;
100  CSRList_Add(_a, &val);
101  CSRList_Add(_ai, &ic);
102  CSRList_Add(_ptr, &zero);
103  // The pointers may have been modified if there has been a
104  // reallocation in CSRList_Add
105 
106  ptr = (INDEX_TYPE *)_ptr->array;
107  ai = (INDEX_TYPE *)_ai->array;
108  a = (scalar *)_a->array;
109 
110  INDEX_TYPE n = CSRList_Nbr(_a) - 1;
111 
112  if(!something[il]) {
113  jptr[il] = n;
114  something[il] = 1;
115  }
116  else
117  ptr[position] = n;
118  }
119  virtual void getMatrix(INDEX_TYPE *&jptr, INDEX_TYPE *&ai, double *&a);
120 
121  virtual void getFromMatrix(int row, int col, scalar &val) const
122  {
123  Msg::Error("getFromMatrix not implemented for CSR");
124  }
125  virtual void addToRightHandSide(int row, const scalar &val, int ith = 0)
126  {
127  if(!_b) return;
128  if(val != scalar()) (*_b)[row] += val;
129  }
130  virtual void addToSolution(int row, const scalar &val)
131  {
132  if(!_x) return;
133  if(val != scalar()) (*_x)[row] += val;
134  }
135  virtual void getFromRightHandSide(int row, scalar &val) const
136  {
137  if(!_b) return;
138  val = (*_b)[row];
139  }
140  virtual void getFromSolution(int row, scalar &val) const
141  {
142  if(!_x) return;
143  val = (*_x)[row];
144  }
145  virtual void zeroMatrix()
146  {
147  if(!_a) return;
148  int N = CSRList_Nbr(_a);
149  scalar *a = (scalar *)_a->array;
150  for(int i = 0; i < N; i++) a[i] = scalar();
151  }
152  virtual void zeroRightHandSide()
153  {
154  if(!_b) return;
155  for(std::size_t i = 0; i < _b->size(); i++) (*_b)[i] = scalar();
156  }
157  virtual void zeroSolution()
158  {
159  if(!_x) return;
160  for(std::size_t i = 0; i < _x->size(); i++) (*_x)[i] = scalar();
161  }
162  virtual double normInfRightHandSide() const
163  {
164  if(!_b) return 0.;
165  double nor = 0.;
166  double temp;
167  for(std::size_t i = 0; i < _b->size(); i++) {
168  temp = std::abs((*_b)[i]);
169  if(nor < temp) nor = temp;
170  }
171  return nor;
172  }
173 };
174 
175 template <class scalar>
176 class linearSystemCSRGmm : public linearSystemCSR<scalar> {
177 private:
178  std::string _method;
179  double _tol;
180  int _noisy;
181 
182 public:
183  linearSystemCSRGmm(const std::string &method = "gmres", double tol = 1e-8,
184  int noisy = 0)
185  : _method(method), _tol(tol), _noisy(noisy) {}
186  virtual ~linearSystemCSRGmm() {}
187  void setPrec(double p) { _tol = p; }
188  void setNoisy(int n) { _noisy = n; }
189  void setGmres(int n) { _method = (n ? "gmres" : "cg"); }
190  virtual int systemSolve()
191 #if !defined(HAVE_GMM)
192  {
193  Msg::Error("Gmm++ is not available in this version of Gmsh");
194  return 0;
195  }
196 #endif
197  ;
198 };
199 
200 #endif
linearSystemCSR::something
char * something
Definition: linearSystemCSR.h:33
CSRList_Nbr
int CSRList_Nbr(CSRList_T *liste)
Definition: linearSystemCSR.cpp:94
linearSystemCSR::_jptr
CSRList_T * _jptr
Definition: linearSystemCSR.h:34
linearSystemCSR::normInfRightHandSide
virtual double normInfRightHandSide() const
Definition: linearSystemCSR.h:162
sparsityPattern
Definition: sparsityPattern.h:13
linearSystemCSR::_b
std::vector< scalar > * _b
Definition: linearSystemCSR.h:35
linearSystemCSRGmm::_tol
double _tol
Definition: linearSystemCSR.h:179
linearSystemCSR
Definition: linearSystemCSR.h:29
linearSystemCSR::_x
std::vector< scalar > * _x
Definition: linearSystemCSR.h:35
linearSystemCSR::_entriesPreAllocated
bool _entriesPreAllocated
Definition: linearSystemCSR.h:32
CSRList_T::nmax
int nmax
Definition: linearSystemCSR.h:18
linearSystemCSRGmm::~linearSystemCSRGmm
virtual ~linearSystemCSRGmm()
Definition: linearSystemCSR.h:186
sparsityPattern::insertEntry
void insertEntry(int i, int j)
Definition: sparsityPattern.cpp:36
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
linearSystemCSR::allocate
virtual void allocate(int)
linearSystemCSR::getNbUnk
int getNbUnk()
Definition: linearSystemCSR.h:40
linearSystemCSRGmm::setPrec
void setPrec(double p)
Definition: linearSystemCSR.h:187
GmshMessage.h
CSRList_T::isorder
int isorder
Definition: linearSystemCSR.h:22
CSRList_T::n
int n
Definition: linearSystemCSR.h:21
linearSystemCSR::isAllocated
virtual bool isAllocated() const
Definition: linearSystemCSR.h:45
INDEX_TYPE
int INDEX_TYPE
Definition: linearSystemCSR.h:16
linearSystemCSRGmm::setGmres
void setGmres(int n)
Definition: linearSystemCSR.h:189
linearSystemCSR::~linearSystemCSR
virtual ~linearSystemCSR()
Definition: linearSystemCSR.h:48
CSRList_T::array
char * array
Definition: linearSystemCSR.h:23
linearSystemCSR::_ptr
CSRList_T * _ptr
Definition: linearSystemCSR.h:34
linearSystemCSR::linearSystemCSR
linearSystemCSR()
Definition: linearSystemCSR.h:41
linearSystemCSR::getMatrix
virtual void getMatrix(INDEX_TYPE *&jptr, INDEX_TYPE *&ai, double *&a)
linearSystemCSR::sorted
bool sorted
Definition: linearSystemCSR.h:31
linearSystemCSR::addToSolution
virtual void addToSolution(int row, const scalar &val)
Definition: linearSystemCSR.h:130
linearSystemCSR::getNNZ
int getNNZ()
Definition: linearSystemCSR.h:39
linearSystemCSR::_sparsity
sparsityPattern _sparsity
Definition: linearSystemCSR.h:36
linearSystemCSR::getFromMatrix
virtual void getFromMatrix(int row, int col, scalar &val) const
Definition: linearSystemCSR.h:121
CSRList_Add
void CSRList_Add(CSRList_T *liste, const void *data)
Definition: linearSystemCSR.cpp:86
linearSystemCSR::zeroSolution
virtual void zeroSolution()
Definition: linearSystemCSR.h:157
linearSystemCSR::_ai
CSRList_T * _ai
Definition: linearSystemCSR.h:34
linearSystemCSRGmm
Definition: linearSystemCSR.h:176
linearSystemCSRGmm::_method
std::string _method
Definition: linearSystemCSR.h:178
linearSystemCSR::insertInSparsityPattern
virtual void insertInSparsityPattern(int i, int j)
Definition: linearSystemCSR.h:49
CSRList_T::size
int size
Definition: linearSystemCSR.h:19
linearSystemCSR::clear
virtual void clear()
Definition: linearSystemCSR.h:47
linearSystemCSR::getFromSolution
virtual void getFromSolution(int row, scalar &val) const
Definition: linearSystemCSR.h:140
linearSystemCSR::preAllocateEntries
virtual void preAllocateEntries()
linearSystemCSR::addToRightHandSide
virtual void addToRightHandSide(int row, const scalar &val, int ith=0)
Definition: linearSystemCSR.h:125
sparsityPattern.h
linearSystemCSR::zeroMatrix
virtual void zeroMatrix()
Definition: linearSystemCSR.h:145
linearSystemCSR::zeroRightHandSide
virtual void zeroRightHandSide()
Definition: linearSystemCSR.h:152
linearSystemCSR::addToMatrix
virtual void addToMatrix(int il, int ic, const scalar &val)
Definition: linearSystemCSR.h:54
linearSystemCSRGmm::linearSystemCSRGmm
linearSystemCSRGmm(const std::string &method="gmres", double tol=1e-8, int noisy=0)
Definition: linearSystemCSR.h:183
linearSystemCSRGmm::systemSolve
virtual int systemSolve()
Definition: linearSystemCSR.h:190
linearSystemCSR::_a
CSRList_T * _a
Definition: linearSystemCSR.h:34
linearSystemCSRGmm::setNoisy
void setNoisy(int n)
Definition: linearSystemCSR.h:188
linearSystem
Definition: linearSystem.h:38
linearSystemCSR::getFromRightHandSide
virtual void getFromRightHandSide(int row, scalar &val) const
Definition: linearSystemCSR.h:135
CSRList_T
Definition: linearSystemCSR.h:17
linearSystemCSRGmm::_noisy
int _noisy
Definition: linearSystemCSR.h:180
CSRList_T::incr
int incr
Definition: linearSystemCSR.h:20
linearSystem.h