monolish  0.16.2
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  std::vector<Float> val;
46 
50  mutable bool gpu_status = false;
51 
52 public:
53  vector() {}
54 
55  // constructor ///////////////////////////////////////////////////////
64  vector(const size_t N);
65 
75  vector(const size_t N, const Float value);
76 
85  vector(const std::vector<Float> &vec);
86 
95  vector(const std::initializer_list<Float> &list);
96 
109  vector(const vector<Float> &vec);
110 
123  vector(const view1D<vector<Float>, Float> &vec);
124 
137  vector(const view1D<matrix::Dense<Float>, Float> &vec);
138 
148  vector(const Float *start, const Float *end);
149 
160  vector(const size_t N, const Float min, const Float max);
161 
174  vector(const size_t N, const Float min, const Float max,
175  const std::uint32_t seed);
176 
177  // communication
178  // ///////////////////////////////////////////////////////////////////////////
186  void send() const;
187 
195  void recv();
196 
204  void nonfree_recv();
205 
213  void device_free() const;
214 
223  [[nodiscard]] bool get_device_mem_stat() const { return gpu_status; }
224 
233  if (get_device_mem_stat()) {
234  device_free();
235  }
236  }
237 
238  [[nodiscard]] size_t get_offset() const { return 0; }
239 
240  // util
241  // ///////////////////////////////////////////////////////////////////////////
242 
249  [[nodiscard]] const Float *data() const { return val.data(); }
250 
257  [[nodiscard]] Float *data() { return val.data(); }
258 
267  void resize(size_t N) {
268  if (get_device_mem_stat()) {
269  throw std::runtime_error("Error, GPU vector cant use resize");
270  }
271  val.resize(N);
272  }
273 
280  void push_back(Float val) {
281  if (get_device_mem_stat()) {
282  throw std::runtime_error("Error, GPU vector cant use push_back");
283  }
284  val.push_back(val);
285  }
286 
293  [[nodiscard]] const Float *begin() const { return val.data(); }
294 
301  [[nodiscard]] Float *begin() { return val.data(); }
302 
309  [[nodiscard]] const Float *end() const { return val.data() + size(); }
310 
317  [[nodiscard]] Float *end() { return val.data() + size(); }
318 
325  [[nodiscard]] size_t size() const { return val.size(); }
326 
333  [[nodiscard]] size_t get_nnz() const { return val.size(); }
334 
343  void fill(Float value);
344 
353  void print_all(bool force_cpu = false) const;
354 
363  void print_all(std::string filename) const;
364 
365  // operator
366  // ///////////////////////////////////////////////////////////////////////////
367 
380  void operator=(const vector<Float> &vec);
381 
394  void operator=(const view1D<vector<Float>, Float> &vec);
395 
408  void operator=(const view1D<matrix::Dense<Float>, Float> &vec);
409 
419  void operator=(const std::vector<Float> &vec);
420 
429  [[nodiscard]] vector<Float> operator-();
430 
440  [[nodiscard]] Float &operator[](size_t i) {
441  if (get_device_mem_stat()) {
442  throw std::runtime_error("Error, GPU vector cant use operator[]");
443  }
444  return val[i];
445  }
446 
457  bool equal(const vector<Float> &vec,
458  bool compare_cpu_and_device = false) const;
459 
470  bool equal(const view1D<vector<Float>, Float> &vec,
471  bool compare_cpu_and_device = false) const;
472 
483  bool equal(const view1D<matrix::Dense<Float>, Float> &vec,
484  bool compare_cpu_and_device = false) const;
485 
497  bool operator==(const vector<Float> &vec) const;
498 
510  bool operator==(const view1D<vector<Float>, Float> &vec) const;
511 
523  bool operator==(const view1D<matrix::Dense<Float>, Float> &vec) const;
524 
536  bool operator!=(const vector<Float> &vec) const;
537 
549  bool operator!=(const view1D<vector<Float>, Float> &vec) const;
550 
562  bool operator!=(const view1D<matrix::Dense<Float>, Float> &vec) const;
563 };
565 } // 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
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 )
bool operator!=(const vector< Float > &vec) const
Comparing vectors (v != vec)
bool operator==(const view1D< vector< Float >, Float > &vec) const
Comparing vectors (v == vec)
void recv()
recv data from GPU, and free data on GPU
bool gpu_status
true: sended, false: not send
std::vector< Float > val
size N vector data
const Float * data() const
returns a direct pointer to the vector
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
size_t size() const
get vector size
bool operator==(const view1D< matrix::Dense< Float >, Float > &vec) const
Comparing vectors (v == vec)
size_t get_offset() const
Float * end()
returns a end iterator
vector< Float > operator-()
Sign inversion.
vector(const size_t N)
allocate size N vector
vector(const vector< Float > &vec)
copy from monolish::vector
void push_back(Float val)
Add a new element at the end of the vector (only CPU)
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 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 resize(size_t N)
resize vector (only CPU)
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
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