monolish  0.15.1
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_crs.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <exception>
3 #include <omp.h>
4 #include <stdexcept>
5 #include <string>
6 #include <vector>
7 
8 #if USE_SXAT
9 #undef _HAS_CPP17
10 #endif
11 #include <random>
12 #if USE_SXAT
13 #define _HAS_CPP17 1
14 #endif
15 
16 namespace monolish {
17 template <typename Float> class vector;
18 template <typename TYPE, typename Float> class view1D;
19 namespace matrix {
20 template <typename Float> class Dense;
21 template <typename Float> class COO;
22 
29 template <typename Float> class CRS {
30 private:
34  size_t rowN;
35 
39  size_t colN;
40 
44  size_t nnz;
45 
49  mutable bool gpu_status = false;
50 
55 
56 public:
61  std::vector<Float> val;
62 
67  std::vector<int> col_ind;
68 
73  std::vector<int> row_ptr;
74 
75  CRS() {}
76 
87  CRS(const size_t M, const size_t N, const size_t NNZ);
88 
104  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
105  const int *colind, const Float *value);
106 
124  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
125  const int *colind, const Float *value, const size_t origin);
126 
141  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
142  const std::vector<int> &colind, const std::vector<Float> &value);
143 
158  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
159  const std::vector<int> &colind, const vector<Float> &value);
160 
168  void convert(COO<Float> &coo);
169 
177  void convert(CRS<Float> &crs);
178 
187  CRS(COO<Float> &coo) { convert(coo); }
188 
201  CRS(const CRS<Float> &mat);
202 
217  void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
218  const std::vector<int> &colind, const std::vector<Float> &value);
219 
228  void print_all(bool force_cpu = false) const;
229 
237  [[nodiscard]] size_t get_row() const { return rowN; }
238 
246  [[nodiscard]] size_t get_col() const { return colN; }
247 
255  [[nodiscard]] size_t get_nnz() const { return nnz; }
256 
264  [[nodiscard]] std::string type() const { return "CRS"; }
265 
273  void compute_hash();
274 
280  [[nodiscard]] size_t get_hash() const { return structure_hash; }
281 
282  // communication
283  // ///////////////////////////////////////////////////////////////////////////
291  void send() const;
292 
300  void recv();
301 
309  void nonfree_recv();
310 
318  void device_free() const;
319 
324  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
325 
333  ~CRS() {
334  if (get_device_mem_stat()) {
335  device_free();
336  }
337  }
338 
340 
348  void diag(vector<Float> &vec) const;
349  void diag(view1D<vector<Float>, Float> &vec) const;
350  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
351 
361  void row(const size_t r, vector<Float> &vec) const;
362  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
363  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
364 
374  void col(const size_t c, vector<Float> &vec) const;
375  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
376  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
377 
379 
388  void transpose();
389 
398  void transpose(const CRS &B);
399 
400  /*
401  * @brief Memory data space required by the matrix
402  * @note
403  * - # of computation: 3
404  * - Multi-threading: false
405  * - GPU acceleration: false
406  **/
407  [[nodiscard]] double get_data_size() const {
408  return (get_nnz() * sizeof(Float) + (get_row() + 1) * sizeof(int) +
409  get_nnz() * sizeof(int)) /
410  1.0e+9;
411  }
412 
421  void fill(Float value);
422 
433  void operator=(const CRS<Float> &mat);
434 
445  [[nodiscard]] bool equal(const CRS<Float> &mat,
446  bool compare_cpu_and_device = false) const;
447 
459  [[nodiscard]] bool operator==(const CRS<Float> &mat) const;
460 
472  [[nodiscard]] bool operator!=(const CRS<Float> &mat) const;
473 };
474 } // namespace matrix
475 } // namespace monolish
monolish::matrix::CRS::operator=
void operator=(const CRS< Float > &mat)
matrix copy
monolish::matrix::CRS::get_data_size
double get_data_size() const
Definition: monolish_crs.hpp:407
monolish::matrix::CRS::val
std::vector< Float > val
CRS format value, which stores values of the non-zero elements (size nnz)
Definition: monolish_crs.hpp:61
monolish::matrix::CRS::transpose
void transpose()
get transposed matrix (A^T)
monolish::matrix::CRS::CRS
CRS(COO< Float > &coo)
Create CRS matrix from COO matrix, also compute the hash.
Definition: monolish_crs.hpp:187
monolish::matrix::CRS::type
std::string type() const
get format name "CRS"
Definition: monolish_crs.hpp:264
monolish::matrix::CRS::operator==
bool operator==(const CRS< Float > &mat) const
Comparing matricies (A == mat)
monolish::matrix::CRS::fill
void fill(Float value)
fill matrix elements with a scalar value
monolish::matrix::CRS::colN
size_t colN
# of col
Definition: monolish_crs.hpp:39
monolish::matrix::CRS::get_hash
size_t get_hash() const
get index array hash (to compare structure)
Definition: monolish_crs.hpp:280
monolish::matrix::CRS::set_ptr
void set_ptr(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const std::vector< Float > &value)
Set CRS array from std::vector.
monolish::matrix::CRS::send
void send() const
send data to GPU
monolish::matrix::CRS::get_nnz
size_t get_nnz() const
get # of non-zeros
Definition: monolish_crs.hpp:255
monolish::matrix::CRS::nnz
size_t nnz
# of non-zero element
Definition: monolish_crs.hpp:44
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::matrix::CRS::operator!=
bool operator!=(const CRS< Float > &mat) const
Comparing matricies (A != mat)
monolish::matrix::CRS::col_ind
std::vector< int > col_ind
CRS format column index, which stores column numbers of the non-zero elements (size nnz)
Definition: monolish_crs.hpp:67
monolish::matrix::CRS::rowN
size_t rowN
# of row
Definition: monolish_crs.hpp:34
monolish::matrix::CRS::gpu_status
bool gpu_status
true: sended, false: not send
Definition: monolish_crs.hpp:49
monolish::matrix::CRS::col
void col(const size_t c, vector< Float > &vec) const
get column vector
monolish::matrix::CRS::CRS
CRS()
Definition: monolish_crs.hpp:75
monolish::matrix::CRS::~CRS
~CRS()
destructor of CRS matrix, free GPU memory
Definition: monolish_crs.hpp:333
monolish::matrix::CRS::structure_hash
size_t structure_hash
hash, created from row_ptr and col_ind
Definition: monolish_crs.hpp:54
monolish::matrix::CRS::equal
bool equal(const CRS< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matricies (A == mat)
monolish::matrix::CRS::row
void row(const size_t r, vector< Float > &vec) const
get row vector
monolish::matrix::CRS::get_row
size_t get_row() const
get # of row
Definition: monolish_crs.hpp:237
monolish::matrix::CRS::compute_hash
void compute_hash()
compute index array hash (to compare structure)
monolish
Definition: monolish_matrix_blas.hpp:10
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:38
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::matrix::CRS::convert
void convert(COO< Float > &coo)
Convert CRS matrix from COO matrix, also compute the hash.
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::matrix::CRS::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_crs.hpp:324
monolish::matrix::CRS::diag
void diag(vector< Float > &vec) const
get diag. vector
monolish::matrix::CRS::print_all
void print_all(bool force_cpu=false) const
print all elements to standard I/O
monolish::matrix::CRS::device_free
void device_free() const
free data on GPU
monolish::matrix::CRS::get_col
size_t get_col() const
get # of col
Definition: monolish_crs.hpp:246
monolish::matrix::CRS::row_ptr
std::vector< int > row_ptr
CRS format row pointer, which stores the starting points of the rows of the arrays value and col_ind ...
Definition: monolish_crs.hpp:73
monolish::matrix::CRS::nonfree_recv
void nonfree_recv()
recv. data to GPU (w/o free)
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29
monolish::matrix::CRS::recv
void recv()
recv. data to GPU, and free data on GPU