Eigen-unsupported  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
39 namespace internal {
40 template<typename XprType>
41 struct traits<TensorLayoutSwapOp<XprType> > : public traits<XprType>
42 {
43  typedef typename XprType::Scalar Scalar;
44  typedef traits<XprType> XprTraits;
45  typedef typename XprTraits::StorageKind StorageKind;
46  typedef typename XprTraits::Index Index;
47  typedef typename XprType::Nested Nested;
48  typedef std::remove_reference_t<Nested> Nested_;
49  static constexpr int NumDimensions = traits<XprType>::NumDimensions;
50  static constexpr int Layout = (traits<XprType>::Layout == ColMajor) ? RowMajor : ColMajor;
51  typedef typename XprTraits::PointerType PointerType;
52 };
53 
54 template<typename XprType>
55 struct eval<TensorLayoutSwapOp<XprType>, Eigen::Dense>
56 {
57  typedef const TensorLayoutSwapOp<XprType>& type;
58 };
59 
60 template<typename XprType>
61 struct nested<TensorLayoutSwapOp<XprType>, 1, typename eval<TensorLayoutSwapOp<XprType> >::type>
62 {
63  typedef TensorLayoutSwapOp<XprType> type;
64 };
65 
66 } // end namespace internal
67 
68 
69 
70 template<typename XprType>
71 class TensorLayoutSwapOp : public TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors>
72 {
73  public:
74  typedef TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors> Base;
75  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Scalar Scalar;
76  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
77  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
78  typedef typename Eigen::internal::nested<TensorLayoutSwapOp>::type Nested;
79  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::StorageKind StorageKind;
80  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Index Index;
81 
82  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorLayoutSwapOp(const XprType& expr)
83  : m_xpr(expr) {}
84 
85  EIGEN_DEVICE_FUNC
86  const internal::remove_all_t<typename XprType::Nested>&
87  expression() const { return m_xpr; }
88 
89  EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorLayoutSwapOp)
90  protected:
91  typename XprType::Nested m_xpr;
92 };
93 
94 
95 // Eval as rvalue
96 template<typename ArgType, typename Device>
97 struct TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device>
98 {
99  typedef TensorLayoutSwapOp<ArgType> XprType;
100  typedef typename XprType::Index Index;
101  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
102  typedef DSizes<Index, NumDims> Dimensions;
103 
104  static constexpr int Layout = (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
105  enum {
106  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
107  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
108  BlockAccess = false,
109  PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
110  CoordAccess = false, // to be implemented
111  RawAccess = TensorEvaluator<ArgType, Device>::RawAccess
112  };
113 
114  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
115  typedef internal::TensorBlockNotImplemented TensorBlock;
116  //===--------------------------------------------------------------------===//
117 
118  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
119  : m_impl(op.expression(), device)
120  {
121  for(int i = 0; i < NumDims; ++i) {
122  m_dimensions[i] = m_impl.dimensions()[NumDims-1-i];
123  }
124  }
125 
126 #ifdef EIGEN_USE_SYCL
127  // binding placeholder accessors to a command group handler for SYCL
128  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
129  m_impl.bind(cgh);
130  }
131 #endif
132 
133  typedef typename XprType::Scalar Scalar;
134  typedef typename XprType::CoeffReturnType CoeffReturnType;
135  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
136  typedef StorageMemory<CoeffReturnType, Device> Storage;
137  typedef typename Storage::Type EvaluatorPointerType;
138 
139  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
140 
141  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
142  return m_impl.evalSubExprsIfNeeded(data);
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(index);
151  }
152 
153  template<int LoadMode>
154  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
155  {
156  return m_impl.template packet<LoadMode>(index);
157  }
158 
159  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
160  return m_impl.costPerCoeff(vectorized);
161  }
162 
163  EIGEN_DEVICE_FUNC typename Storage::Type data() const {
164  return constCast(m_impl.data());
165  }
166 
167  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
168 
169  protected:
170  TensorEvaluator<ArgType, Device> m_impl;
171  Dimensions m_dimensions;
172 };
173 
174 
175 // Eval as lvalue
176 template<typename ArgType, typename Device>
177  struct TensorEvaluator<TensorLayoutSwapOp<ArgType>, Device>
178  : public TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device>
179 {
180  typedef TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> Base;
181  typedef TensorLayoutSwapOp<ArgType> XprType;
182 
183  static constexpr int Layout = (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
184  enum {
185  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
186  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
187  BlockAccess = false,
188  PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
189  CoordAccess = false // to be implemented
190  };
191 
192  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
193  typedef internal::TensorBlockNotImplemented TensorBlock;
194  //===--------------------------------------------------------------------===//
195 
196  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
197  : Base(op, device)
198  { }
199 
200  typedef typename XprType::Index Index;
201  typedef typename XprType::Scalar Scalar;
202  typedef typename XprType::CoeffReturnType CoeffReturnType;
203  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
204 
205  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
206  {
207  return this->m_impl.coeffRef(index);
208  }
209  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
210  void writePacket(Index index, const PacketReturnType& x)
211  {
212  this->m_impl.template writePacket<StoreMode>(index, x);
213  }
214 };
215 
216 } // end namespace Eigen
217 
218 #endif // EIGEN_CXX11_TENSOR_TENSOR_LAYOUT_SWAP_H
WriteAccessors
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index