Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
Loading...
Searching...
No Matches
TensorStriding.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_STRIDING_H
11#define EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename Strides, typename XprType>
17struct traits<TensorStridingOp<Strides, 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 typedef typename XprTraits::PointerType PointerType;
28};
29
30template<typename Strides, typename XprType>
31struct eval<TensorStridingOp<Strides, XprType>, Eigen::Dense>
32{
33 typedef const TensorStridingOp<Strides, XprType>EIGEN_DEVICE_REF type;
34};
35
36template<typename Strides, typename XprType>
37struct nested<TensorStridingOp<Strides, XprType>, 1, typename eval<TensorStridingOp<Strides, XprType> >::type>
38{
39 typedef TensorStridingOp<Strides, XprType> type;
40};
41
42} // end namespace internal
43
49template <typename Strides, typename XprType>
50class TensorStridingOp : public TensorBase<TensorStridingOp<Strides, XprType> > {
51 public:
53 typedef typename Eigen::internal::traits<TensorStridingOp>::Scalar Scalar;
54 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
55 typedef typename XprType::CoeffReturnType CoeffReturnType;
56 typedef typename Eigen::internal::nested<TensorStridingOp>::type Nested;
57 typedef typename Eigen::internal::traits<TensorStridingOp>::StorageKind StorageKind;
58 typedef typename Eigen::internal::traits<TensorStridingOp>::Index Index;
59
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStridingOp(const XprType& expr, const Strides& dims)
61 : m_xpr(expr), m_dims(dims) {}
62
63 EIGEN_DEVICE_FUNC
64 const Strides& strides() const { return m_dims; }
65
66 EIGEN_DEVICE_FUNC
67 const typename internal::remove_all<typename XprType::Nested>::type&
68 expression() const { return m_xpr; }
69
70 EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorStridingOp)
71
72 protected:
73 typename XprType::Nested m_xpr;
74 const Strides m_dims;
75};
76
77
78// Eval as rvalue
79template<typename Strides, typename ArgType, typename Device>
80struct TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
81{
83 typedef typename XprType::Index Index;
84 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
85 typedef DSizes<Index, NumDims> Dimensions;
86 typedef typename XprType::Scalar Scalar;
87 typedef typename XprType::CoeffReturnType CoeffReturnType;
88 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
89 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
90 typedef StorageMemory<CoeffReturnType, Device> Storage;
91 typedef typename Storage::Type EvaluatorPointerType;
92
93 enum {
94 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
95 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
96 BlockAccess = false,
97 PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
98 Layout = TensorEvaluator<ArgType, Device>::Layout,
99 CoordAccess = false, // to be implemented
100 RawAccess = false
101 };
102
103 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
104 typedef internal::TensorBlockNotImplemented TensorBlock;
105 //===--------------------------------------------------------------------===//
106
107 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
108 : m_impl(op.expression(), device)
109 {
110 m_dimensions = m_impl.dimensions();
111 for (int i = 0; i < NumDims; ++i) {
112 m_dimensions[i] =Eigen::numext::ceil(static_cast<float>(m_dimensions[i]) / op.strides()[i]);
113 }
114
115 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
116 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
117 m_outputStrides[0] = 1;
118 m_inputStrides[0] = 1;
119 for (int i = 1; i < NumDims; ++i) {
120 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
121 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
122 m_inputStrides[i-1] *= op.strides()[i-1];
123 }
124 m_inputStrides[NumDims-1] *= op.strides()[NumDims-1];
125 } else { // RowMajor
126 m_outputStrides[NumDims-1] = 1;
127 m_inputStrides[NumDims-1] = 1;
128 for (int i = NumDims - 2; i >= 0; --i) {
129 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
130 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
131 m_inputStrides[i+1] *= op.strides()[i+1];
132 }
133 m_inputStrides[0] *= op.strides()[0];
134 }
135 }
136
137
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
139
140 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType/*data*/) {
141 m_impl.evalSubExprsIfNeeded(NULL);
142 return true;
143 }
144 EIGEN_STRONG_INLINE void cleanup() {
145 m_impl.cleanup();
146 }
147
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
149 {
150 return m_impl.coeff(srcCoeff(index));
151 }
152
153 template<int LoadMode>
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
155 {
156 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
157 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
158
159 Index inputIndices[] = {0, 0};
160 Index indices[] = {index, index + PacketSize - 1};
161 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
162 EIGEN_UNROLL_LOOP
163 for (int i = NumDims - 1; i > 0; --i) {
164 const Index idx0 = indices[0] / m_outputStrides[i];
165 const Index idx1 = indices[1] / m_outputStrides[i];
166 inputIndices[0] += idx0 * m_inputStrides[i];
167 inputIndices[1] += idx1 * m_inputStrides[i];
168 indices[0] -= idx0 * m_outputStrides[i];
169 indices[1] -= idx1 * m_outputStrides[i];
170 }
171 inputIndices[0] += indices[0] * m_inputStrides[0];
172 inputIndices[1] += indices[1] * m_inputStrides[0];
173 } else { // RowMajor
174 EIGEN_UNROLL_LOOP
175 for (int i = 0; i < NumDims - 1; ++i) {
176 const Index idx0 = indices[0] / m_outputStrides[i];
177 const Index idx1 = indices[1] / m_outputStrides[i];
178 inputIndices[0] += idx0 * m_inputStrides[i];
179 inputIndices[1] += idx1 * m_inputStrides[i];
180 indices[0] -= idx0 * m_outputStrides[i];
181 indices[1] -= idx1 * m_outputStrides[i];
182 }
183 inputIndices[0] += indices[0] * m_inputStrides[NumDims-1];
184 inputIndices[1] += indices[1] * m_inputStrides[NumDims-1];
185 }
186 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
187 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
188 return rslt;
189 }
190 else {
191 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
192 values[0] = m_impl.coeff(inputIndices[0]);
193 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
194 EIGEN_UNROLL_LOOP
195 for (int i = 1; i < PacketSize-1; ++i) {
196 values[i] = coeff(index+i);
197 }
198 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
199 return rslt;
200 }
201 }
202
203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
204 double compute_cost = (NumDims - 1) * (TensorOpCost::AddCost<Index>() +
205 TensorOpCost::MulCost<Index>() +
206 TensorOpCost::DivCost<Index>()) +
207 TensorOpCost::MulCost<Index>();
208 if (vectorized) {
209 compute_cost *= 2; // packet() computes two indices
210 }
211 const int innerDim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : (NumDims - 1);
212 return m_impl.costPerCoeff(vectorized && m_inputStrides[innerDim] == 1) +
213 // Computation is not vectorized per se, but it is done once per packet.
214 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
215 }
216
217 EIGEN_DEVICE_FUNC typename Storage::Type data() const { return NULL; }
218
219#ifdef EIGEN_USE_SYCL
220 // binding placeholder accessors to a command group handler for SYCL
221 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
222 m_impl.bind(cgh);
223 }
224#endif
225 protected:
226 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
227 {
228 Index inputIndex = 0;
229 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
230 EIGEN_UNROLL_LOOP
231 for (int i = NumDims - 1; i > 0; --i) {
232 const Index idx = index / m_outputStrides[i];
233 inputIndex += idx * m_inputStrides[i];
234 index -= idx * m_outputStrides[i];
235 }
236 inputIndex += index * m_inputStrides[0];
237 } else { // RowMajor
238 EIGEN_UNROLL_LOOP
239 for (int i = 0; i < NumDims - 1; ++i) {
240 const Index idx = index / m_outputStrides[i];
241 inputIndex += idx * m_inputStrides[i];
242 index -= idx * m_outputStrides[i];
243 }
244 inputIndex += index * m_inputStrides[NumDims-1];
245 }
246 return inputIndex;
247 }
248
249 Dimensions m_dimensions;
250 array<Index, NumDims> m_outputStrides;
251 array<Index, NumDims> m_inputStrides;
252 TensorEvaluator<ArgType, Device> m_impl;
253};
254
255// Eval as lvalue
256template<typename Strides, typename ArgType, typename Device>
257struct TensorEvaluator<TensorStridingOp<Strides, ArgType>, Device>
258 : public TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
259{
260 typedef TensorStridingOp<Strides, ArgType> XprType;
261 typedef TensorEvaluator<const XprType, Device> Base;
262 // typedef typename XprType::Index Index;
263 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
264 // typedef DSizes<Index, NumDims> Dimensions;
265
266 enum {
267 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
268 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
269 PreferBlockAccess = false,
270 Layout = TensorEvaluator<ArgType, Device>::Layout,
271 CoordAccess = false, // to be implemented
272 RawAccess = false
273 };
274
275 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
276 : Base(op, device) { }
277
278 typedef typename XprType::Index Index;
279 typedef typename XprType::Scalar Scalar;
280 typedef typename XprType::CoeffReturnType CoeffReturnType;
281 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
282 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
283
284 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
285 {
286 return this->m_impl.coeffRef(this->srcCoeff(index));
287 }
288
289 template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
290 void writePacket(Index index, const PacketReturnType& x)
291 {
292 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
293 eigen_assert(index+PacketSize-1 < this->dimensions().TotalSize());
294
295 Index inputIndices[] = {0, 0};
296 Index indices[] = {index, index + PacketSize - 1};
297 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
298 EIGEN_UNROLL_LOOP
299 for (int i = NumDims - 1; i > 0; --i) {
300 const Index idx0 = indices[0] / this->m_outputStrides[i];
301 const Index idx1 = indices[1] / this->m_outputStrides[i];
302 inputIndices[0] += idx0 * this->m_inputStrides[i];
303 inputIndices[1] += idx1 * this->m_inputStrides[i];
304 indices[0] -= idx0 * this->m_outputStrides[i];
305 indices[1] -= idx1 * this->m_outputStrides[i];
306 }
307 inputIndices[0] += indices[0] * this->m_inputStrides[0];
308 inputIndices[1] += indices[1] * this->m_inputStrides[0];
309 } else { // RowMajor
310 EIGEN_UNROLL_LOOP
311 for (int i = 0; i < NumDims - 1; ++i) {
312 const Index idx0 = indices[0] / this->m_outputStrides[i];
313 const Index idx1 = indices[1] / this->m_outputStrides[i];
314 inputIndices[0] += idx0 * this->m_inputStrides[i];
315 inputIndices[1] += idx1 * this->m_inputStrides[i];
316 indices[0] -= idx0 * this->m_outputStrides[i];
317 indices[1] -= idx1 * this->m_outputStrides[i];
318 }
319 inputIndices[0] += indices[0] * this->m_inputStrides[NumDims-1];
320 inputIndices[1] += indices[1] * this->m_inputStrides[NumDims-1];
321 }
322 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
323 this->m_impl.template writePacket<Unaligned>(inputIndices[0], x);
324 }
325 else {
326 EIGEN_ALIGN_MAX Scalar values[PacketSize];
327 internal::pstore<Scalar, PacketReturnType>(values, x);
328 this->m_impl.coeffRef(inputIndices[0]) = values[0];
329 this->m_impl.coeffRef(inputIndices[1]) = values[PacketSize-1];
330 EIGEN_UNROLL_LOOP
331 for (int i = 1; i < PacketSize-1; ++i) {
332 this->coeffRef(index+i) = values[i];
333 }
334 }
335 }
336};
337
338
339} // end namespace Eigen
340
341#endif // EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
The tensor base class.
Definition TensorForwardDeclarations.h:56
Tensor striding class.
Definition TensorStriding.h:50
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27