Eigen-Contrib  5.0.1
 
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 typedef TensorMap<PlainObjectType, Options_, MakePointer_> Self;
38#ifdef EIGEN_USE_SYCL
39 typedef std::remove_reference_t<typename Eigen::internal::ref_selector<Self>::type> Nested;
40#else
41 typedef typename Eigen::internal::ref_selector<Self>::type Nested;
42#endif
43 typedef typename internal::traits<PlainObjectType>::StorageKind StorageKind;
44 typedef typename internal::traits<PlainObjectType>::Index Index;
45 typedef typename internal::traits<PlainObjectType>::Scalar Scalar;
46 typedef typename NumTraits<Scalar>::Real RealScalar;
47 typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
48
49 typedef typename MakePointer_<Scalar>::Type PointerType;
50 typedef typename MakePointer_<Scalar>::ConstType PointerConstType;
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 typedef 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 StoragePointerType;
61
62 // If TensorMap was constructed over rvalue expression (e.g. const Tensor),
63 // we should return a reference to const from operator() (and others), even
64 // if TensorMap itself is not const.
65 typedef std::conditional_t<bool(internal::is_lvalue<PlainObjectType>::value), Scalar&, const Scalar&> StorageRefType;
66
67 static constexpr int Options = Options_;
68
69 static constexpr Index NumIndices = PlainObjectType::NumIndices;
70 typedef typename PlainObjectType::Dimensions Dimensions;
71
72 static constexpr int Layout = PlainObjectType::Layout;
73 enum { IsAligned = ((int(Options_) & Aligned) == Aligned), CoordAccess = true, RawAccess = true };
74
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr) : m_data(dataPtr), m_dimensions() {
76 // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
77 EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
78 }
79
80 template <typename... IndexTypes>
81 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension,
82 IndexTypes... otherDimensions)
83 : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
84 // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
85 EIGEN_STATIC_ASSERT((sizeof...(otherDimensions) + 1 == NumIndices || NumIndices == Dynamic),
86 YOU_MADE_A_PROGRAMMING_MISTAKE)
87 }
88
89 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr,
90 const array<Index, NumIndices>& dimensions)
91 : m_data(dataPtr), m_dimensions(dimensions) {}
92
93 template <typename Dimensions>
94 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions& dimensions)
95 : m_data(dataPtr), m_dimensions(dimensions) {}
96
97 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType& tensor)
98 : m_data(tensor.data()), m_dimensions(tensor.dimensions()) {}
99
100 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return m_dimensions.rank(); }
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_dimensions[n]; }
102 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
104 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
106
107 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const {
108 EIGEN_IF_CONSTEXPR (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::indices_fit<Index>(otherIndices...));
132 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
133 const Index index = m_dimensions.IndexOfRowMajor(
134 array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
135 return m_data[index];
136 } else {
137 const Index index = m_dimensions.IndexOfColMajor(
138 array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(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_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
145 const Index index = m_dimensions.IndexOfRowMajor(indices);
146 return m_data[index];
147 } else {
148 const Index index = m_dimensions.IndexOfColMajor(indices);
149 return m_data[index];
150 }
151 }
152
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() {
154 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
155 return m_data[0];
156 }
157
158 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) {
159 eigen_internal_assert(index >= 0 && index < size());
160 return m_data[index];
161 }
162
163 template <typename... IndexTypes>
164 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex,
165 IndexTypes... otherIndices) {
166 static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic,
167 "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
168 eigen_assert(internal::indices_fit<Index>(otherIndices...));
169 const std::size_t NumDims = sizeof...(otherIndices) + 2;
170 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
171 const Index index = m_dimensions.IndexOfRowMajor(
172 array<Index, NumDims>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
173 return m_data[index];
174 } else {
175 const Index index = m_dimensions.IndexOfColMajor(
176 array<Index, NumDims>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
177 return m_data[index];
178 }
179 }
180
181 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorMap)
182
183 private:
184 StoragePointerType m_data;
185 Dimensions m_dimensions;
186};
187
188} // end namespace Eigen
189
190#endif // EIGEN_TENSOR_TENSOR_MAP_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Namespace containing all symbols from the Eigen library.