monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_linearoperator.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <exception>
3 #include <functional>
4 #include <omp.h>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
9 #if USE_SXAT
10 #undef _HAS_CPP17
11 #endif
12 #include <random>
13 #if USE_SXAT
14 #define _HAS_CPP17 1
15 #endif
16 
17 namespace monolish {
18 template <typename Float> class vector;
19 namespace matrix {
20 template <typename Float> class Dense;
21 template <typename Float> class COO;
22 template <typename Float> class CRS;
23 
35 template <typename Float> class LinearOperator {
36 private:
40  size_t rowN;
41 
45  size_t colN;
46 
50  mutable bool gpu_status = false;
51 
55  std::function<vector<Float>(const vector<Float> &)> matvec = nullptr;
56 
61  std::function<vector<Float>(const vector<Float> &)> rmatvec = nullptr;
62 
66  std::function<Dense<Float>(const Dense<Float> &)> matmul_dense = nullptr;
67 
72  std::function<Dense<Float>(const Dense<Float> &)> rmatmul_dense = nullptr;
73 
74 public:
76 
86  LinearOperator(const size_t M, const size_t N);
87 
99  const size_t M, const size_t N,
100  const std::function<vector<Float>(const vector<Float> &)> &MATVEC);
101 
115  const size_t M, const size_t N,
116  const std::function<vector<Float>(const vector<Float> &)> &MATVEC,
117  const std::function<vector<Float>(const vector<Float> &)> &RMATVEC);
118 
130  const size_t M, const size_t N,
131  const std::function<Dense<Float>(const Dense<Float> &)> &MATMUL);
132 
146  const size_t M, const size_t N,
147  const std::function<Dense<Float>(const Dense<Float> &)> &MATMUL,
148  const std::function<Dense<Float>(const Dense<Float> &)> &RMATMUL);
149 
157  void convert(COO<Float> &coo);
158 
168 
169  void convert(CRS<Float> &crs);
170 
172 
173  void convert(Dense<Float> &dense);
174 
175  LinearOperator(Dense<Float> &dense) { convert(dense); }
176 
177  void convert_to_Dense(Dense<Float> &dense) const;
178 
187  LinearOperator(const LinearOperator<Float> &linearoperator);
188 
196  [[nodiscard]] size_t get_row() const { return rowN; }
197 
205  [[nodiscard]] size_t get_col() const { return colN; }
206 
214  [[nodiscard]] std::function<vector<Float>(const vector<Float> &)>
215  get_matvec() const {
216  return matvec;
217  }
218 
227  [[nodiscard]] std::function<vector<Float>(const vector<Float> &)>
228  get_rmatvec() const {
229  return rmatvec;
230  }
231 
239  [[nodiscard]] std::function<
242  return matmul_dense;
243  }
244 
253  [[nodiscard]] std::function<
256  return rmatmul_dense;
257  }
258 
266  [[nodiscard]] bool get_matvec_init_flag() const {
267  return !(matvec == nullptr);
268  }
269 
277  [[nodiscard]] bool get_rmatvec_init_flag() const {
278  return !(rmatvec == nullptr);
279  }
280 
288  [[nodiscard]] bool get_matmul_dense_init_flag() const {
289  return !(matmul_dense == nullptr);
290  }
291 
299  [[nodiscard]] bool get_rmatmul_dense_init_flag() const {
300  return !(rmatmul_dense == nullptr);
301  }
302 
310  void
311  set_matvec(const std::function<vector<Float>(const vector<Float> &)> &MATVEC);
312 
322  const std::function<vector<Float>(const vector<Float> &)> &RMATVEC);
323 
332  const std::function<matrix::Dense<Float>(const matrix::Dense<Float> &)>
333  &MATMUL);
334 
344  const std::function<matrix::Dense<Float>(const matrix::Dense<Float> &)>
345  &RMATMUL);
346 
354  [[nodiscard]] std::string type() const { return "LinearOperator"; }
355 
362  void send() const {};
363 
370  void recv() const {};
371 
378  void nonfree_recv() const {};
379 
386  void device_free() const {};
387 
392  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; };
393 
394  void set_device_mem_stat(bool status) {
395  gpu_status = status;
396  return;
397  };
398 
406 
411  void diag(vector<Float> &vec) const;
412  void diag(view1D<vector<Float>, Float> &vec) const;
413  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
414  void diag(view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
415 
425 };
428 } // namespace matrix
429 } // namespace monolish
Coodinate (COO) format Matrix (need to sort)
Compressed Row Storage (CRS) format Matrix.
Dense format Matrix.
Linear Operator imitating Matrix.
std::function< vector< Float >const vector< Float > &)> rmatvec
pseudo multiplication function of (Hermitian) transposed matrix and vector
void convert(CRS< Float > &crs)
LinearOperator(const size_t M, const size_t N, const std::function< Dense< Float >(const Dense< Float > &)> &MATMUL, const std::function< Dense< Float >(const Dense< Float > &)> &RMATMUL)
declare LinearOperator
void set_matmul_dense(const std::function< matrix::Dense< Float >(const matrix::Dense< Float > &)> &MATMUL)
set multiplication function of matrix and matrix dense
void convert_to_Dense(Dense< Float > &dense) const
std::function< vector< Float >const vector< Float > &)> matvec
pseudo multiplication function of matrix and vector
bool get_device_mem_stat() const
true: sended, false: not send
void set_rmatmul_dense(const std::function< matrix::Dense< Float >(const matrix::Dense< Float > &)> &RMATMUL)
set multiplication function of (Hermitian) transposed matrix and matrix dense
bool get_matmul_dense_init_flag() const
get flag that shows matmul_dense is defined or not
~LinearOperator()
destructor of LinearOperator, free GPU memory
void nonfree_recv() const
recv. data to GPU (w/o free)
bool get_rmatmul_dense_init_flag() const
get flag that shows rmatmul_dense is defined or not
void recv() const
recv. data to GPU, and free data on GPU
bool gpu_status
true: sended, false: not send
void convert(COO< Float > &coo)
Convert LinearOperator from COO.
void set_matvec(const std::function< vector< Float >(const vector< Float > &)> &MATVEC)
set multiplication function of matrix and vector
std::function< Dense< Float >const Dense< Float > &)> rmatmul_dense
pseudo multiplication function of (Hermitian) transposed matrix and dense matrix
bool get_rmatvec_init_flag() const
get flag that shows rmatvec is defined or not
void diag(view1D< vector< Float >, Float > &vec) const
LinearOperator(const size_t M, const size_t N)
declare LinearOperator
void operator=(const LinearOperator< Float > &mat)
operator copy
LinearOperator(COO< Float > &coo)
Create LinearOperator from COO.
std::function< matrix::Dense< Float >const matrix::Dense< Float > &)> get_rmatmul_dense() const
get multiplication function of (Hermitian) transposed matrix and matrix dense;
void diag(vector< Float > &vec) const
get diag. vector
void set_rmatvec(const std::function< vector< Float >(const vector< Float > &)> &RMATVEC)
set multiplication function of (Hermitian) transposed matrix and vector
void device_free() const
free data on GPU
std::function< vector< Float >const vector< Float > &)> get_rmatvec() const
get multiplication function of (Hermitian) transposed matrix and vector C = A;
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
LinearOperator(const LinearOperator< Float > &linearoperator)
Create LinearOperator from LinearOperator.
std::string type() const
get format name "LinearOperator"
bool get_matvec_init_flag() const
get flag that shows matvec is defined or not
LinearOperator(const size_t M, const size_t N, const std::function< vector< Float >(const vector< Float > &)> &MATVEC)
declare LinearOperator
void convert(Dense< Float > &dense)
LinearOperator(const size_t M, const size_t N, const std::function< vector< Float >(const vector< Float > &)> &MATVEC, const std::function< vector< Float >(const vector< Float > &)> &RMATVEC)
declare LinearOperator
void diag(view1D< tensor::tensor_Dense< Float >, Float > &vec) const
std::function< vector< Float >const vector< Float > &)> get_matvec() const
get multiplication function of matrix and vector
LinearOperator(const size_t M, const size_t N, const std::function< Dense< Float >(const Dense< Float > &)> &MATMUL)
declare LinearOperator
std::function< Dense< Float >const Dense< Float > &)> matmul_dense
pseudo multiplication function of matrix and dense matrix
std::function< matrix::Dense< Float >const matrix::Dense< Float > &)> get_matmul_dense() const
get multiplication function of matrix and matrix dense
monolish namespaces