monolish  0.16.2
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 
411  void diag_add(const Float alpha);
412 
425  void diag_sub(const Float alpha);
426 
439  void diag_mul(const Float alpha);
440 
453  void diag_div(const Float alpha);
454 
467  void diag_add(const vector<Float> &vec);
468  void diag_add(const view1D<vector<Float>, Float> &vec);
469  void diag_add(const view1D<matrix::Dense<Float>, Float> &vec);
470 
483  void diag_sub(const vector<Float> &vec);
484  void diag_sub(const view1D<vector<Float>, Float> &vec);
485  void diag_sub(const view1D<matrix::Dense<Float>, Float> &vec);
486 
499  void diag_mul(const vector<Float> &vec);
500  void diag_mul(const view1D<vector<Float>, Float> &vec);
501  void diag_mul(const view1D<matrix::Dense<Float>, Float> &vec);
502 
515  void diag_div(const vector<Float> &vec);
516  void diag_div(const view1D<vector<Float>, Float> &vec);
517  void diag_div(const view1D<matrix::Dense<Float>, Float> &vec);
518 
520 
529  void transpose();
530 
539  void transpose(const CRS &B);
540 
541  /*
542  * @brief Memory data space required by the matrix
543  * @note
544  * - # of computation: 3
545  * - Multi-threading: false
546  * - GPU acceleration: false
547  **/
548  [[nodiscard]] double get_data_size() const {
549  return (get_nnz() * sizeof(Float) + (get_row() + 1) * sizeof(int) +
550  get_nnz() * sizeof(int)) /
551  1.0e+9;
552  }
553 
562  void fill(Float value);
563 
574  void operator=(const CRS<Float> &mat);
575 
586  [[nodiscard]] bool equal(const CRS<Float> &mat,
587  bool compare_cpu_and_device = false) const;
588 
600  [[nodiscard]] bool operator==(const CRS<Float> &mat) const;
601 
613  [[nodiscard]] bool operator!=(const CRS<Float> &mat) const;
614 };
617 } // namespace matrix
618 } // namespace monolish
Coodinate (COO) format Matrix (need to sort)
Compressed Row Storage (CRS) format Matrix.
bool gpu_status
true: sended, false: not send
bool equal(const CRS< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
size_t rowN
# of row
CRS(const CRS< Float > &mat)
Create CRS matrix from CRS matrix.
void diag_mul(const Float alpha)
Scalar and diag. vector of CRS format matrix mul.
size_t get_col() const
get # of col
CRS(const size_t M, const size_t N, const size_t NNZ)
declare CRS matrix
void diag(vector< Float > &vec) const
get diag. vector
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr, const int *colind, const Float *value, const size_t origin)
Create CRS matrix from array, also compute the hash.
CRS(COO< Float > &coo)
Create CRS matrix from COO matrix, also compute the hash.
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix mul.
double get_data_size() const
void diag_sub(const Float alpha)
Scalar and diag. vector of CRS format matrix sub.
void compute_hash()
compute index array hash (to compare structure)
void diag_sub(const view1D< vector< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr, const int *colind, const Float *value)
Create CRS matrix from array, also compute the hash.
void row(const size_t r, view1D< matrix::Dense< Float >, Float > &vec) const
bool operator!=(const CRS< Float > &mat) const
Comparing matrices (A != mat)
void diag_add(const view1D< vector< Float >, Float > &vec)
void fill(Float value)
fill matrix elements with a scalar value
size_t get_hash() const
get index array hash (to compare structure)
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
bool operator==(const CRS< Float > &mat) const
Comparing matrices (A == mat)
void send() const
send data to GPU
void convert(CRS< Float > &crs)
Convert CRS matrix from COO matrix.
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void col(const size_t c, vector< Float > &vec) const
get column vector
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix sub.
void operator=(const CRS< Float > &mat)
matrix copy
void transpose(const CRS &B)
create transposed matrix from CRS format matrix (B = A^T)
CRS(const CRS< Float > &mat, Float value)
Create CRS matrix of the same size as input matrix.
std::vector< Float > val
CRS format value, which stores values of the non-zero elements (size nnz)
size_t get_nnz() const
get # of non-zeros
size_t structure_hash
hash, created from row_ptr and col_ind
void col(const size_t c, view1D< vector< Float >, Float > &vec) const
void diag(view1D< vector< Float >, Float > &vec) const
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
size_t get_row() const
get # of row
std::vector< int > col_ind
CRS format column index, which stores column numbers of the non-zero elements (size nnz)
~CRS()
destructor of CRS matrix, free GPU memory
std::string type() const
get format name "CRS"
void nonfree_recv()
recv. data to GPU (w/o free)
void convert(COO< Float > &coo)
Convert CRS matrix from COO matrix, also compute the hash.
void diag_add(const Float alpha)
Scalar and diag. vector of CRS format matrix add.
void transpose()
get transposed matrix (A^T)
void device_free() const
free data on GPU
void row(const size_t r, view1D< vector< Float >, Float > &vec) const
void diag_add(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix add.
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_div(const Float alpha)
Scalar and diag. vector of CRS format matrix div.
void diag_mul(const view1D< vector< Float >, Float > &vec)
void recv()
recv. data to GPU, and free data on GPU
size_t nnz
# of non-zero element
void row(const size_t r, vector< Float > &vec) const
get row vector
void diag_div(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix div.
size_t colN
# of col
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.
void col(const size_t c, view1D< matrix::Dense< Float >, Float > &vec) const
std::vector< int > row_ptr
CRS format row pointer, which stores the starting points of the rows of the arrays value and col_ind ...
CRS(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const std::vector< Float > &value)
Create CRS matrix from std::vector, also compute the hash.
bool get_device_mem_stat() const
true: sended, false: not send
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const vector< Float > &value)
Create CRS matrix from std::vector, also compute the hash.
void diag_div(const view1D< vector< Float >, Float > &vec)
Dense format Matrix.
monolish namespaces