OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
EMatrix.hpp
1 /*
2  * EMatrix.hpp
3  *
4  * Created on: Feb 12, 2018
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_SPACE_EMATRIX_HPP_
9 #define OPENFPM_DATA_SRC_SPACE_EMATRIX_HPP_
10 
11 #include "config.h"
12 
13 #ifdef HAVE_EIGEN
14 #include <Eigen/Dense>
15 #include "memory/ExtPreAlloc.hpp"
16 #include "memory/HeapMemory.hpp"
17 #include "Packer_Unpacker/Packer_util.hpp"
18 #include "Packer_Unpacker/Packer.hpp"
19 #include "Packer_Unpacker/Unpacker.hpp"
20 
21 template<typename _Scalar, int _Rows, int _Cols,
22  int _Options = Eigen::AutoAlign |
23 #if EIGEN_GNUC_AT(3,4)
24  // workaround a bug in at least gcc 3.4.6
25  // the innermost ?: ternary operator is misparsed. We write it slightly
26  // differently and this makes gcc 3.4.6 happy, but it's ugly.
27  // The error would only show up with EIGEN_DEFAULT_TO_ROW_MAJOR is defined
28  // (when EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION is RowMajor)
29  ( (_Rows==1 && _Cols!=1) ? Eigen::RowMajor
30  : !(_Cols==1 && _Rows!=1) ? EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION
31  : Eigen::ColMajor ),
32 #else
33  ( (_Rows==1 && _Cols!=1) ? Eigen::RowMajor
34  : (_Cols==1 && _Rows!=1) ? Eigen::ColMajor
35  : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ),
36 #endif
37  int _MaxRows = _Rows,
38  int _MaxCols = _Cols
39 > class EMatrix;
40 
41 namespace Eigen {
42 
43 namespace internal {
44 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
45 struct traits<EMatrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
46 {
47 private:
48  enum { size = internal::size_at_compile_time<_Rows,_Cols>::ret };
49  typedef typename find_best_packet<_Scalar,size>::type PacketScalar;
50  enum {
51  row_major_bit = _Options&RowMajor ? RowMajorBit : 0,
52  is_dynamic_size_storage = _MaxRows==Dynamic || _MaxCols==Dynamic,
53  max_size = is_dynamic_size_storage ? Dynamic : _MaxRows*_MaxCols,
54  default_alignment = compute_default_alignment<_Scalar,max_size>::value,
55  actual_alignment = ((_Options&DontAlign)==0) ? default_alignment : 0,
56  required_alignment = unpacket_traits<PacketScalar>::alignment,
57  packet_access_bit = (packet_traits<_Scalar>::Vectorizable && (EIGEN_UNALIGNED_VECTORIZE || (actual_alignment>=required_alignment))) ? PacketAccessBit : 0
58  };
59 
60 public:
61  typedef _Scalar Scalar;
62  typedef Dense StorageKind;
63  typedef Eigen::Index StorageIndex;
64  typedef MatrixXpr XprKind;
65  enum {
66  RowsAtCompileTime = _Rows,
67  ColsAtCompileTime = _Cols,
68  MaxRowsAtCompileTime = _MaxRows,
69  MaxColsAtCompileTime = _MaxCols,
70  Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
71  Options = _Options,
72  InnerStrideAtCompileTime = 1,
73  OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime,
74 
75  // FIXME, the following flag in only used to define NeedsToAlign in PlainObjectBase
76  EvaluatorFlags = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit,
77  Alignment = actual_alignment
78  };
79 };
80 }
81 }
82 
87 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
88 class EMatrix : public Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>
89 {
90 
91  typedef typename Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>::Base Base;
92 
93 public:
94 
95  //typedef typename Eigen::internal::traits<Eigen::Matrix<_Scalar,_Rows,_Cols>>::Scalar Scalar;
96 
97  EIGEN_DENSE_PUBLIC_INTERFACE(EMatrix)
98 
99 
106  template<int ... prp> inline void packRequest(size_t & req) const
107  {
108  // Memory required to serialize the Matrix
109  req += sizeof(_Scalar)*this->rows()*this->cols() + 2*sizeof(_Scalar);
110  }
111 
112 
119  template<int ... prp> inline void pack(ExtPreAlloc<HeapMemory> & mem, Pack_stat & sts) const
120  {
121  //Pack the number of rows and colums
122  Packer<size_t, HeapMemory>::pack(mem,this->rows(),sts);
123  Packer<size_t, HeapMemory>::pack(mem,this->cols(),sts);
124 
125  mem.allocate(sizeof(_Scalar)*this->rows()*this->cols());
126  void * dst = mem.getPointer();
127  void * src = (void *)this->data();
128 
129  memcpy(dst,src,sizeof(_Scalar)*this->rows()*this->cols());
130  sts.incReq();
131  }
132 
138  template<int ... prp> inline void unpack(ExtPreAlloc<HeapMemory> & mem, Unpack_stat & ps)
139  {
140  size_t row;
141  size_t col;
142 
143  //Pack the number of rows and colums
146 
147  this->resize(row,col);
148 
149  void * dst = this->data();
150  void * src = mem.getPointerOffset(ps.getOffset());
151 
152  memcpy(dst,src,sizeof(_Scalar)*row*col);
153 
154  ps.addOffset(sizeof(_Scalar)*row*col);
155  }
156 
158 
169  EIGEN_DEVICE_FUNC
170  EIGEN_STRONG_INLINE EMatrix()
171  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>()
172  {}
173 
182  EIGEN_DEVICE_FUNC
183  EIGEN_STRONG_INLINE EMatrix& operator=(const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>& other)
184  {
185  ((Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> *)this)->operator=(other);
186  return *this;
187  }
188 
199  template<typename OtherDerived>
200  EIGEN_DEVICE_FUNC
201  EIGEN_STRONG_INLINE EMatrix& operator=(const Eigen::DenseBase<OtherDerived>& other)
202  {
203  ((Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> *)this)->operator=(other);
204  return *this;
205  }
206 
211  template<typename OtherDerived>
212  EIGEN_DEVICE_FUNC
213  EIGEN_STRONG_INLINE EMatrix& operator=(const Eigen::EigenBase<OtherDerived> &other)
214  {
215  ((Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> *)this)->operator=(other);
216  return *this;
217  }
218 
219  template<typename OtherDerived>
220  EIGEN_DEVICE_FUNC
221  EIGEN_STRONG_INLINE EMatrix& operator=(const Eigen::ReturnByValue<OtherDerived>& func)
222  {
223  ((Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> *)this)->operator=(func);
224  return *this;
225  }
226 
227 #if EIGEN_HAS_RVALUE_REFERENCES
228  EIGEN_DEVICE_FUNC
229  EMatrix(Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> && other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
230  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(other)
231  {}
232 
233  EIGEN_DEVICE_FUNC
234  EMatrix& operator=(Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> && other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value)
235  {return *this;}
236 
237 #endif
238 
239  #ifndef EIGEN_PARSED_BY_DOXYGEN
240 
241  // This constructor is for both 1x1 matrices and dynamic vectors
242  template<typename T>
243  EIGEN_DEVICE_FUNC
244  EIGEN_STRONG_INLINE explicit EMatrix(const T& x)
245  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x)
246  {}
247 
248  template<typename T0, typename T1>
249  EIGEN_DEVICE_FUNC
250  EIGEN_STRONG_INLINE EMatrix(const T0& x, const T1& y)
251  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x,y)
252  {}
253 
254  #else
255 
256  EIGEN_DEVICE_FUNC
257  explicit EMatrix(const Scalar *data)
258  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(data)
259  {}
260 
273  EIGEN_STRONG_INLINE explicit EMatrix(Index dim)
274  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(dim)
275  {}
276 
278  EMatrix(const Scalar& x)
279  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x)
280  {}
293  EIGEN_DEVICE_FUNC
294  EMatrix(Index rows, Index cols)
295  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(rows,cols)
296  {}
297 
299  EMatrix(const Scalar& x, const Scalar& y)
300  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x,y)
301  {};
302  #endif
303 
305  EIGEN_DEVICE_FUNC
306  EIGEN_STRONG_INLINE EMatrix(const Scalar& x, const Scalar& y, const Scalar& z)
307  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x,y,z)
308  {}
309 
311  EIGEN_DEVICE_FUNC
312  EIGEN_STRONG_INLINE EMatrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
313  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(x,y,z,w)
314  {}
315 
316 
318  EIGEN_DEVICE_FUNC
319  EIGEN_STRONG_INLINE EMatrix(const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & other)
320  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(other)
321  {}
322 
326  template<typename OtherDerived>
327  EIGEN_DEVICE_FUNC
328  EIGEN_STRONG_INLINE EMatrix(const Eigen::EigenBase<OtherDerived> &other)
329  :Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols>(other)
330  {}
331 };
332 
333 // Standard typedef from eigen
334 typedef EMatrix< std::complex< double >, 2, 2 > EMatrix2cd;
335 typedef EMatrix< std::complex< float >, 2, 2 > EMatrix2cf;
336 typedef EMatrix< double, 2, 2 > EMatrix2d;
337 typedef EMatrix< float, 2, 2 > EMatrix2f;
338 typedef EMatrix< int, 2, 2 > EMatrix2i;
339 typedef EMatrix< std::complex< double >, 2, Eigen::Dynamic > EMatrix2Xcd;
340 typedef EMatrix< std::complex< float >, 2, Eigen::Dynamic > EMatrix2Xcf;
341 typedef EMatrix< double, 2, Eigen::Dynamic > EMatrix2Xd;
342 typedef EMatrix< float, 2, Eigen::Dynamic > EMatrix2Xf;
343 typedef EMatrix< int, 2, Eigen::Dynamic > EMatrix2Xi;
344 typedef EMatrix< std::complex< double >, 3, 3 > EMatrix3cd;
345 typedef EMatrix< std::complex< float >, 3, 3 > EMatrix3cf;
346 typedef EMatrix< double, 3, 3 > EMatrix3d;
347 typedef EMatrix< float, 3, 3 > EMatrix3f;
348 typedef EMatrix< int, 3, 3 > EMatrix3i;
349 typedef EMatrix< std::complex< double >, 3, Eigen::Dynamic > EMatrix3Xcd;
350 typedef EMatrix< std::complex< float >, 3, Eigen::Dynamic > EMatrix3Xcf;
351 typedef EMatrix< double, 3, Eigen::Dynamic > EMatrix3Xd;
352 typedef EMatrix< float, 3, Eigen::Dynamic > EMatrix3Xf;
353 typedef EMatrix< int, 3, Eigen::Dynamic > EMatrix3Xi;
354 typedef EMatrix< std::complex< double >, 4, 4 > EMatrix4cd;
355 typedef EMatrix< std::complex< float >, 4, 4 > EMatrix4cf;
356 typedef EMatrix< double, 4, 4 > EMatrix4d;
357 typedef EMatrix< float, 4, 4 > EMatrix4f;
358 typedef EMatrix< int, 4, 4 > EMatrix4i;
359 typedef EMatrix< std::complex< double >, 4, Eigen::Dynamic > EMatrix4Xcd;
360 typedef EMatrix< std::complex< float >, 4, Eigen::Dynamic > EMatrix4Xcf;
361 typedef EMatrix< double, 4, Eigen::Dynamic > EMatrix4Xd;
362 typedef EMatrix< float, 4, Eigen::Dynamic > EMatrix4Xf;
363 typedef EMatrix< int, 4, Eigen::Dynamic > EMatrix4Xi;
364 typedef EMatrix< std::complex< double >, Eigen::Dynamic, 2 > EMatrixX2cd;
365 typedef EMatrix< std::complex< float >, Eigen::Dynamic, 2 > EMatrixX2cf;
366 typedef EMatrix< double, Eigen::Dynamic, 2 > EMatrixX2d;
367 typedef EMatrix< float, Eigen::Dynamic, 2 > EMatrixX2f;
368 typedef EMatrix< int, Eigen::Dynamic, 2 > EMatrixX2i;
369 typedef EMatrix< std::complex< double >, Eigen::Dynamic, 3 > EMatrixX3cd;
370 typedef EMatrix< std::complex< float >, Eigen::Dynamic, 3 > EMatrixX3cf;
371 typedef EMatrix< double, Eigen::Dynamic, 3 > EMatrixX3d;
372 typedef EMatrix< float, Eigen::Dynamic, 3 > EMatrixX3f;
373 typedef EMatrix< int, Eigen::Dynamic, 3 > EMatrixX3i;
374 typedef EMatrix< std::complex< double >, Eigen::Dynamic, 4 > EMatrixX4cd;
375 typedef EMatrix< std::complex< float >, Eigen::Dynamic, 4 > EMatrixX4cf;
376 typedef EMatrix< double, Eigen::Dynamic, 4 > EMatrixX4d;
377 typedef EMatrix< float, Eigen::Dynamic, 4 > EMatrixX4f;
378 typedef EMatrix< int, Eigen::Dynamic, 4 > EMatrixX4i;
379 typedef EMatrix< std::complex< double >, Eigen::Dynamic, Eigen::Dynamic > EMatrixXcd;
380 typedef EMatrix< std::complex< float >, Eigen::Dynamic, Eigen::Dynamic > EMatrixXcf;
381 typedef EMatrix< double, Eigen::Dynamic, Eigen::Dynamic > EMatrixXd;
382 typedef EMatrix< float, Eigen::Dynamic, Eigen::Dynamic > EMatrixXf;
383 typedef EMatrix< int, Eigen::Dynamic, Eigen::Dynamic > EMatrixXi;
384 typedef EMatrix< std::complex< double >, 1, 2 > ERowVector2cd;
385 typedef EMatrix< std::complex< float >, 1, 2 > ERowVector2cf;
386 typedef EMatrix< double, 1, 2 > ERowVector2d;
387 typedef EMatrix< float, 1, 2 > ERowVector2f;
388 typedef EMatrix< int, 1, 2 > ERowVector2i;
389 typedef EMatrix< std::complex< double >, 1, 3 > ERowVector3cd;
390 typedef EMatrix< std::complex< float >, 1, 3 > ERowVector3cf;
391 typedef EMatrix< double, 1, 3 > ERowVector3d;
392 typedef EMatrix< float, 1, 3 > ERowVector3f;
393 typedef EMatrix< int, 1, 3 > ERowVector3i;
394 typedef EMatrix< std::complex< double >, 1, 4 > ERowVector4cd;
395 typedef EMatrix< std::complex< float >, 1, 4 > ERowVector4cf;
396 typedef EMatrix< double, 1, 4 > ERowVector4d;
397 typedef EMatrix< float, 1, 4 > ERowVector4f;
398 typedef EMatrix< int, 1, 4 > ERowVector4i;
399 typedef EMatrix< std::complex< double >, 1, Eigen::Dynamic > ERowVectorXcd;
400 typedef EMatrix< std::complex< float >, 1, Eigen::Dynamic > ERowVectorXcf;
401 typedef EMatrix< double, 1, Eigen::Dynamic > ERowVectorXd;
402 typedef EMatrix< float, 1, Eigen::Dynamic > ERowVectorXf;
403 typedef EMatrix< int, 1, Eigen::Dynamic > ERowVectorXi;
404 typedef EMatrix< std::complex< double >, 2, 1 > EVector2cd;
405 typedef EMatrix< std::complex< float >, 2, 1 > EVector2cf;
406 typedef EMatrix< double, 2, 1 > EVector2d;
407 typedef EMatrix< float, 2, 1 > EVector2f;
408 typedef EMatrix< int, 2, 1 > EVector2i;
409 typedef EMatrix< std::complex< double >, 3, 1 > EVector3cd;
410 typedef EMatrix< std::complex< float >, 3, 1 > EVector3cf;
411 typedef EMatrix< double, 3, 1 > EVector3d;
412 typedef EMatrix< float, 3, 1 > EVector3f;
413 typedef EMatrix< int, 3, 1 > EVector3i;
414 typedef EMatrix< std::complex< double >, 4, 1 > EVector4cd;
415 typedef EMatrix< std::complex< float >, 4, 1 > EVector4cf;
416 typedef EMatrix< double, 4, 1 > EVector4d;
417 typedef EMatrix< float, 4, 1 > EVector4f;
418 typedef EMatrix< int, 4, 1 > EVector4i;
419 typedef EMatrix< std::complex< double >, Eigen::Dynamic, 1 > EVectorXcd;
420 typedef EMatrix< std::complex< float >, Eigen::Dynamic, 1 > EVectorXcf;
421 typedef EMatrix< double, Eigen::Dynamic, 1 > EVectorXd;
422 typedef EMatrix< float, Eigen::Dynamic, 1 > EVectorXf;
423 typedef EMatrix< int, Eigen::Dynamic, 1 > EVectorXi;
424 
425 #else
426 
427 // There is not EMatrix if we do not have Eigen
428 
429 #endif
430 
431 #endif /* OPENFPM_DATA_SRC_SPACE_EMATRIX_HPP_ */
size_t getOffset()
Return the actual counter.
Definition: Pack_stat.hpp:41
virtual void * getPointer()
Return the pointer of the last allocation.
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
Definition: ExtPreAlloc.hpp:92
void incReq()
Increment the request pointer.
Definition: Pack_stat.hpp:70
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
This class implement an NxN (dense) matrix.
Definition: Matrix.hpp:32
void addOffset(size_t off)
Increment the offset pointer by off.
Definition: Pack_stat.hpp:31
Unpacking status object.
Definition: Pack_stat.hpp:15
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:36
Packing status object.
Definition: Pack_stat.hpp:51
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:51