monolish  0.14.0
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 <random>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
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 
123  CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr,
124  const int *colind, const Float *value, const size_t origin);
125 
140  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
141  const std::vector<int> &colind, const std::vector<Float> &value);
142 
157  CRS(const size_t M, const size_t N, const std::vector<int> &rowptr,
158  const std::vector<int> &colind, const vector<Float> &value);
159 
167  void convert(COO<Float> &coo);
168 
176  void convert(CRS<Float> &crs);
177 
186  CRS(COO<Float> &coo) { convert(coo); }
187 
200  CRS(const CRS<Float> &mat);
201 
216  void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
217  const std::vector<int> &colind, const std::vector<Float> &value);
218 
227  void print_all(bool force_cpu = false) const;
228 
236  size_t get_row() const { return rowN; }
237 
245  size_t get_col() const { return colN; }
246 
254  size_t get_nnz() const { return nnz; }
255 
263  std::string type() const { return "CRS"; }
264 
272  void compute_hash();
273 
279  size_t get_hash() const { return structure_hash; }
280 
281  // communication
282  // ///////////////////////////////////////////////////////////////////////////
290  void send() const;
291 
299  void recv();
300 
308  void nonfree_recv();
309 
317  void device_free() const;
318 
323  bool get_device_mem_stat() const { return gpu_status; }
324 
332  ~CRS() {
333  if (get_device_mem_stat()) {
334  device_free();
335  }
336  }
337 
339 
347  void diag(vector<Float> &vec) const;
348  void diag(view1D<vector<Float>, Float> &vec) const;
349  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
350 
360  void row(const size_t r, vector<Float> &vec) const;
361  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
362  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
363 
373  void col(const size_t c, vector<Float> &vec) const;
374  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
375  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
376 
378 
379  /*
380  * @brief Memory data space required by the matrix
381  * @note
382  * - # of computation: 3
383  * - Multi-threading: false
384  * - GPU acceleration: false
385  **/
386  double get_data_size() const {
387  return (get_nnz() * sizeof(Float) + (get_row() + 1) * sizeof(int) +
388  get_nnz() * sizeof(int)) /
389  1.0e+9;
390  }
391 
400  void fill(Float value);
401 
412  void operator=(const CRS<Float> &mat);
413 
424  bool equal(const CRS<Float> &mat, bool compare_cpu_and_device = false) const;
425 
437  bool operator==(const CRS<Float> &mat) const;
438 
450  bool operator!=(const CRS<Float> &mat) const;
451 };
452 } // namespace matrix
453 } // 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:386
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::CRS
CRS(COO< Float > &coo)
Create CRS matrix from COO matrix, also compute the hash.
Definition: monolish_crs.hpp:186
monolish::matrix::CRS::type
std::string type() const
get format name "CRS"
Definition: monolish_crs.hpp:263
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:39
monolish::matrix::CRS::get_hash
size_t get_hash() const
get index array hash (to compare structure)
Definition: monolish_crs.hpp:279
monolish::matrix::CRS::get_nnz
size_t get_nnz() const
get # of non-zeros
Definition: monolish_crs.hpp:254
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)
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: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:332
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::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:236
monolish
Definition: monolish_matrix_blas.hpp:9
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::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:25
monolish::matrix::CRS::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_crs.hpp:323
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:245
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:73
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29
monolish::matrix::CRS::device_free
void device_free() const
free data on GPU
Definition: gpu_comm.cpp:153