TensorLayoutSwap.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_LAYOUT_SWAP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_LAYOUT_SWAP_H
12 
13 namespace Eigen {
14 
37 namespace internal {
38 template<typename XprType>
39 struct traits<TensorLayoutSwapOp<XprType> > : public traits<XprType>
40 {
41  typedef typename XprType::Scalar Scalar;
42  typedef traits<XprType> XprTraits;
43  typedef typename packet_traits<Scalar>::type Packet;
44  typedef typename XprTraits::StorageKind StorageKind;
45  typedef typename XprTraits::Index Index;
46  typedef typename XprType::Nested Nested;
47  typedef typename remove_reference<Nested>::type _Nested;
48  static const int NumDimensions = traits<XprType>::NumDimensions;
49  static const int Layout = (traits<XprType>::Layout == ColMajor) ? RowMajor : ColMajor;
50 };
51 
52 template<typename XprType>
53 struct eval<TensorLayoutSwapOp<XprType>, Eigen::Dense>
54 {
55  typedef const TensorLayoutSwapOp<XprType>& type;
56 };
57 
58 template<typename XprType>
59 struct nested<TensorLayoutSwapOp<XprType>, 1, typename eval<TensorLayoutSwapOp<XprType> >::type>
60 {
61  typedef TensorLayoutSwapOp<XprType> type;
62 };
63 
64 } // end namespace internal
65 
66 
67 
68 template<typename XprType>
69 class TensorLayoutSwapOp : public TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors>
70 {
71  public:
72  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Scalar Scalar;
73  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Packet Packet;
74  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
75  typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
76  typedef typename internal::remove_const<typename XprType::PacketReturnType>::type PacketReturnType;
77  typedef typename Eigen::internal::nested<TensorLayoutSwapOp>::type Nested;
78  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::StorageKind StorageKind;
79  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Index Index;
80 
81  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorLayoutSwapOp(const XprType& expr)
82  : m_xpr(expr) {}
83 
84  EIGEN_DEVICE_FUNC
85  const typename internal::remove_all<typename XprType::Nested>::type&
86  expression() const { return m_xpr; }
87 
88  EIGEN_DEVICE_FUNC
89  EIGEN_STRONG_INLINE TensorLayoutSwapOp& operator = (const TensorLayoutSwapOp& other)
90  {
91  typedef TensorAssignOp<TensorLayoutSwapOp, const TensorLayoutSwapOp> Assign;
92  Assign assign(*this, other);
93  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
94  return *this;
95  }
96 
97  template<typename OtherDerived>
98  EIGEN_DEVICE_FUNC
99  EIGEN_STRONG_INLINE TensorLayoutSwapOp& operator = (const OtherDerived& other)
100  {
101  typedef TensorAssignOp<TensorLayoutSwapOp, const OtherDerived> Assign;
102  Assign assign(*this, other);
103  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
104  return *this;
105  }
106 
107  protected:
108  typename XprType::Nested m_xpr;
109 };
110 
111 
112 // Eval as rvalue
113 template<typename ArgType, typename Device>
114 struct TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device>
115 {
116  typedef TensorLayoutSwapOp<ArgType> XprType;
117  typedef typename XprType::Index Index;
118  static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
119  typedef DSizes<Index, NumDims> Dimensions;
120 
121  enum {
122  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
123  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
124  Layout = (static_cast<int>(TensorEvaluator<ArgType, Device>::Layout) == static_cast<int>(ColMajor)) ? RowMajor : ColMajor,
125  CoordAccess = false, // to be implemented
126  };
127 
128  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
129  : m_impl(op.expression(), device)
130  {
131  for(int i = 0; i < NumDims; ++i) {
132  m_dimensions[i] = m_impl.dimensions()[NumDims-1-i];
133  }
134  }
135 
136  typedef typename XprType::Scalar Scalar;
137  typedef typename XprType::CoeffReturnType CoeffReturnType;
138  typedef typename XprType::PacketReturnType PacketReturnType;
139 
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
141 
142  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(CoeffReturnType* data) {
143  return m_impl.evalSubExprsIfNeeded(data);
144  }
145  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
146  m_impl.cleanup();
147  }
148 
149  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
150  {
151  return m_impl.coeff(index);
152  }
153 
154  template<int LoadMode>
155  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
156  {
157  return m_impl.template packet<LoadMode>(index);
158  }
159 
160  EIGEN_DEVICE_FUNC Scalar* data() const { return m_impl.data(); }
161 
162  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
163 
164  protected:
165  TensorEvaluator<ArgType, Device> m_impl;
166  Dimensions m_dimensions;
167 };
168 
169 
170 // Eval as lvalue
171 template<typename ArgType, typename Device>
172  struct TensorEvaluator<TensorLayoutSwapOp<ArgType>, Device>
173  : public TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device>
174 {
175  typedef TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> Base;
176  typedef TensorLayoutSwapOp<ArgType> XprType;
177 
178  enum {
179  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
180  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
181  Layout = (static_cast<int>(TensorEvaluator<ArgType, Device>::Layout) == static_cast<int>(ColMajor)) ? RowMajor : ColMajor,
182  CoordAccess = false, // to be implemented
183  };
184 
185  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
186  : Base(op, device)
187  { }
188 
189  typedef typename XprType::Index Index;
190  typedef typename XprType::Scalar Scalar;
191  typedef typename XprType::CoeffReturnType CoeffReturnType;
192  typedef typename XprType::PacketReturnType PacketReturnType;
193 
194  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
195  {
196  return this->m_impl.coeffRef(index);
197  }
198  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
199  void writePacket(Index index, const PacketReturnType& x)
200  {
201  this->m_impl.template writePacket<StoreMode>(index, x);
202  }
203 };
204 
205 } // end namespace Eigen
206 
207 #endif // EIGEN_CXX11_TENSOR_TENSOR_LAYOUT_SWAP_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13