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 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Shuffle, typename XprType>
24 struct traits<TensorShufflingOp<Shuffle, 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 Shuffle, typename XprType>
38 struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense>
39 {
40  typedef const TensorShufflingOp<Shuffle, XprType>& type;
41 };
42 
43 template<typename Shuffle, typename XprType>
44 struct nested<TensorShufflingOp<Shuffle, XprType>, 1, typename eval<TensorShufflingOp<Shuffle, XprType> >::type>
45 {
46  typedef TensorShufflingOp<Shuffle, XprType> type;
47 };
48 
49 } // end namespace internal
50 
51 
52 
53 template<typename Shuffle, typename XprType>
54 class TensorShufflingOp : public TensorBase<TensorShufflingOp<Shuffle, XprType> >
55 {
56  public:
57  typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
58  typedef typename Eigen::internal::traits<TensorShufflingOp>::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<TensorShufflingOp>::type Nested;
63  typedef typename Eigen::internal::traits<TensorShufflingOp>::StorageKind StorageKind;
64  typedef typename Eigen::internal::traits<TensorShufflingOp>::Index Index;
65 
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(const XprType& expr, const Shuffle& shuffle)
67  : m_xpr(expr), m_shuffle(shuffle) {}
68 
69  EIGEN_DEVICE_FUNC
70  const Shuffle& shufflePermutation() const { return m_shuffle; }
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 TensorShufflingOp& operator = (const TensorShufflingOp& other)
78  {
79  typedef TensorAssignOp<TensorShufflingOp, const TensorShufflingOp> 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 TensorShufflingOp& operator = (const OtherDerived& other)
88  {
89  typedef TensorAssignOp<TensorShufflingOp, 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 Shuffle m_shuffle;
98 };
99 
100 
101 // Eval as rvalue
102 template<typename Shuffle, typename ArgType, typename Device>
103 struct TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
104 {
105  typedef TensorShufflingOp<Shuffle, 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  typedef typename XprType::Scalar Scalar;
110 
111  enum {
112  IsAligned = false,
113  PacketAccess = (internal::packet_traits<Scalar>::size > 1),
114  Layout = TensorEvaluator<ArgType, Device>::Layout,
115  CoordAccess = false, // to be implemented
116  };
117 
118  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
119  : m_impl(op.expression(), device)
120  {
121  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
122  const Shuffle& shuffle = op.shufflePermutation();
123  for (int i = 0; i < NumDims; ++i) {
124  m_dimensions[i] = input_dims[shuffle[i]];
125  }
126 
127  array<Index, NumDims> inputStrides;
128 
129  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
130  inputStrides[0] = 1;
131  m_outputStrides[0] = 1;
132  for (int i = 1; i < NumDims; ++i) {
133  inputStrides[i] = inputStrides[i - 1] * input_dims[i - 1];
134  m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
135  }
136  } else {
137  inputStrides[NumDims - 1] = 1;
138  m_outputStrides[NumDims - 1] = 1;
139  for (int i = NumDims - 2; i >= 0; --i) {
140  inputStrides[i] = inputStrides[i + 1] * input_dims[i + 1];
141  m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
142  }
143  }
144 
145  for (int i = 0; i < NumDims; ++i) {
146  m_inputStrides[i] = inputStrides[shuffle[i]];
147  }
148  }
149 
150  typedef typename XprType::CoeffReturnType CoeffReturnType;
151  typedef typename XprType::PacketReturnType PacketReturnType;
152 
153  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
154 
155  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
156  m_impl.evalSubExprsIfNeeded(NULL);
157  return true;
158  }
159  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
160  m_impl.cleanup();
161  }
162 
163  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
164  {
165  return m_impl.coeff(srcCoeff(index));
166  }
167 
168  template<int LoadMode>
169  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
170  {
171  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
172  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
173  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
174 
175  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
176  for (int i = 0; i < packetSize; ++i) {
177  values[i] = coeff(index+i);
178  }
179  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
180  return rslt;
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
213 template<typename Shuffle, typename ArgType, typename Device>
214 struct 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 
225  enum {
226  IsAligned = false,
227  PacketAccess = (internal::packet_traits<Scalar>::size > 1),
228  };
229 
230  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
231  : Base(op, device)
232  { }
233 
234  typedef typename XprType::CoeffReturnType CoeffReturnType;
235  typedef typename XprType::PacketReturnType PacketReturnType;
236 
237  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
238  {
239  return this->m_impl.coeffRef(this->srcCoeff(index));
240  }
241 
242  template <int StoreMode> EIGEN_STRONG_INLINE
243  void writePacket(Index index, const PacketReturnType& x)
244  {
245  static const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
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
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13