Flow123d  DF_patch_fe_data_tables-6bd0dc8
arena_vec.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file arena_vec.hh
15  */
16 
17 #ifndef ARENA_VEC_HH_
18 #define ARENA_VEC_HH_
19 
20 #include "fem/arena_resource.hh"
21 #include "system/asserts.hh"
22 
23 #include <Eigen/Core>
24 #include <Eigen/Dense>
25 
26 
27 
28 template<class T> class ArenaOVec;
29 
30 
31 /**
32  * Define vector allocated in Arena and aligned to SIMD size.
33  */
34 template<class T>
35 class ArenaVec {
36 public:
37  /// Type definition
38  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> VecData;
39  typedef Eigen::Array<T, Eigen::Dynamic, 1> ArrayData;
40 
41  /// Default constructor, set invalid data pointer
43  : data_ptr_(nullptr), data_size_(0), arena_(nullptr) {}
44 
45  /**
46  * Constructor. Set scalar value
47  */
48  ArenaVec(T scalar_val)
49  : data_ptr_(nullptr), data_size_(0), arena_(nullptr), scalar_val_(scalar_val) {}
50 
51  /**
52  * Constructor. Set sizes and allocate data pointer
53  */
55  : data_ptr_(nullptr), data_size_(data_size), arena_(&arena) {
57  }
58 
59  /// Copy constructor
60  ArenaVec(const ArenaVec<T> &other)
61  : data_ptr_(other.data_ptr_), data_size_(other.data_size_),
62  arena_(other.arena_), scalar_val_(other.scalar_val_)
63  {}
64 
65  /**
66  * Maps data pointer to Eigen Map of dimensions given data_size_ and returns it.
67  */
68  inline Eigen::Map<VecData> eigen_map() {
70  return Eigen::Map<VecData>(data_ptr_, data_size_, 1);
71  }
72 
73  /// Smae as previous but with const modifier
74  inline const Eigen::Map<VecData> eigen_map() const {
76  return Eigen::Map<VecData>(data_ptr_, data_size_, 1);
77  }
78 
79  /**
80  * Maps data pointer to Eigen Map of dimensions given data_size_ and returns it.
81  */
82  inline Eigen::Map<ArrayData> array_map() {
84  return Eigen::Map<ArrayData>(data_ptr_, data_size_, 1);
85  }
86 
87  /// Smae as previous but with const modifier
88  inline const Eigen::Map<ArrayData> array_map() const {
90  return Eigen::Map<ArrayData>(data_ptr_, data_size_, 1);
91  }
92 
93  /// Return data pointer (development method)
94  T* data_ptr() {
95  return data_ptr_;
96  }
97 
98  /// Smae as previous but return const pointer
99  const T* data_ptr() const {
100  return data_ptr_;
101  }
102 
103  /// Getter for data_size_
104  inline size_t data_size() const {
105  return data_size_;
106  }
107 
108  /// Getter for arena_
110  return *arena_;
111  }
112 
113  /// Set pointer to PatchArena
116  this->arena_ = &arena;
117  }
118 
119  inline ArenaVec<T> sqrt() const {
122  Eigen::Map<ArrayData> result_map = res.array_map();
123  result_map = this->array_map().sqrt();
124  return res;
125  }
126 
127  inline ArenaVec<T> inverse() const {
130  Eigen::Map<ArrayData> result_map = res.array_map();
131  result_map = this->array_map().inverse();
132  return res;
133  }
134 
135  inline ArenaVec<T> abs() const {
138  Eigen::Map<ArrayData> result_map = res.array_map();
139  result_map = this->array_map().abs();
140  return res;
141  }
142 
143  /// For development only. TODO remove
144  inline T & operator()(std::size_t item) {
146  ASSERT_LT(item, data_size_);
147  return data_ptr_[item];
148  }
149 
150  /// For development only. TODO remove
151  inline const T & operator()(std::size_t item) const {
152  ASSERT_LT(item, data_size_);
153  return data_ptr_[item];
154  }
155 
156  inline ArenaVec<T> operator+(const ArenaVec<T> &other) const {
158  ASSERT_PTR(other.data_ptr());
159  ASSERT_EQ(data_size_, other.data_size());
161  Eigen::Map<VecData> result_map = res.eigen_map();
162  result_map = this->eigen_map() + other.eigen_map();
163  return res;
164  }
165 
166  inline ArenaVec<T> operator-(const ArenaVec<T> &other) const {
168  ASSERT_PTR(other.data_ptr());
169  ASSERT_EQ(data_size_, other.data_size());
171  Eigen::Map<ArrayData> result_map = res.array_map();
172  result_map = this->array_map() - other.array_map();
173  return res;
174  }
175 
176  inline ArenaVec<T> operator*(T multi) const {
179  Eigen::Map<ArrayData> result_map = res.array_map();
180  result_map = this->array_map() * multi;
181  return res;
182  }
183 
184  inline ArenaVec<T> operator*(const ArenaVec<T> &other) const {
186  ASSERT_PTR(other.data_ptr());
187  ASSERT_EQ(data_size_, other.data_size());
189  Eigen::Map<ArrayData> result_map = res.array_map();
190  result_map = this->array_map() * other.array_map();
191  return res;
192  }
193 
194  inline ArenaVec<T> operator/(T div_by) const {
197  Eigen::Map<ArrayData> result_map = res.array_map();
198  result_map = this->array_map() / div_by;
199  return res;
200  }
201 
202  inline ArenaVec<T> operator/(const ArenaVec<T> &other) const {
204  ASSERT_PTR(other.data_ptr());
205  ASSERT_EQ(data_size_, other.data_size());
207  Eigen::Map<ArrayData> result_map = res.array_map();
208  result_map = this->array_map() / other.array_map();
209  return res;
210  }
211 
212 protected:
213  /// Constructor. Allows create ArenaVec from ArenaOVec
216 
217  T* data_ptr_; ///< Pointer to data array
218  size_t data_size_; ///< Length of data array
219  AssemblyArena *arena_; ///< Pointer to Arena where intermediate calculations and results are stored, should be changed by set_patch_arena
220  T scalar_val_; ///< Scalar value of T type
221 
222  friend class ArenaOVec<T>;
223 };
224 
225 
226 
227 /// Outer product - only proposal of multi operator
228 template<class T>
229 class ArenaOVec : public ArenaVec<T> {
230 public:
232  : vec_(vec) {
233  ASSERT_PTR(vec.data_ptr());
234  this->data_ptr_ = vec_.data_ptr();
235  this->data_size_ = vec_.data_size();
236  this->arena_ = vec_.arena_;
237  }
238 
240  return ArenaVec<T>(*this);
241  }
242 
243  inline ArenaOVec<T> operator+(const ArenaOVec<T> &other) const {
244  // Test of valid data_ptr is in constructor
245  ASSERT_EQ(this->data_size_, other.data_size());
246  ArenaVec<T> res_vec(this->data_size_, *this->arena_);
247  ArenaOVec<T> res(res_vec);
248  Eigen::Map<typename ArenaVec<T>::VecData> result_map = res.eigen_map();
249  result_map = this->eigen_map() + other.eigen_map();
250  return res;
251  }
252 
253 
254  //v obou testovat operatorech (v DEBUG), �e oba operandy maj� ArenaVec ji� alokovan�.
255 
256  inline ArenaOVec<T> operator*(const ArenaOVec<T> &other) const {
257  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> MatData;
258 
259  ArenaVec<T> res_vec(this->data_size_*other.data_size(), *this->arena_);
260  ArenaOVec<T> res(res_vec);
261  Eigen::Map<MatData> result_map = Eigen::Map<MatData>(res.data_ptr(), this->data_size_, other.data_size());
262  result_map = this->eigen_map() * other.eigen_map().transpose();
263  return res;
264  }
265 protected:
266  ArenaVec<T> &vec_; ///< Reference to ArenaVec
267 };
268 
269 
270 
271 
272 #endif /* ARENA_VEC_HH_ */
Definitions of ASSERTS.
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
Outer product - only proposal of multi operator.
Definition: arena_vec.hh:229
ArenaOVec< T > operator+(const ArenaOVec< T > &other) const
Definition: arena_vec.hh:243
ArenaVec< T > get_vec()
Definition: arena_vec.hh:239
ArenaOVec(ArenaVec< T > &vec)
Definition: arena_vec.hh:231
ArenaOVec< T > operator*(const ArenaOVec< T > &other) const
Definition: arena_vec.hh:256
ArenaVec< T > & vec_
Reference to ArenaVec.
Definition: arena_vec.hh:266
T * allocate_simd(size_t n_items)
Allocate and return data pointer of n_item array of type T (alignment to length given by simd_alignme...
ArenaVec(T scalar_val)
Definition: arena_vec.hh:48
Eigen::Array< T, Eigen::Dynamic, 1 > ArrayData
Definition: arena_vec.hh:39
ArenaVec()
Default constructor, set invalid data pointer.
Definition: arena_vec.hh:42
Eigen::Matrix< T, Eigen::Dynamic, 1 > VecData
Type definition.
Definition: arena_vec.hh:38
ArenaVec< T > operator/(const ArenaVec< T > &other) const
Definition: arena_vec.hh:202
ArenaVec(size_t data_size, AssemblyArena &arena)
Definition: arena_vec.hh:54
const T * data_ptr() const
Smae as previous but return const pointer.
Definition: arena_vec.hh:99
T * data_ptr()
Return data pointer (development method)
Definition: arena_vec.hh:94
const T & operator()(std::size_t item) const
For development only. TODO remove.
Definition: arena_vec.hh:151
const Eigen::Map< ArrayData > array_map() const
Smae as previous but with const modifier.
Definition: arena_vec.hh:88
ArenaVec< T > sqrt() const
Definition: arena_vec.hh:119
ArenaVec< T > operator*(T multi) const
Definition: arena_vec.hh:176
ArenaVec< T > operator*(const ArenaVec< T > &other) const
Definition: arena_vec.hh:184
Eigen::Map< VecData > eigen_map()
Definition: arena_vec.hh:68
ArenaVec< T > abs() const
Definition: arena_vec.hh:135
AssemblyArena & arena()
Getter for arena_.
Definition: arena_vec.hh:109
Eigen::Map< ArrayData > array_map()
Definition: arena_vec.hh:82
size_t data_size_
Length of data array.
Definition: arena_vec.hh:218
AssemblyArena * arena_
Pointer to Arena where intermediate calculations and results are stored, should be changed by set_pat...
Definition: arena_vec.hh:219
const Eigen::Map< VecData > eigen_map() const
Smae as previous but with const modifier.
Definition: arena_vec.hh:74
ArenaVec< T > operator/(T div_by) const
Definition: arena_vec.hh:194
ArenaVec< T > inverse() const
Definition: arena_vec.hh:127
ArenaVec(T *data_ptr, size_t data_size, AssemblyArena &arena)
Constructor. Allows create ArenaVec from ArenaOVec.
Definition: arena_vec.hh:214
void set_patch_arena(PatchArena &arena)
Set pointer to PatchArena.
Definition: arena_vec.hh:114
ArenaVec< T > operator-(const ArenaVec< T > &other) const
Definition: arena_vec.hh:166
T & operator()(std::size_t item)
For development only. TODO remove.
Definition: arena_vec.hh:144
ArenaVec< T > operator+(const ArenaVec< T > &other) const
Definition: arena_vec.hh:156
ArenaVec(const ArenaVec< T > &other)
Copy constructor.
Definition: arena_vec.hh:60
size_t data_size() const
Getter for data_size_.
Definition: arena_vec.hh:104
T scalar_val_
Scalar value of T type.
Definition: arena_vec.hh:220
T * data_ptr_
Pointer to data array.
Definition: arena_vec.hh:217
ArmaVec< double, N > vec
Definition: armor.hh:933