Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_MAP_H
12#define EIGEN_TENSOR_TENSOR_MAP_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19// FIXME: Use proper doxygen documentation (e.g. \tparam MakePointer_).
20
33template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
34class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> > {
35 public:
36 using Self = TensorMap<PlainObjectType, Options_, MakePointer_>;
38#ifdef EIGEN_USE_SYCL
39 using Nested = std::remove_reference_t<typename Eigen::internal::ref_selector<Self>::type>;
40#else
41 using Nested = typename Eigen::internal::ref_selector<Self>::type;
42#endif
43 using StorageKind = typename internal::traits<PlainObjectType>::StorageKind;
44 using Index = typename internal::traits<PlainObjectType>::Index;
45 using Scalar = typename internal::traits<PlainObjectType>::Scalar;
46 using RealScalar = typename NumTraits<Scalar>::Real;
47 using CoeffReturnType = typename PlainObjectType::Base::CoeffReturnType;
48
49 using PointerType = typename MakePointer_<Scalar>::Type;
50 using PointerConstType = typename MakePointer_<Scalar>::ConstType;
51
52 // WARN: PointerType still can be a pointer to const (const Scalar*), for
53 // example in TensorMap<Tensor<const Scalar, ...>> expression. This type of
54 // expression should be illegal, but adding this restriction is not possible
55 // in practice (see https://bitbucket.org/eigen/eigen/pull-requests/488).
56 using StoragePointerType = std::conditional_t<bool(internal::is_lvalue<PlainObjectType>::value),
57 PointerType, // use simple pointer in lvalue expressions
58 PointerConstType // use const pointer in rvalue expressions
59 >;
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 using StorageRefType = std::conditional_t<bool(internal::is_lvalue<PlainObjectType>::value), Scalar&, const Scalar&>;
65
66 static constexpr int Options = Options_;
67
68 static constexpr Index NumIndices = PlainObjectType::NumIndices;
69 using Dimensions = typename PlainObjectType::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 NumIndices; }
100 template <Index N>
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension() const {
102 EIGEN_STATIC_ASSERT(0 <= N && N < NumIndices, INVALID_DIMENSION);
103 return m_dimensions[N];
104 }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const {
106 eigen_assert(0 <= n && n < NumIndices);
107 return m_dimensions[n];
108 }
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
111 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
112 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
113
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const {
115 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
116 const Index index = m_dimensions.IndexOfRowMajor(indices);
117 return m_data[index];
118 } else {
119 const Index index = m_dimensions.IndexOfColMajor(indices);
120 return m_data[index];
121 }
122 }
123
124 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() const {
125 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
126 return m_data[0];
127 }
128
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const {
130 eigen_internal_assert(index >= 0 && index < size());
131 return m_data[index];
132 }
133
134 template <typename... IndexTypes>
135 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex,
136 IndexTypes... otherIndices) const {
137 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
138 eigen_assert(internal::indices_fit<Index>(otherIndices...));
139 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
140 const Index index = m_dimensions.IndexOfRowMajor(
141 array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
142 return m_data[index];
143 } else {
144 const Index index = m_dimensions.IndexOfColMajor(
145 array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
146 return m_data[index];
147 }
148 }
149
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) {
151 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
152 const Index index = m_dimensions.IndexOfRowMajor(indices);
153 return m_data[index];
154 } else {
155 const Index index = m_dimensions.IndexOfColMajor(indices);
156 return m_data[index];
157 }
158 }
159
160 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() {
161 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
162 return m_data[0];
163 }
164
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) {
166 eigen_internal_assert(index >= 0 && index < size());
167 return m_data[index];
168 }
169
170 template <typename... IndexTypes>
171 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex,
172 IndexTypes... otherIndices) {
173 static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic,
174 "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
175 eigen_assert(internal::indices_fit<Index>(otherIndices...));
176 const std::size_t NumDims = sizeof...(otherIndices) + 2;
177 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
178 const Index index = m_dimensions.IndexOfRowMajor(
179 array<Index, NumDims>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
180 return m_data[index];
181 } else {
182 const Index index = m_dimensions.IndexOfColMajor(
183 array<Index, NumDims>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
184 return m_data[index];
185 }
186 }
187
188 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorMap)
189
190 private:
191 StoragePointerType m_data;
192 Dimensions m_dimensions;
193};
194
195#if EIGEN_COMP_CXXVER < 17
196template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
197constexpr typename TensorMap<PlainObjectType, Options_, MakePointer_>::Index
198 TensorMap<PlainObjectType, Options_, MakePointer_>::NumIndices;
199template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
200constexpr int TensorMap<PlainObjectType, Options_, MakePointer_>::Layout;
201template <typename PlainObjectType, int Options_, template <class> class MakePointer_>
202constexpr int TensorMap<PlainObjectType, Options_, MakePointer_>::Options;
203#endif
204
205} // end namespace Eigen
206
207#endif // EIGEN_TENSOR_TENSOR_MAP_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Namespace containing all symbols from the Eigen library.