TensorLayoutSwap.h
Go to the documentation of this file.
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:
75  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Scalar Scalar;
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 
87  expression() const { return m_xpr; }
88 
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 {
100  typedef typename XprType::Index Index;
101  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
103 
104  static constexpr int Layout = (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
105  enum {
108  BlockAccess = false,
110  CoordAccess = false, // to be implemented
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  typedef typename XprType::Scalar Scalar;
131 
132  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
133 
134  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
135  return m_impl.evalSubExprsIfNeeded(data);
136  }
137  EIGEN_STRONG_INLINE void cleanup() {
138  m_impl.cleanup();
139  }
140 
141  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
142  {
143  return m_impl.coeff(index);
144  }
145 
146  template<int LoadMode>
147  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
148  {
149  return m_impl.template packet<LoadMode>(index);
150  }
151 
152  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
153  return m_impl.costPerCoeff(vectorized);
154  }
155 
157  return constCast(m_impl.data());
158  }
159 
160  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
161 
162  protected:
165 };
166 
167 
168 // Eval as lvalue
169 template<typename ArgType, typename Device>
170  struct TensorEvaluator<TensorLayoutSwapOp<ArgType>, Device>
171  : public TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device>
172 {
175 
176  static constexpr int Layout = (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
177  enum {
180  BlockAccess = false,
182  CoordAccess = false // to be implemented
183  };
184 
185  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
186  typedef internal::TensorBlockNotImplemented TensorBlock;
187  //===--------------------------------------------------------------------===//
188 
189  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
190  : Base(op, device)
191  { }
192 
193  typedef typename XprType::Index Index;
194  typedef typename XprType::Scalar Scalar;
197 
198  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index) const
199  {
200  return this->m_impl.coeffRef(index);
201  }
202  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
203  void writePacket(Index index, const PacketReturnType& x) const
204  {
205  this->m_impl.template writePacket<StoreMode>(index, x);
206  }
207 };
208 
209 } // end namespace Eigen
210 
211 #endif // EIGEN_CXX11_TENSOR_TENSOR_LAYOUT_SWAP_H
int i
#define EIGEN_DEVICE_FUNC
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: TensorMacros.h:80
The tensor base class.
Eigen::internal::traits< TensorLayoutSwapOp >::Scalar Scalar
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Eigen::internal::traits< TensorLayoutSwapOp >::StorageKind StorageKind
TensorBase< TensorLayoutSwapOp< XprType >, WriteAccessors > Base
Eigen::internal::nested< TensorLayoutSwapOp >::type Nested
Eigen::NumTraits< Scalar >::Real RealScalar
const internal::remove_all_t< typename XprType::Nested > & expression() const
Eigen::internal::traits< TensorLayoutSwapOp >::Index Index
TensorLayoutSwapOp(const XprType &expr)
WriteAccessors
typename remove_all< T >::type remove_all_t
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
T * constCast(const T *data)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
void writePacket(Index index, const PacketReturnType &x) const
PacketType< CoeffReturnType, Device >::type PacketReturnType
TensorEvaluator(const XprType &op, const Device &device)
TensorEvaluator< const TensorLayoutSwapOp< ArgType >, Device > Base
const TensorEvaluator< ArgType, Device > & impl() const
A cost model used to limit the number of threads used for evaluating tensor expression.
static constexpr int Layout
EvaluatorPointerType data() const