monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_vector.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 #include "./monolish_dense.hpp"
10 #include "./monolish_logger.hpp"
11 #include "./monolish_view1D.hpp"
12 #include <exception>
13 #include <fstream>
14 #include <iostream>
15 #include <iterator>
16 #include <omp.h>
17 #include <random>
18 #include <stdexcept>
19 #include <string>
20 #include <vector>
21 
22 #include <memory>
23 
24 #if defined USE_MPI
25 #include <mpi.h>
26 #endif
27 
28 namespace monolish {
29 
36 template <typename Float> class vector {
37 private:
41  std::vector<Float> val;
42 
46  mutable bool gpu_status = false;
47 
48 public:
49  vector() {}
50 
51  // constructor ///////////////////////////////////////////////////////
60  vector(const size_t N);
61 
71  vector(const size_t N, const Float value);
72 
81  vector(const std::vector<Float> &vec);
82 
91  vector(const std::initializer_list<Float> &list);
92 
105  vector(const vector<Float> &vec);
106 
119  vector(const view1D<vector<Float>, Float> &vec);
120 
133  vector(const view1D<matrix::Dense<Float>, Float> &vec);
134 
144  vector(const Float *start, const Float *end);
145 
156  vector(const size_t N, const Float min, const Float max);
157 
158  // communication
159  // ///////////////////////////////////////////////////////////////////////////
167  void send() const;
168 
176  void recv();
177 
185  void nonfree_recv();
186 
194  void device_free() const;
195 
204  bool get_device_mem_stat() const { return gpu_status; }
205 
214  if (get_device_mem_stat()) {
215  device_free();
216  }
217  }
218 
219  size_t get_offset() const { return 0; }
220 
221  // util
222  // ///////////////////////////////////////////////////////////////////////////
223 
230  const Float *data() const { return val.data(); }
231 
238  Float *data() { return val.data(); }
239 
248  void resize(size_t N) {
249  if (get_device_mem_stat()) {
250  throw std::runtime_error("Error, GPU vector cant use resize");
251  }
252  val.resize(N);
253  }
254 
261  void push_back(Float val) {
262  if (get_device_mem_stat()) {
263  throw std::runtime_error("Error, GPU vector cant use push_back");
264  }
265  val.push_back(val);
266  }
267 
274  const Float *begin() const { return val.data(); }
275 
282  Float *begin() { return val.data(); }
283 
290  const Float *end() const { return val.data() + size(); }
291 
298  Float *end() { return val.data() + size(); }
299 
306  auto size() const { return val.size(); }
307 
314  auto get_nnz() const { return val.size(); }
315 
324  void fill(Float value);
325 
334  void print_all(bool force_cpu = false) const;
335 
344  void print_all(std::string filename) const;
345 
346  // operator
347  // ///////////////////////////////////////////////////////////////////////////
348 
361  void operator=(const vector<Float> &vec);
362 
375  void operator=(const view1D<vector<Float>, Float> &vec);
376 
389  void operator=(const view1D<matrix::Dense<Float>, Float> &vec);
390 
400  void operator=(const std::vector<Float> &vec);
401 
411 
421  Float &operator[](size_t i) {
422  if (get_device_mem_stat()) {
423  throw std::runtime_error("Error, GPU vector cant use operator[]");
424  }
425  return val[i];
426  }
427 
438  bool equal(const vector<Float> &vec,
439  bool compare_cpu_and_device = false) const;
440 
451  bool equal(const view1D<vector<Float>, Float> &vec,
452  bool compare_cpu_and_device = false) const;
453 
464  bool equal(const view1D<matrix::Dense<Float>, Float> &vec,
465  bool compare_cpu_and_device = false) const;
466 
478  bool operator==(const vector<Float> &vec) const;
479 
491  bool operator==(const view1D<vector<Float>, Float> &vec) const;
492 
504  bool operator==(const view1D<matrix::Dense<Float>, Float> &vec) const;
505 
517  bool operator!=(const vector<Float> &vec) const;
518 
530  bool operator!=(const view1D<vector<Float>, Float> &vec) const;
531 
543  bool operator!=(const view1D<matrix::Dense<Float>, Float> &vec) const;
544 };
545 } // namespace monolish
monolish::vector::vector
vector()
Definition: monolish_vector.hpp:49
monolish::vml::min
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],...
Definition: matrix_vml.cpp:390
monolish::vector::val
std::vector< Float > val
size N vector data
Definition: monolish_vector.hpp:41
monolish::vector::data
Float * data()
returns a direct pointer to the vector
Definition: monolish_vector.hpp:238
monolish::vector::get_offset
size_t get_offset() const
Definition: monolish_vector.hpp:219
monolish::vector::operator-
vector< Float > operator-()
Sign inversion.
Definition: vector_minus.cpp:8
monolish::vml::max
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],...
Definition: matrix_vml.cpp:382
monolish::vector::end
const Float * end() const
returns a end iterator
Definition: monolish_vector.hpp:290
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::vector::size
auto size() const
get vector size
Definition: monolish_vector.hpp:306
monolish::vector::get_nnz
auto get_nnz() const
get vector size
Definition: monolish_vector.hpp:314
monolish::vector::end
Float * end()
returns a end iterator
Definition: monolish_vector.hpp:298
monolish::vector::data
const Float * data() const
returns a direct pointer to the vector
Definition: monolish_vector.hpp:230
monolish_view1D.hpp
monolish::vector::operator=
void operator=(const vector< Float > &vec)
copy vector, It is same as copy ( Copy the memory on CPU and GPU )
monolish::vector::operator[]
Float & operator[](size_t i)
refetrence to the element at position (v[i])
Definition: monolish_vector.hpp:421
monolish::vector::push_back
void push_back(Float val)
Add a new element at the end of the vector (only CPU)
Definition: monolish_vector.hpp:261
monolish::vector::nonfree_recv
void nonfree_recv()
recv data from GPU (w/o free)
Definition: gpu_comm.cpp:45
monolish::vector::~vector
~vector()
destructor of vector, free GPU memory
Definition: monolish_vector.hpp:213
monolish
Definition: monolish_matrix_blas.hpp:9
monolish_logger.hpp
monolish::vector::print_all
void print_all(bool force_cpu=false) const
print all elements to standart I/O
Definition: IO_vector.cpp:17
monolish::vector::get_device_mem_stat
bool get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_vector.hpp:204
monolish_dense.hpp
monolish::vector::begin
const Float * begin() const
returns a begin iterator
Definition: monolish_vector.hpp:274
monolish::vector::operator!=
bool operator!=(const vector< Float > &vec) const
Comparing vectors (v != vec)
monolish::vector::fill
void fill(Float value)
fill vector elements with a scalar value
Definition: fill_vector.cpp:9
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::vector::begin
Float * begin()
returns a begin iterator
Definition: monolish_vector.hpp:282
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::vector::operator==
bool operator==(const vector< Float > &vec) const
Comparing vectors (v == vec)
monolish::vector::device_free
void device_free() const
free data on GPU
Definition: gpu_comm.cpp:60
monolish::vector::send
void send() const
send data to GPU
Definition: gpu_comm.cpp:8
monolish::vector::equal
bool equal(const vector< Float > &vec, bool compare_cpu_and_device=false) const
Comparing matricies (A == mat)
monolish::vector::resize
void resize(size_t N)
resize vector (only CPU)
Definition: monolish_vector.hpp:248
monolish::vector::recv
void recv()
recv data from GPU, and free data on GPU
Definition: gpu_comm.cpp:27
monolish::vector::gpu_status
bool gpu_status
true: sended, false: not send
Definition: monolish_vector.hpp:46