monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
convert_linearoperator.cpp
Go to the documentation of this file.
1 #include "../../../include/common/monolish_dense.hpp"
2 #include "../../../include/common/monolish_logger.hpp"
3 #include "../../../include/common/monolish_matrix.hpp"
4 #include "../../../include/common/monolish_vector.hpp"
5 #include "../../../include/monolish_blas.hpp"
6 #include "../../internal/monolish_internal.hpp"
7 
8 #include <fstream>
9 #include <iomanip>
10 #include <limits>
11 #include <sstream>
12 
13 namespace monolish {
14 namespace matrix {
15 
16 template <typename T> void LinearOperator<T>::convert(COO<T> &coo) {
17  Logger &logger = Logger::get_instance();
18  logger.util_in(monolish_func);
19 
20  // todo coo err check (only square)
21 
22  rowN = coo.get_row();
23  colN = coo.get_col();
24 
25  gpu_status = coo.get_device_mem_stat();
26 
27  set_matvec([&](const monolish::vector<T> &VEC) {
28  CRS<T> crs(coo);
29  monolish::vector<T> vec(crs.get_row(), 0);
30  monolish::blas::matvec(crs, VEC, vec);
31  return vec;
32  });
33  rmatvec_init_flag = false;
34 
35  logger.util_out();
36 }
37 
38 template void LinearOperator<double>::convert(COO<double> &coo);
39 template void LinearOperator<float>::convert(COO<float> &coo);
40 
41 template <typename T> void LinearOperator<T>::convert(CRS<T> &crs) {
42  Logger &logger = Logger::get_instance();
43  logger.util_in(monolish_func);
44 
45  // todo crs err check (only square)
46 
47  rowN = crs.get_row();
48  colN = crs.get_col();
49 
50  gpu_status = crs.get_device_mem_stat();
51 
52  set_matvec([&](const monolish::vector<T> &VEC) {
53  monolish::vector<T> vec(crs.get_row(), 0);
54  if (gpu_status) {
56  }
57  monolish::blas::matvec(crs, VEC, vec);
58  return vec;
59  });
60  rmatvec_init_flag = false;
61 
62  logger.util_out();
63 }
64 
65 template void LinearOperator<double>::convert(CRS<double> &crs);
66 template void LinearOperator<float>::convert(CRS<float> &crs);
67 
68 template <typename T>
70  if (!matvec_init_flag) {
71  Dense<T> A(rowN, colN);
72  dense = A;
73  return;
74  }
75 
76  std::vector<T> values(rowN * colN);
77  for (size_t i = 0; i < colN; ++i) {
78  std::vector<T> vec_tmp(colN, 0);
79  vec_tmp[i] = 1;
80  vector<T> vec(vec_tmp);
81  vector<T> ans(rowN);
82  if (gpu_status) {
83  util::send(ans, vec);
84  }
85  ans = matvec(vec);
86  if (gpu_status) {
87  util::recv(ans);
88  }
89  for (size_t j = 0; j < rowN; ++j) {
90  values[j * colN + i] = ans[j];
91  }
92  }
93 
94  dense = Dense<T>(rowN, colN, values);
95 }
96 
99 
100 } // namespace matrix
101 } // namespace monolish
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::matrix::LinearOperator::convert_to_Dense
void convert_to_Dense(Dense< Float > &dense) const
Definition: convert_linearoperator.cpp:69
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::matrix::LinearOperator::convert
void convert(COO< Float > &coo)
Convert LinearOperator from COO.
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::util::recv
auto recv(T &x)
recv. and free data from GPU
Definition: monolish_common.hpp:612
monolish::vector
vector class
Definition: monolish_coo.hpp:25
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