monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
bicgstab.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 #include <fstream>
7 #include <iomanip>
8 #include <string>
9 
10 namespace monolish {
11 
12 template <typename MATRIX, typename T>
14  vector<T> &b) {
15  Logger &logger = Logger::get_instance();
16  logger.solver_in(monolish_func);
17 
18  if (A.get_row() != A.get_col()) {
19  throw std::runtime_error("error A.row != A.col");
20  }
21  if (A.get_device_mem_stat() != x.get_device_mem_stat() &&
22  A.get_device_mem_stat() != b.get_device_mem_stat()) {
23  throw std::runtime_error("error, A.get_device_mem_stat != "
24  "x.get_device_mem_stat != b.get_device_mem_stat");
25  }
26 
27  vector<T> r(A.get_row(), 0.0);
28  vector<T> r0(A.get_row(), 0.0);
29 
30  vector<T> p(A.get_row(), 0.0);
31  vector<T> phat(A.get_row(), 0.0);
32  vector<T> s(A.get_row(), 0.0);
33  vector<T> shat(A.get_row(), 0.0);
34 
35  vector<T> v(A.get_row(), 0.0);
36  vector<T> t(A.get_row(), 0.0);
37 
38  if (A.get_device_mem_stat() == true) {
39  monolish::util::send(r, r0, p, phat, s, shat, v, t);
40  }
41 
42  T rho_old = 1, rho = 1, alpha = 1, beta, omega = 1;
43 
44  this->precond.create_precond(A);
45 
46  // r = b-Ax
47  blas::matvec(A, x, r);
48  vml::sub(b, r, r);
49 
50  // r0 = r, (r*0, r0)!=0
51  blas::copy(r, r0);
52 
53  for (size_t iter = 0; iter < this->get_maxiter(); iter++) {
54 
55  // alpha = (r(i-1), r0) / (AM^-1*p(i-1), r0)
56  rho = blas::dot(r, r0);
57 
58  if (rho == 0.0) {
59  printf("breakdown\n");
60  return 0;
61  }
62 
63  if (iter == 0) {
64  blas::copy(r, p);
65  } else {
66  // beta = (rho / rho_old) * (alpha / omega)
67  beta = (rho / rho_old) * (alpha / omega);
68 
69  // p = r + beta(p + omega * AM-1 p(i-1) )
70  blas::axpy(-omega, v, p); // p = -omega*v + p
71  blas::xpay(beta, r, p); // p = r + beta*p
72  }
73 
74  // phat = M^-1 p(i-1)
75  this->precond.apply_precond(p, phat);
76  // v = AM^-1p(i-1)
77  blas::matvec(A, phat, v);
78  alpha = rho / blas::dot(v, r0);
79 
80  // s(i) = r(i-1) - alpha v
81  blas::axpyz(-alpha, v, r, s);
82 
83  // shat = M^-1 s(i)
84  this->precond.apply_precond(s, shat);
85  // t = A * shat
86  blas::matvec(A, shat, t);
87 
88  // omega = (AM-1s, s) / (AM-1s, AM-1s)
89  omega = blas::dot(t, s) / blas::dot(t, t);
90 
91  if (omega == 0.0) {
93  }
94 
95  // x(i) = x(i-1) + alpha * M^-1 p(i-1) + omega * M^-1 s(i)
96  blas::axpy(alpha, phat, x);
97  blas::axpy(omega, shat, x);
98 
99  // r(i) = s(i-1) - omega * AM^-1 s(i-1)
100  blas::axpyz(-omega, t, s, r);
101 
102  // convergence check
103  auto resid = this->get_residual(r);
104  if (this->get_print_rhistory() == true) {
105  *this->rhistory_stream << iter + 1 << "\t" << std::scientific << resid
106  << std::endl;
107  }
108 
109  if (resid < this->get_tol() && this->get_miniter() <= iter + 1) {
110  logger.solver_out();
112  }
113 
114  if (std::isnan(resid)) {
116  }
117 
118  rho_old = rho;
119  }
120 
121  logger.solver_out();
123 }
124 template int
125 equation::BiCGSTAB<matrix::Dense<double>, double>::monolish_BiCGSTAB(
127 template int equation::BiCGSTAB<matrix::Dense<float>, float>::monolish_BiCGSTAB(
129 
130 template int equation::BiCGSTAB<matrix::CRS<double>, double>::monolish_BiCGSTAB(
132 template int equation::BiCGSTAB<matrix::CRS<float>, float>::monolish_BiCGSTAB(
134 
135 template int
136 equation::BiCGSTAB<matrix::LinearOperator<double>, double>::monolish_BiCGSTAB(
138 template int
139 equation::BiCGSTAB<matrix::LinearOperator<float>, float>::monolish_BiCGSTAB(
141 
142 template <typename MATRIX, typename T>
144  vector<T> &b) {
145  Logger &logger = Logger::get_instance();
146  logger.solver_in(monolish_func);
147 
148  int ret = 0;
149  if (this->get_lib() == 0) {
150  ret = monolish_BiCGSTAB(A, x, b);
151  }
152 
153  logger.solver_out();
154  return ret; // err code
155 }
156 template int equation::BiCGSTAB<matrix::Dense<double>, double>::solve(
158 template int equation::BiCGSTAB<matrix::Dense<float>, float>::solve(
160 
161 template int equation::BiCGSTAB<matrix::CRS<double>, double>::solve(
163 template int equation::BiCGSTAB<matrix::CRS<float>, float>::solve(
165 
166 template int equation::BiCGSTAB<matrix::LinearOperator<double>, double>::solve(
168 template int equation::BiCGSTAB<matrix::LinearOperator<float>, float>::solve(
170 } // namespace monolish
monolish::equation::BiCGSTAB
BiCGSTAB solver class.
Definition: monolish_equation.hpp:111
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:37
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
monolish::blas::axpy
void axpy(const double alpha, const vector< double > &x, vector< double > &y)
axpy: y = ax + y
Definition: vector_blas.cpp:594
monolish::Logger::solver_in
void solver_in(const std::string func_name)
Definition: logger_utils.cpp:7
monolish::vml::sub
void sub(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
element by element subtract CRS matrix A and CRS matrix B.
Definition: matrix_vml.cpp:228
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:35
MONOLISH_SOLVER_SUCCESS
#define MONOLISH_SOLVER_SUCCESS
Definition: monolish_common.hpp:13
MONOLISH_SOLVER_BREAKDOWN
#define MONOLISH_SOLVER_BREAKDOWN
Definition: monolish_common.hpp:16
MONOLISH_SOLVER_MAXITER
#define MONOLISH_SOLVER_MAXITER
Definition: monolish_common.hpp:15
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:10
monolish::vector::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_vector.hpp:210
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:10
monolish::equation::BiCGSTAB::monolish_BiCGSTAB
int monolish_BiCGSTAB(MATRIX &A, vector< Float > &x, vector< Float > &b)
Definition: bicgstab.cpp:13
monolish::blas::dot
void dot(const vector< double > &x, const vector< double > &y, double &ans)
inner product (dot)
Definition: vector_blas.cpp:973
monolish::blas::xpay
void xpay(const double alpha, const vector< double > &x, vector< double > &y)
xpay: y = x + ay
Definition: vector_blas.cpp:1107
monolish::Logger::solver_out
void solver_out()
Definition: logger_utils.cpp:35
monolish::equation::BiCGSTAB::solve
int solve(MATRIX &A, vector< Float > &x, vector< Float > &b)
solve Ax = b by BiCGSTAB method (lib=0: monolish)
Definition: bicgstab.cpp:143
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:665
monolish::vector
vector class
Definition: monolish_coo.hpp:32
MONOLISH_SOLVER_RESIDUAL_NAN
#define MONOLISH_SOLVER_RESIDUAL_NAN
Definition: monolish_common.hpp:17
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:642
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:36