monolish  0.17.1
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_tensor_dense.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "monolish_matrix.hpp"
3 #include "monolish_tensor.hpp"
4 #include "monolish_vector.hpp"
5 
6 namespace monolish {
7 template <typename Float> class vector;
8 template <typename TYPE, typename Float> class view1D;
9 namespace tensor {
10 template <typename Float> class tensor_COO;
11 template <typename Float> class tensor_Dense {
12 private:
16  std::vector<size_t> shape;
17 
21  mutable bool gpu_status = false;
22 
23 public:
27  std::shared_ptr<Float> val;
28 
32  size_t val_nnz = 0;
33 
37  size_t alloc_nnz = 0;
38 
42  bool val_create_flag = false;
43 
45 
55 
69 
79 
89  convert(tens);
90  val_create_flag = true;
91  }
92 
100  void convert(const matrix::Dense<Float> &dense);
101 
110  convert(dense);
111  val_create_flag = true;
112  }
113 
121  void convert(const vector<Float> &vec);
122 
131  convert(vec);
132  val_create_flag = true;
133  }
134 
143  tensor_Dense(const std::vector<size_t> &shape);
144 
153  tensor_Dense(const std::vector<size_t> &shape, const Float *value);
154 
164  tensor_Dense(const std::vector<size_t> &shape,
165  const std::vector<Float> &value);
166 
177  tensor_Dense(const std::vector<size_t> &shape, const Float min,
178  const Float max);
179 
191  tensor_Dense(const std::vector<size_t> &shape, const Float min,
192  const Float max, const std::uint32_t seed);
193 
207  tensor_Dense(const tensor_Dense<Float> &tens, Float value);
208 
218  void set_ptr(const std::vector<size_t> &shape,
219  const std::vector<Float> &value);
220 
230  void set_ptr(const std::vector<size_t> &shape, const Float *value);
231 
239  [[nodiscard]] std::vector<size_t> get_shape() const { return shape; }
240 
248  [[nodiscard]] size_t get_nnz() const { return val_nnz; }
249 
257  void set_shape(const std::vector<size_t> &shape) { this->shape = shape; };
258 
266  [[nodiscard]] std::string type() const { return "tensor_Dense"; }
267 
268  // TODO
276  [[nodiscard]] double get_data_size() const {
277  return get_nnz() * sizeof(Float) / 1.0e+9;
278  }
279 
289  [[nodiscard]] Float at(const std::vector<size_t> &pos) const;
290 
300  template <typename... Args>
301  [[nodiscard]] Float at(const std::vector<size_t> &pos, const size_t dim,
302  const Args... args) const {
303  std::vector<size_t> pos_copy = pos;
304  pos_copy.push_back(dim);
305  return this->at(pos_copy, args...);
306  };
307 
317  template <typename... Args>
318  [[nodiscard]] Float at(const size_t dim, const Args... args) const {
319  std::vector<size_t> pos(1);
320  pos[0] = dim;
321  return this->at(pos, args...);
322  };
323 
333  [[nodiscard]] Float at(const std::vector<size_t> &pos) {
334  return static_cast<const tensor_Dense *>(this)->at(pos);
335  };
336 
346  template <typename... Args>
347  [[nodiscard]] Float at(const std::vector<size_t> &pos, const Args... args) {
348  return static_cast<const tensor_Dense *>(this)->at(pos, args...);
349  };
350 
360  template <typename... Args>
361  [[nodiscard]] Float at(const size_t dim, const Args... args) {
362  return static_cast<const tensor_Dense *>(this)->at(dim, args...);
363  };
364 
374  void insert(const std::vector<size_t> &pos, const Float Val);
375 
384  void print_all(bool force_cpu = false) const;
385 
386  // communication
387  // ///////////////////////////////////////////////////////////////////////////
395  void send() const;
396 
404  void recv();
405 
413  void nonfree_recv();
414 
422  void device_free() const;
423 
428  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
429 
438  if (val_create_flag) {
439  if (get_device_mem_stat()) {
440  device_free();
441  }
442  }
443  }
444 
451  [[nodiscard]] const Float *data() const { return val.get(); }
452 
459  [[nodiscard]] Float *data() { return val.get(); }
460 
469  void resize(const size_t N, Float Val = 0) {
470  if (get_device_mem_stat()) {
471  throw std::runtime_error("Error, GPU matrix cant use resize");
472  }
473  if (val_create_flag) {
474  std::shared_ptr<Float> tmp(new Float[N], std::default_delete<Float[]>());
475  size_t copy_size = std::min(val_nnz, N);
476  for (size_t i = 0; i < copy_size; ++i) {
477  tmp.get()[i] = data()[i];
478  }
479  for (size_t i = copy_size; i < N; ++i) {
480  tmp.get()[i] = Val;
481  }
482  val = tmp;
483  alloc_nnz = N;
484  val_nnz = N;
485  } else {
486  throw std::runtime_error("Error, not create vector cant use resize");
487  }
488  }
489 
498  void resize(const std::vector<size_t> &shape, Float val = 0) {
499  size_t N = 1;
500  for (auto n : shape) {
501  N *= n;
502  }
503  resize(N, val);
504  this->shape = shape;
505  }
506 
512  void move(const matrix::Dense<Float> &dense);
513 
519  void move(const vector<Float> &vec);
520 
529  void fill(Float value);
530 
543  void operator=(const tensor_Dense<Float> &tens);
544 
554  [[nodiscard]] Float &operator[](size_t i) {
555  if (get_device_mem_stat()) {
556  throw std::runtime_error("Error, GPU vector cant use operator[]");
557  }
558  return data()[i];
559  }
560 
571  [[nodiscard]] bool equal(const tensor_Dense<Float> &tens,
572  bool compare_cpu_and_device = false) const;
573 
585  [[nodiscard]] bool operator==(const tensor_Dense<Float> &tens) const;
586 
598  [[nodiscard]] bool operator!=(const tensor_Dense<Float> &tens) const;
599 
609  size_t get_index(const std::vector<size_t> &pos) const {
610  if (pos.size() != this->shape.size()) {
611  throw std::runtime_error("pos size should be same with the shape");
612  }
613  size_t ind = 0;
614  for (auto i = 0; i < pos.size(); ++i) {
615  ind *= this->shape[i];
616  ind += pos[i];
617  }
618  return ind;
619  }
620 
630  std::vector<size_t> get_index(const size_t pos) const {
631  std::vector<size_t> ind(this->shape.size(), 0);
632  auto pos_copy = pos;
633  for (int i = (int)this->shape.size() - 1; i >= 0; --i) {
634  ind[i] = pos_copy % this->shape[i];
635  pos_copy /= this->shape[i];
636  }
637  return ind;
638  }
639 
648  void reshape(const std::vector<int> &shape);
649 
658  template <typename... Args>
659  void reshape(const std::vector<int> &shape, const size_t dim,
660  const Args... args) {
661  std::vector<int> shape_copy = shape;
662  shape_copy.push_back(dim);
663  reshape(shape_copy, args...);
664  return;
665  }
666 
675  template <typename... Args> void reshape(const int dim, const Args... args) {
676  std::vector<int> shape(1);
677  shape[0] = dim;
678  reshape(shape, args...);
679  return;
680  }
681 
683 
692  void diag_add(const Float alpha);
693 
702  void diag_sub(const Float alpha);
703 
712  void diag_mul(const Float alpha);
713 
722  void diag_div(const Float alpha);
723 
732  void diag_add(const vector<Float> &vec);
733  void diag_add(const view1D<vector<Float>, Float> &vec);
734  void diag_add(const view1D<matrix::Dense<Float>, Float> &vec);
735  void diag_add(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
736 
745  void diag_sub(const vector<Float> &vec);
746  void diag_sub(const view1D<vector<Float>, Float> &vec);
747  void diag_sub(const view1D<matrix::Dense<Float>, Float> &vec);
748  void diag_sub(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
749 
758  void diag_mul(const vector<Float> &vec);
759  void diag_mul(const view1D<vector<Float>, Float> &vec);
760  void diag_mul(const view1D<matrix::Dense<Float>, Float> &vec);
761  void diag_mul(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
762 
771  void diag_div(const vector<Float> &vec);
772  void diag_div(const view1D<vector<Float>, Float> &vec);
773  void diag_div(const view1D<matrix::Dense<Float>, Float> &vec);
774  void diag_div(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
775 };
776 } // namespace tensor
777 } // namespace monolish
Dense format Matrix.
void reshape(const std::vector< int > &shape)
Reshape tensor.
bool equal(const tensor_Dense< Float > &tens, bool compare_cpu_and_device=false) const
Comparing tensors (A == tens)
void reshape(const std::vector< int > &shape, const size_t dim, const Args... args)
Reshape tensor.
std::vector< size_t > get_index(const size_t pos) const
get vector index from aligned index (A[pos[0]][pos[1]]... = A[ind])
std::string type() const
get format name "tensor_Dense"
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix mul.
size_t get_index(const std::vector< size_t > &pos) const
get aligned index from vector index (A[pos] = A[ind[0]][ind[1]]...)
void convert(const matrix::Dense< Float > &dense)
create tensor_Dense tensor from Dense matrix
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
bool operator!=(const tensor_Dense< Float > &tens) const
Comparing tensors (A != tens)
void diag_div(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
std::vector< size_t > shape
shape
tensor_Dense(const std::vector< size_t > &shape, const std::vector< Float > &value)
Allocate tensor_Dense tensor.
std::shared_ptr< Float > val
Dense tensor format value (pointer)
Float at(const std::vector< size_t > &pos, const Args... args)
get element A[pos[0]][pos[1]]... (onlu CPU)
Float at(const std::vector< size_t > &pos)
get element A[pos[0]][pos[1]]... (onlu CPU)
void diag_add(const Float alpha)
Scalar and diag. vector of Dense format matrix add.
void insert(const std::vector< size_t > &pos, const Float Val)
set element A[pos[0]][pos[1]]...
void recv()
recv. data to GPU, and free data on GPU
void send() const
send data to GPU
void diag_sub(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
size_t val_nnz
# of non-zero element (M * N)
tensor_Dense(const matrix::Dense< Float > &dense)
Create tensor_Dense tensor from Dense matrix.
void diag_add(const view1D< vector< Float >, Float > &vec)
std::vector< size_t > get_shape() const
get shape
void diag_add(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void diag_sub(const view1D< vector< Float >, Float > &vec)
size_t alloc_nnz
allocated tensor size
void resize(const std::vector< size_t > &shape, Float val=0)
resize tensor value
void fill(Float value)
fill tensor elements with a scalar value
Float at(const size_t dim, const Args... args) const
get element A[pos[0]][pos[1]]...
tensor_Dense(const tensor::tensor_Dense< Float > &tens)
Create tensor_Dense tensor from tensor_Dense tensor.
Float at(const std::vector< size_t > &pos) const
get element A[pos[0]][pos[1]]...
void move(const matrix::Dense< Float > &dense)
move tensor_Dense tensor from Dense matrix
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_add(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix add.
Float * data()
returns a direct pointer to the tensor
void diag_mul(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void nonfree_recv()
recv. data to GPU (w/o free)
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
const Float * data() const
returns a direct pointer to the tensor
Float at(const std::vector< size_t > &pos, const size_t dim, const Args... args) const
get element A[pos[0]][pos[1]]...
void set_shape(const std::vector< size_t > &shape)
Set shape.
tensor_Dense(const std::vector< size_t > &shape, const Float min, const Float max)
Allocate tensor_Dense tensor.
bool val_create_flag
tensor create flag
void diag_div(const Float alpha)
Scalar and diag. vector of Dense format matrix div.
void move(const vector< Float > &vec)
move tensor_Dense tensor from vector
void convert(const tensor::tensor_COO< Float > &tens)
Create tensor_Dense tensor from tensor_COO tensor.
size_t get_nnz() const
get # of non-zeros
tensor_Dense(const std::vector< size_t > &shape, const Float min, const Float max, const std::uint32_t seed)
Allocate tensor_Dense tensor.
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix sub.
tensor_Dense(const std::vector< size_t > &shape, const Float *value)
Allocate dense tensor.
Float & operator[](size_t i)
reference to the element at position (v[i])
void diag_sub(const Float alpha)
Scalar and diag. vector of Dense format matrix sub.
tensor_Dense(const tensor::tensor_COO< Float > &tens)
Create tensor_Dense tensor from tensor_COO tensor.
tensor_Dense(const vector< Float > &vec)
create tensor_Dense tensor from vector
void convert(const vector< Float > &vec)
create tensor_Dense tensor from vector
void set_ptr(const std::vector< size_t > &shape, const Float *value)
Set tensor_Dense array from array.
void operator=(const tensor_Dense< Float > &tens)
tensor copy
void device_free() const
free data on GPU
Float at(const size_t dim, const Args... args)
get element A[pos[0]][pos[1]]... (onlu CPU)
void set_ptr(const std::vector< size_t > &shape, const std::vector< Float > &value)
Set tensor_Dense array from std::vector.
void reshape(const int dim, const Args... args)
Reshape tensor.
double get_data_size() const
Memory data space required by the matrix.
void resize(const size_t N, Float Val=0)
resize tensor value
~tensor_Dense()
destructor of dense tensor, free GPU memory
tensor_Dense(const tensor_Dense< Float > &tens, Float value)
Create tensor_Dense tensor of the same size as input tensor.
void diag_mul(const Float alpha)
Scalar and diag. vector of Dense format matrix mul.
void diag_div(const view1D< vector< Float >, Float > &vec)
bool operator==(const tensor_Dense< Float > &tens) const
Comparing tensors (A == tens)
void diag_div(const vector< Float > &vec)
Vector and diag. vector of Dense format matrix div.
bool get_device_mem_stat() const
true: sended, false: not send
tensor_Dense(const std::vector< size_t > &shape)
Allocate dense tensor.
bool gpu_status
true: sended, false: not send
void diag_mul(const view1D< vector< Float >, Float > &vec)
void convert(const tensor::tensor_Dense< Float > &tens)
Create tensor_Dense tensor from tensor_Dense tensor.
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