monolish  0.17.1
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 
40 template <typename Float> class vector {
41 private:
45  mutable bool gpu_status = false;
46 
47 public:
51  std::shared_ptr<Float> val;
52 
56  std::size_t val_nnz = 0;
57 
61  std::size_t alloc_nnz = 0;
62 
66  bool val_create_flag = false;
67 
68  vector() { val_create_flag = true; }
69 
70  // constructor ///////////////////////////////////////////////////////
79  vector(const size_t N);
80 
90  vector(const size_t N, const Float value);
91 
100  vector(const std::vector<Float> &vec);
101 
110  vector(const std::initializer_list<Float> &list);
111 
124  vector(const vector<Float> &vec);
125 
138  vector(const view1D<vector<Float>, Float> &vec);
139 
152  vector(const view1D<matrix::Dense<Float>, Float> &vec);
153 
167 
177  vector(const Float *start, const Float *end);
178 
189  vector(const size_t N, const Float min, const Float max);
190 
203  vector(const size_t N, const Float min, const Float max,
204  const std::uint32_t seed);
205 
206  // communication
207  // ///////////////////////////////////////////////////////////////////////////
215  void send() const;
216 
224  void recv();
225 
233  void nonfree_recv();
234 
242  void device_free() const;
243 
252  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
253 
262  if (val_create_flag) {
263  if (get_device_mem_stat()) {
264  device_free();
265  }
266  }
267  }
268 
269  [[nodiscard]] size_t get_offset() const { return 0; }
270 
271  // util
272  // ///////////////////////////////////////////////////////////////////////////
273 
280  [[nodiscard]] const Float *data() const { return val.get(); }
281 
288  [[nodiscard]] Float *data() { return val.get(); }
289 
298  void resize(size_t N, Float Val = 0) {
299  if (get_device_mem_stat()) {
300  throw std::runtime_error("Error, GPU vector cant use resize");
301  }
302  if (val_create_flag) {
303  std::shared_ptr<Float> tmp(new Float[N], std::default_delete<Float[]>());
304  size_t copy_size = std::min(val_nnz, N);
305  for (size_t i = 0; i < copy_size; i++) {
306  tmp.get()[i] = data()[i];
307  }
308  for (size_t i = copy_size; i < N; i++) {
309  tmp.get()[i] = Val;
310  }
311  val = tmp;
312  alloc_nnz = N;
313  val_nnz = N;
314  } else {
315  throw std::runtime_error("Error, not create vector cant use resize");
316  }
317  }
318 
325  void push_back(Float Val) {
326  if (get_device_mem_stat()) {
327  throw std::runtime_error("Error, GPU vector cant use push_back");
328  }
329  if (val_create_flag) {
330  if (val_nnz >= alloc_nnz) {
331  size_t tmp = val_nnz;
332  alloc_nnz = 2 * alloc_nnz + 1;
333  resize(alloc_nnz);
334  val_nnz = tmp;
335  }
336  data()[val_nnz] = Val;
337  val_nnz++;
338  } else {
339  throw std::runtime_error("Error, not create vector cant use push_back");
340  }
341  }
342 
343  void move(const tensor::tensor_Dense<Float> &tensor_dense);
344 
345  void move(const tensor::tensor_Dense<Float> &tensor_dense, int N);
346 
353  [[nodiscard]] const Float *begin() const { return data(); }
354 
361  [[nodiscard]] Float *begin() { return data(); }
362 
369  [[nodiscard]] const Float *end() const { return data() + size(); }
370 
377  [[nodiscard]] Float *end() { return data() + size(); }
378 
385  [[nodiscard]] size_t size() const { return val_nnz; }
386 
393  [[nodiscard]] size_t get_nnz() const { return val_nnz; }
394 
403  void fill(Float value);
404 
413  void print_all(bool force_cpu = false) const;
414 
423  void print_all(std::string filename) const;
424 
425  // operator
426  // ///////////////////////////////////////////////////////////////////////////
427 
440  void operator=(const vector<Float> &vec);
441 
454  void operator=(const view1D<vector<Float>, Float> &vec);
455 
468  void operator=(const view1D<matrix::Dense<Float>, Float> &vec);
469 
482  void operator=(const view1D<tensor::tensor_Dense<Float>, Float> &vec);
483 
493  void operator=(const std::vector<Float> &vec);
494 
503  [[nodiscard]] vector<Float> operator-();
504 
514  [[nodiscard]] Float &operator[](size_t i) {
515  if (get_device_mem_stat()) {
516  throw std::runtime_error("Error, GPU vector cant use operator[]");
517  }
518  return data()[i];
519  }
520 
531  bool equal(const vector<Float> &vec,
532  bool compare_cpu_and_device = false) const;
533 
544  bool equal(const view1D<vector<Float>, Float> &vec,
545  bool compare_cpu_and_device = false) const;
546 
557  bool equal(const view1D<matrix::Dense<Float>, Float> &vec,
558  bool compare_cpu_and_device = false) const;
559 
570  bool equal(const view1D<tensor::tensor_Dense<Float>, Float> &vec,
571  bool compare_cpu_and_device = false) const;
572 
584  bool operator==(const vector<Float> &vec) const;
585 
597  bool operator==(const view1D<vector<Float>, Float> &vec) const;
598 
610  bool operator==(const view1D<matrix::Dense<Float>, Float> &vec) const;
611 
623  bool operator==(const view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
624 
636  bool operator!=(const vector<Float> &vec) const;
637 
649  bool operator!=(const view1D<vector<Float>, Float> &vec) const;
650 
662  bool operator!=(const view1D<matrix::Dense<Float>, Float> &vec) const;
663 
675  bool operator!=(const view1D<tensor::tensor_Dense<Float>, Float> &vec) const;
676 };
678 } // namespace monolish
Dense format Matrix.
vector(const view1D< matrix::Dense< Float >, Float > &vec)
copy from monolish::view1D from monolish::matrix::Dense
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)
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)
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 )
bool gpu_status
true: sended, false: not send
const Float * data() const
returns a direct pointer to the vector
std::shared_ptr< Float > val
vector data (pointer)
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)
size_t get_offset() const
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
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::size_t alloc_nnz
alloced vector size
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)
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