monolish  0.17.3-dev.16
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_vector.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "./monolish_dense.hpp"
3 #include "./monolish_logger.hpp"
4 #include "./monolish_view1D.hpp"
5 #include <exception>
6 #include <fstream>
7 #include <iostream>
8 #include <iterator>
9 #include <memory>
10 #include <omp.h>
11 #include <stdexcept>
12 #include <string>
13 #include <vector>
14 
15 #if USE_SXAT
16 #undef _HAS_CPP17
17 #endif
18 #include <random>
19 #if USE_SXAT
20 #define _HAS_CPP17 1
21 #endif
22 
23 #if defined USE_MPI
24 #include <mpi.h>
25 #endif
26 
27 namespace monolish {
28 template <typename TYPE, typename Float> class view1D;
29 template <typename TYPE, typename Float> class view_Dense;
30 template <typename TYPE, typename Float> class view_tensor_Dense;
31 
32 namespace tensor {
33 template <typename Float> class tensor_Dense;
34 }
35 
47 template <typename Float> class vector {
48 private:
52  mutable std::shared_ptr<bool> gpu_status = std::make_shared<bool>(false);
53 
57  size_t first = 0;
58 
59 public:
63  std::shared_ptr<Float> val;
64 
68  std::size_t val_nnz = 0;
69 
73  std::size_t alloc_nnz = 0;
74 
78  bool val_create_flag = false;
79 
80  vector() { val_create_flag = true; }
81 
82  // constructor ///////////////////////////////////////////////////////
91  vector(const size_t N);
92 
102  vector(const size_t N, const Float value);
103 
112  vector(const std::vector<Float> &vec);
113 
122  vector(const std::initializer_list<Float> &list);
123 
136  vector(const vector<Float> &vec);
137 
150  vector(const view1D<vector<Float>, Float> &vec);
151 
164  vector(const view1D<matrix::Dense<Float>, Float> &vec);
165 
179 
189  vector(const Float *start, const Float *end);
190 
201  vector(const size_t N, const Float min, const Float max);
202 
215  vector(const size_t N, const Float min, const Float max,
216  const std::uint32_t seed);
217 
225  [[nodiscard]] std::string type() const { return "vector"; }
226 
227  // communication
228  // ///////////////////////////////////////////////////////////////////////////
236  void send() const;
237 
245  void recv();
246 
254  void nonfree_recv();
255 
263  void device_free() const;
264 
273  [[nodiscard]] bool get_device_mem_stat() const { return *gpu_status; }
274 
279  [[nodiscard]] std::shared_ptr<bool> get_gpu_status() const {
280  return gpu_status;
281  }
282 
291  if (val_create_flag) {
292  if (get_device_mem_stat()) {
293  device_free();
294  }
295  }
296  }
297 
298  // util
299  // ///////////////////////////////////////////////////////////////////////////
300 
307  [[nodiscard]] const Float *data() const { return val.get(); }
308 
315  [[nodiscard]] Float *data() { return val.get(); }
316 
323  [[nodiscard]] const Float *begin() const { return data() + get_offset(); }
324 
331  [[nodiscard]] Float *begin() { return data() + get_offset(); }
332 
339  [[nodiscard]] const Float *end() const {
340  return data() + get_offset() + get_nnz();
341  }
342 
349  [[nodiscard]] Float *end() { return data() + get_offset() + get_nnz(); }
350 
357  [[nodiscard]] size_t size() const { return val_nnz; }
358 
365  [[nodiscard]] size_t get_nnz() const { return val_nnz; }
366 
374  [[nodiscard]] size_t get_alloc_nnz() const { return alloc_nnz; }
375 
382  [[nodiscard]] size_t get_first() const { return first; }
383 
390  [[nodiscard]] size_t get_offset() const { return get_first(); }
391 
397  void set_first(size_t i) { first = i; }
398 
407  void fill(Float value);
408 
417  void print_all(bool force_cpu = false) const;
418 
427  void print_all(std::string filename) const;
428 
437  void resize(size_t N, Float Val = 0) {
438  if (get_device_mem_stat()) {
439  throw std::runtime_error("Error, GPU vector cant use resize");
440  }
441  if (val_create_flag) {
442  std::shared_ptr<Float> tmp(new Float[N], std::default_delete<Float[]>());
443  size_t copy_size = std::min(val_nnz, N);
444  for (size_t i = 0; i < copy_size; i++) {
445  tmp.get()[i] = data()[i];
446  }
447  for (size_t i = copy_size; i < N; i++) {
448  tmp.get()[i] = Val;
449  }
450  val = tmp;
451  alloc_nnz = N;
452  val_nnz = N;
453  } else {
454  throw std::runtime_error("Error, not create vector cant use resize");
455  }
456  }
457 
464  void push_back(Float Val) {
465  if (get_device_mem_stat()) {
466  throw std::runtime_error("Error, GPU vector cant use push_back");
467  }
468  if (val_create_flag) {
469  if (val_nnz >= alloc_nnz) {
470  size_t tmp = val_nnz;
471  alloc_nnz = 2 * alloc_nnz + 1;
472  resize(alloc_nnz);
473  val_nnz = tmp;
474  }
475  data()[val_nnz] = Val;
476  val_nnz++;
477  } else {
478  throw std::runtime_error("Error, not create vector cant use push_back");
479  }
480  }
481 
482  void move(const tensor::tensor_Dense<Float> &tensor_dense);
483 
484  void move(const tensor::tensor_Dense<Float> &tensor_dense, int N);
485 
486  void move(const view_tensor_Dense<vector<Float>, Float> &tensor_dense);
487 
488  void move(const view_tensor_Dense<matrix::Dense<Float>, Float> &tensor_dense);
489 
491  &tensor_dense);
492 
493  void move(const view_tensor_Dense<vector<Float>, Float> &tensor_dense, int N);
494 
495  void move(const view_tensor_Dense<matrix::Dense<Float>, Float> &tensor_dense,
496  int N);
497 
498  void move(
499  const view_tensor_Dense<tensor::tensor_Dense<Float>, Float> &tensor_dense,
500  int N);
501 
502  // operator
503  // ///////////////////////////////////////////////////////////////////////////
504 
517  void operator=(const vector<Float> &vec);
518 
531  void operator=(const view1D<vector<Float>, Float> &vec);
532 
545  void operator=(const view1D<matrix::Dense<Float>, Float> &vec);
546 
559  void operator=(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
560 
570  void operator=(const std::vector<Float> &vec);
571 
580  [[nodiscard]] vector<Float> operator-();
581 
591  [[nodiscard]] Float &operator[](size_t i) {
592  if (get_device_mem_stat()) {
593  throw std::runtime_error("Error, GPU vector cant use operator[]");
594  }
595  return data()[first + i];
596  }
597 
608  bool equal(const vector<Float> &vec,
609  bool compare_cpu_and_device = false) const;
610 
621  bool equal(const view1D<vector<Float>, Float> &vec,
622  bool compare_cpu_and_device = false) const;
623 
634  bool equal(const view1D<matrix::Dense<Float>, Float> &vec,
635  bool compare_cpu_and_device = false) const;
636 
647  bool equal(const view1D<tensor::tensor_Dense<Float>, Float> &vec,
648  bool compare_cpu_and_device = false) const;
649 
661  bool operator==(const vector<Float> &vec) const;
662 
674  bool operator==(const view1D<vector<Float>, Float> &vec) const;
675 
687  bool operator==(const view1D<matrix::Dense<Float>, Float> &vec) const;
688 
700  bool operator==(const view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
701 
713  bool operator!=(const vector<Float> &vec) const;
714 
726  bool operator!=(const view1D<vector<Float>, Float> &vec) const;
727 
739  bool operator!=(const view1D<matrix::Dense<Float>, Float> &vec) const;
740 
752  bool operator!=(const view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
753 };
755 } // namespace monolish
Dense format Matrix.
vector(const view1D< matrix::Dense< Float >, Float > &vec)
copy from monolish::view1D from monolish::matrix::Dense
void set_first(size_t i)
change first position
void send() const
send data to GPU
Float & operator[](size_t i)
reference to the element at position (v[i])
vector(const size_t N, const Float min, const Float max, const std::uint32_t seed)
create N length rand(min~max) vector with random seed
void operator=(const std::vector< Float > &vec)
copy vector from std::vector
void resize(size_t N, Float Val=0)
resize vector (only CPU)
void move(const view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &tensor_dense, int N)
vector(const std::vector< Float > &vec)
copy from std::vector
void operator=(const vector< Float > &vec)
copy vector, It is same as copy ( Copy the memory on CPU and GPU )
void move(const tensor::tensor_Dense< Float > &tensor_dense, int N)
void move(const view_tensor_Dense< vector< Float >, Float > &tensor_dense)
bool operator!=(const vector< Float > &vec) const
Comparing vectors (v != vec)
void move(const tensor::tensor_Dense< Float > &tensor_dense)
bool operator==(const view1D< vector< Float >, Float > &vec) const
Comparing vectors (v == vec)
void recv()
recv data from GPU, and free data on GPU
void operator=(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
copy vector, It is same as copy ( Copy the memory on CPU and GPU )
const Float * data() const
returns a direct pointer to the vector
std::shared_ptr< Float > val
vector data (pointer)
void move(const view_tensor_Dense< matrix::Dense< Float >, Float > &tensor_dense, int N)
void move(const view_tensor_Dense< matrix::Dense< Float >, Float > &tensor_dense)
void fill(Float value)
fill vector elements with a scalar value
const Float * end() const
returns a end iterator
void operator=(const view1D< matrix::Dense< Float >, Float > &vec)
copy vector, It is same as copy ( Copy the memory on CPU and GPU )
vector(const std::initializer_list< Float > &list)
copy from initializer_list
bool operator==(const view1D< tensor::tensor_Dense< Float >, Float > &vec) const
Comparing vectors (v == vec)
size_t size() const
get vector size
bool operator==(const view1D< matrix::Dense< Float >, Float > &vec) const
Comparing vectors (v == vec)
bool operator!=(const view1D< tensor::tensor_Dense< Float >, Float > &vec) const
Comparing vectors (v != vec)
std::string type() const
get format name "vector"
void move(const view_tensor_Dense< vector< Float >, Float > &tensor_dense, int N)
size_t get_first() const
get first position
size_t get_offset() const
get first position (same as get_first())
Float * end()
returns a end iterator
vector(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
copy from monolish::view1D from monolish::tensor::tensor_Dense
bool val_create_flag
vector create flag;
vector< Float > operator-()
Sign inversion.
vector(const size_t N)
allocate size N vector
vector(const vector< Float > &vec)
copy from monolish::vector
std::size_t val_nnz
vector size
bool equal(const view1D< tensor::tensor_Dense< Float >, Float > &vec, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
vector(const Float *start, const Float *end)
copy from pointer
size_t first
first position of data array
Float * begin()
returns a begin iterator
Float * data()
returns a direct pointer to the vector
void push_back(Float Val)
Add a new element at the end of the vector (only CPU)
void nonfree_recv()
recv data from GPU (w/o free)
vector(const size_t N, const Float min, const Float max)
create N length rand(min~max) vector
bool equal(const view1D< vector< Float >, Float > &vec, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
void print_all(std::string filename) const
print all elements to file
~vector()
destructor of vector, free GPU memory
vector(const view1D< vector< Float >, Float > &vec)
copy from monolish::view1D from vector
bool operator==(const vector< Float > &vec) const
Comparing vectors (v == vec)
void print_all(bool force_cpu=false) const
print all elements to standart I/O
bool equal(const view1D< matrix::Dense< Float >, Float > &vec, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
void operator=(const view1D< vector< Float >, Float > &vec)
copy vector, It is same as copy ( Copy the memory on CPU and GPU )
const Float * begin() const
returns a begin iterator
bool get_device_mem_stat() const
true: sended, false: not send
std::shared_ptr< bool > get_gpu_status() const
gpu status shared pointer
std::size_t alloc_nnz
alloced vector size
size_t get_alloc_nnz() const
get # of alloced non-zeros
void move(const view_tensor_Dense< tensor::tensor_Dense< Float >, Float > &tensor_dense)
bool equal(const vector< Float > &vec, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
bool operator!=(const view1D< vector< Float >, Float > &vec) const
Comparing vectors (v != vec)
void device_free() const
free data on GPU
bool operator!=(const view1D< matrix::Dense< Float >, Float > &vec) const
Comparing vectors (v != vec)
std::shared_ptr< bool > gpu_status
true: sended, false: not send
vector(const size_t N, const Float value)
initialize size N vector, value to fill the container
size_t get_nnz() const
get vector size
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