monolish  0.17.2
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_tensor_coo.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_Dense;
11 template <typename Float> class tensor_COO {
12 private:
16  std::vector<size_t> shape;
17 
21  mutable bool gpu_status = false;
22 
23 public:
28  std::vector<std::vector<size_t>> index;
29 
34  std::shared_ptr<Float> val;
35 
39  size_t val_nnz = 0;
40 
44  std::size_t alloc_nnz = 0;
45 
49  bool val_create_flag = false;
50 
51  tensor_COO() : shape(), gpu_status(false), index(), val_nnz(0) {
52  val_create_flag = true;
53  }
54 
63  tensor_COO(const std::vector<size_t> &shape_)
64  : shape(shape_), gpu_status(false), index(), val_nnz(0) {
65  val_create_flag = true;
66  }
67 
76  tensor_COO(const std::initializer_list<size_t> &shape_)
77  : shape(shape_), gpu_status(false), index(), val_nnz(0) {
78  val_create_flag = true;
79  }
80 
90 
100  val_create_flag = true;
101  convert(tens);
102  }
103 
115  tensor_COO(const std::vector<size_t> &shape_,
116  const std::vector<std::vector<size_t>> &index_,
117  const Float *value);
118 
128 
138  tensor_COO(const tensor_COO<Float> &coo, Float value);
139 
148  void print_all(bool force_cpu = false) const;
149 
158  void print_all(const std::string filename) const;
159 
160  // TODO
168  [[nodiscard]] double get_data_size() const {
169  return get_nnz() * sizeof(Float) / 1.0e+9;
170  }
171 
181  [[nodiscard]] Float at(const std::vector<size_t> &pos) const;
182 
192  [[nodiscard]] Float at(const std::vector<size_t> &pos) {
193  return static_cast<const tensor_COO *>(this)->at(pos);
194  };
195 
206  void set_ptr(const std::vector<size_t> &shape,
207  const std::vector<std::vector<size_t>> &index,
208  const std::vector<Float> &v);
209 
221  void set_ptr(const std::vector<size_t> &shape,
222  const std::vector<std::vector<size_t>> &index,
223  const size_t vsize, const Float *v);
224 
236  void set_ptr(const std::vector<size_t> &shape,
237  const std::vector<std::vector<size_t>> &index,
238  const size_t vsize, const Float v);
239 
247  [[nodiscard]] std::vector<size_t> get_shape() const { return shape; }
248 
256  [[nodiscard]] size_t get_nnz() const { return val_nnz; }
257 
266  void fill(Float value);
267 
275  void set_shape(const std::vector<size_t> &shape) { this->shape = shape; }
276 
281  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
282 
289  [[nodiscard]] const Float *data() const { return val.get(); }
290 
297  [[nodiscard]] Float *data() { return val.get(); }
298 
307  void resize(const size_t N, Float Val = 0) {
308  if (get_device_mem_stat()) {
309  throw std::runtime_error("Error, GPU matrix cant use resize");
310  }
311  if (val_create_flag) {
312  std::shared_ptr<Float> tmp(new Float[N], std::default_delete<Float[]>());
313  size_t copy_size = std::min(val_nnz, N);
314  for (size_t i = 0; i < copy_size; ++i) {
315  tmp.get()[i] = data()[i];
316  }
317  for (size_t i = copy_size; i < N; ++i) {
318  tmp.get()[i] = Val;
319  }
320  val = tmp;
321  alloc_nnz = N;
322  val_nnz = N;
323 
324  index.resize(N);
325  } else {
326  throw std::runtime_error("Error, not create vector cant use resize");
327  }
328  }
329 
337  [[nodiscard]] std::string type() const { return "tensor_COO"; }
338 
345  [[nodiscard]] const Float *begin() const { return data(); }
346 
353  [[nodiscard]] Float *begin() { return data(); }
354 
361  [[nodiscard]] const Float *end() const { return data() + get_nnz(); }
362 
369  [[nodiscard]] Float *end() { return data() + get_nnz(); }
370 
379  void diag(vector<Float> &vec) const;
380  void diag(view1D<vector<Float>, Float> &vec) const;
381  void diag(view1D<matrix::Dense<Float>, Float> &vec) const;
382  void diag(view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
383 
394  void operator=(const tensor_COO<Float> &tens);
395 
405  [[nodiscard]] Float &operator[](size_t i) {
406  if (get_device_mem_stat()) {
407  throw std::runtime_error("Error, GPU vector cant use operator[]");
408  }
409  return data()[i];
410  }
411 
422  [[nodiscard]] bool equal(const tensor_COO<Float> &tens,
423  bool compare_cpu_and_device = false) const;
424 
436  [[nodiscard]] bool operator==(const tensor_COO<Float> &tens) const;
437 
449  [[nodiscard]] bool operator!=(const tensor_COO<Float> &tens) const;
450 
460  size_t get_index(const std::vector<size_t> &pos) {
461  if (pos.size() != this->shape.size()) {
462  throw std::runtime_error("pos size should be same with the shape");
463  }
464  size_t ind = 0;
465  for (auto i = 0; i < pos.size(); ++i) {
466  ind *= this->shape[i];
467  ind += pos[i];
468  }
469  return ind;
470  }
471 
481  std::vector<size_t> get_index(const size_t pos) {
482  std::vector<size_t> ind(this->shape.size(), 0);
483  auto pos_copy = pos;
484  for (int i = (int)this->shape.size() - 1; i >= 0; --i) {
485  ind[i] = pos_copy % this->shape[i];
486  pos_copy /= this->shape[i];
487  }
488  return ind;
489  }
490 
500  void insert(const std::vector<size_t> &pos, const Float val);
501 
502 private:
503  void _q_sort(int lo, int hi);
504 
505 public:
514  void sort(bool merge);
515 };
516 } // namespace tensor
517 } // namespace monolish
Dense format Matrix.
void convert(const tensor::tensor_Dense< Float > &tens)
Create tensor_COO tensor from tensor_Dense tensor.
void diag(view1D< tensor::tensor_Dense< Float >, Float > &vec) const
bool operator!=(const tensor_COO< Float > &tens) const
Comparing tensors (A != tens)
std::size_t alloc_nnz
alloced matrix size
void resize(const size_t N, Float Val=0)
resize tensor value
void diag(vector< Float > &vec) const
get diag. vector
void sort(bool merge)
sort tensor_COO tensor elements (and merge elements)
std::string type() const
get format name "tensor_COO"
Float * begin()
returns a begin iterator
void _q_sort(int lo, int hi)
tensor_COO(const std::vector< size_t > &shape_)
Initialize tensor_COO tensor.
const Float * end() const
returns a end iterator
Float * end()
returns a end iterator
void set_ptr(const std::vector< size_t > &shape, const std::vector< std::vector< size_t >> &index, const std::vector< Float > &v)
Set tensor_COO array from std::vector.
const Float * data() const
returns a direct pointer to the tensor
const Float * begin() const
returns a begin iterator
bool get_device_mem_stat() const
true: sended, false: not send
size_t get_index(const std::vector< size_t > &pos)
get aligned index from vector index (A[pos] = A[ind[0]][ind[1]]...)
std::vector< size_t > get_index(const size_t pos)
get vector index from aligned index (A[pos[0]][pos[1]]... = A[ind])
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void set_ptr(const std::vector< size_t > &shape, const std::vector< std::vector< size_t >> &index, const size_t vsize, const Float v)
Set tensor_COO array from array.
std::vector< size_t > shape
shape
std::shared_ptr< Float > val
Coodinate format value array (pointer), which stores values of the non-zero elements.
tensor_COO(const std::vector< size_t > &shape_, const std::vector< std::vector< size_t >> &index_, const Float *value)
Create tensor_COO tensor from n-origin array.
void print_all(const std::string filename) const
print all elements to file
std::vector< std::vector< size_t > > index
Coodinate format index, which stores index numbers of the non-zero elements (size nnz)
Float * data()
returns a direct pointer to the tensor
size_t get_nnz() const
get # of non-zeros
bool operator==(const tensor_COO< Float > &tens) const
Comparing tensors (A == tens)
std::vector< size_t > get_shape() const
get shape
bool equal(const tensor_COO< Float > &tens, bool compare_cpu_and_device=false) const
Comparing tensors (A == tens)
size_t val_nnz
# of non-zero element
bool val_create_flag
matrix create flag;
Float & operator[](size_t i)
reference to the element at position (v[i])
void operator=(const tensor_COO< Float > &tens)
tensor copy
void set_shape(const std::vector< size_t > &shape)
Set shape.
tensor_COO(const tensor_COO< Float > &coo, Float value)
Initialize tensor_COO tensor of the same size as input tensor.
Float at(const std::vector< size_t > &pos)
get element A[pos[0]][pos[1]]... (onlu CPU)
bool gpu_status
true: sended, false: not send
void set_ptr(const std::vector< size_t > &shape, const std::vector< std::vector< size_t >> &index, const size_t vsize, const Float *v)
Set tensor_COO array from array.
void fill(Float value)
fill tensor elements with a scalar value
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
Float at(const std::vector< size_t > &pos) const
get element A[pos[0]][pos[1]]...
void diag(view1D< vector< Float >, Float > &vec) const
tensor_COO(const tensor::tensor_Dense< Float > &tens)
Create tensor_COO tensor from tensor_Dense tensor.
tensor_COO(const tensor_COO< Float > &coo)
Create tensor_COO tensor from tensor_COO tensor.
tensor_COO(const std::initializer_list< size_t > &shape_)
Initialize tensor_COO tensor.
void insert(const std::vector< size_t > &pos, const Float val)
insert element A[pos[0]][pos[1]]...
double get_data_size() const
Memory data space required by the matrix.
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