monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
linearoperator_getvec.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 namespace matrix {
6 
7 // diag
8 template <typename T> void LinearOperator<T>::diag(vector<T> &vec) const {
9  Logger &logger = Logger::get_instance();
10  logger.func_in(monolish_func);
11 
12  T *vecd = vec.data();
13 
14  const size_t N = get_col();
15  const size_t Len = std::min(get_row(), get_col());
16 
17  assert(Len == vec.size());
18  assert(get_device_mem_stat() == vec.get_device_mem_stat());
19 
20  if (gpu_status == true) {
21 #if MONOLISH_USE_NVIDIA_GPU // gpu
22  const size_t M = get_row();
23  vector<T> vec_tmp(N, 0);
24  vector<T> vec_ans(M);
25  T *vec_tmpd = vec_tmp.data();
26  T *vec_ansd = vec_ans.data();
27  util::send(vec_tmp, vec_ans);
28  for (size_t i = 0; i < Len; i++) {
29 #pragma omp target
30  { vec_tmpd[i] = 1; }
31  vec_ans = matvec(vec_tmp);
32 #pragma omp target
33  {
34  vecd[i] = vec_ansd[i];
35  vec_tmpd[i] = 0;
36  }
37  }
38 #else
39  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
40 #endif
41  } else {
42 #pragma omp parallel
43  {
44  vector<T> vec_tmp(N, 0);
45 #pragma omp for
46  for (size_t i = 0; i < Len; i++) {
47  vec_tmp[i] = 1;
48  vecd[i] = matvec(vec_tmp)[i];
49  vec_tmp[i] = 0;
50  }
51  }
52  }
53 
54  logger.func_out();
55 }
56 
57 template void
58 monolish::matrix::LinearOperator<double>::diag(vector<double> &vec) const;
59 template void
60 monolish::matrix::LinearOperator<float>::diag(vector<float> &vec) const;
61 
62 template <typename T>
63 void LinearOperator<T>::diag(view1D<vector<T>, T> &vec) const {
64  Logger &logger = Logger::get_instance();
65  logger.func_in(monolish_func);
66 
67  T *vecd = vec.data();
68 
69  const size_t N = get_col();
70  const size_t Len = std::min(get_row(), get_col());
71 
72  assert(Len == vec.size());
73  assert(get_device_mem_stat() == vec.get_device_mem_stat());
74 
75  if (gpu_status == true) {
76 #if MONOLISH_USE_NVIDIA_GPU // gp
77  const size_t M = get_row();
78  vector<T> vec_tmp(N, 0);
79  vector<T> vec_ans(M);
80  T *vec_tmpd = vec_tmp.data();
81  T *vec_ansd = vec_ans.data();
82  util::send(vec_tmp, vec_ans);
83  for (size_t i = 0; i < Len; i++) {
84 #pragma omp target
85  { vec_tmpd[i] = 1; }
86  vec_ans = matvec(vec_tmp);
87 #pragma omp target
88  {
89  vecd[i] = vec_ansd[i];
90  vec_tmpd[i] = 0;
91  }
92  }
93 #else
94  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
95 #endif
96  } else {
97  vector<T> vec_tmp(N, 0);
98 #pragma omp parallel for
99  for (size_t i = 0; i < Len; i++) {
100  vec_tmp[i] = 1;
101  vecd[i] = matvec(vec_tmp)[i];
102  vec_tmp[i] = 0;
103  }
104  }
105 
106  logger.func_out();
107 }
108 
110  view1D<vector<double>, double> &vec) const;
112  view1D<vector<float>, float> &vec) const;
113 
114 template <typename T>
115 void LinearOperator<T>::diag(view1D<matrix::Dense<T>, T> &vec) const {
116  Logger &logger = Logger::get_instance();
117  logger.func_in(monolish_func);
118 
119  T *vecd = vec.data();
120 
121  const size_t N = get_col();
122  const size_t Len = std::min(get_row(), get_col());
123 
124  assert(Len == vec.size());
125  assert(get_device_mem_stat() == vec.get_device_mem_stat());
126 
127  if (gpu_status == true) {
128 #if MONOLISH_USE_NVIDIA_GPU // gpu
129  const size_t M = get_row();
130  vector<T> vec_tmp(N, 0);
131  vector<T> vec_ans(M);
132  T *vec_tmpd = vec_tmp.data();
133  T *vec_ansd = vec_ans.data();
134  util::send(vec_tmp, vec_ans);
135  for (size_t i = 0; i < Len; i++) {
136 #pragma omp target
137  { vec_tmpd[i] = 1; }
138  vec_ans = matvec(vec_tmp);
139 #pragma omp target
140  {
141  vecd[i] = vec_ansd[i];
142  vec_tmpd[i] = 0;
143  }
144  }
145 #else
146  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
147 #endif
148  } else {
149  vector<T> vec_tmp(N, 0);
150 #pragma omp parallel for
151  for (size_t i = 0; i < Len; i++) {
152  vec_tmp[i] = 1;
153  vecd[i] = matvec(vec_tmp)[i];
154  vec_tmp[i] = 0;
155  }
156  }
157 
158  logger.func_out();
159 }
160 
162  view1D<matrix::Dense<double>, double> &vec) const;
164  view1D<matrix::Dense<float>, float> &vec) const;
165 
166 } // namespace matrix
167 } // namespace monolish
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::vml::min
void min(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with smallest elements of two matrices (C[0:nnz] = min(A[0:nnz],...
Definition: matrix_vml.cpp:390
monolish::matrix::LinearOperator::diag
void diag(vector< Float > &vec) const
get diag. vector
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::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