monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_dense.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "monolish_matrix.hpp"
3 #include "monolish_tensor.hpp"
4 #include <iostream>
5 #include <memory>
6 
7 namespace monolish {
8 template <typename Float> class vector;
9 template <typename TYPE, typename Float> class view1D;
10 template <typename TYPE, typename Float> class view_Dense;
11 template <typename TYPE, typename Float> class view_tensor_Dense;
12 
13 namespace tensor {
14 template <typename Float> class tensor_Dense;
15 }
16 
17 namespace matrix {
18 
30 template <typename Float> class Dense {
31 private:
35  size_t rowN;
36 
40  size_t colN;
41 
45  // size_t nnz;
46 
50  mutable std::shared_ptr<bool> gpu_status = std::make_shared<bool>(false);
51 
55  size_t first = 0;
56 
57 public:
61  std::shared_ptr<Float> val;
62 
66  size_t val_nnz = 0;
67 
71  size_t alloc_nnz = 0;
72 
76  bool val_create_flag = false;
77 
78  Dense() { val_create_flag = true; }
79 
88  void convert(const COO<Float> &coo);
89 
98  void convert(const Dense<Float> &dense);
99 
108  Dense(const COO<Float> &coo) {
109  val_create_flag = true;
110  convert(coo);
111  }
112 
125  Dense(const Dense<Float> &dense);
126 
140  Dense(const Dense<Float> &dense, Float value);
141 
154  Dense(const view_Dense<vector<Float>, Float> &dense);
155 
168  Dense(const view_Dense<matrix::Dense<Float>, Float> &dense);
169 
183 
193  Dense(const size_t M, const size_t N);
194 
205  Dense(const size_t M, const size_t N, const Float *value);
206 
217  Dense(const size_t M, const size_t N, const std::vector<Float> &value);
218 
229  Dense(const size_t M, const size_t N, const vector<Float> &value);
230 
241  Dense(const size_t M, const size_t N,
242  const std::initializer_list<Float> &list);
243 
256  Dense(const size_t M, const size_t N, const Float min, const Float max);
257 
270  Dense(const size_t M, const size_t N, const Float min, const Float max,
271  const std::uint32_t seed);
272 
283  Dense(const size_t M, const size_t N, const Float value);
284 
295  void set_ptr(const size_t M, const size_t N, const std::vector<Float> &value);
296 
307  void set_ptr(const size_t M, const size_t N, const Float *value);
308 
319  void set_ptr(const size_t M, const size_t N, const Float value);
320 
328  [[nodiscard]] size_t get_row() const { return rowN; }
329 
337  [[nodiscard]] size_t get_col() const { return colN; }
338 
346  [[nodiscard]] size_t get_nnz() const { return val_nnz; }
347 
355  [[nodiscard]] size_t get_alloc_nnz() const { return alloc_nnz; }
356 
363  [[nodiscard]] size_t get_first() const { return first; }
364 
371  [[nodiscard]] size_t get_offset() const { return get_first(); }
372 
380  void set_row(const size_t N) { rowN = N; };
381 
389  void set_col(const size_t M) { colN = M; };
390 
398  // void set_nnz(const size_t NZ) { val_nnz = NZ; };
399 
405  void set_first(size_t i) { first = i; }
406 
414  [[nodiscard]] std::string type() const { return "Dense"; }
415 
426  void transpose();
427 
436  void transpose(const Dense &B);
437 
445  [[nodiscard]] double get_data_size() const {
446  return get_nnz() * sizeof(Float) / 1.0e+9;
447  }
448 
458  [[nodiscard]] Float at(const size_t i) const;
459 
470  [[nodiscard]] Float at(const size_t i, const size_t j) const;
471 
481  [[nodiscard]] Float at(const size_t i) {
482  return static_cast<const Dense *>(this)->at(i);
483  };
484 
495  [[nodiscard]] Float at(const size_t i, const size_t j) {
496  return static_cast<const Dense *>(this)->at(i, j);
497  };
498 
508  void insert(const size_t i, const Float Val);
509 
520  void insert(const size_t i, const size_t j, const Float Val);
521 
530  void print_all(bool force_cpu = false) const;
531 
532  // communication
533  // ///////////////////////////////////////////////////////////////////////////
541  void send() const;
542 
550  void recv();
551 
559  void nonfree_recv();
560 
568  void device_free() const;
569 
574  [[nodiscard]] bool get_device_mem_stat() const { return *gpu_status; }
575 
580  [[nodiscard]] std::shared_ptr<bool> get_gpu_status() const {
581  return gpu_status;
582  }
583 
591  ~Dense() {
592  if (val_create_flag) {
593  if (get_device_mem_stat()) {
594  device_free();
595  }
596  }
597  }
598 
605  [[nodiscard]] const Float *data() const { return val.get(); }
606 
613  [[nodiscard]] Float *data() { return val.get(); }
614 
623  void resize(size_t N, Float Val = 0) {
624  if (first + N < alloc_nnz) {
625  for (size_t i = val_nnz; i < N; ++i) {
626  begin()[i] = Val;
627  }
628  val_nnz = N;
629  return;
630  }
631  if (get_device_mem_stat()) {
632  throw std::runtime_error("Error, GPU matrix cant use resize");
633  }
634  if (val_create_flag) {
635  std::shared_ptr<Float> tmp(new Float[N], std::default_delete<Float[]>());
636  size_t copy_size = std::min(val_nnz, N);
637  for (size_t i = 0; i < copy_size; ++i) {
638  tmp.get()[i] = data()[i];
639  }
640  for (size_t i = copy_size; i < N; ++i) {
641  tmp.get()[i] = Val;
642  }
643  val = tmp;
644  alloc_nnz = N;
645  val_nnz = N;
646  } else {
647  throw std::runtime_error("Error, not create vector cant use resize");
648  }
649  }
650 
651  void move(const tensor::tensor_Dense<Float> &tensor_dense);
652 
653  void move(const tensor::tensor_Dense<Float> &tensor_dense, int rowN,
654  int colN);
655 
656  void move(const view_tensor_Dense<vector<Float>, Float> &tensor_dense);
657 
658  void move(const view_tensor_Dense<matrix::Dense<Float>, Float> &tensor_dense);
659 
661  &tensor_dense);
662 
663  void move(const view_tensor_Dense<vector<Float>, Float> &tensor_dense,
664  int rowN, int colN);
665 
666  void move(const view_tensor_Dense<matrix::Dense<Float>, Float> &tensor_dense,
667  int rowN, int colN);
668 
669  void move(
670  const view_tensor_Dense<tensor::tensor_Dense<Float>, Float> &tensor_dense,
671  int rowN, int colN);
672 
679  [[nodiscard]] const Float *begin() const { return data() + get_offset(); }
680 
687  [[nodiscard]] Float *begin() { return data() + get_offset(); }
688 
695  [[nodiscard]] const Float *end() const {
696  return data() + get_offset() + get_nnz();
697  }
698 
705  [[nodiscard]] Float *end() { return data() + get_offset() + get_nnz(); }
706 
708 
716  void diag(vector<Float> &vec) const;
717  void diag(view1D<vector<Float>, Float> &vec) const;
718  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
719  void diag(view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
720 
730  void row(const size_t r, vector<Float> &vec) const;
731  void row(const size_t r, view1D<vector<Float>, Float> &vec) const;
732  void row(const size_t r, view1D<matrix::Dense<Float>, Float> &vec) const;
733  void row(const size_t r,
734  view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
735 
745  void col(const size_t c, vector<Float> &vec) const;
746  void col(const size_t c, view1D<vector<Float>, Float> &vec) const;
747  void col(const size_t c, view1D<matrix::Dense<Float>, Float> &vec) const;
748  void col(const size_t c,
749  view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
750 
752 
761  void fill(Float value);
762 
775  void operator=(const Dense<Float> &mat);
776 
789  void operator=(const view_Dense<vector<Float>, Float> &mat);
790 
803  void operator=(const view_Dense<matrix::Dense<Float>, Float> &mat);
804 
818 
828  [[nodiscard]] Float &operator[](size_t i) {
829  if (get_device_mem_stat()) {
830  throw std::runtime_error("Error, GPU vector cant use operator[]");
831  }
832  return data()[first + i];
833  }
834 
845  [[nodiscard]] bool equal(const Dense<Float> &mat,
846  bool compare_cpu_and_device = false) const;
847 
859  [[nodiscard]] bool operator==(const Dense<Float> &mat) const;
860 
872  [[nodiscard]] bool operator!=(const Dense<Float> &mat) const;
873 
883  void reshape(const size_t row, const size_t col);
884 
886 
895  void diag_add(const Float alpha);
896 
905  void diag_sub(const Float alpha);
906 
915  void diag_mul(const Float alpha);
916 
925  void diag_div(const Float alpha);
926 
935  void diag_add(const vector<Float> &vec);
936  void diag_add(const view1D<vector<Float>, Float> &vec);
937  void diag_add(const view1D<matrix::Dense<Float>, Float> &vec);
938  void diag_add(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
939 
948  void diag_sub(const vector<Float> &vec);
949  void diag_sub(const view1D<vector<Float>, Float> &vec);
950  void diag_sub(const view1D<matrix::Dense<Float>, Float> &vec);
951  void diag_sub(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
952 
961  void diag_mul(const vector<Float> &vec);
962  void diag_mul(const view1D<vector<Float>, Float> &vec);
963  void diag_mul(const view1D<matrix::Dense<Float>, Float> &vec);
964  void diag_mul(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
965 
974  void diag_div(const vector<Float> &vec);
975  void diag_div(const view1D<vector<Float>, Float> &vec);
976  void diag_div(const view1D<matrix::Dense<Float>, Float> &vec);
977  void diag_div(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
978 };
981 } // namespace matrix
982 } // namespace monolish
Coodinate (COO) format Matrix (need to sort)
Dense format Matrix.
void row(const size_t r, view1D< vector< Float >, Float > &vec) const
Float at(const size_t i, const size_t j) const
get element A[i][j]
Dense(const view_Dense< matrix::Dense< Float >, Float > &dense)
Create Dense matrix from view Dense matrix.
Float at(const size_t i, const size_t j)
get element A[i][j] (only CPU)
void row(const size_t r, view1D< matrix::Dense< Float >, Float > &vec) const
void move(const view_tensor_Dense< vector< Float >, Float > &tensor_dense, int rowN, int colN)
std::string type() const
get format name "Dense"
void move(const view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &tensor_dense)
Dense(const Dense< Float > &dense, Float value)
Create Dense matrix of the same size as input matrix.
bool operator!=(const Dense< Float > &mat) const
Comparing matrices (A != mat)
void set_first(size_t i)
Set # of non-zero elements.
bool get_device_mem_stat() const
true: sended, false: not send
const Float * end() const
returns a end iterator
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void col(const size_t c, view1D< vector< Float >, Float > &vec) const
std::shared_ptr< bool > get_gpu_status() const
gpu status shared pointer
size_t val_nnz
# of non-zero element (M * N)
void diag_add(const view1D< vector< Float >, Float > &vec)
void operator=(const view_Dense< matrix::Dense< Float >, Float > &mat)
matrix copy
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
void convert(const Dense< Float > &dense)
Create Dense matrix from Dense matrix.
void set_ptr(const size_t M, const size_t N, const Float *value)
Set Dense array from std::vector.
Float at(const size_t i)
get element A[i/col][icol] (only CPU)
Dense(const size_t M, const size_t N, const Float min, const Float max, const std::uint32_t seed)
Create random dense matrix from dense matrix.
Dense(const size_t M, const size_t N, const Float value)
Create construct dense matrix.
void insert(const size_t i, const Float Val)
set element A[i/col][jcol]
void diag(view1D< tensor::tensor_Dense< Float >, Float > &vec) const
Dense(const COO< Float > &coo)
Create dense matrix from COO matrix.
void reshape(const size_t row, const size_t col)
Reshape matrix.
Dense(const size_t M, const size_t N, const Float *value)
Create dense matrix from array.
void diag(vector< Float > &vec) const
get diag. vector
Dense(const size_t M, const size_t N, const vector< Float > &value)
Create dense matrix from monolish::vector.
std::shared_ptr< Float > val
Dense format value (pointer)
void diag_mul(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
const Float * data() const
returns a direct pointer to the matrix
size_t get_col() const
get # of col
~Dense()
destructor of Dense matrix, free GPU memory
size_t get_first() const
get first position
void move(const view_tensor_Dense< matrix::Dense< Float >, Float > &tensor_dense)
void diag_mul(const view1D< vector< Float >, Float > &vec)
void transpose()
get transposed matrix (A = A^T)
bool operator==(const Dense< Float > &mat) const
Comparing matrices (A == mat)
void set_ptr(const size_t M, const size_t N, const std::vector< Float > &value)
Set Dense array from std::vector.
Float * end()
returns a end iterator
void col(const size_t c, view1D< matrix::Dense< Float >, Float > &vec) const
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix sub.
Dense(const size_t M, const size_t N)
Allocate dense matrix.
void set_ptr(const size_t M, const size_t N, const Float value)
Set Dense array from std::vector.
void move(const view_tensor_Dense< matrix::Dense< Float >, Float > &tensor_dense, int rowN, int colN)
std::shared_ptr< bool > gpu_status
# of non-zero element (M * N)
void diag_add(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void move(const view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &tensor_dense, int rowN, int colN)
void diag(view1D< vector< Float >, Float > &vec) const
bool equal(const Dense< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
size_t get_row() const
get # of row
void recv()
recv. data to GPU, and free data on GPU
void resize(size_t N, Float Val=0)
resize matrix value
const Float * begin() const
returns a begin iterator
void fill(Float value)
fill matrix elements with a scalar value
void transpose(const Dense &B)
create transposed matrix from Dense format matrix (A = B^T)
void diag_add(const Float alpha)
Scalar and diag. vector of Dense format matrix add.
void diag_add(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix add.
void row(const size_t r, vector< Float > &vec) const
get row vector
double get_data_size() const
Memory data space required by the matrix.
Float at(const size_t i) const
get element A[i/col][jcol]
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
size_t get_nnz() const
get # of non-zeros
void move(const view_tensor_Dense< vector< Float >, Float > &tensor_dense)
void device_free() const
free data on GPU
Dense(const Dense< Float > &dense)
Create Dense matrix from Dense matrix.
void diag_sub(const view1D< vector< Float >, Float > &vec)
Dense(const size_t M, const size_t N, const std::initializer_list< Float > &list)
Create dense matrix from std::initializer_list.
void diag_div(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_sub(const Float alpha)
Scalar and diag. vector of Dense format matrix sub.
Float & operator[](size_t i)
reference to the element at position (v[i])
void diag_div(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix div.
void move(const tensor::tensor_Dense< Float > &tensor_dense)
void col(const size_t c, vector< Float > &vec) const
get column vector
bool val_create_flag
matrix create flag;
size_t alloc_nnz
alloced matrix size
Dense(const view_Dense< tensor::tensor_Dense< Float >, Float > &dense)
Create Dense matrix from view Dense matrix.
size_t get_offset() const
get first position (same as get_first())
Float * data()
returns a direct pointer to the matrix
void col(const size_t c, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void diag_sub(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void operator=(const Dense< Float > &mat)
matrix copy
void row(const size_t r, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void set_col(const size_t M)
Set column number.
size_t get_alloc_nnz() const
get # of alloced non-zeros
void move(const tensor::tensor_Dense< Float > &tensor_dense, int rowN, int colN)
void diag_div(const view1D< vector< Float >, Float > &vec)
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
void diag_mul(const Float alpha)
Scalar and diag. vector of Dense format matrix mul.
void insert(const size_t i, const size_t j, const Float Val)
set element A[i][j]
void convert(const COO< Float > &coo)
Create Dense matrix from COO matrix.
void set_row(const size_t N)
Set row number.
Dense(const view_Dense< vector< Float >, Float > &dense)
Create Dense matrix from view Dense matrix.
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix mul.
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
Dense(const size_t M, const size_t N, const std::vector< Float > &value)
Create dense matrix from std::vector.
void operator=(const view_Dense< tensor::tensor_Dense< Float >, Float > &mat)
matrix copy
Dense(const size_t M, const size_t N, const Float min, const Float max)
Create random dense matrix from dense matrix.
void send() const
send data to GPU
void nonfree_recv()
recv. data to GPU (w/o free)
Float * begin()
returns a begin iterator
void diag_div(const Float alpha)
Scalar and diag. vector of Dense format matrix div.
size_t first
first position of data array
void operator=(const view_Dense< vector< Float >, Float > &mat)
matrix copy
void max(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with greatest elements of two matrices (C[0:nnz] = max(A[0:nnz],...
void min(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with smallest elements of two matrices (C[0:nnz] = min(A[0:nnz],...
monolish namespaces