OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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
21template<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
41namespace Eigen {
42
43namespace internal {
44template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
45struct traits<EMatrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
46{
47private:
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
60public:
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
87template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
88class 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
93public:
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
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
334typedef EMatrix< std::complex< double >, 2, 2 > EMatrix2cd;
335typedef EMatrix< std::complex< float >, 2, 2 > EMatrix2cf;
336typedef EMatrix< double, 2, 2 > EMatrix2d;
337typedef EMatrix< float, 2, 2 > EMatrix2f;
338typedef EMatrix< int, 2, 2 > EMatrix2i;
339typedef EMatrix< std::complex< double >, 2, Eigen::Dynamic > EMatrix2Xcd;
340typedef EMatrix< std::complex< float >, 2, Eigen::Dynamic > EMatrix2Xcf;
341typedef EMatrix< double, 2, Eigen::Dynamic > EMatrix2Xd;
342typedef EMatrix< float, 2, Eigen::Dynamic > EMatrix2Xf;
343typedef EMatrix< int, 2, Eigen::Dynamic > EMatrix2Xi;
344typedef EMatrix< std::complex< double >, 3, 3 > EMatrix3cd;
345typedef EMatrix< std::complex< float >, 3, 3 > EMatrix3cf;
346typedef EMatrix< double, 3, 3 > EMatrix3d;
347typedef EMatrix< float, 3, 3 > EMatrix3f;
348typedef EMatrix< int, 3, 3 > EMatrix3i;
349typedef EMatrix< std::complex< double >, 3, Eigen::Dynamic > EMatrix3Xcd;
350typedef EMatrix< std::complex< float >, 3, Eigen::Dynamic > EMatrix3Xcf;
351typedef EMatrix< double, 3, Eigen::Dynamic > EMatrix3Xd;
352typedef EMatrix< float, 3, Eigen::Dynamic > EMatrix3Xf;
353typedef EMatrix< int, 3, Eigen::Dynamic > EMatrix3Xi;
354typedef EMatrix< std::complex< double >, 4, 4 > EMatrix4cd;
355typedef EMatrix< std::complex< float >, 4, 4 > EMatrix4cf;
356typedef EMatrix< double, 4, 4 > EMatrix4d;
357typedef EMatrix< float, 4, 4 > EMatrix4f;
358typedef EMatrix< int, 4, 4 > EMatrix4i;
359typedef EMatrix< std::complex< double >, 4, Eigen::Dynamic > EMatrix4Xcd;
360typedef EMatrix< std::complex< float >, 4, Eigen::Dynamic > EMatrix4Xcf;
361typedef EMatrix< double, 4, Eigen::Dynamic > EMatrix4Xd;
362typedef EMatrix< float, 4, Eigen::Dynamic > EMatrix4Xf;
363typedef EMatrix< int, 4, Eigen::Dynamic > EMatrix4Xi;
364typedef EMatrix< std::complex< double >, Eigen::Dynamic, 2 > EMatrixX2cd;
365typedef EMatrix< std::complex< float >, Eigen::Dynamic, 2 > EMatrixX2cf;
366typedef EMatrix< double, Eigen::Dynamic, 2 > EMatrixX2d;
367typedef EMatrix< float, Eigen::Dynamic, 2 > EMatrixX2f;
368typedef EMatrix< int, Eigen::Dynamic, 2 > EMatrixX2i;
369typedef EMatrix< std::complex< double >, Eigen::Dynamic, 3 > EMatrixX3cd;
370typedef EMatrix< std::complex< float >, Eigen::Dynamic, 3 > EMatrixX3cf;
371typedef EMatrix< double, Eigen::Dynamic, 3 > EMatrixX3d;
372typedef EMatrix< float, Eigen::Dynamic, 3 > EMatrixX3f;
373typedef EMatrix< int, Eigen::Dynamic, 3 > EMatrixX3i;
374typedef EMatrix< std::complex< double >, Eigen::Dynamic, 4 > EMatrixX4cd;
375typedef EMatrix< std::complex< float >, Eigen::Dynamic, 4 > EMatrixX4cf;
376typedef EMatrix< double, Eigen::Dynamic, 4 > EMatrixX4d;
377typedef EMatrix< float, Eigen::Dynamic, 4 > EMatrixX4f;
378typedef EMatrix< int, Eigen::Dynamic, 4 > EMatrixX4i;
379typedef EMatrix< std::complex< double >, Eigen::Dynamic, Eigen::Dynamic > EMatrixXcd;
380typedef EMatrix< std::complex< float >, Eigen::Dynamic, Eigen::Dynamic > EMatrixXcf;
381typedef EMatrix< double, Eigen::Dynamic, Eigen::Dynamic > EMatrixXd;
382typedef EMatrix< float, Eigen::Dynamic, Eigen::Dynamic > EMatrixXf;
383typedef EMatrix< int, Eigen::Dynamic, Eigen::Dynamic > EMatrixXi;
384typedef EMatrix< std::complex< double >, 1, 2 > ERowVector2cd;
385typedef EMatrix< std::complex< float >, 1, 2 > ERowVector2cf;
386typedef EMatrix< double, 1, 2 > ERowVector2d;
387typedef EMatrix< float, 1, 2 > ERowVector2f;
388typedef EMatrix< int, 1, 2 > ERowVector2i;
389typedef EMatrix< std::complex< double >, 1, 3 > ERowVector3cd;
390typedef EMatrix< std::complex< float >, 1, 3 > ERowVector3cf;
391typedef EMatrix< double, 1, 3 > ERowVector3d;
392typedef EMatrix< float, 1, 3 > ERowVector3f;
393typedef EMatrix< int, 1, 3 > ERowVector3i;
394typedef EMatrix< std::complex< double >, 1, 4 > ERowVector4cd;
395typedef EMatrix< std::complex< float >, 1, 4 > ERowVector4cf;
396typedef EMatrix< double, 1, 4 > ERowVector4d;
397typedef EMatrix< float, 1, 4 > ERowVector4f;
398typedef EMatrix< int, 1, 4 > ERowVector4i;
399typedef EMatrix< std::complex< double >, 1, Eigen::Dynamic > ERowVectorXcd;
400typedef EMatrix< std::complex< float >, 1, Eigen::Dynamic > ERowVectorXcf;
401typedef EMatrix< double, 1, Eigen::Dynamic > ERowVectorXd;
402typedef EMatrix< float, 1, Eigen::Dynamic > ERowVectorXf;
403typedef EMatrix< int, 1, Eigen::Dynamic > ERowVectorXi;
404typedef EMatrix< std::complex< double >, 2, 1 > EVector2cd;
405typedef EMatrix< std::complex< float >, 2, 1 > EVector2cf;
406typedef EMatrix< double, 2, 1 > EVector2d;
407typedef EMatrix< float, 2, 1 > EVector2f;
408typedef EMatrix< int, 2, 1 > EVector2i;
409typedef EMatrix< std::complex< double >, 3, 1 > EVector3cd;
410typedef EMatrix< std::complex< float >, 3, 1 > EVector3cf;
411typedef EMatrix< double, 3, 1 > EVector3d;
412typedef EMatrix< float, 3, 1 > EVector3f;
413typedef EMatrix< int, 3, 1 > EVector3i;
414typedef EMatrix< std::complex< double >, 4, 1 > EVector4cd;
415typedef EMatrix< std::complex< float >, 4, 1 > EVector4cf;
416typedef EMatrix< double, 4, 1 > EVector4d;
417typedef EMatrix< float, 4, 1 > EVector4f;
418typedef EMatrix< int, 4, 1 > EVector4i;
419typedef EMatrix< std::complex< double >, Eigen::Dynamic, 1 > EVectorXcd;
420typedef EMatrix< std::complex< float >, Eigen::Dynamic, 1 > EVectorXcf;
421typedef EMatrix< double, Eigen::Dynamic, 1 > EVectorXd;
422typedef EMatrix< float, Eigen::Dynamic, 1 > EVectorXf;
423typedef 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_ */
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
virtual void * getPointer()
Return the pointer of the last allocation.
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
This class implement an NxN (dense) matrix.
Definition Matrix.hpp:33
Packing status object.
Definition Pack_stat.hpp:61
void incReq()
Increment the request pointer.
Definition Pack_stat.hpp:79
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition Packer.hpp:56
Unpacking status object.
Definition Pack_stat.hpp:16
size_t getOffset()
Return the actual counter.
Definition Pack_stat.hpp:41
void addOffset(size_t off)
Increment the offset pointer by off.
Definition Pack_stat.hpp:31
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition Unpacker.hpp:40
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data