monolish  0.16.2
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_coo.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 #define MM_BANNER "%%MatrixMarket"
17 #define MM_MAT "matrix"
18 #define MM_VEC "vector"
19 #define MM_FMT "coordinate"
20 #define MM_TYPE_REAL "real"
21 #define MM_TYPE_GENERAL "general"
22 #define MM_TYPE_SYMM "symmetric"
23 
24 namespace monolish {
25 template <typename Float> class vector;
26 template <typename TYPE, typename Float> class view1D;
27 namespace matrix {
28 template <typename Float> class Dense;
29 template <typename Float> class CRS;
30 template <typename Float> class LinearOperator;
31 
43 template <typename Float> class COO {
44 private:
48  size_t rowN;
49 
53  size_t colN;
54 
58  size_t nnz;
59 
63  mutable bool gpu_status = false;
64 
65 public:
70  std::vector<int> row_index;
71 
76  std::vector<int> col_index;
77 
82  std::vector<Float> val;
83 
84  COO()
85  : rowN(0), colN(0), nnz(0), gpu_status(false), row_index(), col_index(),
86  val() {}
87 
97  COO(const size_t M, const size_t N)
98  : rowN(M), colN(N), nnz(0), gpu_status(false), row_index(), col_index(),
99  val() {}
100 
116  COO(const size_t M, const size_t N, const size_t NNZ, const int *row,
117  const int *col, const Float *value);
118 
134  COO(const size_t M, const size_t N, const size_t NNZ,
135  const std::vector<int> &row, const std::vector<int> &col,
136  const std::vector<Float> &value) {
137  this = COO(M, N, NNZ, row.data(), col.data(), value.data());
138  }
139 
157  COO(const size_t M, const size_t N, const size_t NNZ,
158  const std::vector<int> &row, const std::vector<int> &col,
159  const vector<Float> &value) {
160  assert(value.get_device_mem_stat() == false);
161  this = COO(M, N, NNZ, row.data(), col.data(), value.data());
162  }
163 
181  COO(const size_t M, const size_t N, const size_t NNZ, const int *row,
182  const int *col, const Float *value, const size_t origin);
183 
201  COO(const size_t M, const size_t N, const size_t NNZ,
202  const std::vector<int> &row, const std::vector<int> &col,
203  const std::vector<Float> &value, const size_t origin) {
204  this = COO(M, N, NNZ, row.data(), col.data(), value.data(), origin);
205  }
206 
215  COO(const matrix::COO<Float> &coo);
216 
226  COO(const matrix::COO<Float> &coo, Float value);
227 
236  void convert(const matrix::CRS<Float> &crs);
237 
246  COO(const matrix::CRS<Float> &crs) { convert(crs); }
247 
256  void convert(const matrix::Dense<Float> &dense);
257 
266  COO(const matrix::Dense<Float> &dense) { convert(dense); }
267 
268  void convert(const matrix::LinearOperator<Float> &linearoperator);
269 
270  COO(const matrix::LinearOperator<Float> &linearoperator) {
271  convert(linearoperator);
272  }
273 
282  void set_row(const size_t M) { rowN = M; };
283 
292  void set_col(const size_t N) { colN = N; };
293 
302  void set_nnz(const size_t NNZ) { nnz = NNZ; };
303 
304  // communication
305  // ///////////////////////////////////////////////////////////////////////////
311  void send() const {
312  throw std::runtime_error("error, GPU util of COO format is not impl. ");
313  };
314 
320  void recv() const {
321  throw std::runtime_error("error, GPU util of COO format is not impl. ");
322  };
323 
329  void device_free() const {};
330 
337  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
338 
344  ~COO() {
345  if (get_device_mem_stat()) {
346  device_free();
347  }
348  }
349 
350  // I/O
351  // ///////////////////////////////////////////////////////////////////////////
352 
361  void input_mm(const std::string filename);
362 
371  COO(const std::string filename) { input_mm(filename); }
372 
381  void output_mm(const std::string filename) const;
382 
391  void print_all(bool force_cpu = false) const;
392 
401  void print_all(const std::string filename) const;
402 
410  [[nodiscard]] Float at(const size_t i, const size_t j) const;
411 
419  [[nodiscard]] Float at(const size_t i, const size_t j) {
420  return static_cast<const COO *>(this)->at(i, j);
421  };
422 
435  void set_ptr(const size_t rN, const size_t cN, const std::vector<int> &r,
436  const std::vector<int> &c, const std::vector<Float> &v);
437 
445  [[nodiscard]] size_t get_row() const { return rowN; }
446 
454  [[nodiscard]] size_t get_col() const { return colN; }
455 
463  [[nodiscard]] size_t get_nnz() const { return nnz; }
464 
473  void fill(Float value);
474 
483  [[nodiscard]] std::vector<int> &get_row_ptr() { return row_index; }
484 
493  [[nodiscard]] std::vector<int> &get_col_ind() { return col_index; }
494 
503  [[nodiscard]] std::vector<Float> &get_val_ptr() { return val; }
504 
513  [[nodiscard]] const std::vector<int> &get_row_ptr() const {
514  return row_index;
515  }
516 
525  [[nodiscard]] const std::vector<int> &get_col_ind() const {
526  return col_index;
527  }
528 
537  [[nodiscard]] const std::vector<Float> &get_val_ptr() const { return val; }
538 
539  // Utility
540  // ///////////////////////////////////////////////////////////////////////////
541 
550  void transpose();
551 
560  void transpose(const COO &B);
561 
569  double get_data_size() const {
570  return 3 * get_nnz() * sizeof(Float) / 1.0e+9;
571  }
572 
580  [[nodiscard]] std::string type() const { return "COO"; }
581 
590  void diag(vector<Float> &vec) const;
591  void diag(view1D<vector<Float>, Float> &vec) const;
592  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
593 
603  void row(const size_t r, vector<Float> &vec) const;
604  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
605  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
606 
616  void col(const size_t c, vector<Float> &vec) const;
617  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
618  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
619 
621 
632  void operator=(const COO<Float> &mat);
633 
644  [[nodiscard]] bool equal(const COO<Float> &mat,
645  bool compare_cpu_and_device = false) const;
646 
656  [[nodiscard]] bool operator==(const COO<Float> &mat) const;
657 
667  [[nodiscard]] bool operator!=(const COO<Float> &mat) const;
668 
684  void insert(const size_t m, const size_t n, const Float val);
685 
686 private:
687  void _q_sort(int lo, int hi);
688 
689 public:
698  void sort(bool merge);
699 };
702 } // namespace matrix
703 } // namespace monolish
Coodinate (COO) format Matrix (need to sort)
void diag(view1D< vector< Float >, Float > &vec) const
bool operator!=(const COO< Float > &mat) const
Comparing matrices (A != mat)
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void recv() const
recv data from GPU
size_t nnz
# of non-zero element
Float at(const size_t i, const size_t j) const
Get matrix element (A(i,j))
void convert(const matrix::LinearOperator< Float > &linearoperator)
COO(const size_t M, const size_t N, const size_t NNZ, const int *row, const int *col, const Float *value)
Create COO matrix from array.
bool equal(const COO< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
void operator=(const COO< Float > &mat)
matrix copy
void transpose(const COO &B)
create transposed matrix from COO matrix (B = A^T)
void fill(Float value)
fill matrix elements with a scalar value
COO(const std::string filename)
Create COO matrix from MatrixMarket format file (only real general) (MatrixMarket format: https://mat...
void col(const size_t c, view1D< vector< Float >, Float > &vec) const
void transpose()
get transposed matrix (A^T)
COO(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
COO(const matrix::COO< Float > &coo)
Create COO matrix from COO matrix.
std::vector< int > row_index
Coodinate format row index, which stores row numbers of the non-zero elements (size nnz)
void diag(vector< Float > &vec) const
get diag. vector
void print_all(const std::string filename) const
print all elements to file
COO(const matrix::Dense< Float > &dense)
Create COO matrix from Dense matrix (drop zero)
bool operator==(const COO< Float > &mat) const
Comparing matrices (A == mat)
void set_col(const size_t N)
Set col number.
void col(const size_t c, vector< Float > &vec) const
get column vector
void sort(bool merge)
sort COO matrix elements (and merge elements)
COO(const matrix::COO< Float > &coo, Float value)
Initialize COO matrix of the same size as input matrix.
~COO()
; free gpu mem.
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const vector< Float > &value)
Create COO matrix from monolish::vector.
std::vector< int > & get_col_ind()
get column index
void col(const size_t c, view1D< matrix::Dense< Float >, Float > &vec) const
COO(const size_t M, const size_t N)
Initialize M x N COO matrix.
void _q_sort(int lo, int hi)
void set_ptr(const size_t rN, const size_t cN, const std::vector< int > &r, const std::vector< int > &c, const std::vector< Float > &v)
Set COO array from std::vector.
const std::vector< int > & get_col_ind() const
get column index
size_t colN
# of col
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const std::vector< Float > &value)
Create COO matrix from std::vector.
void set_nnz(const size_t NNZ)
Set # of non-zero elements.
std::string type() const
get format name "COO"
void insert(const size_t m, const size_t n, const Float val)
insert element to (m, n)
std::vector< int > & get_row_ptr()
get row index
Float at(const size_t i, const size_t j)
Get matrix element (A(i,j))
void device_free() const
free data on GPU
const std::vector< Float > & get_val_ptr() const
get value
void input_mm(const std::string filename)
Create COO matrix from MatrixMarket format file (only real general) (MatrixMarket format: https://mat...
COO(const matrix::LinearOperator< Float > &linearoperator)
const std::vector< int > & get_row_ptr() const
get row index
bool gpu_status
true: sended, false: not send
bool get_device_mem_stat() const
false; // true: sended, false: not send
void row(const size_t r, view1D< vector< Float >, Float > &vec) const
size_t get_nnz() const
get # of non-zeros
std::vector< Float > val
Coodinate format value array, which stores values of the non-zero elements (size nnz)
void send() const
send data to GPU
size_t rowN
# of row
void convert(const matrix::Dense< Float > &dense)
Create COO matrix from Dense matrix (drop zero)
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const std::vector< Float > &value, const size_t origin)
Create COO matrix from n-origin array.
size_t get_col() const
get # of col
std::vector< Float > & get_val_ptr()
get value
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
std::vector< int > col_index
Coodinate format column index, which stores column numbers of the non-zero elements (size nnz)
size_t get_row() const
get # of row
void row(const size_t r, view1D< matrix::Dense< Float >, Float > &vec) const
void row(const size_t r, vector< Float > &vec) const
get row vector
void output_mm(const std::string filename) const
output matrix elements in MatrixMarket format (MatrixMarket format: https://math.nist....
COO(const size_t M, const size_t N, const size_t NNZ, const int *row, const int *col, const Float *value, const size_t origin)
Create COO matrix from n-origin array.
double get_data_size() const
Memory data space required by the matrix.
void convert(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
void set_row(const size_t M)
Set row number.
Compressed Row Storage (CRS) format Matrix.
Dense format Matrix.
Linear Operator imitating Matrix.
const Float * data() const
returns a direct pointer to the vector
bool get_device_mem_stat() const
true: sended, false: not send
monolish namespaces