$darkmode
Eigen-unsupported  5.0.1-dev
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename XprType>
20 struct traits<TensorLayoutSwapOp<XprType> > : public traits<XprType> {
21  typedef typename XprType::Scalar Scalar;
22  typedef traits<XprType> XprTraits;
23  typedef typename XprTraits::StorageKind StorageKind;
24  typedef typename XprTraits::Index Index;
25  typedef typename XprType::Nested Nested;
26  typedef std::remove_reference_t<Nested> Nested_;
27  static constexpr int NumDimensions = traits<XprType>::NumDimensions;
28  static constexpr int Layout = (traits<XprType>::Layout == ColMajor) ? RowMajor : ColMajor;
29  typedef typename XprTraits::PointerType PointerType;
30 };
31 
32 template <typename XprType>
33 struct eval<TensorLayoutSwapOp<XprType>, Eigen::Dense> {
34  typedef const TensorLayoutSwapOp<XprType>& type;
35 };
36 
37 template <typename XprType>
38 struct nested<TensorLayoutSwapOp<XprType>, 1, typename eval<TensorLayoutSwapOp<XprType> >::type> {
39  typedef TensorLayoutSwapOp<XprType> type;
40 };
41 
42 } // end namespace internal
43 
66 template <typename XprType>
67 class TensorLayoutSwapOp : public TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors> {
68  public:
69  typedef TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors> Base;
70  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Scalar Scalar;
71  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
72  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
73  typedef typename Eigen::internal::nested<TensorLayoutSwapOp>::type Nested;
74  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::StorageKind StorageKind;
75  typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Index Index;
76 
77  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorLayoutSwapOp(const XprType& expr) : m_xpr(expr) {}
78 
79  EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
80 
81  EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorLayoutSwapOp)
82  protected:
83  typename XprType::Nested m_xpr;
84 };
85 
86 // Eval as rvalue
87 template <typename ArgType, typename Device>
88 struct TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> {
89  typedef TensorLayoutSwapOp<ArgType> XprType;
90  typedef typename XprType::Index Index;
91  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
92  typedef DSizes<Index, NumDims> Dimensions;
93 
94  static constexpr int Layout =
95  (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
96  enum {
97  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
98  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
99  BlockAccess = false,
100  PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
101  CoordAccess = false, // to be implemented
102  RawAccess = TensorEvaluator<ArgType, Device>::RawAccess
103  };
104 
105  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
106  typedef internal::TensorBlockNotImplemented TensorBlock;
107  //===--------------------------------------------------------------------===//
108 
109  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {
110  for (int i = 0; i < NumDims; ++i) {
111  m_dimensions[i] = m_impl.dimensions()[NumDims - 1 - i];
112  }
113  }
114 
115  typedef typename XprType::Scalar Scalar;
116  typedef typename XprType::CoeffReturnType CoeffReturnType;
117  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
118  typedef StorageMemory<CoeffReturnType, Device> Storage;
119  typedef typename Storage::Type EvaluatorPointerType;
120 
121  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
122 
123  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) { return m_impl.evalSubExprsIfNeeded(data); }
124  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
125 
126  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_impl.coeff(index); }
127 
128  template <int LoadMode>
129  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
130  return m_impl.template packet<LoadMode>(index);
131  }
132 
133  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
134  return m_impl.costPerCoeff(vectorized);
135  }
136 
137  EIGEN_DEVICE_FUNC typename Storage::Type data() const { return constCast(m_impl.data()); }
138 
139  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
140 
141  protected:
142  TensorEvaluator<ArgType, Device> m_impl;
143  Dimensions m_dimensions;
144 };
145 
146 // Eval as lvalue
147 template <typename ArgType, typename Device>
148 struct TensorEvaluator<TensorLayoutSwapOp<ArgType>, Device>
149  : public TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> {
150  typedef TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> Base;
151  typedef TensorLayoutSwapOp<ArgType> XprType;
152 
153  static constexpr int Layout =
154  (TensorEvaluator<ArgType, Device>::Layout == static_cast<int>(ColMajor)) ? RowMajor : ColMajor;
155  enum {
156  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
157  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
158  BlockAccess = false,
159  PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
160  CoordAccess = false // to be implemented
161  };
162 
163  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
164  typedef internal::TensorBlockNotImplemented TensorBlock;
165  //===--------------------------------------------------------------------===//
166 
167  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : Base(op, device) {}
168 
169  typedef typename XprType::Index Index;
170  typedef typename XprType::Scalar Scalar;
171  typedef typename XprType::CoeffReturnType CoeffReturnType;
172  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index) const {
175  return this->m_impl.coeffRef(index);
176  }
177  template <int StoreMode>
178  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType& x) const {
179  this->m_impl.template writePacket<StoreMode>(index, x);
180  }
181 };
182 
183 } // end namespace Eigen
184 
185 #endif // EIGEN_CXX11_TENSOR_TENSOR_LAYOUT_SWAP_H
WriteAccessors
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index