monolish  0.16.0
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 
34 template <typename Float> class CRS {
35 private:
39  size_t rowN;
40 
44  size_t colN;
45 
49  size_t nnz;
50 
54  mutable bool gpu_status = false;
55 
60 
61 public:
66  std::vector<Float> val;
67 
72  std::vector<int> col_ind;
73 
78  std::vector<int> row_ptr;
79 
80  CRS() {}
81 
92  CRS(const size_t M, const size_t N, const size_t NNZ);
93 
109  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
110  const int *colind, const Float *value);
111 
129  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
130  const int *colind, const Float *value, const size_t origin);
131 
146  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
147  const std::vector<int> &colind, const std::vector<Float> &value);
148 
163  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
164  const std::vector<int> &colind, const vector<Float> &value);
165 
173  void convert(COO<Float> &coo);
174 
182  void convert(CRS<Float> &crs);
183 
192  CRS(COO<Float> &coo) { convert(coo); }
193 
206  CRS(const CRS<Float> &mat);
207 
221  CRS(const CRS<Float> &mat, Float value);
222 
237  void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
238  const std::vector<int> &colind, const std::vector<Float> &value);
239 
248  void print_all(bool force_cpu = false) const;
249 
257  [[nodiscard]] size_t get_row() const { return rowN; }
258 
266  [[nodiscard]] size_t get_col() const { return colN; }
267 
275  [[nodiscard]] size_t get_nnz() const { return nnz; }
276 
284  [[nodiscard]] std::string type() const { return "CRS"; }
285 
293  void compute_hash();
294 
300  [[nodiscard]] size_t get_hash() const { return structure_hash; }
301 
302  // communication
303  // ///////////////////////////////////////////////////////////////////////////
311  void send() const;
312 
320  void recv();
321 
329  void nonfree_recv();
330 
338  void device_free() const;
339 
344  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
345 
353  ~CRS() {
354  if (get_device_mem_stat()) {
355  device_free();
356  }
357  }
358 
360 
368  void diag(vector<Float> &vec) const;
369  void diag(view1D<vector<Float>, Float> &vec) const;
370  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
371 
381  void row(const size_t r, vector<Float> &vec) const;
382  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
383  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
384 
394  void col(const size_t c, vector<Float> &vec) const;
395  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
396  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
397 
399 
408  void transpose();
409 
418  void transpose(const CRS &B);
419 
420  /*
421  * @brief Memory data space required by the matrix
422  * @note
423  * - # of computation: 3
424  * - Multi-threading: false
425  * - GPU acceleration: false
426  **/
427  [[nodiscard]] double get_data_size() const {
428  return (get_nnz() * sizeof(Float) + (get_row() + 1) * sizeof(int) +
429  get_nnz() * sizeof(int)) /
430  1.0e+9;
431  }
432 
441  void fill(Float value);
442 
453  void operator=(const CRS<Float> &mat);
454 
465  [[nodiscard]] bool equal(const CRS<Float> &mat,
466  bool compare_cpu_and_device = false) const;
467 
479  [[nodiscard]] bool operator==(const CRS<Float> &mat) const;
480 
492  [[nodiscard]] bool operator!=(const CRS<Float> &mat) const;
493 };
496 } // namespace matrix
497 } // 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:427
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:66
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:192
monolish::matrix::CRS::type
std::string type() const
get format name "CRS"
Definition: monolish_crs.hpp:284
monolish::matrix::CRS::operator==
bool operator==(const CRS< Float > &mat) const
Comparing matrices (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:44
monolish::matrix::CRS::get_hash
size_t get_hash() const
get index array hash (to compare structure)
Definition: monolish_crs.hpp:300
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:275
monolish::matrix::CRS::nnz
size_t nnz
# of non-zero element
Definition: monolish_crs.hpp:49
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::matrix::CRS::operator!=
bool operator!=(const CRS< Float > &mat) const
Comparing matrices (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:72
monolish::matrix::CRS::rowN
size_t rowN
# of row
Definition: monolish_crs.hpp:39
monolish::matrix::CRS::gpu_status
bool gpu_status
true: sended, false: not send
Definition: monolish_crs.hpp:54
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:80
monolish::matrix::CRS::~CRS
~CRS()
destructor of CRS matrix, free GPU memory
Definition: monolish_crs.hpp:353
monolish::matrix::CRS::structure_hash
size_t structure_hash
hash, created from row_ptr and col_ind
Definition: monolish_crs.hpp:59
monolish::matrix::CRS::equal
bool equal(const CRS< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (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:257
monolish::matrix::CRS::compute_hash
void compute_hash()
compute index array hash (to compare structure)
monolish
monolish namespaces
Definition: monolish_matrix_blas.hpp:5
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:43
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:344
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:266
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:78
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