MapBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_MAPBASE_H
12 #define EIGEN_MAPBASE_H
13 
14 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
15  EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
16  YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
17 
18 // IWYU pragma: private
19 #include "./InternalHeaderCheck.h"
20 
21 namespace Eigen {
22 
40 template <typename Derived>
41 class MapBase<Derived, ReadOnlyAccessors> : public internal::dense_xpr_base<Derived>::type {
42  public:
44  enum {
48  SizeAtCompileTime = Base::SizeAtCompileTime
49  };
50 
55  typedef std::conditional_t<bool(internal::is_lvalue<Derived>::value), Scalar*, const Scalar*> PointerType;
56 
57  using Base::derived;
58  // using Base::RowsAtCompileTime;
59  // using Base::ColsAtCompileTime;
60  // using Base::SizeAtCompileTime;
61  using Base::Flags;
62  using Base::IsRowMajor;
63  using Base::IsVectorAtCompileTime;
64  using Base::MaxColsAtCompileTime;
65  using Base::MaxRowsAtCompileTime;
66  using Base::MaxSizeAtCompileTime;
67 
68  using Base::coeff;
69  using Base::coeffRef;
70  using Base::cols;
71  using Base::eval;
72  using Base::lazyAssign;
73  using Base::rows;
74  using Base::size;
75 
76  using Base::colStride;
77  using Base::innerStride;
78  using Base::outerStride;
79  using Base::rowStride;
80 
81  // bug 217 - compile error on ICC 11.1
82  using Base::operator=;
83 
84  typedef typename Base::CoeffReturnType CoeffReturnType;
85 
87  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rows() const EIGEN_NOEXCEPT { return m_rows.value(); }
89  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return m_cols.value(); }
90 
97  EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return m_data; }
98 
100  EIGEN_DEVICE_FUNC inline const Scalar& coeff(Index rowId, Index colId) const {
101  return m_data[colId * colStride() + rowId * rowStride()];
102  }
103 
105  EIGEN_DEVICE_FUNC inline const Scalar& coeff(Index index) const {
107  return m_data[index * innerStride()];
108  }
109 
111  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index rowId, Index colId) const {
112  return this->m_data[colId * colStride() + rowId * rowStride()];
113  }
114 
116  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
118  return this->m_data[index * innerStride()];
119  }
120 
122  template <int LoadMode>
123  inline PacketScalar packet(Index rowId, Index colId) const {
124  return internal::ploadt<PacketScalar, LoadMode>(m_data + (colId * colStride() + rowId * rowStride()));
125  }
126 
128  template <int LoadMode>
129  inline PacketScalar packet(Index index) const {
131  return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
132  }
133 
135  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr)
136  : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime) {
138  checkSanity<Derived>();
139  }
140 
142  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize)
143  : m_data(dataPtr),
144  m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
145  m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime)) {
147  eigen_assert(vecSize >= 0);
148  eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
149  checkSanity<Derived>();
150  }
151 
154  : m_data(dataPtr), m_rows(rows), m_cols(cols) {
155  eigen_assert((dataPtr == 0) || (rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) &&
156  cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
157  checkSanity<Derived>();
158  }
159 
160 #ifdef EIGEN_MAPBASE_PLUGIN
161 #include EIGEN_MAPBASE_PLUGIN
162 #endif
163 
164  protected:
167 
168  template <typename T>
169  EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<(internal::traits<T>::Alignment > 0), void*> = 0) const {
170 // Temporary macro to allow scalars to not be properly aligned. This is while we sort out failures
171 // in TensorFlow Lite that are currently relying on this UB.
172 #ifndef EIGEN_ALLOW_UNALIGNED_SCALARS
173  // Pointer must be aligned to the Scalar type, otherwise we get UB.
174  eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned");
175 #endif
176 #if EIGEN_MAX_ALIGN_BYTES > 0
177  // innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible
178  // value:
179  const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime);
180  EIGEN_ONLY_USED_FOR_DEBUG(minInnerStride);
181  eigen_assert((((std::uintptr_t(m_data) % internal::traits<Derived>::Alignment) == 0) ||
182  (cols() * rows() * minInnerStride * sizeof(Scalar)) < internal::traits<Derived>::Alignment) &&
183  "data is not aligned");
184 #endif
185  }
186 
187  template <typename T>
188  EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<internal::traits<T>::Alignment == 0, void*> = 0) const {
189 #ifndef EIGEN_ALLOW_UNALIGNED_SCALARS
190  // Pointer must be aligned to the Scalar type, otherwise we get UB.
191  eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned");
192 #endif
193  }
194 
198 };
199 
210 template <typename Derived>
211 class MapBase<Derived, WriteAccessors> : public MapBase<Derived, ReadOnlyAccessors> {
213 
214  public:
216 
217  typedef typename Base::Scalar Scalar;
218  typedef typename Base::PacketScalar PacketScalar;
219  typedef typename Base::StorageIndex StorageIndex;
220  typedef typename Base::PointerType PointerType;
221 
222  using Base::coeff;
223  using Base::coeffRef;
224  using Base::cols;
225  using Base::derived;
226  using Base::rows;
227  using Base::size;
228 
229  using Base::colStride;
230  using Base::innerStride;
231  using Base::outerStride;
232  using Base::rowStride;
233 
235 
236  EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return this->m_data; }
238  return this->m_data;
239  } // no const-cast here so non-const-correct code will give a compile error
240 
242  return this->m_data[col * colStride() + row * rowStride()];
243  }
244 
247  return this->m_data[index * innerStride()];
248  }
249 
250  template <int StoreMode>
251  inline void writePacket(Index row, Index col, const PacketScalar& val) {
252  internal::pstoret<Scalar, PacketScalar, StoreMode>(this->m_data + (col * colStride() + row * rowStride()), val);
253  }
254 
255  template <int StoreMode>
256  inline void writePacket(Index index, const PacketScalar& val) {
258  internal::pstoret<Scalar, PacketScalar, StoreMode>(this->m_data + index * innerStride(), val);
259  }
260 
261  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
262  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
263  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
264 
265  EIGEN_DEVICE_FUNC Derived& operator=(const MapBase& other) {
266  ReadOnlyMapBase::Base::operator=(other);
267  return derived();
268  }
269 
270  // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
271  // see bugs 821 and 920.
272  using ReadOnlyMapBase::Base::operator=;
273 
274  protected:
277 };
278 
279 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
280 
281 } // end namespace Eigen
282 
283 #endif // EIGEN_MAPBASE_H
#define EIGEN_DEFAULT_COPY_CONSTRUCTOR(CLASS)
Macro to explicitly define the default copy constructor. This is necessary, because the implicit defi...
Definition: Macros.h:1119
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Macro to manually define default constructors and destructors. This is necessary when the copy constr...
Definition: Macros.h:1137
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:922
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
Definition: MapBase.h:14
m col(1)
m row(1)
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:40
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for dense Map and Block expression with direct access.
Definition: MapBase.h:41
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: MapBase.h:97
PacketScalar packet(Index rowId, Index colId) const
Definition: MapBase.h:123
internal::dense_xpr_base< Derived >::type Base
Definition: MapBase.h:43
internal::packet_traits< Scalar >::type PacketScalar
Definition: MapBase.h:53
EIGEN_DEVICE_FUNC const Scalar & coeff(Index index) const
Definition: MapBase.h:105
Base::CoeffReturnType CoeffReturnType
Definition: MapBase.h:84
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr, Index vecSize)
Definition: MapBase.h:142
EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<(internal::traits< T >::Alignment > 0), void * >=0) const
Definition: MapBase.h:169
PacketScalar packet(Index index) const
Definition: MapBase.h:129
std::conditional_t< bool(internal::is_lvalue< Derived >::value), Scalar *, const Scalar * > PointerType
Definition: MapBase.h:55
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
Definition: MapBase.h:197
PointerType m_data
Definition: MapBase.h:195
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index index) const
Definition: MapBase.h:116
internal::traits< Derived >::Scalar Scalar
Definition: MapBase.h:52
EIGEN_DEVICE_FUNC const Scalar & coeff(Index rowId, Index colId) const
Definition: MapBase.h:100
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
Definition: MapBase.h:196
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr)
Definition: MapBase.h:135
NumTraits< Scalar >::Real RealScalar
Definition: MapBase.h:54
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index rowId, Index colId) const
Definition: MapBase.h:111
EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t< internal::traits< T >::Alignment==0, void * >=0) const
Definition: MapBase.h:188
internal::traits< Derived >::StorageKind StorageKind
Definition: MapBase.h:51
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr, Index rows, Index cols)
Definition: MapBase.h:153
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: MapBase.h:87
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: MapBase.h:89
Base::PacketScalar PacketScalar
Definition: MapBase.h:218
constexpr EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue * data()
Definition: MapBase.h:237
EIGEN_DEVICE_FUNC Derived & operator=(const MapBase &other)
Definition: MapBase.h:265
EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue & coeffRef(Index index)
Definition: MapBase.h:245
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr)
Definition: MapBase.h:261
MapBase< Derived, ReadOnlyAccessors > Base
Definition: MapBase.h:215
Base::StorageIndex StorageIndex
Definition: MapBase.h:219
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr, Index vecSize)
Definition: MapBase.h:262
void writePacket(Index row, Index col, const PacketScalar &val)
Definition: MapBase.h:251
MapBase< Derived, ReadOnlyAccessors > ReadOnlyMapBase
Definition: MapBase.h:212
Base::PointerType PointerType
Definition: MapBase.h:220
std::conditional_t< internal::is_lvalue< Derived >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: MapBase.h:234
void writePacket(Index index, const PacketScalar &val)
Definition: MapBase.h:256
EIGEN_DEVICE_FUNC MapBase(PointerType dataPtr, Index rows, Index cols)
Definition: MapBase.h:263
EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue & coeffRef(Index row, Index col)
Definition: MapBase.h:241
Base::Scalar Scalar
Definition: MapBase.h:217
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: MapBase.h:236
Definition: ForwardDeclarations.h:150
@ ReadOnlyAccessors
Definition: Constants.h:372
@ WriteAccessors
Definition: Constants.h:374
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
val
Definition: calibrate.py:119
type
Definition: compute_granudrum_aor.py:141
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:47
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: XprHelper.h:558
Definition: XprHelper.h:819
Definition: ForwardDeclarations.h:21