monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_coo.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 #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 
38 template <typename Float> class COO {
39 private:
43  size_t rowN;
44 
48  size_t colN;
49 
53  size_t nnz;
54 
58  mutable bool gpu_status = false;
59 
60 public:
65  std::vector<int> row_index;
66 
71  std::vector<int> col_index;
72 
77  std::vector<Float> val;
78 
79  COO()
80  : rowN(0), colN(0), nnz(0), gpu_status(false), row_index(), col_index(),
81  val() {}
82 
92  COO(const size_t M, const size_t N)
93  : rowN(M), colN(N), nnz(0), gpu_status(false), row_index(), col_index(),
94  val() {}
95 
111  COO(const size_t M, const size_t N, const size_t NNZ, const int *row,
112  const int *col, const Float *value);
113 
129  COO(const size_t M, const size_t N, const size_t NNZ,
130  const std::vector<int> &row, const std::vector<int> &col,
131  const std::vector<Float> &value) {
132  this = COO(M, N, NNZ, row.data(), col.data(), value.data());
133  }
134 
152  COO(const size_t M, const size_t N, const size_t NNZ,
153  const std::vector<int> &row, const std::vector<int> &col,
154  const vector<Float> &value) {
155  assert(value.get_device_mem_stat() == false);
156  this = COO(M, N, NNZ, row.data(), col.data(), value.data());
157  }
158 
176  COO(const size_t M, const size_t N, const size_t NNZ, const int *row,
177  const int *col, const Float *value, const size_t origin);
178 
196  COO(const size_t M, const size_t N, const size_t NNZ,
197  const std::vector<int> &row, const std::vector<int> &col,
198  const std::vector<Float> &value, const size_t origin) {
199  this = COO(M, N, NNZ, row.data(), col.data(), value.data(), origin);
200  }
201 
210  COO(const matrix::COO<Float> &coo);
211 
220  void convert(const matrix::CRS<Float> &crs);
221 
230  COO(const matrix::CRS<Float> &crs) { convert(crs); }
231 
240  void convert(const matrix::Dense<Float> &dense);
241 
250  COO(const matrix::Dense<Float> &dense) { convert(dense); }
251 
252  void convert(const matrix::LinearOperator<Float> &linearoperator);
253 
254  COO(const matrix::LinearOperator<Float> &linearoperator) {
255  convert(linearoperator);
256  }
257 
266  void set_row(const size_t M) { rowN = M; };
267 
276  void set_col(const size_t N) { colN = N; };
277 
286  void set_nnz(const size_t NNZ) { nnz = NNZ; };
287 
288  // communication
289  // ///////////////////////////////////////////////////////////////////////////
295  void send() const {
296  throw std::runtime_error("error, GPU util of COO format is not impl. ");
297  };
298 
304  void recv() const {
305  throw std::runtime_error("error, GPU util of COO format is not impl. ");
306  };
307 
313  void device_free() const {};
314 
321  bool get_device_mem_stat() const { return gpu_status; }
322 
328  ~COO() {
329  if (get_device_mem_stat()) {
330  device_free();
331  }
332  }
333 
334  // I/O
335  // ///////////////////////////////////////////////////////////////////////////
336 
345  void input_mm(const std::string filename);
346 
355  COO(const std::string filename) { input_mm(filename); }
356 
365  void output_mm(const std::string filename) const;
366 
375  void print_all(bool force_cpu = false) const;
376 
385  void print_all(const std::string filename) const;
386 
394  Float at(const size_t i, const size_t j) const;
395 
403  Float at(const size_t i, const size_t j) {
404  return static_cast<const COO *>(this)->at(i, j);
405  };
406 
419  void set_ptr(const size_t rN, const size_t cN, const std::vector<int> &r,
420  const std::vector<int> &c, const std::vector<Float> &v);
421 
429  size_t get_row() const { return rowN; }
430 
438  size_t get_col() const { return colN; }
439 
447  size_t get_nnz() const { return nnz; }
448 
457  void fill(Float value);
458 
467  std::vector<int> &get_row_ptr() { return row_index; }
468 
477  std::vector<int> &get_col_ind() { return col_index; }
478 
487  std::vector<Float> &get_val_ptr() { return val; }
488 
497  const std::vector<int> &get_row_ptr() const { return row_index; }
498 
507  const std::vector<int> &get_col_ind() const { return col_index; }
508 
517  const std::vector<Float> &get_val_ptr() const { return val; }
518 
519  // Utility
520  // ///////////////////////////////////////////////////////////////////////////
521 
530  COO &transpose();
531 
540  void transpose(COO &B) const;
541 
549  double get_data_size() const {
550  return 3 * get_nnz() * sizeof(Float) / 1.0e+9;
551  }
552 
560  std::string type() const { return "COO"; }
561 
570  void diag(vector<Float> &vec) const;
571  void diag(view1D<vector<Float>, Float> &vec) const;
572  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
573 
583  void row(const size_t r, vector<Float> &vec) const;
584  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
585  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
586 
596  void col(const size_t c, vector<Float> &vec) const;
597  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
598  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
599 
601 
612  void operator=(const COO<Float> &mat);
613 
624  bool equal(const COO<Float> &mat, bool compare_cpu_and_device = false) const;
625 
635  bool operator==(const COO<Float> &mat) const;
636 
646  bool operator!=(const COO<Float> &mat) const;
647 
663  void insert(const size_t m, const size_t n, const Float val);
664 
665 private:
666  void _q_sort(int lo, int hi);
667 
668 public:
677  void sort(bool merge);
678 };
679 
680 } // namespace matrix
681 } // namespace monolish
monolish::matrix::COO::row_index
std::vector< int > row_index
Coodinate format row index, which stores row numbers of the non-zero elements (size nnz)
Definition: monolish_coo.hpp:65
monolish::matrix::COO::set_col
void set_col(const size_t N)
Set col number.
Definition: monolish_coo.hpp:276
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:30
monolish::matrix::COO::COO
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.
Definition: monolish_coo.hpp:196
monolish::matrix::COO::device_free
void device_free() const
free data on GPU
Definition: monolish_coo.hpp:313
monolish::matrix::COO::set_row
void set_row(const size_t M)
Set row number.
Definition: monolish_coo.hpp:266
monolish::matrix::COO::output_mm
void output_mm(const std::string filename) const
output matrix elements in MatrixMarket format (MatrixMarket format: https://math.nist....
Definition: IO_coo.cpp:48
monolish::matrix::COO::set_ptr
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.
Definition: copy_coo.cpp:26
monolish::matrix::COO::operator==
bool operator==(const COO< Float > &mat) const
Comparing matricies (A == mat)
Definition: compare_coo.cpp:71
monolish::matrix::COO::get_row
size_t get_row() const
get # of row
Definition: monolish_coo.hpp:429
monolish::matrix::COO::operator=
void operator=(const COO< Float > &mat)
matrix copy
Definition: copy_coo.cpp:10
monolish::matrix::COO::nnz
size_t nnz
# of non-zero element
Definition: monolish_coo.hpp:53
monolish::matrix::COO::COO
COO(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
Definition: monolish_coo.hpp:230
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::matrix::COO::insert
void insert(const size_t m, const size_t n, const Float val)
insert element to (m, n)
Definition: at_insert_sort_coo.cpp:30
monolish::matrix::COO::get_row_ptr
std::vector< int > & get_row_ptr()
get row index
Definition: monolish_coo.hpp:467
monolish::matrix::COO::COO
COO(const matrix::Dense< Float > &dense)
Create COO matrix from Dense matrix.
Definition: monolish_coo.hpp:250
monolish::matrix::COO::operator!=
bool operator!=(const COO< Float > &mat) const
Comparing matricies (A != mat)
Definition: compare_coo.cpp:83
monolish::vector::data
const Float * data() const
returns a direct pointer to the vector
Definition: monolish_vector.hpp:230
monolish::matrix::COO::input_mm
void input_mm(const std::string filename)
Create COO matrix from MatrixMatrket format file (only real general) (MatrixMarket format: https://ma...
Definition: IO_coo.cpp:68
monolish::matrix::COO::val
std::vector< Float > val
Coodinate format value array, which stores values of the non-zero elements (size nnz)
Definition: monolish_coo.hpp:77
monolish::matrix::COO::sort
void sort(bool merge)
sort COO matrix elements (and merge elements)
Definition: at_insert_sort_coo.cpp:108
monolish::matrix::COO::type
std::string type() const
get format name "COO"
Definition: monolish_coo.hpp:560
monolish::matrix::COO::get_val_ptr
std::vector< Float > & get_val_ptr()
get value
Definition: monolish_coo.hpp:487
monolish::matrix::COO::COO
COO(const matrix::LinearOperator< Float > &linearoperator)
Definition: monolish_coo.hpp:254
monolish::matrix::COO::print_all
void print_all(bool force_cpu=false) const
print all elements to standard I/O
Definition: IO_coo.cpp:17
monolish::matrix::COO::fill
void fill(Float value)
fill matrix elements with a scalar value
Definition: fill_coo.cpp:9
monolish::matrix::COO::COO
COO(const size_t M, const size_t N)
Initialize M x N COO matrix.
Definition: monolish_coo.hpp:92
monolish::matrix::COO::~COO
~COO()
; free gpu mem.
Definition: monolish_coo.hpp:328
monolish::matrix::COO::diag
void diag(vector< Float > &vec) const
get diag. vector
monolish::matrix::COO::get_row_ptr
const std::vector< int > & get_row_ptr() const
get row index
Definition: monolish_coo.hpp:497
monolish::matrix::COO::get_nnz
size_t get_nnz() const
get # of non-zeros
Definition: monolish_coo.hpp:447
monolish::matrix::COO::row
void row(const size_t r, vector< Float > &vec) const
get row vector
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::matrix::COO::get_col_ind
std::vector< int > & get_col_ind()
get column index
Definition: monolish_coo.hpp:477
monolish::vector::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_vector.hpp:204
monolish::matrix::COO::at
Float at(const size_t i, const size_t j) const
Get matrix element (A(i,j))
Definition: at_insert_sort_coo.cpp:9
monolish::matrix::COO::get_device_mem_stat
bool get_device_mem_stat() const
false; // true: sended, false: not send
Definition: monolish_coo.hpp:321
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:38
monolish::matrix::COO::send
void send() const
send data to GPU
Definition: monolish_coo.hpp:295
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::matrix::COO::set_nnz
void set_nnz(const size_t NNZ)
Set # of non-zero elements.
Definition: monolish_coo.hpp:286
monolish::matrix::COO::convert
void convert(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
monolish::matrix::COO::at
Float at(const size_t i, const size_t j)
Get matrix element (A(i,j))
Definition: monolish_coo.hpp:403
monolish::matrix::COO::COO
COO()
Definition: monolish_coo.hpp:79
monolish::matrix::COO::get_data_size
double get_data_size() const
Memory data space required by the matrix.
Definition: monolish_coo.hpp:549
monolish::matrix::COO::COO
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.
Definition: monolish_coo.hpp:152
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::matrix::COO::colN
size_t colN
# of col
Definition: monolish_coo.hpp:48
monolish::matrix::COO::COO
COO(const std::string filename)
Create COO matrix from MatrixMatrket format file (only real general) (MatrixMarket format: https://ma...
Definition: monolish_coo.hpp:355
monolish::matrix::COO::transpose
COO & transpose()
get transposed matrix (A^T)
Definition: transpose_coo.cpp:9
monolish::matrix::COO::COO
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.
Definition: monolish_coo.hpp:129
monolish::matrix::COO::gpu_status
bool gpu_status
true: sended, false: not send
Definition: monolish_coo.hpp:58
monolish::matrix::COO::recv
void recv() const
recv data from GPU
Definition: monolish_coo.hpp:304
monolish::matrix::COO::equal
bool equal(const COO< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matricies (A == mat)
Definition: compare_coo.cpp:10
monolish::matrix::COO::rowN
size_t rowN
# of row
Definition: monolish_coo.hpp:43
monolish::matrix::COO::_q_sort
void _q_sort(int lo, int hi)
Definition: at_insert_sort_coo.cpp:48
monolish::matrix::COO::get_col
size_t get_col() const
get # of col
Definition: monolish_coo.hpp:438
monolish::matrix::COO::get_val_ptr
const std::vector< Float > & get_val_ptr() const
get value
Definition: monolish_coo.hpp:517
monolish::matrix::COO::get_col_ind
const std::vector< int > & get_col_ind() const
get column index
Definition: monolish_coo.hpp:507
monolish::matrix::COO::col_index
std::vector< int > col_index
Coodinate format column index, which stores column numbers of the non-zero elements (size nnz)
Definition: monolish_coo.hpp:71
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29
monolish::matrix::COO::col
void col(const size_t c, vector< Float > &vec) const
get column vector