TensorShuffling.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_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;
28  typedef typename XprTraits::StorageKind StorageKind;
29  typedef typename XprTraits::Index Index;
30  typedef typename XprType::Nested Nested;
32  static const int NumDimensions = XprTraits::NumDimensions;
33  static const int Layout = XprTraits::Layout;
34 };
35 
36 template<typename Shuffle, typename XprType>
37 struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense>
38 {
40 };
41 
42 template<typename Shuffle, typename XprType>
43 struct nested<TensorShufflingOp<Shuffle, XprType>, 1, typename eval<TensorShufflingOp<Shuffle, XprType> >::type>
44 {
46 };
47 
48 } // end namespace internal
49 
50 
51 
52 template<typename Shuffle, typename XprType>
53 class TensorShufflingOp : public TensorBase<TensorShufflingOp<Shuffle, XprType> >
54 {
55  public:
58  typedef typename XprType::CoeffReturnType CoeffReturnType;
62 
63  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(const XprType& expr, const Shuffle& shuffle)
64  : m_xpr(expr), m_shuffle(shuffle) {}
65 
66  EIGEN_DEVICE_FUNC
67  const Shuffle& shufflePermutation() const { return m_shuffle; }
68 
69  EIGEN_DEVICE_FUNC
71  expression() const { return m_xpr; }
72 
73  EIGEN_DEVICE_FUNC
75  {
77  Assign assign(*this, other);
79  return *this;
80  }
81 
82  template<typename OtherDerived>
83  EIGEN_DEVICE_FUNC
85  {
87  Assign assign(*this, other);
89  return *this;
90  }
91 
92  protected:
93  typename XprType::Nested m_xpr;
94  const Shuffle m_shuffle;
95 };
96 
97 
98 // Eval as rvalue
99 template<typename Shuffle, typename ArgType, typename Device>
100 struct TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
101 {
103  typedef typename XprType::Index Index;
106  typedef typename XprType::Scalar Scalar;
110 
111  enum {
112  IsAligned = false,
115  CoordAccess = false, // to be implemented
116  RawAccess = false
117  };
118 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
120  : m_impl(op.expression(), device)
121  {
122  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
123  const Shuffle& shuffle = op.shufflePermutation();
124  for (int i = 0; i < NumDims; ++i) {
125  m_dimensions[i] = input_dims[shuffle[i]];
126  }
127 
128  array<Index, NumDims> inputStrides;
129 
130  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
131  inputStrides[0] = 1;
132  m_outputStrides[0] = 1;
133  for (int i = 1; i < NumDims; ++i) {
134  inputStrides[i] = inputStrides[i - 1] * input_dims[i - 1];
135  m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
136  }
137  } else {
138  inputStrides[NumDims - 1] = 1;
139  m_outputStrides[NumDims - 1] = 1;
140  for (int i = NumDims - 2; i >= 0; --i) {
141  inputStrides[i] = inputStrides[i + 1] * input_dims[i + 1];
142  m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
143  }
144  }
145 
146  for (int i = 0; i < NumDims; ++i) {
147  m_inputStrides[i] = inputStrides[shuffle[i]];
148  }
149  }
150 
151  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
152 
153  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
154  m_impl.evalSubExprsIfNeeded(NULL);
155  return true;
156  }
157  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
158  m_impl.cleanup();
159  }
160 
161  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
162  {
163  return m_impl.coeff(srcCoeff(index));
164  }
165 
166  template<int LoadMode>
167  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
168  {
169  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
170  eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
171 
173  for (int i = 0; i < PacketSize; ++i) {
174  values[i] = coeff(index+i);
175  }
176  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
177  return rslt;
178  }
179 
180  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
181  const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
182  2 * TensorOpCost::MulCost<Index>() +
183  TensorOpCost::DivCost<Index>());
184  return m_impl.costPerCoeff(vectorized) +
185  TensorOpCost(0, 0, compute_cost, false /* vectorized */, PacketSize);
186  }
187 
188  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
189 
190  protected:
191  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const {
192  Index inputIndex = 0;
193  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
194  for (int i = NumDims - 1; i > 0; --i) {
195  const Index idx = index / m_outputStrides[i];
196  inputIndex += idx * m_inputStrides[i];
197  index -= idx * m_outputStrides[i];
198  }
199  return inputIndex + index * m_inputStrides[0];
200  } else {
201  for (int i = 0; i < NumDims - 1; ++i) {
202  const Index idx = index / m_outputStrides[i];
203  inputIndex += idx * m_inputStrides[i];
204  index -= idx * m_outputStrides[i];
205  }
206  return inputIndex + index * m_inputStrides[NumDims - 1];
207  }
208  }
209 
214 };
215 
216 
217 // Eval as lvalue
218 template<typename Shuffle, typename ArgType, typename Device>
219 struct TensorEvaluator<TensorShufflingOp<Shuffle, ArgType>, Device>
220  : public TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
221 {
223 
225  typedef typename XprType::Index Index;
228  typedef typename XprType::Scalar Scalar;
232 
233  enum {
234  IsAligned = false,
236  RawAccess = false
237  };
238 
239  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
240  : Base(op, device)
241  { }
242 
244  {
245  return this->m_impl.coeffRef(this->srcCoeff(index));
246  }
247 
248  template <int StoreMode> EIGEN_STRONG_INLINE
249  void writePacket(Index index, const PacketReturnType& x)
250  {
251  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
252 
254  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
255  for (int i = 0; i < PacketSize; ++i) {
256  this->coeffRef(index+i) = values[i];
257  }
258  }
259 };
260 
261 
262 } // end namespace Eigen
263 
264 #endif // EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:54
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorShuffling.h:103
Eigen::internal::nested< TensorShufflingOp< Shuffle, XprType >, 1, typename eval< TensorShufflingOp< Shuffle, XprType > >::type >::type
TensorShufflingOp< Shuffle, XprType > type
Definition: TensorShuffling.h:45
Eigen::TensorEvaluator::device
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition: TensorEvaluator.h:114
Eigen::TensorShufflingOp::TensorShufflingOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(const XprType &expr, const Shuffle &shuffle)
Definition: TensorShuffling.h:63
Eigen
Definition: common.h:73
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::data
EIGEN_DEVICE_FUNC Scalar * data() const
Definition: TensorShuffling.h:188
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::TensorEvaluator
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorShuffling.h:239
Eigen::TensorShufflingOp::operator=
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp & operator=(const TensorShufflingOp &other)
Definition: TensorShuffling.h:74
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::Dimensions
DSizes< Index, NumDims > Dimensions
Definition: TensorShuffling.h:105
Eigen::array< Index, NumDims >
Eigen::TensorShufflingOp::Index
Eigen::internal::traits< TensorShufflingOp >::Index Index
Definition: TensorShuffling.h:61
Eigen::internal::nested
Definition: TensorTraits.h:170
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:42
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:579
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::writePacket
EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType &x)
Definition: TensorShuffling.h:249
Eigen::internal::packet_traits
Definition: GenericPacketMath.h:96
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorShuffling.h:107
Eigen::TensorEvaluator::m_impl
const Derived & m_impl
Definition: TensorEvaluator.h:120
Eigen::PacketType::type
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:51
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::coeffRef
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType & coeffRef(Index index)
Definition: TensorShuffling.h:243
Eigen::TensorShufflingOp::Nested
Eigen::internal::nested< TensorShufflingOp >::type Nested
Definition: TensorShuffling.h:59
Eigen::TensorShufflingOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorShuffling.h:57
Eigen::internal::remove_all::type
T type
Definition: Meta.h:78
Scalar
SCALAR Scalar
Definition: common.h:84
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorShuffling.h:27
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::m_inputStrides
array< Index, NumDims > m_inputStrides
Definition: TensorShuffling.h:212
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: Macros.h:757
Eigen::TensorEvaluator::coeffRef
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: TensorEvaluator.h:71
Eigen::DSizes< Index, NumDims >
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorShuffling.h:28
Eigen::DefaultDevice
Definition: TensorDeviceDefault.h:17
Eigen::internal::true_type
Definition: Meta.h:54
Eigen::internal::remove_reference::type
T type
Definition: Meta.h:66
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::XprType
TensorShufflingOp< Shuffle, ArgType > XprType
Definition: TensorShuffling.h:102
Eigen::TensorShufflingOp::shufflePermutation
const EIGEN_DEVICE_FUNC Shuffle & shufflePermutation() const
Definition: TensorShuffling.h:67
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorShuffling.h:151
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorShuffling.h:167
Eigen::internal::unpacket_traits
Definition: XprHelper.h:158
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:41
x
Scalar * x
Definition: level1_cplx_impl.h:89
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::Index
XprTraits::Index Index
Definition: TensorShuffling.h:29
Eigen::TensorShufflingOp::StorageKind
Eigen::internal::traits< TensorShufflingOp >::StorageKind StorageKind
Definition: TensorShuffling.h:60
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorShuffling.h:225
Eigen::TensorShufflingOp::expression
const EIGEN_DEVICE_FUNC internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorShuffling.h:71
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::Dimensions
DSizes< Index, NumDims > Dimensions
Definition: TensorShuffling.h:227
Eigen::TensorShufflingOp::m_xpr
XprType::Nested m_xpr
Definition: TensorShuffling.h:93
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorShuffling.h:210
Eigen::Map< Matrix< Scalar, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> >
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:829
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorShuffling.h:26
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:43
Eigen::internal::array_size
Definition: EmulateArray.h:203
Eigen::internal::eval< TensorShufflingOp< Shuffle, XprType >, Eigen::Dense >::type
const typedef TensorShufflingOp< Shuffle, XprType > & type
Definition: TensorShuffling.h:39
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::cleanup
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
Definition: TensorShuffling.h:157
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorShuffling.h:31
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >
Definition: TensorShuffling.h:100
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::srcCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
Definition: TensorShuffling.h:191
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorShuffling.h:153
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:124
Eigen::TensorShufflingOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorShuffling.h:58
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::m_outputStrides
array< Index, NumDims > m_outputStrides
Definition: TensorShuffling.h:211
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorShuffling.h:229
Eigen::internal::TensorExecutor::run
static EIGEN_DEVICE_FUNC void run(const Expression &expr, const Device &device=Device())
Definition: TensorExecutor.h:32
Eigen::internal::traits< TensorShufflingOp< Shuffle, XprType > >::Nested
XprType::Nested Nested
Definition: TensorShuffling.h:30
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorShuffling.h:161
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorShuffling.h:230
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::costPerCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorShuffling.h:180
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:66
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::m_impl
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorShuffling.h:213
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
Eigen::TensorShufflingOp
Definition: TensorForwardDeclarations.h:54
internal
Definition: BandTriangularSolver.h:13
Eigen::TensorShufflingOp::m_shuffle
const Shuffle m_shuffle
Definition: TensorShuffling.h:94
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:320
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::Base
TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device > Base
Definition: TensorShuffling.h:222
Eigen::internal::eval
Definition: XprHelper.h:312
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::TensorEvaluator
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorShuffling.h:119
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorShuffling.h:108
Eigen::TensorAssignOp
Definition: TensorAssign.h:60
Eigen::TensorOpCost
Definition: TensorCostModel.h:25
Eigen::TensorEvaluator< const TensorShufflingOp< Shuffle, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorShuffling.h:106
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorShuffling.h:228
Eigen::TensorEvaluator< TensorShufflingOp< Shuffle, ArgType >, Device >::XprType
TensorShufflingOp< Shuffle, ArgType > XprType
Definition: TensorShuffling.h:224
Eigen::GenericNumTraits::Real
T Real
Definition: NumTraits.h:100
Eigen::TensorShufflingOp::Scalar
Eigen::internal::traits< TensorShufflingOp >::Scalar Scalar
Definition: TensorShuffling.h:56
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Eigen::Dense
Definition: Constants.h:491


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:07:04