monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_view1D.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 #include "./monolish_logger.hpp"
10 #include <cassert>
11 #include <exception>
12 #include <fstream>
13 #include <iostream>
14 #include <iterator>
15 #include <omp.h>
16 #include <random>
17 #include <stdexcept>
18 #include <string>
19 #include <vector>
20 
21 #include <memory>
22 
23 #if defined USE_MPI
24 #include <mpi.h>
25 #endif
26 
27 namespace monolish {
28 template <typename Float> class vector;
29 
30 namespace matrix {
31 template <typename Float> class Dense;
32 template <typename Float> class CRS;
33 template <typename Float> class LinearOperator;
34 } // namespace matrix
35 
42 template <typename TYPE, typename Float> class view1D {
43 private:
44  TYPE &target;
45  Float *target_data;
46  size_t first;
47  size_t last;
48  size_t range;
49 
50 public:
61  view1D(vector<Float> &x, const size_t start, const size_t end) : target(x) {
62  first = start;
63  last = end;
64  range = last - first;
65  target_data = x.data();
66  }
67 
78  view1D(matrix::Dense<Float> &A, const size_t start, const size_t end)
79  : target(A) {
80  first = start;
81  last = end;
82  range = last - first;
83  target_data = A.val.data();
84  }
85 
96  view1D(view1D<vector<Float>, Float> &x, const size_t start, const size_t end)
97  : target(x) {
98  first = x.get_first() + start;
99  last = x.get_last() + end;
100  range = last - first;
101  target_data = x.data();
102  }
103 
114  view1D(view1D<matrix::Dense<Float>, Float> &x, const size_t start,
115  const size_t end)
116  : target(x) {
117  first = x.get_first() + start;
118  last = x.get_last() + end;
119  range = last - first;
120  target_data = x.data();
121  }
122 
129  size_t size() const { return range; }
130 
137  size_t get_nnz() const { return range; }
138 
145  size_t get_first() const { return first; }
146 
153  size_t get_last() const { return last; }
154 
161  size_t get_offset() const { return first; }
162 
168  void set_first(size_t i) { first = i; }
169 
175  void set_last(size_t i) {
176  assert(first + i <= target.get_nnz());
177  last = i;
178  }
179 
188  size_t get_device_mem_stat() const { return target.get_device_mem_stat(); }
189 
197  Float *data() const { return target_data; }
198 
205  Float *data() { return target_data; }
206 
213  Float *begin() const { return target_data + get_offset(); }
214 
221  Float *begin() { return target_data + get_offset(); }
222 
229  Float *end() const { return target_data + range; }
230 
237  Float *end() { return target_data + range; }
238 
247  void print_all(bool force_cpu = false) const;
248 
259  void resize(size_t N) {
260  assert(first + N <= target.get_nnz());
261  last = first + N;
262  }
263 
273  Float &operator[](const size_t i) {
274  if (target.get_device_mem_stat()) {
275  throw std::runtime_error("Error, GPU vector cant use operator[]");
276  }
277  return target_data[i + first];
278  }
279 
288  void fill(Float value);
289 };
290 
291 } // namespace monolish
monolish::view1D::size
size_t size() const
get view1D size (end-start)
Definition: monolish_view1D.hpp:129
monolish::view1D::view1D
view1D(matrix::Dense< Float > &A, const size_t start, const size_t end)
create view1D(start:end) from Dense matrix
Definition: monolish_view1D.hpp:78
monolish::view1D::end
Float * end() const
returns a end iterator
Definition: monolish_view1D.hpp:229
monolish::view1D::first
size_t first
Definition: monolish_view1D.hpp:46
monolish::view1D::target
TYPE & target
Definition: monolish_view1D.hpp:44
monolish::view1D::get_device_mem_stat
size_t get_device_mem_stat() const
true: sended, false: not send
Definition: monolish_view1D.hpp:188
monolish::matrix::Dense::val
std::vector< Float > val
Dense format value(size M x N)
Definition: monolish_dense.hpp:47
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::view1D::view1D
view1D(view1D< vector< Float >, Float > &x, const size_t start, const size_t end)
create view1D(start:end) from monolish::vector
Definition: monolish_view1D.hpp:96
monolish::vector::data
const Float * data() const
returns a direct pointer to the vector
Definition: monolish_vector.hpp:230
monolish::view1D::fill
void fill(Float value)
fill vector elements with a scalar value
Definition: fill_view1D.cpp:9
monolish::view1D::last
size_t last
Definition: monolish_view1D.hpp:47
monolish::view1D::resize
void resize(size_t N)
change last postion
Definition: monolish_view1D.hpp:259
monolish::view1D::get_first
size_t get_first() const
get first position
Definition: monolish_view1D.hpp:145
monolish::view1D::get_offset
size_t get_offset() const
get first position (same as get_first())
Definition: monolish_view1D.hpp:161
monolish::view1D::data
Float * data() const
returns a direct pointer to the original vector (dont include offset)
Definition: monolish_view1D.hpp:197
monolish
Definition: monolish_matrix_blas.hpp:9
monolish_logger.hpp
monolish::view1D::set_first
void set_first(size_t i)
change first position
Definition: monolish_view1D.hpp:168
monolish::view1D::view1D
view1D(view1D< matrix::Dense< Float >, Float > &x, const size_t start, const size_t end)
create view1D(start:end) from monolish::matrix::Dense
Definition: monolish_view1D.hpp:114
monolish::view1D::begin
Float * begin() const
returns begin iterator (include offset)
Definition: monolish_view1D.hpp:213
monolish::view1D::view1D
view1D(vector< Float > &x, const size_t start, const size_t end)
create view1D(start:end) from vector
Definition: monolish_view1D.hpp:61
monolish::view1D::print_all
void print_all(bool force_cpu=false) const
print all elements to standart I/O
monolish::view1D::get_last
size_t get_last() const
get end position
Definition: monolish_view1D.hpp:153
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::view1D::get_nnz
size_t get_nnz() const
get view1D size (same as size())
Definition: monolish_view1D.hpp:137
monolish::view1D::range
size_t range
Definition: monolish_view1D.hpp:48
monolish::view1D::target_data
Float * target_data
Definition: monolish_view1D.hpp:45
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::view1D::data
Float * data()
returns a direct pointer to the vector (dont include offset)
Definition: monolish_view1D.hpp:205
monolish::view1D::set_last
void set_last(size_t i)
change last position
Definition: monolish_view1D.hpp:175
monolish::view1D::begin
Float * begin()
returns begin iterator (include offset)
Definition: monolish_view1D.hpp:221
monolish::view1D::operator[]
Float & operator[](const size_t i)
refetrence to the element at position
Definition: monolish_view1D.hpp:273
monolish::view1D::end
Float * end()
returns a end iterator
Definition: monolish_view1D.hpp:237