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};
28
29template<typename Strides, typename XprType>
30struct eval<TensorStridingOp<Strides, XprType>, Eigen::Dense>
31{
32 typedef const TensorStridingOp<Strides, XprType>& type;
33};
34
35template<typename Strides, typename XprType>
36struct nested<TensorStridingOp<Strides, XprType>, 1, typename eval<TensorStridingOp<Strides, XprType> >::type>
37{
38 typedef TensorStridingOp<Strides, XprType> type;
39};
40
41} // end namespace internal
42
48template <typename Strides, typename XprType>
49class TensorStridingOp : public TensorBase<TensorStridingOp<Strides, XprType> > {
50 public:
51 typedef typename Eigen::internal::traits<TensorStridingOp>::Scalar Scalar;
52 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorStridingOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorStridingOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorStridingOp>::Index Index;
57
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStridingOp(const XprType& expr, const Strides& dims)
59 : m_xpr(expr), m_dims(dims) {}
60
61 EIGEN_DEVICE_FUNC
62 const Strides& strides() const { return m_dims; }
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 TensorStridingOp& operator = (const TensorStridingOp& 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 TensorStridingOp& 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 Strides m_dims;
90};
91
92
93// Eval as rvalue
94template<typename Strides, typename ArgType, typename Device>
95struct TensorEvaluator<const TensorStridingOp<Strides, 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 = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
108 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
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 m_dimensions = m_impl.dimensions();
118 for (int i = 0; i < NumDims; ++i) {
119 m_dimensions[i] = ceilf(static_cast<float>(m_dimensions[i]) / op.strides()[i]);
120 }
121
122 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
123 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
124 m_outputStrides[0] = 1;
125 m_inputStrides[0] = 1;
126 for (int i = 1; i < NumDims; ++i) {
127 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
128 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
129 m_inputStrides[i-1] *= op.strides()[i-1];
130 }
131 m_inputStrides[NumDims-1] *= op.strides()[NumDims-1];
132 } else { // RowMajor
133 m_outputStrides[NumDims-1] = 1;
134 m_inputStrides[NumDims-1] = 1;
135 for (int i = NumDims - 2; i >= 0; --i) {
136 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
137 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
138 m_inputStrides[i+1] *= op.strides()[i+1];
139 }
140 m_inputStrides[0] *= op.strides()[0];
141 }
142 }
143
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
145
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
147 m_impl.evalSubExprsIfNeeded(NULL);
148 return true;
149 }
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
151 m_impl.cleanup();
152 }
153
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
155 {
156 return m_impl.coeff(srcCoeff(index));
157 }
158
159 template<int LoadMode>
160 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
161 {
162 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
163 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
164
165 Index inputIndices[] = {0, 0};
166 Index indices[] = {index, index + PacketSize - 1};
167 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
168 for (int i = NumDims - 1; i > 0; --i) {
169 const Index idx0 = indices[0] / m_outputStrides[i];
170 const Index idx1 = indices[1] / m_outputStrides[i];
171 inputIndices[0] += idx0 * m_inputStrides[i];
172 inputIndices[1] += idx1 * m_inputStrides[i];
173 indices[0] -= idx0 * m_outputStrides[i];
174 indices[1] -= idx1 * m_outputStrides[i];
175 }
176 inputIndices[0] += indices[0] * m_inputStrides[0];
177 inputIndices[1] += indices[1] * m_inputStrides[0];
178 } else { // RowMajor
179 for (int i = 0; i < NumDims - 1; ++i) {
180 const Index idx0 = indices[0] / m_outputStrides[i];
181 const Index idx1 = indices[1] / m_outputStrides[i];
182 inputIndices[0] += idx0 * m_inputStrides[i];
183 inputIndices[1] += idx1 * m_inputStrides[i];
184 indices[0] -= idx0 * m_outputStrides[i];
185 indices[1] -= idx1 * m_outputStrides[i];
186 }
187 inputIndices[0] += indices[0] * m_inputStrides[NumDims-1];
188 inputIndices[1] += indices[1] * m_inputStrides[NumDims-1];
189 }
190 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
191 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
192 return rslt;
193 }
194 else {
195 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
196 values[0] = m_impl.coeff(inputIndices[0]);
197 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
198 for (int i = 1; i < PacketSize-1; ++i) {
199 values[i] = coeff(index+i);
200 }
201 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
202 return rslt;
203 }
204 }
205
206 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
207 double compute_cost = (NumDims - 1) * (TensorOpCost::AddCost<Index>() +
208 TensorOpCost::MulCost<Index>() +
209 TensorOpCost::DivCost<Index>()) +
210 TensorOpCost::MulCost<Index>();
211 if (vectorized) {
212 compute_cost *= 2; // packet() computes two indices
213 }
214 const int innerDim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : (NumDims - 1);
215 return m_impl.costPerCoeff(vectorized && m_inputStrides[innerDim] == 1) +
216 // Computation is not vectorized per se, but it is done once per packet.
217 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
218 }
219
220 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
221
222 protected:
223 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
224 {
225 Index inputIndex = 0;
226 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
227 for (int i = NumDims - 1; i > 0; --i) {
228 const Index idx = index / m_outputStrides[i];
229 inputIndex += idx * m_inputStrides[i];
230 index -= idx * m_outputStrides[i];
231 }
232 inputIndex += index * m_inputStrides[0];
233 } else { // RowMajor
234 for (int i = 0; i < NumDims - 1; ++i) {
235 const Index idx = index / m_outputStrides[i];
236 inputIndex += idx * m_inputStrides[i];
237 index -= idx * m_outputStrides[i];
238 }
239 inputIndex += index * m_inputStrides[NumDims-1];
240 }
241 return inputIndex;
242 }
243
244 Dimensions m_dimensions;
245 array<Index, NumDims> m_outputStrides;
246 array<Index, NumDims> m_inputStrides;
247 TensorEvaluator<ArgType, Device> m_impl;
248};
249
250
251// Eval as lvalue
252template<typename Strides, typename ArgType, typename Device>
253struct TensorEvaluator<TensorStridingOp<Strides, ArgType>, Device>
254 : public TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
255{
256 typedef TensorStridingOp<Strides, ArgType> XprType;
257 typedef TensorEvaluator<const XprType, Device> Base;
258 // typedef typename XprType::Index Index;
259 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
260 // typedef DSizes<Index, NumDims> Dimensions;
261
262 enum {
263 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
264 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
265 Layout = TensorEvaluator<ArgType, Device>::Layout,
266 CoordAccess = false, // to be implemented
267 RawAccess = false
268 };
269
270 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
271 : Base(op, device) { }
272
273 typedef typename XprType::Index Index;
274 typedef typename XprType::Scalar Scalar;
275 typedef typename XprType::CoeffReturnType CoeffReturnType;
276 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
277 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
278
279 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
280 {
281 return this->m_impl.coeffRef(this->srcCoeff(index));
282 }
283
284 template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
285 void writePacket(Index index, const PacketReturnType& x)
286 {
287 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
288 eigen_assert(index+PacketSize-1 < this->dimensions().TotalSize());
289
290 Index inputIndices[] = {0, 0};
291 Index indices[] = {index, index + PacketSize - 1};
292 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
293 for (int i = NumDims - 1; i > 0; --i) {
294 const Index idx0 = indices[0] / this->m_outputStrides[i];
295 const Index idx1 = indices[1] / this->m_outputStrides[i];
296 inputIndices[0] += idx0 * this->m_inputStrides[i];
297 inputIndices[1] += idx1 * this->m_inputStrides[i];
298 indices[0] -= idx0 * this->m_outputStrides[i];
299 indices[1] -= idx1 * this->m_outputStrides[i];
300 }
301 inputIndices[0] += indices[0] * this->m_inputStrides[0];
302 inputIndices[1] += indices[1] * this->m_inputStrides[0];
303 } else { // RowMajor
304 for (int i = 0; i < NumDims - 1; ++i) {
305 const Index idx0 = indices[0] / this->m_outputStrides[i];
306 const Index idx1 = indices[1] / this->m_outputStrides[i];
307 inputIndices[0] += idx0 * this->m_inputStrides[i];
308 inputIndices[1] += idx1 * this->m_inputStrides[i];
309 indices[0] -= idx0 * this->m_outputStrides[i];
310 indices[1] -= idx1 * this->m_outputStrides[i];
311 }
312 inputIndices[0] += indices[0] * this->m_inputStrides[NumDims-1];
313 inputIndices[1] += indices[1] * this->m_inputStrides[NumDims-1];
314 }
315 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
316 this->m_impl.template writePacket<Unaligned>(inputIndices[0], x);
317 }
318 else {
319 EIGEN_ALIGN_MAX Scalar values[PacketSize];
320 internal::pstore<Scalar, PacketReturnType>(values, x);
321 this->m_impl.coeffRef(inputIndices[0]) = values[0];
322 this->m_impl.coeffRef(inputIndices[1]) = values[PacketSize-1];
323 for (int i = 1; i < PacketSize-1; ++i) {
324 this->coeffRef(index+i) = values[i];
325 }
326 }
327 }
328};
329
330
331} // end namespace Eigen
332
333#endif // EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
Definition TensorAssign.h:56
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor striding class.
Definition TensorStriding.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