Loading...
Searching...
No Matches
TensorShuffling.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_SHUFFLING_H
11#define EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename Shuffle, typename XprType>
17struct traits<TensorShufflingOp<Shuffle, XprType> > : public traits<XprType>
18{
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions;
26 static const int Layout = XprTraits::Layout;
27};
28
29template<typename Shuffle, typename XprType>
30struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense>
31{
32 typedef const TensorShufflingOp<Shuffle, XprType>& type;
33};
34
35template<typename Shuffle, typename XprType>
36struct nested<TensorShufflingOp<Shuffle, XprType>, 1, typename eval<TensorShufflingOp<Shuffle, XprType> >::type>
37{
38 typedef TensorShufflingOp<Shuffle, XprType> type;
39};
40
41} // end namespace internal
42
48template <typename Shuffle, typename XprType>
49class TensorShufflingOp : public TensorBase<TensorShufflingOp<Shuffle, XprType> > {
50 public:
51 typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
52 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorShufflingOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorShufflingOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorShufflingOp>::Index Index;
57
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(const XprType& expr, const Shuffle& shuffle)
59 : m_xpr(expr), m_shuffle(shuffle) {}
60
61 EIGEN_DEVICE_FUNC
62 const Shuffle& shufflePermutation() const { return m_shuffle; }
63
64 EIGEN_DEVICE_FUNC
65 const typename internal::remove_all<typename XprType::Nested>::type&
66 expression() const { return m_xpr; }
67
68 EIGEN_DEVICE_FUNC
69 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (const TensorShufflingOp& other)
70 {
72 Assign assign(*this, other);
73 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
74 return *this;
75 }
76
77 template<typename OtherDerived>
78 EIGEN_DEVICE_FUNC
79 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (const OtherDerived& other)
80 {
82 Assign assign(*this, other);
83 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
84 return *this;
85 }
86
87 protected:
88 typename XprType::Nested m_xpr;
89 const Shuffle m_shuffle;
90};
91
92
93// Eval as rvalue
94template<typename Shuffle, typename ArgType, typename Device>
95struct TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
96{
98 typedef typename XprType::Index Index;
99 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
100 typedef DSizes<Index, NumDims> Dimensions;
101 typedef typename XprType::Scalar Scalar;
102 typedef typename XprType::CoeffReturnType CoeffReturnType;
103 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
104 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
105
106 enum {
107 IsAligned = false,
108 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
109 Layout = TensorEvaluator<ArgType, Device>::Layout,
110 CoordAccess = false, // to be implemented
111 RawAccess = false
112 };
113
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
115 : m_impl(op.expression(), device)
116 {
117 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
118 const Shuffle& shuffle = op.shufflePermutation();
119 for (int i = 0; i < NumDims; ++i) {
120 m_dimensions[i] = input_dims[shuffle[i]];
121 }
122
123 array<Index, NumDims> inputStrides;
124
125 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
126 inputStrides[0] = 1;
127 m_outputStrides[0] = 1;
128 for (int i = 1; i < NumDims; ++i) {
129 inputStrides[i] = inputStrides[i - 1] * input_dims[i - 1];
130 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
131 }
132 } else {
133 inputStrides[NumDims - 1] = 1;
134 m_outputStrides[NumDims - 1] = 1;
135 for (int i = NumDims - 2; i >= 0; --i) {
136 inputStrides[i] = inputStrides[i + 1] * input_dims[i + 1];
137 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
138 }
139 }
140
141 for (int i = 0; i < NumDims; ++i) {
142 m_inputStrides[i] = inputStrides[shuffle[i]];
143 }
144 }
145
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
147
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
149 m_impl.evalSubExprsIfNeeded(NULL);
150 return true;
151 }
152 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
153 m_impl.cleanup();
154 }
155
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
157 {
158 return m_impl.coeff(srcCoeff(index));
159 }
160
161 template<int LoadMode>
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
163 {
164 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
165 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
166
167 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
168 for (int i = 0; i < PacketSize; ++i) {
169 values[i] = coeff(index+i);
170 }
171 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
172 return rslt;
173 }
174
175 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
176 const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
177 2 * TensorOpCost::MulCost<Index>() +
178 TensorOpCost::DivCost<Index>());
179 return m_impl.costPerCoeff(vectorized) +
180 TensorOpCost(0, 0, compute_cost, false /* vectorized */, PacketSize);
181 }
182
183 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
184
185 protected:
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const {
187 Index inputIndex = 0;
188 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
189 for (int i = NumDims - 1; i > 0; --i) {
190 const Index idx = index / m_outputStrides[i];
191 inputIndex += idx * m_inputStrides[i];
192 index -= idx * m_outputStrides[i];
193 }
194 return inputIndex + index * m_inputStrides[0];
195 } else {
196 for (int i = 0; i < NumDims - 1; ++i) {
197 const Index idx = index / m_outputStrides[i];
198 inputIndex += idx * m_inputStrides[i];
199 index -= idx * m_outputStrides[i];
200 }
201 return inputIndex + index * m_inputStrides[NumDims - 1];
202 }
203 }
204
205 Dimensions m_dimensions;
206 array<Index, NumDims> m_outputStrides;
207 array<Index, NumDims> m_inputStrides;
208 TensorEvaluator<ArgType, Device> m_impl;
209};
210
211
212// Eval as lvalue
213template<typename Shuffle, typename ArgType, typename Device>
214struct TensorEvaluator<TensorShufflingOp<Shuffle, ArgType>, Device>
215 : public TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
216{
217 typedef TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> Base;
218
219 typedef TensorShufflingOp<Shuffle, ArgType> XprType;
220 typedef typename XprType::Index Index;
221 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
222 typedef DSizes<Index, NumDims> Dimensions;
223 typedef typename XprType::Scalar Scalar;
224 typedef typename XprType::CoeffReturnType CoeffReturnType;
225 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
226 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
227
228 enum {
229 IsAligned = false,
230 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
231 RawAccess = false
232 };
233
234 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
235 : Base(op, device)
236 { }
237
238 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
239 {
240 return this->m_impl.coeffRef(this->srcCoeff(index));
241 }
242
243 template <int StoreMode> EIGEN_STRONG_INLINE
244 void writePacket(Index index, const PacketReturnType& x)
245 {
246 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
247
248 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
249 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
250 for (int i = 0; i < PacketSize; ++i) {
251 this->coeffRef(index+i) = values[i];
252 }
253 }
254};
255
256
257} // end namespace Eigen
258
259#endif // EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H
Definition TensorAssign.h:56
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor shuffling class.
Definition TensorShuffling.h:49
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112