$darkmode
Eigen-unsupported  5.0.1-dev
TensorMap.h
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 
26 template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
33 class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> > {
34  public:
35  typedef TensorMap<PlainObjectType, Options_, MakePointer_> Self;
36  typedef TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> > Base;
37 #ifdef EIGEN_USE_SYCL
38  typedef std::remove_reference_t<typename Eigen::internal::nested<Self>::type> Nested;
39 #else
40  typedef typename Eigen::internal::nested<Self>::type Nested;
41 #endif
42  typedef typename internal::traits<PlainObjectType>::StorageKind StorageKind;
43  typedef typename internal::traits<PlainObjectType>::Index Index;
44  typedef typename internal::traits<PlainObjectType>::Scalar Scalar;
45  typedef typename NumTraits<Scalar>::Real RealScalar;
46  typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
47 
48  typedef typename MakePointer_<Scalar>::Type PointerType;
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  >
59  StoragePointerType;
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.
64  typedef std::conditional_t<bool(internal::is_lvalue<PlainObjectType>::value), Scalar&, const Scalar&> StorageRefType;
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 
74  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr) : m_data(dataPtr), m_dimensions() {
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>
80  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension,
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 
88  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr,
89  const array<Index, NumIndices>& dimensions)
90  : m_data(dataPtr), m_dimensions(dimensions) {}
91 
92  template <typename Dimensions>
93  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions& dimensions)
94  : m_data(dataPtr), m_dimensions(dimensions) {}
95 
96  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType& tensor)
97  : m_data(tensor.data()), m_dimensions(tensor.dimensions()) {}
98 
99  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return m_dimensions.rank(); }
100  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_dimensions[n]; }
101  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
102  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
103  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
104  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
105 
106  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const {
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 
117  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() const {
118  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
119  return m_data[0];
120  }
121 
122  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const {
123  eigen_internal_assert(index >= 0 && index < size());
124  return m_data[index];
125  }
126 
127  template <typename... IndexTypes>
128  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex,
129  IndexTypes... otherIndices) const {
130  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
131  eigen_assert(internal::all((Eigen::NumTraits<Index>::highest() >= otherIndices)...));
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 
143  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) {
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 
154  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() {
155  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
156  return m_data[0];
157  }
158 
159  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) {
160  eigen_internal_assert(index >= 0 && index < size());
161  return m_data[index];
162  }
163 
164  template <typename... IndexTypes>
165  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex,
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.");
169  eigen_assert(internal::all((Eigen::NumTraits<Index>::highest() >= otherIndices)...));
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 
182  EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorMap)
183 
184  private:
185  StoragePointerType m_data;
186  Dimensions m_dimensions;
187 };
188 
189 } // end namespace Eigen
190 
191 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H
static constexpr Eigen::internal::all_t all
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const int Dynamic