monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_view_tensor_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_tensor_Dense {
51 private:
52  TYPE &target;
53  Float *target_data;
54  size_t first;
55  size_t last;
56  size_t range;
57 
58  std::vector<size_t> shape;
59 
60 public:
71  view_tensor_Dense(vector<Float> &x, const size_t start,
72  const std::vector<size_t> &shape_)
73  : target(x) {
74  first = start;
75  shape = shape_;
76  range = calc_range();
77  last = start + range;
78  target_data = x.data();
79  }
80 
91  view_tensor_Dense(matrix::Dense<Float> &A, const size_t start,
92  const std::vector<size_t> &shape_)
93  : target(A) {
94  first = start;
95  shape = shape_;
96  range = calc_range();
97  last = start + range;
98  target_data = A.data();
99  }
100 
112  const std::vector<size_t> &shape_)
113  : target(A) {
114  first = start;
115  shape = shape_;
116  range = calc_range();
117  last = start + range;
118  target_data = A.data();
119  }
120 
132  const size_t start, const std::vector<size_t> &shape_)
133  : target(x) {
134  first = x.get_first() + start;
135  shape = shape_;
136  range = calc_range();
137  last = first + range;
138  target_data = x.data();
139  }
140 
153  const size_t start, const std::vector<size_t> &shape_)
154  : target(x) {
155  first = x.get_first() + start;
156  shape = shape_;
157  range = calc_range();
158  last = first + range;
159  target_data = x.data();
160  }
161 
174  const size_t start, const std::vector<size_t> &shape_)
175  : target(x) {
176  first = x.get_first() + start;
177  shape = shape_;
178  range = calc_range();
179  last = first + range;
180  target_data = x.data();
181  }
182 
190  [[nodiscard]] std::string type() const {
191  return "view_tensor_Dense(" + target.type() + ")";
192  }
193 
194  // communication
195  // ///////////////////////////////////////////////////////////////////////////
203  void send() const { target.send(); };
204 
212  void recv() { target.recv(); };
213 
220  [[nodiscard]] size_t calc_range() const {
221  size_t N = 1;
222  for (auto n : shape) {
223  N *= n;
224  }
225  return N;
226  };
227 
235  [[nodiscard]] std::vector<size_t> get_shape() const { return shape; }
236 
243  [[nodiscard]] size_t size() const { return range; }
244 
251  [[nodiscard]] size_t get_nnz() const { return range; }
252 
259  [[nodiscard]] size_t get_first() const { return first; }
260 
267  [[nodiscard]] size_t get_last() const { return last; }
268 
275  [[nodiscard]] size_t get_offset() const { return first; }
276 
277  [[nodiscard]] std::shared_ptr<Float> get_val() const { return target.val; }
278 
279  [[nodiscard]] size_t get_alloc_nnz() const { return target.get_alloc_nnz(); }
280 
286  void set_first(size_t i) { first = i; }
287 
293  void set_last(size_t i) {
294  assert(first + i <= target.get_nnz());
295  last = i;
296  }
297 
307  [[nodiscard]] Float at(const size_t pos) const {
308  return target.at(first + pos);
309  }
310 
320  [[nodiscard]] Float at(const std::vector<size_t> &pos) const {
321  return target.at(first + get_index(pos));
322  }
323 
333  template <typename... Args>
334  [[nodiscard]] Float at(const std::vector<size_t> &pos, const size_t dim,
335  const Args... args) const {
336  std::vector<size_t> pos_copy = pos;
337  pos_copy.push_back(dim);
338  return this->at(pos_copy, args...);
339  };
340 
350  template <typename... Args>
351  [[nodiscard]] Float at(const size_t dim, const Args... args) const {
352  std::vector<size_t> pos(1);
353  pos[0] = dim;
354  return this->at(pos, args...);
355  };
356 
366  [[nodiscard]] Float at(const size_t pos) {
367  return static_cast<const view_tensor_Dense<TYPE, Float> *>(this)->at(pos);
368  };
369 
379  [[nodiscard]] Float at(const std::vector<size_t> &pos) {
380  return static_cast<const view_tensor_Dense<TYPE, Float> *>(this)->at(pos);
381  };
382 
392  template <typename... Args>
393  [[nodiscard]] Float at(const std::vector<size_t> &pos, const Args... args) {
394  return static_cast<const view_tensor_Dense *>(this)->at(pos, args...);
395  };
396 
406  template <typename... Args>
407  [[nodiscard]] Float at(const size_t dim, const Args... args) {
408  return static_cast<const view_tensor_Dense *>(this)->at(dim, args...);
409  };
410 
420  void insert(const size_t i, const Float Val) {
421  target.insert(first + i, Val);
422  return;
423  };
424 
434  void insert(const std::vector<size_t> &pos, const Float Val) {
435  target.insert(first + get_index(pos), Val);
436  return;
437  };
438 
447  [[nodiscard]] size_t get_device_mem_stat() const {
448  return target.get_device_mem_stat();
449  }
450 
455  [[nodiscard]] std::shared_ptr<bool> get_gpu_status() const {
456  return target.get_gpu_status();
457  }
458 
466  [[nodiscard]] Float *data() const { return target_data; }
467 
474  [[nodiscard]] Float *data() { return target_data; }
475 
482  [[nodiscard]] TYPE &get_target() const { return target; }
483 
490  [[nodiscard]] TYPE &get_target() { return target; }
491 
498  [[nodiscard]] Float *begin() const { return target_data + get_offset(); }
499 
506  [[nodiscard]] Float *begin() { return target_data + get_offset(); }
507 
514  [[nodiscard]] Float *end() const { return target_data + range; }
515 
522  [[nodiscard]] Float *end() { return target_data + range; }
523 
532  void print_all(bool force_cpu = false) const;
533 
544  void resize(std::vector<size_t> &shape_) {
545  shape = shape_;
546  range = calc_range();
547  assert(first + range <= target.get_nnz());
548  last = first + range;
549  }
550 
559  void fill(Float value);
560 
574 
587  void operator=(const view_tensor_Dense<vector<Float>, Float> &tens);
588 
602 
615  void
617 
627  [[nodiscard]] Float &operator[](const size_t i) {
628  if (target.get_device_mem_stat()) {
629  throw std::runtime_error("Error, GPU vector cant use operator[]");
630  }
631  return target_data[i + first];
632  }
633 
643  size_t get_index(const std::vector<size_t> &pos) const {
644  if (pos.size() != this->shape.size()) {
645  throw std::runtime_error("pos size should be same with the shape");
646  }
647  size_t ind = 0;
648  for (auto i = 0; i < pos.size(); ++i) {
649  ind *= this->shape[i];
650  ind += pos[i];
651  }
652  return ind;
653  }
654 
664  std::vector<size_t> get_index(const size_t pos) const {
665  std::vector<size_t> ind(this->shape.size(), 0);
666  auto pos_copy = pos;
667  for (int i = (int)this->shape.size() - 1; i >= 0; --i) {
668  ind[i] = pos_copy % this->shape[i];
669  pos_copy /= this->shape[i];
670  }
671  return ind;
672  }
673 
682  void diag_add(const Float alpha);
683 
692  void diag_sub(const Float alpha);
693 
702  void diag_mul(const Float alpha);
703 
712  void diag_div(const Float alpha);
713 
722  void diag_add(const vector<Float> &vec);
723  void diag_add(const view1D<vector<Float>, Float> &vec);
724  void diag_add(const view1D<matrix::Dense<Float>, Float> &vec);
725  void diag_add(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
726 
735  void diag_sub(const vector<Float> &vec);
736  void diag_sub(const view1D<vector<Float>, Float> &vec);
737  void diag_sub(const view1D<matrix::Dense<Float>, Float> &vec);
738  void diag_sub(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
739 
748  void diag_mul(const vector<Float> &vec);
749  void diag_mul(const view1D<vector<Float>, Float> &vec);
750  void diag_mul(const view1D<matrix::Dense<Float>, Float> &vec);
751  void diag_mul(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
752 
761  void diag_div(const vector<Float> &vec);
762  void diag_div(const view1D<vector<Float>, Float> &vec);
763  void diag_div(const view1D<matrix::Dense<Float>, Float> &vec);
764  void diag_div(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
765 };
768 } // 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
view_tensor_Dense(view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &x, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from monolish::tensor::tensor_Dense(start:start+range)
void diag_div(const view1D< vector< Float >, Float > &vec)
Float at(const std::vector< size_t > &pos, const Args... args)
get element A[pos[0]][pos[1]]... (onlu CPU)
TYPE & get_target()
returns a reference of the target
size_t get_device_mem_stat() const
true: sended, false: not send
size_t get_last() const
get end position
void diag_add(const view1D< vector< Float >, Float > &vec)
view_tensor_Dense(view_tensor_Dense< vector< Float >, Float > &x, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from monolish::vector(start:start+range)
view_tensor_Dense(view_tensor_Dense< matrix::Dense< Float >, Float > &x, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from monolish::matrix::Dense(start:start+range)
size_t get_offset() const
get first position (same as get_first())
void diag_div(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
std::shared_ptr< bool > get_gpu_status() const
gpu status shared pointer
void set_last(size_t i)
change last position
void diag_mul(const Float alpha)
Scalar and diag. vector of Dense format matrix mul.
Float * end() const
returns a end iterator
Float at(const std::vector< size_t > &pos) const
get element A[pos[0]][pos[1]]...
view_tensor_Dense(tensor::tensor_Dense< Float > &A, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from Dense tensor(start:start+range)
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_div(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix div.
void operator=(const view_tensor_Dense< matrix::Dense< Float >, Float > &tens)
tensor copy
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_add(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_add(const Float alpha)
Scalar and diag. vector of Dense format matrix add.
std::vector< size_t > get_shape() const
get shape
Float * begin() const
returns begin iterator (include offset)
view_tensor_Dense(matrix::Dense< Float > &A, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from Dense matrix(start:start+range)
Float * begin()
returns begin iterator (include offset)
Float at(const size_t pos)
get element A[index] (only CPU)
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_mul(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void set_first(size_t i)
change first position
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix sub.
Float at(const size_t dim, const Args... args) const
get element A[pos[0]][pos[1]]...
void resize(std::vector< size_t > &shape_)
change last postion
Float * data()
returns a direct pointer to the vector (dont include offset)
size_t size() const
get view_tensor_Dense size (range)
size_t get_nnz() const
get view_tensor_Dense size (same as size())
void diag_sub(const view1D< vector< Float >, Float > &vec)
void insert(const size_t i, const Float Val)
set element A[index]...
view_tensor_Dense(vector< Float > &x, const size_t start, const std::vector< size_t > &shape_)
create view_tensor_Dense from vector(start:start+range)
void operator=(const view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &tens)
tensor copy
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix mul.
TYPE & get_target() const
returns a reference of the target
std::string type() const
get format name "view_tensor_Dense"
size_t get_first() const
get first position
std::shared_ptr< Float > get_val() const
void print_all(bool force_cpu=false) const
print all elements to standart I/O
Float at(const size_t dim, const Args... args)
get element A[pos[0]][pos[1]]... (onlu CPU)
size_t get_index(const std::vector< size_t > &pos) const
get aligned index from vector index (A[pos] = A[ind[0]][ind[1]]...)
Float & operator[](const size_t i)
reference to the element at position
void send() const
send data to GPU
void diag_div(const Float alpha)
Scalar and diag. vector of Dense format matrix div.
void recv()
recv data from GPU, and free data on GPU
Float * end()
returns a end iterator
std::vector< size_t > get_index(const size_t pos) const
get vector index from aligned index (A[pos[0]][pos[1]]... = A[ind])
void fill(Float value)
fill vector elements with a scalar value
void operator=(const tensor::tensor_Dense< Float > &tens)
tensor copy
void diag_mul(const view1D< vector< Float >, Float > &vec)
Float at(const std::vector< size_t > &pos)
get element A[pos[0]][pos[1]]...
void operator=(const view_tensor_Dense< vector< Float >, Float > &tens)
tensor copy
Float at(const size_t pos) const
get element A[index]
void diag_sub(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
size_t calc_range() const
calculate view_tensor_Dense size from shape
void diag_sub(const Float alpha)
Scalar and diag. vector of Dense format matrix sub.
void insert(const std::vector< size_t > &pos, const Float Val)
set element A[pos[0]][pos[1]]...
Float * data() const
returns a direct pointer to the original vector (dont include offset)
Float at(const std::vector< size_t > &pos, const size_t dim, const Args... args) const
get element A[pos[0]][pos[1]]...
void diag_add(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix add.
monolish namespaces