monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
double_scalar_linearoperator.cpp
Go to the documentation of this file.
1 #include "../../../include/monolish_blas.hpp"
2 #include "../../../include/monolish_vml.hpp"
3 #include "../../internal/monolish_internal.hpp"
4 
5 namespace monolish {
6 
8 // LinearOperator ///////////////////////////
10 
11 void vml::add(const matrix::LinearOperator<double> &A, const double &alpha,
13  Logger &logger = Logger::get_instance();
14  logger.func_in(monolish_func);
15 
16  // err
17  assert(util::is_same_size(A, C));
18  assert(util::is_same_device_mem_stat(A, C));
19 
20  if (A.get_matvec_init_flag()) {
21  C.set_matvec([&](const vector<double> &VEC) {
22  vector<double> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
23  if (A.get_device_mem_stat()) {
24  util::send(vec, vec_tmp);
25  }
26  blas::matvec(A, VEC, vec_tmp);
27  vml::add(vec_tmp, alpha * blas::sum(VEC), vec);
28  if (A.get_device_mem_stat()) {
29  util::device_free(vec_tmp);
30  }
31  return vec;
32  });
33  }
34  if (A.get_rmatvec_init_flag()) {
35  C.set_rmatvec([&](const vector<double> &VEC) {
36  vector<double> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
37  if (A.get_device_mem_stat()) {
38  util::send(vec, vec_tmp);
39  }
40  blas::rmatvec(A, VEC, vec_tmp);
41  vml::add(vec_tmp, alpha * blas::sum(VEC), vec);
42  if (A.get_device_mem_stat()) {
43  util::device_free(vec_tmp);
44  }
45  return vec;
46  });
47  }
48 
49  logger.func_out();
50 }
51 
52 void vml::sub(const matrix::LinearOperator<double> &A, const double &alpha,
54  Logger &logger = Logger::get_instance();
55  logger.func_in(monolish_func);
56 
57  // err
58  assert(util::is_same_size(A, C));
59  assert(util::is_same_device_mem_stat(A, C));
60 
61  if (A.get_matvec_init_flag()) {
62  C.set_matvec([&](const vector<double> &VEC) {
63  vector<double> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
64  if (A.get_device_mem_stat()) {
65  util::send(vec, vec_tmp);
66  }
67  blas::matvec(A, VEC, vec_tmp);
68  vml::sub(vec_tmp, alpha * blas::sum(VEC), vec);
69  if (A.get_device_mem_stat()) {
70  util::device_free(vec_tmp);
71  }
72  return vec;
73  });
74  }
75  if (A.get_rmatvec_init_flag()) {
76  C.set_rmatvec([&](const vector<double> &VEC) {
77  vector<double> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
78  if (A.get_device_mem_stat()) {
79  util::send(vec, vec_tmp);
80  }
81  blas::rmatvec(A, VEC, vec_tmp);
82  vml::sub(vec_tmp, alpha * blas::sum(VEC), vec);
83  if (A.get_device_mem_stat()) {
84  util::device_free(vec_tmp);
85  }
86  return vec;
87  });
88  }
89 
90  logger.func_out();
91 }
92 
93 void vml::mul(const matrix::LinearOperator<double> &A, const double &alpha,
95  Logger &logger = Logger::get_instance();
96  logger.func_in(monolish_func);
97 
98  // err
99  assert(util::is_same_size(A, C));
100  assert(util::is_same_device_mem_stat(A, C));
101 
102  if (A.get_matvec_init_flag()) {
103  C.set_matvec([&](const vector<double> &VEC) {
104  vector<double> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
105  if (A.get_device_mem_stat()) {
106  util::send(vec, vec_tmp);
107  }
108  blas::matvec(A, VEC, vec_tmp);
109  vml::mul(vec_tmp, alpha, vec);
110  if (A.get_device_mem_stat()) {
111  util::device_free(vec_tmp);
112  }
113  return vec;
114  });
115  }
116  if (A.get_rmatvec_init_flag()) {
117  C.set_rmatvec([&](const vector<double> &VEC) {
118  vector<double> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
119  if (A.get_device_mem_stat()) {
120  util::send(vec, vec_tmp);
121  }
122  blas::rmatvec(A, VEC, vec_tmp);
123  vml::mul(vec_tmp, alpha, vec);
124  if (A.get_device_mem_stat()) {
125  util::device_free(vec_tmp);
126  }
127  return vec;
128  });
129  }
130 
131  logger.func_out();
132 }
133 
134 void vml::div(const matrix::LinearOperator<double> &A, const double &alpha,
136  Logger &logger = Logger::get_instance();
137  logger.func_in(monolish_func);
138 
139  // err
140  assert(util::is_same_size(A, C));
141  assert(util::is_same_device_mem_stat(A, C));
142 
143  if (A.get_matvec_init_flag()) {
144  C.set_matvec([&](const vector<double> &VEC) {
145  vector<double> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
146  if (A.get_device_mem_stat()) {
147  util::send(vec, vec_tmp);
148  }
149  blas::matvec(A, VEC, vec_tmp);
150  vml::mul(vec_tmp, 1.0 / alpha, vec);
151  if (A.get_device_mem_stat()) {
152  util::device_free(vec_tmp);
153  }
154  return vec;
155  });
156  }
157  if (A.get_rmatvec_init_flag()) {
158  C.set_rmatvec([&](const vector<double> &VEC) {
159  vector<double> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
160  if (A.get_device_mem_stat()) {
161  util::send(vec, vec_tmp);
162  }
163  blas::rmatvec(A, VEC, vec_tmp);
164  vml::mul(vec_tmp, 1.0 / alpha, vec);
165  if (A.get_device_mem_stat()) {
166  util::device_free(vec_tmp);
167  }
168  return vec;
169  });
170  }
171 
172  logger.func_out();
173 }
174 
175 } // namespace monolish
monolish::util::is_same_size
bool is_same_size(const T &x, const U &y)
compare size of vector or 1Dview (same as is_same_structure())
Definition: monolish_common.hpp:377
monolish::matrix::LinearOperator::set_rmatvec
void set_rmatvec(const std::function< vector< Float >(const vector< Float > &)> &RMATVEC)
set multiplication function of (Hermitian) transposed matrix and vector
Definition: linearoperator_constructor.cpp:148
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:37
monolish::blas::rmatvec
void rmatvec(const matrix::LinearOperator< double > &A, const vector< double > &x, vector< double > &y)
matrix (LinearOperator) and vector multiplication: y = Ax
Definition: matvec_blas.cpp:250
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
monolish::Logger::func_out
void func_out()
Definition: logger_utils.cpp:80
monolish::matrix::LinearOperator::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_linearoperator.hpp:394
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::LinearOperator::get_col
size_t get_col() const
get # of col
Definition: monolish_linearoperator.hpp:207
monolish::util::device_free
auto device_free(T &x)
free data of GPU
Definition: monolish_common.hpp:671
monolish::util::is_same_device_mem_stat
bool is_same_device_mem_stat(const T &arg1, const U &arg2)
compare same device memory status
Definition: monolish_common.hpp:454
monolish::blas::sum
void sum(const vector< double > &x, double &ans)
vector<float> sum
Definition: vector_blas.cpp:583
monolish::matrix::LinearOperator::get_matvec_init_flag
bool get_matvec_init_flag() const
get flag that shows matvec is defined or not
Definition: monolish_linearoperator.hpp:268
monolish
Definition: monolish_matrix_blas.hpp:10
monolish::matrix::LinearOperator::get_row
size_t get_row() const
get # of row
Definition: monolish_linearoperator.hpp:198
monolish::vml::div
void div(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
element by element division CRS matrix A and CRS matrix B.
Definition: matrix_vml.cpp:244
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::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::vector
vector class
Definition: monolish_coo.hpp:32
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::matrix::LinearOperator::set_matvec
void set_matvec(const std::function< vector< Float >(const vector< Float > &)> &MATVEC)
set multiplication function of matrix and vector
Definition: linearoperator_constructor.cpp:137
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::LinearOperator::get_rmatvec_init_flag
bool get_rmatvec_init_flag() const
get flag that shows rmatvec is defined or not
Definition: monolish_linearoperator.hpp:279
monolish::Logger::func_in
void func_in(const std::string func_name)
Definition: logger_utils.cpp:69