monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_solver.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 
4 #if defined USE_MPI
5 #include <mpi.h>
6 #endif
7 
9 #include <functional>
10 
11 namespace monolish {
12 
16 namespace solver {
17 
23 enum class initvec_scheme {
24  RANDOM,
25  USER,
26 };
27 
28 template <typename MATRIX, typename Float> class precondition;
29 
33 template <typename MATRIX, typename Float> class solver {
34 private:
35 protected:
36  int lib = 0;
37  double tol = 1.0e-8;
38  size_t miniter = 0;
39  size_t maxiter = SIZE_MAX;
40  size_t resid_method = 0;
41  bool print_rhistory = false;
42  std::string rhistory_file;
43  std::ostream *rhistory_stream;
45 
46  Float get_residual(vector<Float> &x);
47 
49 
50 public:
55  solver(){};
56 
61  ~solver() {
62  if (rhistory_stream != &std::cout && rhistory_file.empty() != true) {
63  delete rhistory_stream;
64  }
65  }
66 
71  template <class PRECOND> void set_create_precond(PRECOND &p);
72 
77  template <class PRECOND> void set_apply_precond(PRECOND &p);
78 
83  void set_lib(int l) { lib = l; }
84 
89  void set_tol(double t) { tol = t; }
90 
95  void set_maxiter(size_t max) { maxiter = max; }
96 
101  void set_miniter(size_t min) { miniter = min; }
102 
107  void set_residual_method(size_t r) { resid_method = r; }
108 
113  void set_print_rhistory(bool flag) {
114  print_rhistory = flag;
115  rhistory_stream = &std::cout;
116  }
117 
122  void set_rhistory_filename(std::string file) {
123  rhistory_file = file;
124 
125  // file open
126  rhistory_stream = new std::ofstream(rhistory_file);
127  if (rhistory_stream->fail()) {
128  throw std::runtime_error("error bad filename");
129  }
130  }
131 
138 
143  int get_lib() const { return lib; }
144 
149  double get_tol() const { return tol; }
150 
155  size_t get_maxiter() const { return maxiter; }
156 
161  size_t get_miniter() const { return miniter; }
162 
167  size_t get_residual_method() const { return resid_method; }
168 
173  bool get_print_rhistory() const { return print_rhistory; }
174 
180 };
181 
185 template <typename MATRIX, typename Float> class precondition {
186 private:
187 public:
189  MATRIX tmpA;
190 
191  std::function<void(MATRIX &)> create_precond;
192  std::function<void(const vector<Float> &r, vector<Float> &z)> apply_precond;
193 
194  std::function<void(void)> get_precond();
195 
196  void set_precond_data(vector<Float> &m) { M = m; };
198 
200  auto create = [](MATRIX &) {};
201  auto apply = [](const vector<Float> &r, vector<Float> &z) { z = r; };
202  create_precond = create;
203  apply_precond = apply;
204  };
205 };
206 
207 } // namespace solver
208 } // namespace monolish
monolish::vml::min
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],...
Definition: matrix_vml.cpp:390
monolish::solver::solver::set_apply_precond
void set_apply_precond(PRECOND &p)
set precondition apply fucntion
Definition: precond.cpp:51
monolish::solver::solver::precond
precondition< MATRIX, Float > precond
Definition: monolish_solver.hpp:48
monolish::solver::solver::rhistory_file
std::string rhistory_file
Definition: monolish_solver.hpp:42
monolish::solver::solver::print_rhistory
bool print_rhistory
Definition: monolish_solver.hpp:41
monolish::solver::solver::set_lib
void set_lib(int l)
set library option (to change library, monolish, cusolver, etc.)
Definition: monolish_solver.hpp:83
monolish::vml::max
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],...
Definition: matrix_vml.cpp:382
monolish::solver::solver::get_residual_method
size_t get_residual_method() const
get residual method(default=0)
Definition: monolish_solver.hpp:167
monolish::solver::precondition::set_precond_data
void set_precond_data(vector< Float > &m)
Definition: monolish_solver.hpp:196
monolish::solver::solver::get_tol
double get_tol() const
get tolerance
Definition: monolish_solver.hpp:149
monolish::solver::solver::get_residual
Float get_residual(vector< Float > &x)
Definition: equation.cpp:8
monolish::solver::solver::set_residual_method
void set_residual_method(size_t r)
set residual method (default=0)
Definition: monolish_solver.hpp:107
monolish::solver::solver::set_miniter
void set_miniter(size_t min)
set min iter. (default = 0)
Definition: monolish_solver.hpp:101
monolish::solver::initvec_scheme
initvec_scheme
Enum class defining how to handle initial vectors RANDOM use randomly initialized vectors USER use in...
Definition: monolish_solver.hpp:23
monolish::solver::solver::~solver
~solver()
delete solver class
Definition: monolish_solver.hpp:61
monolish::solver::solver::set_maxiter
void set_maxiter(size_t max)
set max iter. (default = SIZE_MAX)
Definition: monolish_solver.hpp:95
monolish_common.hpp
monolish::solver::solver::get_maxiter
size_t get_maxiter() const
get maxiter
Definition: monolish_solver.hpp:155
monolish::solver::precondition::create_precond
std::function< void(MATRIX &)> create_precond
Definition: monolish_solver.hpp:191
monolish::solver::solver
solver base class
Definition: monolish_solver.hpp:33
monolish::solver::solver::solver
solver()
create solver class
Definition: monolish_solver.hpp:55
monolish::solver::initvec_scheme::RANDOM
@ RANDOM
monolish::solver::solver::resid_method
size_t resid_method
Definition: monolish_solver.hpp:40
monolish::solver::solver::initvecscheme
initvec_scheme initvecscheme
Definition: monolish_solver.hpp:44
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::solver::precondition::apply_precond
std::function< void(const vector< Float > &r, vector< Float > &z)> apply_precond
Definition: monolish_solver.hpp:192
monolish::solver::solver::miniter
size_t miniter
Definition: monolish_solver.hpp:38
monolish::solver::solver::get_miniter
size_t get_miniter() const
get miniter
Definition: monolish_solver.hpp:161
monolish::solver::solver::rhistory_stream
std::ostream * rhistory_stream
Definition: monolish_solver.hpp:43
monolish::solver::solver::set_rhistory_filename
void set_rhistory_filename(std::string file)
rhistory filename
Definition: monolish_solver.hpp:122
monolish::solver::solver::set_initvec_scheme
void set_initvec_scheme(initvec_scheme scheme)
set how to handle initial vector
Definition: monolish_solver.hpp:136
monolish::solver::precondition::get_precond
std::function< void(void)> get_precond()
monolish::solver::solver::set_tol
void set_tol(double t)
set tolerance (default:1.0e-8)
Definition: monolish_solver.hpp:89
monolish::solver::precondition::get_precond_data
vector< Float > get_precond_data()
Definition: monolish_solver.hpp:197
monolish::solver::initvec_scheme::USER
@ USER
monolish::solver::solver::maxiter
size_t maxiter
Definition: monolish_solver.hpp:39
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::solver::solver::tol
double tol
Definition: monolish_solver.hpp:37
monolish::solver::solver::set_print_rhistory
void set_print_rhistory(bool flag)
print rhistory to standart out true/false. (default = false)
Definition: monolish_solver.hpp:113
monolish::solver::solver::get_print_rhistory
bool get_print_rhistory() const
get print rhistory status
Definition: monolish_solver.hpp:173
monolish::solver::solver::get_lib
int get_lib() const
get library option
Definition: monolish_solver.hpp:143
monolish::solver::precondition::tmpA
MATRIX tmpA
Definition: monolish_solver.hpp:189
monolish::solver::solver::set_create_precond
void set_create_precond(PRECOND &p)
set precondition create fucntion
Definition: precond.cpp:11
monolish::solver::precondition::precondition
precondition()
Definition: monolish_solver.hpp:199
monolish::solver::precondition
precondition base class
Definition: monolish_solver.hpp:28
monolish::solver::solver::lib
int lib
Definition: monolish_solver.hpp:36
monolish::solver::solver::get_initvec_scheme
initvec_scheme get_initvec_scheme() const
get handling scheme of initial vector handling
Definition: monolish_solver.hpp:179
monolish::solver::precondition::M
vector< Float > M
Definition: monolish_solver.hpp:188