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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 // FIXME use proper doxygen documentation (e.g. \tparam MakePointer_)
18 
31 template<typename PlainObjectType, int Options_, template <class> class MakePointer_> class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> >
32 {
33  public:
36  #ifdef EIGEN_USE_SYCL
37  typedef std::remove_reference_t<typename Eigen::internal::nested<Self>::type> Nested;
38  #else
39  typedef typename Eigen::internal::nested<Self>::type Nested;
40  #endif
41  typedef typename internal::traits<PlainObjectType>::StorageKind StorageKind;
42  typedef typename internal::traits<PlainObjectType>::Index Index;
43  typedef typename internal::traits<PlainObjectType>::Scalar Scalar;
45  typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
46 
47  typedef typename MakePointer_<Scalar>::Type PointerType;
48  typedef typename MakePointer_<Scalar>::ConstType PointerConstType;
49 
50  // WARN: PointerType still can be a pointer to const (const Scalar*), for
51  // example in TensorMap<Tensor<const Scalar, ...>> expression. This type of
52  // expression should be illegal, but adding this restriction is not possible
53  // in practice (see https://bitbucket.org/eigen/eigen/pull-requests/488).
54  typedef std::conditional_t<
55  bool(internal::is_lvalue<PlainObjectType>::value),
56  PointerType, // use simple pointer in lvalue expressions
57  PointerConstType // use const pointer in rvalue expressions
59 
60  // If TensorMap was constructed over rvalue expression (e.g. const Tensor),
61  // we should return a reference to const from operator() (and others), even
62  // if TensorMap itself is not const.
63  typedef std::conditional_t<
64  bool(internal::is_lvalue<PlainObjectType>::value),
65  Scalar&,
66  const Scalar&
68 
69  static constexpr int Options = Options_;
70 
71  static constexpr Index NumIndices = PlainObjectType::NumIndices;
72  typedef typename PlainObjectType::Dimensions Dimensions;
73 
74  static constexpr int Layout = PlainObjectType::Layout;
75  enum {
76  IsAligned = ((int(Options_)&Aligned)==Aligned),
77  CoordAccess = true,
78  RawAccess = true
79  };
80 
82  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr) : m_data(dataPtr), m_dimensions() {
83  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
84  EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
85  }
86 
87  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
88  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension, IndexTypes... otherDimensions) : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
89  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
90  EIGEN_STATIC_ASSERT((sizeof...(otherDimensions) + 1 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
91  }
92 
94  : m_data(dataPtr), m_dimensions(dimensions)
95  { }
96 
97  template <typename Dimensions>
99  : m_data(dataPtr), m_dimensions(dimensions)
100  { }
101 
102  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType& tensor)
103  : m_data(tensor.data()), m_dimensions(tensor.dimensions())
104  { }
105 
107  EIGEN_STRONG_INLINE Index rank() const { return m_dimensions.rank(); }
109  EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_dimensions[n]; }
111  EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
113  EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
115  EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
117  EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
118 
120  EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const
121  {
122  // eigen_assert(checkIndexRange(indices));
123  if (PlainObjectType::Options&RowMajor) {
124  const Index index = m_dimensions.IndexOfRowMajor(indices);
125  return m_data[index];
126  } else {
127  const Index index = m_dimensions.IndexOfColMajor(indices);
128  return m_data[index];
129  }
130  }
131 
133  EIGEN_STRONG_INLINE StorageRefType operator()() const
134  {
135  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
136  return m_data[0];
137  }
138 
140  EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
141  {
142  eigen_internal_assert(index >= 0 && index < size());
143  return m_data[index];
144  }
145 
146  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
147  EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
148  {
149  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
151  if (PlainObjectType::Options&RowMajor) {
152  const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
153  return m_data[index];
154  } else {
155  const Index index = m_dimensions.IndexOfColMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
156  return m_data[index];
157  }
158  }
159 
161  EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices)
162  {
163  // eigen_assert(checkIndexRange(indices));
164  if (PlainObjectType::Options&RowMajor) {
165  const Index index = m_dimensions.IndexOfRowMajor(indices);
166  return m_data[index];
167  } else {
168  const Index index = m_dimensions.IndexOfColMajor(indices);
169  return m_data[index];
170  }
171  }
172 
174  EIGEN_STRONG_INLINE StorageRefType operator()()
175  {
176  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
177  return m_data[0];
178  }
179 
181  EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
182  {
183  eigen_internal_assert(index >= 0 && index < size());
184  return m_data[index];
185  }
186 
187  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
188  EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
189  {
190  static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic, "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
192  const std::size_t NumDims = sizeof...(otherIndices) + 2;
193  if (PlainObjectType::Options&RowMajor) {
194  const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
195  return m_data[index];
196  } else {
197  const Index index = m_dimensions.IndexOfColMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
198  return m_data[index];
199  }
200  }
201 
203 
204  private:
207 };
208 
209 } // end namespace Eigen
210 
211 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H
int n
#define eigen_internal_assert(x)
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_STATIC_ASSERT(X, MSG)
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: TensorMacros.h:80
The tensor base class.
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:32
Index dimension(Index n) const
Definition: TensorMap.h:109
PlainObjectType::Dimensions Dimensions
Definition: TensorMap.h:72
Index rank() const
Definition: TensorMap.h:107
StorageRefType operator()()
Definition: TensorMap.h:174
const Dimensions & dimensions() const
Definition: TensorMap.h:111
Eigen::internal::nested< Self >::type Nested
Definition: TensorMap.h:39
TensorMap(PlainObjectType &tensor)
Definition: TensorMap.h:102
MakePointer_< Scalar >::ConstType PointerConstType
Definition: TensorMap.h:48
MakePointer_< Scalar >::Type PointerType
Definition: TensorMap.h:47
StoragePointerType data() const
Definition: TensorMap.h:117
TensorMap(StoragePointerType dataPtr, const Dimensions &dimensions)
Definition: TensorMap.h:98
TensorMap(StoragePointerType dataPtr, Index firstDimension, IndexTypes... otherDimensions)
Definition: TensorMap.h:88
Index size() const
Definition: TensorMap.h:113
StorageRefType operator()(Index index)
Definition: TensorMap.h:181
std::conditional_t< bool(internal::is_lvalue< PlainObjectType >::value), Scalar &, const Scalar & > StorageRefType
Definition: TensorMap.h:67
StorageRefType operator()() const
Definition: TensorMap.h:133
StoragePointerType m_data
Definition: TensorMap.h:205
static constexpr Index NumIndices
Definition: TensorMap.h:71
Dimensions m_dimensions
Definition: TensorMap.h:206
StorageRefType operator()(const array< Index, NumIndices > &indices)
Definition: TensorMap.h:161
TensorMap< PlainObjectType, Options_, MakePointer_ > Self
Definition: TensorMap.h:34
StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
Definition: TensorMap.h:188
StoragePointerType data()
Definition: TensorMap.h:115
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition: TensorMap.h:41
internal::traits< PlainObjectType >::Scalar Scalar
Definition: TensorMap.h:43
std::conditional_t< bool(internal::is_lvalue< PlainObjectType >::value), PointerType, PointerConstType > StoragePointerType
Definition: TensorMap.h:58
static constexpr int Options
Definition: TensorMap.h:69
PlainObjectType::Base::CoeffReturnType CoeffReturnType
Definition: TensorMap.h:45
StorageRefType operator()(const array< Index, NumIndices > &indices) const
Definition: TensorMap.h:120
TensorBase< TensorMap< PlainObjectType, Options_, MakePointer_ > > Base
Definition: TensorMap.h:35
NumTraits< Scalar >::Real RealScalar
Definition: TensorMap.h:44
internal::traits< PlainObjectType >::Index Index
Definition: TensorMap.h:42
static constexpr int Layout
Definition: TensorMap.h:74
StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
Definition: TensorMap.h:147
TensorMap(StoragePointerType dataPtr)
Definition: TensorMap.h:82
TensorMap(StoragePointerType dataPtr, const array< Index, NumIndices > &dimensions)
Definition: TensorMap.h:93
StorageRefType operator()(Index index) const
Definition: TensorMap.h:140
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
const int Dynamic