monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
linearoperator_mataddsub.cpp
Go to the documentation of this file.
1 #include "../../../../include/monolish_blas.hpp"
2 #include "../../../internal/monolish_internal.hpp"
3 
4 namespace monolish {
5 
6 namespace {
7 
8 template <typename F>
9 void matadd_core(const matrix::LinearOperator<F> &A,
10  const matrix::LinearOperator<F> &B,
11  matrix::LinearOperator<F> &C) {
12  Logger &logger = Logger::get_instance();
13  logger.func_in(monolish_func);
14 
15  // err
16  assert(util::is_same_size(A, B, C));
17  assert(util::is_same_device_mem_stat(A, B, C));
18 
19  assert(A.get_matvec_init_flag() == B.get_matvec_init_flag());
20  assert(A.get_rmatvec_init_flag() == B.get_rmatvec_init_flag());
21 
22  if (A.get_matvec_init_flag()) {
23  C.set_matvec([&](const vector<F> &VEC) {
24  vector<F> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
25  if (A.get_device_mem_stat()) {
26  util::send(vec, vec_tmp);
27  }
28  blas::matvec(A, VEC, vec);
29  blas::matvec(B, VEC, vec_tmp);
30  blas::axpy(1.0, vec_tmp, vec);
31  if (A.get_device_mem_stat()) {
32  util::device_free(vec_tmp);
33  }
34  return vec;
35  });
36  }
37 
38  if (A.get_rmatvec_init_flag()) {
39  C.set_rmatvec([&](const vector<F> &VEC) {
40  vector<F> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
41  if (A.get_device_mem_stat()) {
42  util::send(vec, vec_tmp);
43  }
44  blas::rmatvec(A, VEC, vec);
45  blas::rmatvec(B, VEC, vec_tmp);
46  blas::axpy(1.0, vec_tmp, vec);
47  if (A.get_device_mem_stat()) {
48  util::device_free(vec_tmp);
49  }
50  return vec;
51  });
52  }
53 
54  logger.func_out();
55 }
56 
57 template <typename F>
58 void matsub_core(const matrix::LinearOperator<F> &A,
59  const matrix::LinearOperator<F> &B,
60  matrix::LinearOperator<F> &C) {
61  Logger &logger = Logger::get_instance();
62  logger.func_in(monolish_func);
63 
64  // err
65  assert(util::is_same_size(A, B, C));
66  assert(util::is_same_device_mem_stat(A, B, C));
67 
68  assert(A.get_matvec_init_flag() == B.get_matvec_init_flag());
69  assert(A.get_rmatvec_init_flag() == B.get_rmatvec_init_flag());
70 
71  if (A.get_matvec_init_flag()) {
72  C.set_matvec([&](const vector<F> &VEC) {
73  vector<F> vec(A.get_row(), 0.0), vec_tmp(A.get_row(), 0.0);
74  if (A.get_device_mem_stat()) {
75  util::send(vec, vec_tmp);
76  }
77  blas::matvec(A, VEC, vec);
78  blas::matvec(B, VEC, vec_tmp);
79  blas::axpy(-1.0, vec_tmp, vec);
80  if (A.get_device_mem_stat()) {
81  util::device_free(vec_tmp);
82  }
83  return vec;
84  });
85  }
86 
87  if (A.get_rmatvec_init_flag()) {
88  C.set_rmatvec([&](const vector<F> &VEC) {
89  vector<F> vec(A.get_col(), 0.0), vec_tmp(A.get_col(), 0.0);
90  if (A.get_device_mem_stat()) {
91  util::send(vec, vec_tmp);
92  }
93  blas::rmatvec(A, VEC, vec);
94  blas::rmatvec(B, VEC, vec_tmp);
95  blas::axpy(-1.0, vec_tmp, vec);
96  if (A.get_device_mem_stat()) {
97  util::device_free(vec_tmp);
98  }
99  return vec;
100  });
101  }
102 
103  logger.func_out();
104 }
105 } // namespace
106 
107 namespace blas {
111  matadd_core(A, B, C);
112 }
116  matsub_core(A, B, C);
117 }
121  matadd_core(A, B, C);
122 }
126  matsub_core(A, B, C);
127 }
128 
129 } // namespace blas
130 } // 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
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::blas::axpy
void axpy(const double alpha, const vector< double > &x, vector< double > &y)
axpy: y = ax + y
Definition: vector_blas.cpp:594
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::matsub
void matsub(const matrix::Dense< double > &A, const matrix::Dense< double > &B, matrix::Dense< double > &C)
Dense matrix subtract: C = A - B.
Definition: dense_mataddsub.cpp:46
monolish
Definition: monolish_matrix_blas.hpp:10
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::blas::matadd
void matadd(const matrix::Dense< double > &A, const matrix::Dense< double > &B, matrix::Dense< double > &C)
Dense matrix addition: C = A + B.
Definition: dense_mataddsub.cpp:42
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