monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_solver.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 
5 #include <functional>
6 
7 namespace monolish {
11 namespace solver {
12 
18 enum class initvec_scheme {
19  RANDOM,
20  USER,
21 };
22 
23 template <typename MATRIX, typename Float> class precondition;
24 
32 template <typename MATRIX, typename Float> class solver {
33 private:
34 protected:
35  int lib = 0;
36  double tol = 1.0e-8;
37  size_t miniter = 0;
38  size_t maxiter = SIZE_MAX;
39  size_t resid_method = 0;
40  bool print_rhistory = false;
41  std::string rhistory_file;
42  std::ostream *rhistory_stream;
44 
45  double final_resid = 0;
46  size_t final_iter = 0;
47 
48  Float omega = 1.9; // for SOR
49  int singularity; // for sparse LU/QR/Cholesky
50  int reorder = 3; // for sparse LU/QR/Cholesky;
51 
54 
55 public:
59  solver(){};
60 
64  ~solver() {
65  if (rhistory_stream != &std::cout && rhistory_file.empty() != true) {
66  delete rhistory_stream;
67  }
68  }
69 
74  template <class PRECOND> void set_create_precond(PRECOND &p);
75 
80  template <class PRECOND> void set_apply_precond(PRECOND &p);
81 
86  void set_lib(int l) { lib = l; }
87 
92  void set_tol(double t) { tol = t; }
93 
98  void set_maxiter(size_t max) { maxiter = max; }
99 
104  void set_miniter(size_t min) { miniter = min; }
105 
110  void set_residual_method(size_t r) { resid_method = r; }
111 
116  void set_print_rhistory(bool flag) {
117  print_rhistory = flag;
118  rhistory_stream = &std::cout;
119  }
120 
125  void set_rhistory_filename(std::string file) {
126  rhistory_file = file;
127 
128  // file open
129  rhistory_stream = new std::ofstream(rhistory_file);
130  if (rhistory_stream->fail()) {
131  throw std::runtime_error("error bad filename");
132  }
133  }
134 
141 
146  [[nodiscard]] int get_lib() const { return lib; }
147 
152  [[nodiscard]] double get_tol() const { return tol; }
153 
158  [[nodiscard]] size_t get_maxiter() const { return maxiter; }
159 
164  [[nodiscard]] size_t get_miniter() const { return miniter; }
165 
170  [[nodiscard]] size_t get_residual_method() const { return resid_method; }
171 
176  bool get_print_rhistory() const { return print_rhistory; }
177 
183 
191  void set_omega(Float w) { omega = w; };
192 
199  Float get_omega() { return omega; };
200 
207  void set_reorder(int r) { reorder = r; }
208 
215  int get_reorder() { return reorder; }
216 
223  int get_singularity() { return singularity; }
224 
225  double get_final_residual() { return final_resid; }
226  size_t get_final_iter() { return final_iter; }
227 };
228 
232 template <typename MATRIX, typename Float> class precondition {
233 private:
234 public:
236  MATRIX *A;
237 
238  std::function<void(MATRIX &)> create_precond;
239  std::function<void(const vector<Float> &r, vector<Float> &z)> apply_precond;
240 
241  std::function<void(void)> get_precond();
242 
243  void set_precond_data(vector<Float> &m) { M = m; };
245 
247  auto create = [](MATRIX &) {};
248  auto apply = [](const vector<Float> &r, vector<Float> &z) { z = r; };
249  create_precond = create;
250  apply_precond = apply;
251  };
252 };
254 } // namespace solver
255 } // namespace monolish
std::function< void(const vector< Float > &r, vector< Float > &z)> apply_precond
std::function< void(void)> get_precond()
std::function< void(MATRIX &)> create_precond
void set_precond_data(vector< Float > &m)
std::ostream * rhistory_stream
size_t get_maxiter() const
get maxiter
int get_lib() const
get library option
void set_miniter(size_t min)
set min iter. (default = 0)
void set_tol(double t)
set tolerance (default:1.0e-8)
int get_reorder()
0: no ordering 1: symrcm, 2: symamd, 3: csrmetisnd is used to reduce zero fill-in.
void set_apply_precond(PRECOND &p)
set precondition apply function
void set_residual_method(size_t r)
set residual method (default=0)
void set_print_rhistory(bool flag)
print rhistory to standart out true/false. (default = false)
void set_lib(int l)
set library option (to change library, monolish, cusolver, etc.)
bool get_print_rhistory() const
get print rhistory status
Float get_residual(vector< Float > &x)
precondition< MATRIX, Float > precond
void set_maxiter(size_t max)
set max iter. (default = SIZE_MAX)
void set_reorder(int r)
0: no ordering 1: symrcm, 2: symamd, 3: csrmetisnd is used to reduce zero fill-in.
Float get_omega()
get the relaxation coefficient omega for SOR method (Default: 1.9)
void set_omega(Float w)
set the relaxation coefficient omega for SOR method (0 < w < 2, Default: 1.9)
size_t get_residual_method() const
get residual method(default=0)
void set_initvec_scheme(initvec_scheme scheme)
set how to handle initial vector
~solver()
delete solver class
initvec_scheme get_initvec_scheme() const
get handling scheme of initial vector handling
double get_tol() const
get tolerance
size_t get_miniter() const
get miniter
void set_rhistory_filename(std::string file)
rhistory filename
solver()
create solver class
void set_create_precond(PRECOND &p)
set precondition create function
int get_singularity()
-1 if A is symmetric positive definite. default reorder algorithm is csrmetisnd
void max(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with greatest elements of two matrices (C[0:nnz] = max(A[0:nnz],...
void min(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with smallest elements of two matrices (C[0:nnz] = min(A[0:nnz],...
initvec_scheme
Enum class defining how to handle initial vectors RANDOM use randomly initialized vectors USER use in...
monolish namespaces