monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
jacobi.cpp
Go to the documentation of this file.
1 #include "../../include/monolish_blas.hpp"
2 #include "../../include/monolish_equation.hpp"
3 #include "../../include/monolish_vml.hpp"
4 #include "../internal/monolish_internal.hpp"
5 
6 namespace monolish {
7 
9 // precondition
11 template <typename MATRIX, typename T>
13  Logger &logger = Logger::get_instance();
14  logger.solver_in(monolish_func);
15 
16  if (A.get_row() != A.get_col()) {
17  throw std::runtime_error("error A.row != A.col");
18  }
19 
20  this->precond.M.resize(A.get_row());
21  // send M
22  if (A.get_device_mem_stat() == true) {
23  this->precond.M.send();
24  }
25 
26  A.diag(this->precond.M);
27  vml::reciprocal(this->precond.M, this->precond.M);
28 
29  logger.solver_out();
30 }
31 template void equation::Jacobi<matrix::Dense<float>, float>::create_precond(
33 template void equation::Jacobi<matrix::Dense<double>, double>::create_precond(
35 
36 template void equation::Jacobi<matrix::CRS<float>, float>::create_precond(
38 template void equation::Jacobi<matrix::CRS<double>, double>::create_precond(
40 
41 template <typename MATRIX, typename T>
43  vector<T> &z) {
44  Logger &logger = Logger::get_instance();
45  logger.solver_in(monolish_func);
46 
47  vml::mul(this->precond.M, r, z); // x = Ab
48 
49  logger.solver_out();
50 }
51 template void equation::Jacobi<matrix::Dense<float>, float>::apply_precond(
52  const vector<float> &r, vector<float> &z);
53 template void equation::Jacobi<matrix::Dense<double>, double>::apply_precond(
54  const vector<double> &r, vector<double> &z);
55 
56 template void equation::Jacobi<matrix::CRS<float>, float>::apply_precond(
57  const vector<float> &r, vector<float> &z);
58 template void equation::Jacobi<matrix::CRS<double>, double>::apply_precond(
59  const vector<double> &r, vector<double> &z);
60 
62 // solver
64 
65 template <typename MATRIX, typename T>
67  vector<T> &b) {
68  Logger &logger = Logger::get_instance();
69  logger.solver_in(monolish_func);
70 
71  if (A.get_row() != A.get_col()) {
72  throw std::runtime_error("error, A.row != A.col");
73  }
74  if (A.get_device_mem_stat() != x.get_device_mem_stat() &&
75  A.get_device_mem_stat() != b.get_device_mem_stat()) {
76  throw std::runtime_error("error, A.get_device_mem_stat != "
77  "x.get_device_mem_stat != b.get_device_mem_stat");
78  }
79 
80  vector<T> r(A.get_row(), 0.0);
81  vector<T> t(A.get_row(), 0.0);
82  vector<T> s(A.get_row(), 0.0);
83  vector<T> d(A.get_row(), 0.0);
84  util::send(r, t, s, d);
85 
86  auto bnrm2 = blas::nrm2(b);
87  bnrm2 = 1.0 / bnrm2;
88  T nrm2 = 0.0;
89 
90  A.diag(d);
91  vml::reciprocal(d, d); // d[i] = 1/d[i]
92 
93  this->precond.create_precond(A);
94 
95  for (size_t iter = 0; iter < this->get_maxiter(); iter++) {
96 
97  /* x += D^{-1}(b - Ax) */
98  this->precond.apply_precond(x, s);
99  blas::copy(x, s);
100  blas::matvec(A, s, t);
101  blas::axpyz(-1, t, b, r);
102  nrm2 = blas::nrm2(r);
103  vml::mul(r, d, r);
104  vml::add(x, r, x);
105 
106  nrm2 = nrm2 * bnrm2;
107 
108  if (this->get_print_rhistory() == true) {
109  *this->rhistory_stream << iter + 1 << "\t" << std::scientific << nrm2
110  << std::endl;
111  }
112 
113  if (nrm2 < this->get_tol() && this->get_miniter() <= iter + 1) {
114  logger.solver_out();
116  }
117 
118  if (std::isnan(nrm2)) {
120  }
121  }
122 
123  logger.solver_out();
125 }
126 template int equation::Jacobi<matrix::Dense<double>, double>::monolish_Jacobi(
128 template int equation::Jacobi<matrix::Dense<float>, float>::monolish_Jacobi(
130 
131 template int equation::Jacobi<matrix::CRS<double>, double>::monolish_Jacobi(
133 template int equation::Jacobi<matrix::CRS<float>, float>::monolish_Jacobi(
135 
136 template <typename MATRIX, typename T>
138  Logger &logger = Logger::get_instance();
139  logger.solver_in(monolish_func);
140 
141  int ret = 0;
142  if (this->get_lib() == 0) {
143  ret = monolish_Jacobi(A, x, b);
144  }
145 
146  logger.solver_out();
147  return ret; // err code
148 }
149 
150 template int equation::Jacobi<matrix::Dense<float>, float>::solve(
152 template int equation::Jacobi<matrix::Dense<double>, double>::solve(
154 
155 template int equation::Jacobi<matrix::CRS<float>, float>::solve(
157 template int equation::Jacobi<matrix::CRS<double>, double>::solve(
159 } // namespace monolish
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::equation::Jacobi::solve
int solve(MATRIX &A, vector< Float > &x, vector< Float > &b)
solve Ax = b by jacobi method(lib=0: monolish)
Definition: jacobi.cpp:137
monolish::blas::nrm2
void nrm2(const vector< double > &x, double &ans)
nrm2: ||x||_2
Definition: vector_blas.cpp:1080
MONOLISH_SOLVER_NOT_IMPL
#define MONOLISH_SOLVER_NOT_IMPL
Definition: monolish_common.hpp:15
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
monolish::Logger::solver_in
void solver_in(const std::string func_name)
Definition: logger_utils.cpp:7
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
MONOLISH_SOLVER_SUCCESS
#define MONOLISH_SOLVER_SUCCESS
Definition: monolish_common.hpp:10
monolish::vml::reciprocal
void reciprocal(const matrix::CRS< double > &a, matrix::CRS< double > &y)
reciprocal to CRS matrix elements (C[0:nnz] = 1 / A[0:nnz])
Definition: matrix_vml.cpp:375
monolish::blas::copy
void copy(const matrix::Dense< double > &A, matrix::Dense< double > &C)
Dense matrix copy (C=A)
Definition: dense_copy.cpp:25
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::equation::Jacobi::create_precond
void create_precond(MATRIX &A)
Definition: jacobi.cpp:12
monolish::vector::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_vector.hpp:204
monolish::blas::matvec
void matvec(const matrix::Dense< double > &A, const vector< double > &x, vector< double > &y)
Dense matrix and vector multiplication: y = Ax.
Definition: matvec_blas.cpp:11
monolish::equation::Jacobi::monolish_Jacobi
int monolish_Jacobi(MATRIX &A, vector< Float > &x, vector< Float > &b)
Definition: jacobi.cpp:66
monolish::Logger::solver_out
void solver_out()
Definition: logger_utils.cpp:35
monolish::equation::Jacobi::apply_precond
void apply_precond(const vector< Float > &r, vector< Float > &z)
Definition: jacobi.cpp:42
monolish::vml::add
void add(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
element by element addition CRS matrix A and CRS matrix B.
Definition: matrix_vml.cpp:220
monolish::blas::axpyz
void axpyz(const double alpha, const vector< double > &x, const vector< double > &y, vector< double > &z)
axpyz: z = ax + y
Definition: vector_blas.cpp:666
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::vml::mul
void mul(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
element by element multiplication CRS matrix A and CRS matrix B.
Definition: matrix_vml.cpp:236
monolish::equation::Jacobi
Jacobi solver class.
Definition: monolish_equation.hpp:159
MONOLISH_SOLVER_RESIDUAL_NAN
#define MONOLISH_SOLVER_RESIDUAL_NAN
Definition: monolish_common.hpp:14
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:598
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29