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 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Strides, typename XprType>
24 struct traits<TensorStridingOp<Strides, XprType> > : public traits<XprType>
25 {
26  typedef typename XprType::Scalar Scalar;
27  typedef traits<XprType> XprTraits;
28  typedef typename packet_traits<Scalar>::type Packet;
29  typedef typename XprTraits::StorageKind StorageKind;
30  typedef typename XprTraits::Index Index;
31  typedef typename XprType::Nested Nested;
32  typedef typename remove_reference<Nested>::type _Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
35 };
36 
37 template<typename Strides, typename XprType>
38 struct eval<TensorStridingOp<Strides, XprType>, Eigen::Dense>
39 {
40  typedef const TensorStridingOp<Strides, XprType>& type;
41 };
42 
43 template<typename Strides, typename XprType>
44 struct nested<TensorStridingOp<Strides, XprType>, 1, typename eval<TensorStridingOp<Strides, XprType> >::type>
45 {
46  typedef TensorStridingOp<Strides, XprType> type;
47 };
48 
49 } // end namespace internal
50 
51 
52 
53 template<typename Strides, typename XprType>
54 class TensorStridingOp : public TensorBase<TensorStridingOp<Strides, XprType> >
55 {
56  public:
57  typedef typename Eigen::internal::traits<TensorStridingOp>::Scalar Scalar;
58  typedef typename Eigen::internal::traits<TensorStridingOp>::Packet Packet;
59  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
60  typedef typename XprType::CoeffReturnType CoeffReturnType;
61  typedef typename XprType::PacketReturnType PacketReturnType;
62  typedef typename Eigen::internal::nested<TensorStridingOp>::type Nested;
63  typedef typename Eigen::internal::traits<TensorStridingOp>::StorageKind StorageKind;
64  typedef typename Eigen::internal::traits<TensorStridingOp>::Index Index;
65 
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStridingOp(const XprType& expr, const Strides& dims)
67  : m_xpr(expr), m_dims(dims) {}
68 
69  EIGEN_DEVICE_FUNC
70  const Strides& strides() const { return m_dims; }
71 
72  EIGEN_DEVICE_FUNC
73  const typename internal::remove_all<typename XprType::Nested>::type&
74  expression() const { return m_xpr; }
75 
76  EIGEN_DEVICE_FUNC
77  EIGEN_STRONG_INLINE TensorStridingOp& operator = (const TensorStridingOp& other)
78  {
79  typedef TensorAssignOp<TensorStridingOp, const TensorStridingOp> Assign;
80  Assign assign(*this, other);
81  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
82  return *this;
83  }
84 
85  template<typename OtherDerived>
86  EIGEN_DEVICE_FUNC
87  EIGEN_STRONG_INLINE TensorStridingOp& operator = (const OtherDerived& other)
88  {
89  typedef TensorAssignOp<TensorStridingOp, const OtherDerived> Assign;
90  Assign assign(*this, other);
91  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
92  return *this;
93  }
94 
95  protected:
96  typename XprType::Nested m_xpr;
97  const Strides m_dims;
98 };
99 
100 
101 // Eval as rvalue
102 template<typename Strides, typename ArgType, typename Device>
103 struct TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
104 {
105  typedef TensorStridingOp<Strides, ArgType> XprType;
106  typedef typename XprType::Index Index;
107  static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
108  typedef DSizes<Index, NumDims> Dimensions;
109 
110  enum {
111  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
112  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
113  Layout = TensorEvaluator<ArgType, Device>::Layout,
114  CoordAccess = false, // to be implemented
115  };
116 
117  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
118  : m_impl(op.expression(), device)
119  {
120  m_dimensions = m_impl.dimensions();
121  for (int i = 0; i < NumDims; ++i) {
122  m_dimensions[i] = ceilf(static_cast<float>(m_dimensions[i]) / op.strides()[i]);
123  }
124 
125  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
126  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
127  m_outputStrides[0] = 1;
128  m_inputStrides[0] = 1;
129  for (int i = 1; i < NumDims; ++i) {
130  m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
131  m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
132  m_inputStrides[i-1] *= op.strides()[i-1];
133  }
134  m_inputStrides[NumDims-1] *= op.strides()[NumDims-1];
135  } else { // RowMajor
136  m_outputStrides[NumDims-1] = 1;
137  m_inputStrides[NumDims-1] = 1;
138  for (int i = NumDims - 2; i >= 0; --i) {
139  m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
140  m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
141  m_inputStrides[i+1] *= op.strides()[i+1];
142  }
143  m_inputStrides[0] *= op.strides()[0];
144  }
145  }
146 
147  typedef typename XprType::Scalar Scalar;
148  typedef typename XprType::CoeffReturnType CoeffReturnType;
149  typedef typename XprType::PacketReturnType PacketReturnType;
150 
151  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
152 
153  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
154  m_impl.evalSubExprsIfNeeded(NULL);
155  return true;
156  }
157  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
158  m_impl.cleanup();
159  }
160 
161  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
162  {
163  return m_impl.coeff(srcCoeff(index));
164  }
165 
166  template<int LoadMode>
167  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
168  {
169  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
170  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
171  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
172 
173  Index inputIndices[] = {0, 0};
174  Index indices[] = {index, index + packetSize - 1};
175  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
176  for (int i = NumDims - 1; i > 0; --i) {
177  const Index idx0 = indices[0] / m_outputStrides[i];
178  const Index idx1 = indices[1] / m_outputStrides[i];
179  inputIndices[0] += idx0 * m_inputStrides[i];
180  inputIndices[1] += idx1 * m_inputStrides[i];
181  indices[0] -= idx0 * m_outputStrides[i];
182  indices[1] -= idx1 * m_outputStrides[i];
183  }
184  inputIndices[0] += indices[0] * m_inputStrides[0];
185  inputIndices[1] += indices[1] * m_inputStrides[0];
186  } else { // RowMajor
187  for (int i = 0; i < NumDims - 1; ++i) {
188  const Index idx0 = indices[0] / m_outputStrides[i];
189  const Index idx1 = indices[1] / m_outputStrides[i];
190  inputIndices[0] += idx0 * m_inputStrides[i];
191  inputIndices[1] += idx1 * m_inputStrides[i];
192  indices[0] -= idx0 * m_outputStrides[i];
193  indices[1] -= idx1 * m_outputStrides[i];
194  }
195  inputIndices[0] += indices[0] * m_inputStrides[NumDims-1];
196  inputIndices[1] += indices[1] * m_inputStrides[NumDims-1];
197  }
198  if (inputIndices[1] - inputIndices[0] == packetSize - 1) {
199  PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
200  return rslt;
201  }
202  else {
203  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
204  values[0] = m_impl.coeff(inputIndices[0]);
205  values[packetSize-1] = m_impl.coeff(inputIndices[1]);
206  for (int i = 1; i < packetSize-1; ++i) {
207  values[i] = coeff(index+i);
208  }
209  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
210  return rslt;
211  }
212  }
213 
214  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
215 
216  protected:
217  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
218  {
219  Index inputIndex = 0;
220  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
221  for (int i = NumDims - 1; i > 0; --i) {
222  const Index idx = index / m_outputStrides[i];
223  inputIndex += idx * m_inputStrides[i];
224  index -= idx * m_outputStrides[i];
225  }
226  inputIndex += index * m_inputStrides[0];
227  } else { // RowMajor
228  for (int i = 0; i < NumDims - 1; ++i) {
229  const Index idx = index / m_outputStrides[i];
230  inputIndex += idx * m_inputStrides[i];
231  index -= idx * m_outputStrides[i];
232  }
233  inputIndex += index * m_inputStrides[NumDims-1];
234  }
235  return inputIndex;
236  }
237 
238  Dimensions m_dimensions;
239  array<Index, NumDims> m_outputStrides;
240  array<Index, NumDims> m_inputStrides;
241  TensorEvaluator<ArgType, Device> m_impl;
242 };
243 
244 
245 // Eval as lvalue
246 template<typename Strides, typename ArgType, typename Device>
247 struct TensorEvaluator<TensorStridingOp<Strides, ArgType>, Device>
248  : public TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
249 {
250  typedef TensorStridingOp<Strides, ArgType> XprType;
251  typedef TensorEvaluator<const XprType, Device> Base;
252  // typedef typename XprType::Index Index;
253  static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
254  // typedef DSizes<Index, NumDims> Dimensions;
255 
256  enum {
257  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/false,
258  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
259  Layout = TensorEvaluator<ArgType, Device>::Layout,
260  CoordAccess = false, // to be implemented
261  };
262 
263  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
264  : Base(op, device) { }
265 
266  typedef typename XprType::Index Index;
267  typedef typename XprType::Scalar Scalar;
268  typedef typename XprType::PacketReturnType PacketReturnType;
269 
270  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
271  {
272  return this->m_impl.coeffRef(this->srcCoeff(index));
273  }
274 
275  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
276  void writePacket(Index index, const PacketReturnType& x)
277  {
278  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
279  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
280  eigen_assert(index+packetSize-1 < this->dimensions().TotalSize());
281 
282  Index inputIndices[] = {0, 0};
283  Index indices[] = {index, index + packetSize - 1};
284  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
285  for (int i = NumDims - 1; i > 0; --i) {
286  const Index idx0 = indices[0] / this->m_outputStrides[i];
287  const Index idx1 = indices[1] / this->m_outputStrides[i];
288  inputIndices[0] += idx0 * this->m_inputStrides[i];
289  inputIndices[1] += idx1 * this->m_inputStrides[i];
290  indices[0] -= idx0 * this->m_outputStrides[i];
291  indices[1] -= idx1 * this->m_outputStrides[i];
292  }
293  inputIndices[0] += indices[0] * this->m_inputStrides[0];
294  inputIndices[1] += indices[1] * this->m_inputStrides[0];
295  } else { // RowMajor
296  for (int i = 0; i < NumDims - 1; ++i) {
297  const Index idx0 = indices[0] / this->m_outputStrides[i];
298  const Index idx1 = indices[1] / this->m_outputStrides[i];
299  inputIndices[0] += idx0 * this->m_inputStrides[i];
300  inputIndices[1] += idx1 * this->m_inputStrides[i];
301  indices[0] -= idx0 * this->m_outputStrides[i];
302  indices[1] -= idx1 * this->m_outputStrides[i];
303  }
304  inputIndices[0] += indices[0] * this->m_inputStrides[NumDims-1];
305  inputIndices[1] += indices[1] * this->m_inputStrides[NumDims-1];
306  }
307  if (inputIndices[1] - inputIndices[0] == packetSize - 1) {
308  this->m_impl.template writePacket<Unaligned>(inputIndices[0], x);
309  }
310  else {
311  EIGEN_ALIGN_MAX Scalar values[packetSize];
312  internal::pstore<Scalar, PacketReturnType>(values, x);
313  this->m_impl.coeffRef(inputIndices[0]) = values[0];
314  this->m_impl.coeffRef(inputIndices[1]) = values[packetSize-1];
315  for (int i = 1; i < packetSize-1; ++i) {
316  this->coeffRef(index+i) = values[i];
317  }
318  }
319  }
320 };
321 
322 
323 } // end namespace Eigen
324 
325 #endif // EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13