monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_crs.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 #include <exception>
10 #include <omp.h>
11 #include <stdexcept>
12 #include <string>
13 #include <vector>
14 
15 #if USE_SXAT
16 #undef _HAS_CPP17
17 #endif
18 #include <random>
19 #if USE_SXAT
20 #define _HAS_CPP17 1
21 #endif
22 
23 namespace monolish {
24 template <typename Float> class vector;
25 template <typename TYPE, typename Float> class view1D;
26 namespace matrix {
27 template <typename Float> class Dense;
28 template <typename Float> class COO;
29 
36 template <typename Float> class CRS {
37 private:
41  size_t rowN;
42 
46  size_t colN;
47 
51  size_t nnz;
52 
56  mutable bool gpu_status = false;
57 
62 
63 public:
68  std::vector<Float> val;
69 
74  std::vector<int> col_ind;
75 
80  std::vector<int> row_ptr;
81 
82  CRS() {}
83 
94  CRS(const size_t M, const size_t N, const size_t NNZ);
95 
111  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
112  const int *colind, const Float *value);
113 
130  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
131  const int *colind, const Float *value, const size_t origin);
132 
147  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
148  const std::vector<int> &colind, const std::vector<Float> &value);
149 
164  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
165  const std::vector<int> &colind, const vector<Float> &value);
166 
174  void convert(COO<Float> &coo);
175 
183  void convert(CRS<Float> &crs);
184 
193  CRS(COO<Float> &coo) { convert(coo); }
194 
207  CRS(const CRS<Float> &mat);
208 
223  void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
224  const std::vector<int> &colind, const std::vector<Float> &value);
225 
234  void print_all(bool force_cpu = false) const;
235 
243  [[nodiscard]] size_t get_row() const { return rowN; }
244 
252  [[nodiscard]] size_t get_col() const { return colN; }
253 
261  [[nodiscard]] size_t get_nnz() const { return nnz; }
262 
270  [[nodiscard]] std::string type() const { return "CRS"; }
271 
279  void compute_hash();
280 
286  [[nodiscard]] size_t get_hash() const { return structure_hash; }
287 
288  // communication
289  // ///////////////////////////////////////////////////////////////////////////
297  void send() const;
298 
306  void recv();
307 
315  void nonfree_recv();
316 
324  void device_free() const;
325 
330  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
331 
339  ~CRS() {
340  if (get_device_mem_stat()) {
341  device_free();
342  }
343  }
344 
346 
354  void diag(vector<Float> &vec) const;
355  void diag(view1D<vector<Float>, Float> &vec) const;
356  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
357 
367  void row(const size_t r, vector<Float> &vec) const;
368  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
369  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
370 
380  void col(const size_t c, vector<Float> &vec) const;
381  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
382  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
383 
385 
386  /*
387  * @brief Memory data space required by the matrix
388  * @note
389  * - # of computation: 3
390  * - Multi-threading: false
391  * - GPU acceleration: false
392  **/
393  [[nodiscard]] double get_data_size() const {
394  return (get_nnz() * sizeof(Float) + (get_row() + 1) * sizeof(int) +
395  get_nnz() * sizeof(int)) /
396  1.0e+9;
397  }
398 
407  void fill(Float value);
408 
419  void operator=(const CRS<Float> &mat);
420 
431  [[nodiscard]] bool equal(const CRS<Float> &mat,
432  bool compare_cpu_and_device = false) const;
433 
445  [[nodiscard]] bool operator==(const CRS<Float> &mat) const;
446 
458  [[nodiscard]] bool operator!=(const CRS<Float> &mat) const;
459 };
460 } // namespace matrix
461 } // namespace monolish
monolish::matrix::CRS::fill
void fill(Float value)
fill matrix elements with a scalar value
Definition: fill_crs.cpp:9
monolish::matrix::CRS::get_data_size
double get_data_size() const
Definition: monolish_crs.hpp:393
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:68
monolish::matrix::CRS::CRS
CRS(COO< Float > &coo)
Create CRS matrix from COO matrix, also compute the hash.
Definition: monolish_crs.hpp:193
monolish::matrix::CRS::type
std::string type() const
get format name "CRS"
Definition: monolish_crs.hpp:270
monolish::matrix::CRS::nonfree_recv
void nonfree_recv()
recv. data to GPU (w/o free)
Definition: gpu_comm.cpp:134
monolish::matrix::CRS::operator=
void operator=(const CRS< Float > &mat)
matrix copy
Definition: copy_crs.cpp:10
monolish::matrix::CRS::colN
size_t colN
# of col
Definition: monolish_crs.hpp:46
monolish::matrix::CRS::get_hash
size_t get_hash() const
get index array hash (to compare structure)
Definition: monolish_crs.hpp:286
monolish::matrix::CRS::get_nnz
size_t get_nnz() const
get # of non-zeros
Definition: monolish_crs.hpp:261
monolish::matrix::CRS::nnz
size_t nnz
# of non-zero element
Definition: monolish_crs.hpp:51
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:35
monolish::matrix::CRS::operator!=
bool operator!=(const CRS< Float > &mat) const
Comparing matricies (A != mat)
Definition: compare_crs.cpp:80
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:74
monolish::matrix::CRS::rowN
size_t rowN
# of row
Definition: monolish_crs.hpp:41
monolish::matrix::CRS::gpu_status
bool gpu_status
true: sended, false: not send
Definition: monolish_crs.hpp:56
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:82
monolish::matrix::CRS::~CRS
~CRS()
destructor of CRS matrix, free GPU memory
Definition: monolish_crs.hpp:339
monolish::matrix::CRS::structure_hash
size_t structure_hash
hash, created from row_ptr and col_ind
Definition: monolish_crs.hpp:61
monolish::matrix::CRS::print_all
void print_all(bool force_cpu=false) const
print all elements to standard I/O
Definition: IO_crs.cpp:17
monolish::matrix::CRS::operator==
bool operator==(const CRS< Float > &mat) const
Comparing matricies (A == mat)
Definition: compare_crs.cpp:68
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:243
monolish
Definition: monolish_matrix_blas.hpp:10
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:45
monolish::view1D
1D view class
Definition: monolish_coo.hpp:33
monolish::matrix::CRS::convert
void convert(COO< Float > &coo)
Convert CRS matrix from COO matrix, also compute the hash.
monolish::matrix::CRS::recv
void recv()
recv. data to GPU, and free data on GPU
Definition: gpu_comm.cpp:113
monolish::vector
vector class
Definition: monolish_coo.hpp:32
monolish::matrix::CRS::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_crs.hpp:330
monolish::matrix::CRS::diag
void diag(vector< Float > &vec) const
get diag. vector
monolish::matrix::CRS::compute_hash
void compute_hash()
compute index array hash (to compare structure)
Definition: hash.cpp:9
monolish::matrix::CRS::send
void send() const
send data to GPU
Definition: gpu_comm.cpp:89
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.
Definition: copy_crs.cpp:41
monolish::matrix::CRS::get_col
size_t get_col() const
get # of col
Definition: monolish_crs.hpp:252
monolish::matrix::CRS::equal
bool equal(const CRS< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matricies (A == mat)
Definition: compare_crs.cpp:10
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:80
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:36
monolish::matrix::CRS::device_free
void device_free() const
free data on GPU
Definition: gpu_comm.cpp:153