TensorMap.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) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_MAP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_MAP_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 // FIXME use proper doxygen documentation (e.g. \tparam MakePointer_)
19 
32 template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
33 class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> > {
34  public:
37 #ifdef EIGEN_USE_SYCL
39 #else
41 #endif
46  typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
47 
49  typedef typename MakePointer_<Scalar>::ConstType PointerConstType;
50 
51  // WARN: PointerType still can be a pointer to const (const Scalar*), for
52  // example in TensorMap<Tensor<const Scalar, ...>> expression. This type of
53  // expression should be illegal, but adding this restriction is not possible
54  // in practice (see https://bitbucket.org/eigen/eigen/pull-requests/488).
55  typedef std::conditional_t<bool(internal::is_lvalue<PlainObjectType>::value),
56  PointerType, // use simple pointer in lvalue expressions
57  PointerConstType // use const pointer in rvalue expressions
58  >
60 
61  // If TensorMap was constructed over rvalue expression (e.g. const Tensor),
62  // we should return a reference to const from operator() (and others), even
63  // if TensorMap itself is not const.
65 
66  static constexpr int Options = Options_;
67 
68  static constexpr Index NumIndices = PlainObjectType::NumIndices;
69  typedef typename PlainObjectType::Dimensions Dimensions;
70 
71  static constexpr int Layout = PlainObjectType::Layout;
72  enum { IsAligned = ((int(Options_) & Aligned) == Aligned), CoordAccess = true, RawAccess = true };
73 
75  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
76  EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
77  }
78 
79  template <typename... IndexTypes>
81  IndexTypes... otherDimensions)
82  : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
83  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
84  EIGEN_STATIC_ASSERT((sizeof...(otherDimensions) + 1 == NumIndices || NumIndices == Dynamic),
85  YOU_MADE_A_PROGRAMMING_MISTAKE)
86  }
87 
90  : m_data(dataPtr), m_dimensions(dimensions) {}
91 
92  template <typename Dimensions>
94  : m_data(dataPtr), m_dimensions(dimensions) {}
95 
97  : m_data(tensor.data()), m_dimensions(tensor.dimensions()) {}
98 
105 
107  // eigen_assert(checkIndexRange(indices));
108  if (PlainObjectType::Options & RowMajor) {
109  const Index index = m_dimensions.IndexOfRowMajor(indices);
110  return m_data[index];
111  } else {
112  const Index index = m_dimensions.IndexOfColMajor(indices);
113  return m_data[index];
114  }
115  }
116 
118  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
119  return m_data[0];
120  }
121 
123  eigen_internal_assert(index >= 0 && index < size());
124  return m_data[index];
125  }
126 
127  template <typename... IndexTypes>
129  IndexTypes... otherIndices) const {
130  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
132  if (PlainObjectType::Options & RowMajor) {
133  const Index index =
134  m_dimensions.IndexOfRowMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
135  return m_data[index];
136  } else {
137  const Index index =
138  m_dimensions.IndexOfColMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
139  return m_data[index];
140  }
141  }
142 
144  // eigen_assert(checkIndexRange(indices));
145  if (PlainObjectType::Options & RowMajor) {
146  const Index index = m_dimensions.IndexOfRowMajor(indices);
147  return m_data[index];
148  } else {
149  const Index index = m_dimensions.IndexOfColMajor(indices);
150  return m_data[index];
151  }
152  }
153 
155  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
156  return m_data[0];
157  }
158 
160  eigen_internal_assert(index >= 0 && index < size());
161  return m_data[index];
162  }
163 
164  template <typename... IndexTypes>
166  IndexTypes... otherIndices) {
167  static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic,
168  "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
170  const std::size_t NumDims = sizeof...(otherIndices) + 2;
171  if (PlainObjectType::Options & RowMajor) {
172  const Index index =
173  m_dimensions.IndexOfRowMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
174  return m_data[index];
175  } else {
176  const Index index =
177  m_dimensions.IndexOfColMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
178  return m_data[index];
179  }
180  }
181 
183 
184  private:
187 };
188 
189 } // end namespace Eigen
190 
191 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define eigen_internal_assert(x)
Definition: Macros.h:916
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Macro to manually inherit assignment operators. This is necessary, because the implicitly defined ass...
Definition: TensorMacros.h:81
The tensor base class.
Definition: TensorBase.h:1026
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorMap.h:99
PlainObjectType::Dimensions Dimensions
Definition: TensorMap.h:69
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension, IndexTypes... otherDimensions)
Definition: TensorMap.h:80
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices)
Definition: TensorMap.h:143
std::conditional_t< bool(internal::is_lvalue< PlainObjectType >::value), Scalar &, const Scalar & > StorageRefType
Definition: TensorMap.h:64
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType &tensor)
Definition: TensorMap.h:96
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
Definition: TensorMap.h:128
Eigen::internal::nested< Self >::type Nested
Definition: TensorMap.h:40
MakePointer_< Scalar >::ConstType PointerConstType
Definition: TensorMap.h:49
MakePointer_< Scalar >::Type PointerType
Definition: TensorMap.h:48
std::conditional_t< bool(internal::is_lvalue< PlainObjectType >::value), PointerType, PointerConstType > StoragePointerType
Definition: TensorMap.h:59
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices) const
Definition: TensorMap.h:106
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const
Definition: TensorMap.h:104
@ CoordAccess
Definition: TensorMap.h:72
@ RawAccess
Definition: TensorMap.h:72
@ IsAligned
Definition: TensorMap.h:72
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorMap.h:102
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data()
Definition: TensorMap.h:103
StoragePointerType m_data
Definition: TensorMap.h:185
static constexpr Index NumIndices
Definition: TensorMap.h:68
Dimensions m_dimensions
Definition: TensorMap.h:186
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
Definition: TensorMap.h:165
TensorMap< PlainObjectType, Options_, MakePointer_ > Self
Definition: TensorMap.h:35
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition: TensorMap.h:42
internal::traits< PlainObjectType >::Scalar Scalar
Definition: TensorMap.h:44
static constexpr int Options
Definition: TensorMap.h:66
PlainObjectType::Base::CoeffReturnType CoeffReturnType
Definition: TensorMap.h:46
TensorBase< TensorMap< PlainObjectType, Options_, MakePointer_ > > Base
Definition: TensorMap.h:36
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() const
Definition: TensorMap.h:117
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr)
Definition: TensorMap.h:74
NumTraits< Scalar >::Real RealScalar
Definition: TensorMap.h:45
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()()
Definition: TensorMap.h:154
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
Definition: TensorMap.h:159
internal::traits< PlainObjectType >::Index Index
Definition: TensorMap.h:43
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
Definition: TensorMap.h:122
static constexpr int Layout
Definition: TensorMap.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const array< Index, NumIndices > &dimensions)
Definition: TensorMap.h:88
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorMap.h:101
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorMap.h:100
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions &dimensions)
Definition: TensorMap.h:93
@ Aligned
Definition: Constants.h:242
@ RowMajor
Definition: Constants.h:320
return int(ret)+1
EIGEN_DEVICE_FUNC bool all()
Definition: Macros.h:1276
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
std::array< T, N > array
Definition: EmulateArray.h:231
const int Dynamic
Definition: Constants.h:25
type
Definition: compute_granudrum_aor.py:141
Type
Type of JSON value.
Definition: rapidjson.h:513
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: XprHelper.h:819
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: ForwardDeclarations.h:21