monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_view_dense.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "./monolish_logger.hpp"
3 #include <cassert>
4 #include <exception>
5 #include <fstream>
6 #include <iostream>
7 #include <iterator>
8 #include <memory>
9 #include <omp.h>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 
14 #if USE_SXAT
15 #undef _HAS_CPP17
16 #endif
17 #include <random>
18 #if USE_SXAT
19 #define _HAS_CPP17 1
20 #endif
21 
22 #if defined USE_MPI
23 #include <mpi.h>
24 #endif
25 
26 namespace monolish {
27 template <typename Float> class vector;
28 
29 namespace matrix {
30 template <typename Float> class Dense;
31 template <typename Float> class CRS;
32 template <typename Float> class LinearOperator;
33 } // namespace matrix
34 
35 namespace tensor {
36 template <typename Float> class tensor_Dense;
37 } // namespace tensor
38 
50 template <typename TYPE, typename Float> class view_Dense {
51 private:
52  TYPE &target;
53  Float *target_data;
54  size_t first;
55  size_t last;
56  size_t range;
57 
58  size_t rowN;
59  size_t colN;
60 
61 public:
73  view_Dense(vector<Float> &x, const size_t start, const size_t row,
74  const size_t col)
75  : target(x) {
76  first = start;
77  rowN = row;
78  colN = col;
79  range = rowN * colN;
80  last = start + range;
81  target_data = x.data();
82  }
83 
95  view_Dense(matrix::Dense<Float> &A, const size_t start, const size_t row,
96  const size_t col)
97  : target(A) {
98  first = start;
99  rowN = row;
100  colN = col;
101  range = rowN * colN;
102  last = start + range;
103  target_data = A.data();
104  }
105 
117  view_Dense(tensor::tensor_Dense<Float> &A, const size_t start,
118  const size_t row, const size_t col)
119  : target(A) {
120  first = start;
121  rowN = row;
122  colN = col;
123  range = rowN * colN;
124  last = start + range;
125  target_data = A.data();
126  }
127 
139  view_Dense(view_Dense<vector<Float>, Float> &x, const size_t start,
140  const size_t row, const size_t col)
141  : target(x) {
142  first = x.get_first() + start;
143  rowN = row;
144  colN = col;
145  range = rowN * colN;
146  last = first + range;
147  target_data = x.data();
148  }
149 
161  view_Dense(view_Dense<matrix::Dense<Float>, Float> &x, const size_t start,
162  const size_t row, const size_t col)
163  : target(x) {
164  first = x.get_first() + start;
165  rowN = row;
166  colN = col;
167  range = rowN * colN;
168  last = first + range;
169  target_data = x.data();
170  }
171 
185  const size_t start, const size_t row, const size_t col)
186  : target(x) {
187  first = x.get_first() + start;
188  rowN = row;
189  colN = col;
190  range = rowN * colN;
191  last = first + range;
192  target_data = x.data();
193  }
194 
195  // communication
196  // ///////////////////////////////////////////////////////////////////////////
204  void send() const { target.send(); };
205 
213  void recv() { target.recv(); };
214 
222  [[nodiscard]] size_t get_row() const { return rowN; }
223 
231  [[nodiscard]] size_t get_col() const { return colN; }
232 
239  [[nodiscard]] size_t size() const { return range; }
240 
247  [[nodiscard]] size_t get_nnz() const { return range; }
248 
256  [[nodiscard]] size_t get_alloc_nnz() const { return target.get_alloc_nnz(); }
257 
264  [[nodiscard]] size_t get_first() const { return first; }
265 
272  [[nodiscard]] size_t get_last() const { return last; }
273 
280  [[nodiscard]] size_t get_offset() const { return first; }
281 
282  [[nodiscard]] std::shared_ptr<Float> get_val() const { return target.val; }
283 
289  void set_first(size_t i) { first = i; }
290 
296  void set_last(size_t i) {
297  assert(first + i <= target.get_nnz());
298  last = i;
299  }
300 
308  [[nodiscard]] std::string type() const {
309  return "view_Dense(" + target.type() + ")";
310  }
311 
321  [[nodiscard]] Float at(const size_t i) const { return target.at(first + i); }
322 
333  [[nodiscard]] Float at(const size_t i, const size_t j) const {
334  return target.at(first + i * get_col() + j);
335  }
336 
346  [[nodiscard]] Float at(const size_t i) {
347  return static_cast<const view_Dense<TYPE, Float> *>(this)->at(i);
348  };
349 
360  [[nodiscard]] Float at(const size_t i, const size_t j) {
361  return static_cast<const view_Dense<TYPE, Float> *>(this)->at(i, j);
362  };
363 
373  void insert(const size_t i, const Float Val) {
374  target.insert(first + i, Val);
375  return;
376  };
377 
388  void insert(const size_t i, const size_t j, const Float Val) {
389  target.insert(first + i * get_col() + j, Val);
390  return;
391  };
392 
401  [[nodiscard]] size_t get_device_mem_stat() const {
402  return target.get_device_mem_stat();
403  }
404 
412  [[nodiscard]] Float *data() const { return target_data; }
413 
420  [[nodiscard]] Float *data() { return target_data; }
421 
428  [[nodiscard]] TYPE &get_target() const { return target; }
429 
436  [[nodiscard]] TYPE &get_target() { return target; }
437 
444  [[nodiscard]] Float *begin() const { return target_data + get_offset(); }
445 
452  [[nodiscard]] Float *begin() { return target_data + get_offset(); }
453 
460  [[nodiscard]] Float *end() const { return target_data + range; }
461 
468  [[nodiscard]] Float *end() { return target_data + range; }
469 
478  void print_all(bool force_cpu = false) const;
479 
490  void resize(size_t row, size_t col) {
491  assert(first + row * col <= target.get_nnz());
492  rowN = row;
493  colN = col;
494  range = rowN * colN;
495  last = first + range;
496  }
497 
498  void move(const view_tensor_Dense<TYPE, Float> &view_tensor_dense);
499 
500  void move(const view_tensor_Dense<TYPE, Float> &view_tensor_dense, int rowN,
501  int colN);
502 
511  void fill(Float value);
512 
525  void operator=(const matrix::Dense<Float> &mat);
526 
539  void operator=(const view_Dense<vector<Float>, Float> &mat);
540 
553  void operator=(const view_Dense<matrix::Dense<Float>, Float> &mat);
554 
568 
578  [[nodiscard]] Float &operator[](const size_t i) {
579  if (target.get_device_mem_stat()) {
580  throw std::runtime_error("Error, GPU vector cant use operator[]");
581  }
582  return target_data[i + first];
583  }
584 
593  void diag_add(const Float alpha);
594 
603  void diag_sub(const Float alpha);
604 
613  void diag_mul(const Float alpha);
614 
623  void diag_div(const Float alpha);
624 
633  void diag_add(const vector<Float> &vec);
634  void diag_add(const view1D<vector<Float>, Float> &vec);
635  void diag_add(const view1D<matrix::Dense<Float>, Float> &vec);
636  void diag_add(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
637 
646  void diag_sub(const vector<Float> &vec);
647  void diag_sub(const view1D<vector<Float>, Float> &vec);
648  void diag_sub(const view1D<matrix::Dense<Float>, Float> &vec);
649  void diag_sub(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
650 
659  void diag_mul(const vector<Float> &vec);
660  void diag_mul(const view1D<vector<Float>, Float> &vec);
661  void diag_mul(const view1D<matrix::Dense<Float>, Float> &vec);
662  void diag_mul(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
663 
672  void diag_div(const vector<Float> &vec);
673  void diag_div(const view1D<vector<Float>, Float> &vec);
674  void diag_div(const view1D<matrix::Dense<Float>, Float> &vec);
675  void diag_div(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
676 };
679 } // namespace monolish
Dense format Matrix.
const Float * data() const
returns a direct pointer to the matrix
const Float * data() const
returns a direct pointer to the tensor
const Float * data() const
returns a direct pointer to the vector
size_t get_nnz() const
get view_Dense size (same as size())
void insert(const size_t i, const Float Val)
set element A[i/col][jcol]
void diag_mul(const Float alpha)
Scalar and diag. vector of Dense format matrix mul.
void diag_add(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix add.
void operator=(const view_Dense< tensor::tensor_Dense< Float >, Float > &mat)
matrix copy
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix sub.
void move(const view_tensor_Dense< TYPE, Float > &view_tensor_dense)
void recv()
recv data from GPU, and free data on GPU
void send() const
send data to GPU
void diag_div(const view1D< vector< Float >, Float > &vec)
void diag_sub(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_sub(const view1D< vector< Float >, Float > &vec)
Float at(const size_t i)
get element A[i/col][icol] (only CPU)
size_t get_last() const
get end position
void diag_add(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void fill(Float value)
fill vector elements with a scalar value
size_t get_row() const
get # of row
void diag_div(const Float alpha)
Scalar and diag. vector of Dense format matrix div.
Float * data()
returns a direct pointer to the vector (dont include offset)
Float * end() const
returns a end iterator
Float at(const size_t i, const size_t j)
get element A[i][j] (only CPU)
void move(const view_tensor_Dense< TYPE, Float > &view_tensor_dense, int rowN, int colN)
void diag_div(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
size_t get_device_mem_stat() const
true: sended, false: not send
void operator=(const view_Dense< vector< Float >, Float > &mat)
matrix copy
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_sub(const Float alpha)
Scalar and diag. vector of Dense format matrix sub.
size_t get_first() const
get first position
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_mul(const view1D< vector< Float >, Float > &vec)
void set_last(size_t i)
change last position
Float * begin() const
returns begin iterator (include offset)
void set_first(size_t i)
change first position
void diag_div(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix div.
Float at(const size_t i, const size_t j) const
get element A[i][j]
TYPE & get_target()
returns a reference of the target
std::shared_ptr< Float > get_val() const
size_t size() const
get view_Dense size (range)
void operator=(const matrix::Dense< Float > &mat)
matrix copy
Float * data() const
returns a direct pointer to the original vector (dont include offset)
std::string type() const
get format name "view_Dense"
Float * end()
returns a end iterator
size_t get_alloc_nnz() const
get # of alloced non-zeros
size_t get_col() const
get # of col
TYPE & get_target() const
returns a reference of the target
view_Dense(matrix::Dense< Float > &A, const size_t start, const size_t row, const size_t col)
create view_Dense from Dense matrix(start:start+range)
void diag_mul(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_add(const view1D< vector< Float >, Float > &vec)
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_add(const Float alpha)
Scalar and diag. vector of Dense format matrix add.
view_Dense(vector< Float > &x, const size_t start, const size_t row, const size_t col)
create view_Dense from vector(start:start+range)
view_Dense(view_Dense< matrix::Dense< Float >, Float > &x, const size_t start, const size_t row, const size_t col)
create view_Dense from monolish::matrix::Dense(start:start+range)
void insert(const size_t i, const size_t j, const Float Val)
set element A[i][j]
size_t get_offset() const
get first position (same as get_first())
Float & operator[](const size_t i)
reference to the element at position
void resize(size_t row, size_t col)
change last postion
Float * begin()
returns begin iterator (include offset)
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix mul.
view_Dense(tensor::tensor_Dense< Float > &A, const size_t start, const size_t row, const size_t col)
create view_Dense from Dense tensor(start:start+range)
view_Dense(view_Dense< tensor::tensor_Dense< Float >, Float > &x, const size_t start, const size_t row, const size_t col)
create view_Dense from monolish::tensor::tensor_Dense(start:start+range)
view_Dense(view_Dense< vector< Float >, Float > &x, const size_t start, const size_t row, const size_t col)
create view_Dense from monolish::vector(start:start+range)
void print_all(bool force_cpu=false) const
print all elements to standart I/O
Float at(const size_t i) const
get element A[i/col][jcol]
void operator=(const view_Dense< matrix::Dense< Float >, Float > &mat)
matrix copy
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
monolish namespaces