monolish  0.17.1
MONOlithic LInear equation Solvers for Highly-parallel architecture
monolish_view1D.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "./monolish_logger.hpp"
3 #include <cassert>
4 #include <exception>
5 #include <fstream>
6 #include <iostream>
7 #include <iterator>
8 #include <memory>
9 #include <omp.h>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 
14 #if USE_SXAT
15 #undef _HAS_CPP17
16 #endif
17 #include <random>
18 #if USE_SXAT
19 #define _HAS_CPP17 1
20 #endif
21 
22 #if defined USE_MPI
23 #include <mpi.h>
24 #endif
25 
26 namespace monolish {
27 template <typename Float> class vector;
28 
29 namespace matrix {
30 template <typename Float> class Dense;
31 template <typename Float> class CRS;
32 template <typename Float> class LinearOperator;
33 } // namespace matrix
34 
46 template <typename TYPE, typename Float> class view1D {
47 private:
48  TYPE &target;
49  Float *target_data;
50  size_t first;
51  size_t last;
52  size_t range;
53 
54 public:
65  view1D(vector<Float> &x, const size_t start, const size_t end) : target(x) {
66  first = start;
67  last = end;
68  range = last - first;
69  target_data = x.data();
70  }
71 
82  view1D(matrix::Dense<Float> &A, const size_t start, const size_t end)
83  : target(A) {
84  first = start;
85  last = end;
86  range = last - first;
87  target_data = A.data();
88  }
89 
100  view1D(tensor::tensor_Dense<Float> &A, const size_t start, const size_t end)
101  : target(A) {
102  first = start;
103  last = end;
104  range = last - first;
105  target_data = A.data();
106  }
107 
118  view1D(view1D<vector<Float>, Float> &x, const size_t start, const size_t end)
119  : target(x) {
120  first = x.get_first() + start;
121  last = x.get_last() + end;
122  range = last - first;
123  target_data = x.data();
124  }
125 
136  view1D(view1D<matrix::Dense<Float>, Float> &x, const size_t start,
137  const size_t end)
138  : target(x) {
139  first = x.get_first() + start;
140  last = x.get_last() + end;
141  range = last - first;
142  target_data = x.data();
143  }
144 
155  view1D(view1D<tensor::tensor_Dense<Float>, Float> &x, const size_t start,
156  const size_t end)
157  : target(x) {
158  first = x.get_first() + start;
159  last = x.get_last() + end;
160  range = last - first;
161  target_data = x.data();
162  }
163 
170  [[nodiscard]] size_t size() const { return range; }
171 
178  [[nodiscard]] size_t get_nnz() const { return range; }
179 
186  [[nodiscard]] size_t get_first() const { return first; }
187 
194  [[nodiscard]] size_t get_last() const { return last; }
195 
202  [[nodiscard]] size_t get_offset() const { return first; }
203 
209  void set_first(size_t i) { first = i; }
210 
216  void set_last(size_t i) {
217  assert(first + i <= target.get_nnz());
218  last = i;
219  }
220 
229  [[nodiscard]] size_t get_device_mem_stat() const {
230  return target.get_device_mem_stat();
231  }
232 
240  [[nodiscard]] Float *data() const { return target_data; }
241 
248  [[nodiscard]] Float *data() { return target_data; }
249 
256  [[nodiscard]] Float *begin() const { return target_data + get_offset(); }
257 
264  [[nodiscard]] Float *begin() { return target_data + get_offset(); }
265 
272  [[nodiscard]] Float *end() const { return target_data + range; }
273 
280  [[nodiscard]] Float *end() { return target_data + range; }
281 
290  void print_all(bool force_cpu = false) const;
291 
302  void resize(size_t N) {
303  assert(first + N <= target.get_nnz());
304  last = first + N;
305  }
306 
316  [[nodiscard]] Float &operator[](const size_t i) {
317  if (target.get_device_mem_stat()) {
318  throw std::runtime_error("Error, GPU vector cant use operator[]");
319  }
320  return target_data[i + first];
321  }
322 
331  void fill(Float value);
332 };
335 } // namespace monolish
Dense format Matrix.
const Float * data() const
returns a direct pointer to the matrix
const Float * data() const
returns a direct pointer to the tensor
const Float * data() const
returns a direct pointer to the vector
size_t get_last() const
get end position
Float * data()
returns a direct pointer to the vector (dont include offset)
Float * end() const
returns a end iterator
view1D(view1D< matrix::Dense< Float >, Float > &x, const size_t start, const size_t end)
create view1D(start:end) from monolish::matrix::Dense
Float * begin()
returns begin iterator (include offset)
view1D(view1D< vector< Float >, Float > &x, const size_t start, const size_t end)
create view1D(start:end) from monolish::vector
view1D(tensor::tensor_Dense< Float > &A, const size_t start, const size_t end)
create view1D(start:end) from Dense tensor
view1D(matrix::Dense< Float > &A, const size_t start, const size_t end)
create view1D(start:end) from Dense matrix
void fill(Float value)
fill vector elements with a scalar value
Float * data() const
returns a direct pointer to the original vector (dont include offset)
void print_all(bool force_cpu=false) const
print all elements to standart I/O
size_t get_device_mem_stat() const
true: sended, false: not send
size_t get_nnz() const
get view1D size (same as size())
Float & operator[](const size_t i)
reference to the element at position
void set_last(size_t i)
change last position
view1D(view1D< tensor::tensor_Dense< Float >, Float > &x, const size_t start, const size_t end)
create view1D(start:end) from monolish::tensor::tensor_Dense
size_t get_first() const
get first position
size_t size() const
get view1D size (end-start)
Float * end()
returns a end iterator
void set_first(size_t i)
change first position
size_t get_offset() const
get first position (same as get_first())
view1D(vector< Float > &x, const size_t start, const size_t end)
create view1D(start:end) from vector
Float * begin() const
returns begin iterator (include offset)
void resize(size_t N)
change last postion
monolish namespaces