Flow123d  DF_patch_fe_data_tables-836d647
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  inline ArenaVec<T> sqrt() const {
111  Eigen::Map<ArrayData> result_map = res.array_map();
112  result_map = this->array_map().sqrt();
113  return res;
114  }
115 
116  inline ArenaVec<T> inverse() const {
119  Eigen::Map<ArrayData> result_map = res.array_map();
120  result_map = this->array_map().inverse();
121  return res;
122  }
123 
124  inline ArenaVec<T> abs() const {
127  Eigen::Map<ArrayData> result_map = res.array_map();
128  result_map = this->array_map().abs();
129  return res;
130  }
131 
132  /// For development only. TODO remove
133  inline T & operator()(std::size_t item) {
135  ASSERT_LT(item, data_size_);
136  return data_ptr_[item];
137  }
138 
139  /// For development only. TODO remove
140  inline const T & operator()(std::size_t item) const {
141  ASSERT_LT(item, data_size_);
142  return data_ptr_[item];
143  }
144 
145  inline ArenaVec<T> operator+(const ArenaVec<T> &other) const {
147  ASSERT_PTR(other.data_ptr());
148  ASSERT_EQ(data_size_, other.data_size());
150  Eigen::Map<VecData> result_map = res.eigen_map();
151  result_map = this->eigen_map() + other.eigen_map();
152  return res;
153  }
154 
155  inline ArenaVec<T> operator-(const ArenaVec<T> &other) const {
157  ASSERT_PTR(other.data_ptr());
158  ASSERT_EQ(data_size_, other.data_size());
160  Eigen::Map<ArrayData> result_map = res.array_map();
161  result_map = this->array_map() - other.array_map();
162  return res;
163  }
164 
165  inline ArenaVec<T> operator*(T multi) const {
168  Eigen::Map<ArrayData> result_map = res.array_map();
169  result_map = this->array_map() * multi;
170  return res;
171  }
172 
173  inline ArenaVec<T> operator*(const ArenaVec<T> &other) const {
175  ASSERT_PTR(other.data_ptr());
176  ASSERT_EQ(data_size_, other.data_size());
178  Eigen::Map<ArrayData> result_map = res.array_map();
179  result_map = this->array_map() * other.array_map();
180  return res;
181  }
182 
183  inline ArenaVec<T> operator/(T div_by) const {
186  Eigen::Map<ArrayData> result_map = res.array_map();
187  result_map = this->array_map() / div_by;
188  return res;
189  }
190 
191  inline ArenaVec<T> operator/(const ArenaVec<T> &other) const {
193  ASSERT_PTR(other.data_ptr());
194  ASSERT_EQ(data_size_, other.data_size());
196  Eigen::Map<ArrayData> result_map = res.array_map();
197  result_map = this->array_map() / other.array_map();
198  return res;
199  }
200 
201 protected:
202  /// Constructor. Allows create ArenaVec from ArenaOVec
204  : data_ptr_(data_ptr), data_size_(data_size), arena_(&arena) {}
205 
206  T* data_ptr_; ///< Pointer to data array
207  size_t data_size_; ///< Length of data array
208  AssemblyArena *arena_; ///< Pointer to Arena
209  T scalar_val_; ///< Scalar value of T type
210 
211  friend class ArenaOVec<T>;
212 };
213 
214 
215 
216 /// Outer product - only proposal of multi operator
217 template<class T>
218 class ArenaOVec : public ArenaVec<T> {
219 public:
221  : vec_(vec) {
222  ASSERT_PTR(vec.data_ptr());
223  this->data_ptr_ = vec_.data_ptr();
224  this->data_size_ = vec_.data_size();
225  this->arena_ = vec_.arena_;
226  }
227 
229  return ArenaVec<T>(*this);
230  }
231 
232  inline ArenaOVec<T> operator+(const ArenaOVec<T> &other) const {
233  // Test of valid data_ptr is in constructor
234  ASSERT_EQ(this->data_size_, other.data_size());
235  ArenaVec<T> res_vec(this->data_size_, *this->arena_);
236  ArenaOVec<T> res(res_vec);
237  Eigen::Map<typename ArenaVec<T>::VecData> result_map = res.eigen_map();
238  result_map = this->eigen_map() + other.eigen_map();
239  return res;
240  }
241 
242 
243  //v obou testovat operatorech (v DEBUG), �e oba operandy maj� ArenaVec ji� alokovan�.
244 
245  inline ArenaOVec<T> operator*(const ArenaOVec<T> &other) const {
246  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> MatData;
247 
248  ArenaVec<T> res_vec(this->data_size_*other.data_size(), *this->arena_);
249  ArenaOVec<T> res(res_vec);
250  Eigen::Map<MatData> result_map = Eigen::Map<MatData>(res.data_ptr(), this->data_size_, other.data_size());
251  result_map = this->eigen_map() * other.eigen_map().transpose();
252  return res;
253  }
254 protected:
255  ArenaVec<T> &vec_; ///< Reference to ArenaVec
256 };
257 
258 
259 
260 
261 #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:218
ArenaOVec< T > operator+(const ArenaOVec< T > &other) const
Definition: arena_vec.hh:232
ArenaVec< T > get_vec()
Definition: arena_vec.hh:228
ArenaOVec(ArenaVec< T > &vec)
Definition: arena_vec.hh:220
ArenaOVec< T > operator*(const ArenaOVec< T > &other) const
Definition: arena_vec.hh:245
ArenaVec< T > & vec_
Reference to ArenaVec.
Definition: arena_vec.hh:255
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:191
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:140
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:108
ArenaVec< T > operator*(T multi) const
Definition: arena_vec.hh:165
ArenaVec< T > operator*(const ArenaVec< T > &other) const
Definition: arena_vec.hh:173
Eigen::Map< VecData > eigen_map()
Definition: arena_vec.hh:68
ArenaVec< T > abs() const
Definition: arena_vec.hh:124
Eigen::Map< ArrayData > array_map()
Definition: arena_vec.hh:82
size_t data_size_
Length of data array.
Definition: arena_vec.hh:207
AssemblyArena * arena_
Pointer to Arena.
Definition: arena_vec.hh:208
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:183
ArenaVec< T > inverse() const
Definition: arena_vec.hh:116
ArenaVec(T *data_ptr, size_t data_size, AssemblyArena &arena)
Constructor. Allows create ArenaVec from ArenaOVec.
Definition: arena_vec.hh:203
ArenaVec< T > operator-(const ArenaVec< T > &other) const
Definition: arena_vec.hh:155
T & operator()(std::size_t item)
For development only. TODO remove.
Definition: arena_vec.hh:133
ArenaVec< T > operator+(const ArenaVec< T > &other) const
Definition: arena_vec.hh:145
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:209
T * data_ptr_
Pointer to data array.
Definition: arena_vec.hh:206
ArmaVec< double, N > vec
Definition: armor.hh:933