OGS
LisMatrix.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include <string>
18 #include <vector>
19 
20 #include <lis.h>
21 
24 
25 #include "LisOption.h"
26 #include "LisCheck.h"
27 
28 namespace MathLib
29 {
30 // Forward declarations.
31 class LisVector;
32 class LisMatrix;
33 
34 template <typename SPARSITY_PATTERN>
35 struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>;
36 
44 class LisMatrix
45 {
46 public:
48  enum class MatrixType : int
49  {
50  CRS = 1,
51  CCS = 2,
52  MSR = 3,
53  DIA = 4,
54  ELL = 5,
55  JDS = 6,
56  BSR = 7,
57  BSC = 8,
58  VBR = 9,
59  COO = 10,
60  DNS = 11
61  };
62 
63  using IndexType = LIS_INT;
64 public:
70  LisMatrix(std::size_t n_rows, MatrixType mat_type = MatrixType::CRS);
71 
82  LisMatrix(std::size_t n_rows, int nonzero, IndexType* row_ptr, IndexType* col_idx,
83  double* data);
84 
88  virtual ~LisMatrix();
89 
91  std::size_t getNumberOfRows() const { return n_rows_; }
92 
94  std::size_t getNumberOfColumns() const { return getNumberOfRows(); }
95 
97  std::size_t getRangeBegin() const { return is_; }
98 
100  std::size_t getRangeEnd() const { return ie_; }
101 
103  void setZero();
104 
106  int setValue(IndexType rowId, IndexType colId, double v);
107 
109  int add(IndexType rowId, IndexType colId, double v);
110 
112  void write(const std::string &filename) const;
113 
115  LIS_MATRIX& getRawMatrix() { return AA_; }
116 
119  template<class T_DENSE_MATRIX>
120  void add(std::vector<IndexType> const& row_pos,
121  const T_DENSE_MATRIX &sub_matrix,
122  double fkt = 1.0)
123  {
124  this->add(row_pos, row_pos, sub_matrix, fkt);
125  }
126 
128  template<class T_DENSE_MATRIX>
129  void add(RowColumnIndices<IndexType> const& indices,
130  const T_DENSE_MATRIX &sub_matrix,
131  double fkt = 1.0)
132  {
133  this->add(indices.rows, indices.columns, sub_matrix, fkt);
134  }
135 
136  template <class T_DENSE_MATRIX>
137  void add(std::vector<IndexType> const& row_pos,
138  std::vector<IndexType> const& col_pos,
139  const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0);
140 
142  MatrixType getMatrixType() const { return mat_type_; }
143 
145  bool isAssembled() const { return is_assembled_; }
146 
147 private:
148  std::size_t const n_rows_;
150  LIS_MATRIX AA_;
151  LIS_VECTOR diag_;
155  IndexType
156  ie_;
158 
159  // friend function
160  friend bool finalizeMatrixAssembly(LisMatrix &mat);
161 
162  template <typename MATRIX, typename SPARSITY_PATTERN>
163  friend struct SetMatrixSparsity;
164 };
165 
166 template <class T_DENSE_MATRIX>
167 void LisMatrix::add(std::vector<IndexType> const& row_pos,
168  std::vector<IndexType> const& col_pos,
169  const T_DENSE_MATRIX& sub_matrix, double fkt)
170 {
171  auto const n_rows = row_pos.size();
172  auto const n_cols = col_pos.size();
173  for (auto i = decltype(n_rows){0}; i < n_rows; i++)
174  {
175  auto const row = row_pos[i];
176  for (auto j = decltype(n_cols){0}; j < n_cols; j++)
177  {
178  auto const col = col_pos[j];
179  add(row, col, fkt * sub_matrix(i, j));
180  }
181  }
182 };
183 
186 
188 template <typename SPARSITY_PATTERN>
189 struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN>
190 {
191 void operator()(LisMatrix& matrix,
192  SPARSITY_PATTERN const& sparsity_pattern) const
193 {
194  auto const n_rows = matrix.getNumberOfRows();
195  std::vector<LisMatrix::IndexType> row_sizes;
196  row_sizes.reserve(n_rows);
197 
198  // LIS needs 1 more entry, otherwise it starts reallocating arrays.
199  transform(cbegin(sparsity_pattern), cend(sparsity_pattern),
200  back_inserter(row_sizes), [](auto const i) { return i + 1; });
201 
202  int ierr = lis_matrix_malloc(matrix.AA_, 0, row_sizes.data());
203  checkLisError(ierr);
204 }
205 };
206 
207 
208 } // MathLib
Definition of the LisOption class.
LisMatrix is a wrapper class for matrix types of the linear iterative solvers library.
Definition: LisMatrix.h:45
IndexType ie_
location where the partial matrix AA_ ends in global matrix.
Definition: LisMatrix.h:156
LisMatrix(std::size_t n_rows, MatrixType mat_type=MatrixType::CRS)
Definition: LisMatrix.cpp:26
std::size_t getRangeBegin() const
return a start index of the active data range
Definition: LisMatrix.h:97
std::size_t const n_rows_
Definition: LisMatrix.h:148
std::size_t getNumberOfColumns() const
return the number of columns
Definition: LisMatrix.h:94
LIS_VECTOR diag_
Definition: LisMatrix.h:151
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition: LisMatrix.h:115
MatrixType const mat_type_
Definition: LisMatrix.h:149
std::size_t getNumberOfRows() const
return the number of rows
Definition: LisMatrix.h:91
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition: LisMatrix.cpp:92
std::size_t getRangeEnd() const
return an end index of the active data range
Definition: LisMatrix.h:100
int add(IndexType rowId, IndexType colId, double v)
add value
Definition: LisMatrix.cpp:103
void add(std::vector< IndexType > const &row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Definition: LisMatrix.h:120
void setZero()
reset this matrix with keeping its original dimension
Definition: LisMatrix.cpp:76
MatrixType getMatrixType() const
get this matrix type
Definition: LisMatrix.h:142
bool use_external_arrays_
Definition: LisMatrix.h:157
friend bool finalizeMatrixAssembly(LisMatrix &mat)
finish assembly to make this matrix be ready for use
Definition: LisMatrix.cpp:123
void write(const std::string &filename) const
printout this equation for debugging
Definition: LisMatrix.cpp:114
MatrixType
Matrix type.
Definition: LisMatrix.h:49
LIS_INT IndexType
Definition: LisMatrix.h:63
void add(RowColumnIndices< IndexType > const &indices, const T_DENSE_MATRIX &sub_matrix, double fkt=1.0)
Add sub-matrix at positions given by indices.
Definition: LisMatrix.h:129
LIS_MATRIX AA_
Definition: LisMatrix.h:150
virtual ~LisMatrix()
Definition: LisMatrix.cpp:62
bool isAssembled() const
return if this matrix is already assembled or not
Definition: LisMatrix.h:145
bool checkLisError(int err)
Definition: LisCheck.h:32
bool finalizeMatrixAssembly(MAT_T &)
LineIndex const & columns
void operator()(LisMatrix &matrix, SPARSITY_PATTERN const &sparsity_pattern) const
Definition: LisMatrix.h:191