TensorInflation.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) 2015 Ke Yang <yangke@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_INFLATION_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Strides, typename XprType>
24 struct traits<TensorInflationOp<Strides, 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 Strides, typename XprType>
37 struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
38 {
40 };
41 
42 template<typename Strides, typename XprType>
43 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
44 {
46 };
47 
48 } // end namespace internal
49 
50 template<typename Strides, typename XprType>
51 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors>
52 {
53  public:
56  typedef typename XprType::CoeffReturnType CoeffReturnType;
60 
61  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
62  : m_xpr(expr), m_strides(strides) {}
63 
64  EIGEN_DEVICE_FUNC
65  const Strides& strides() const { return m_strides; }
66 
67  EIGEN_DEVICE_FUNC
69  expression() const { return m_xpr; }
70 
71  protected:
72  typename XprType::Nested m_xpr;
73  const Strides m_strides;
74 };
75 
76 // Eval as rvalue
77 template<typename Strides, typename ArgType, typename Device>
78 struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
79 {
81  typedef typename XprType::Index Index;
84  typedef typename XprType::Scalar Scalar;
88 
89  enum {
90  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
92  BlockAccess = false,
94  CoordAccess = false, // to be implemented
95  RawAccess = false
96  };
97 
98  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
99  : m_impl(op.expression(), device), m_strides(op.strides())
100  {
101  m_dimensions = m_impl.dimensions();
102  // Expand each dimension to the inflated dimension.
103  for (int i = 0; i < NumDims; ++i) {
104  m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
105  }
106 
107  // Remember the strides for fast division.
108  for (int i = 0; i < NumDims; ++i) {
109  m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
110  }
111 
112  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
113  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
114  m_outputStrides[0] = 1;
115  m_inputStrides[0] = 1;
116  for (int i = 1; i < NumDims; ++i) {
117  m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
118  m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
119  }
120  } else { // RowMajor
121  m_outputStrides[NumDims-1] = 1;
122  m_inputStrides[NumDims-1] = 1;
123  for (int i = NumDims - 2; i >= 0; --i) {
124  m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
125  m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
126  }
127  }
128  }
129 
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
131 
132  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
133  m_impl.evalSubExprsIfNeeded(NULL);
134  return true;
135  }
136  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
137  m_impl.cleanup();
138  }
139 
140  // Computes the input index given the output index. Returns true if the output
141  // index doesn't fall into a hole.
142  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
143  {
144  eigen_assert(index < dimensions().TotalSize());
145  *inputIndex = 0;
146  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
147  for (int i = NumDims - 1; i > 0; --i) {
148  const Index idx = index / m_outputStrides[i];
149  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
150  return false;
151  }
152  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
153  index -= idx * m_outputStrides[i];
154  }
155  if (index != index / m_fastStrides[0] * m_strides[0]) {
156  return false;
157  }
158  *inputIndex += index / m_strides[0];
159  return true;
160  } else {
161  for (int i = 0; i < NumDims - 1; ++i) {
162  const Index idx = index / m_outputStrides[i];
163  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
164  return false;
165  }
166  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
167  index -= idx * m_outputStrides[i];
168  }
169  if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
170  return false;
171  }
172  *inputIndex += index / m_strides[NumDims - 1];
173  }
174  return true;
175  }
176 
177  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
178  {
179  Index inputIndex = 0;
180  if (getInputIndex(index, &inputIndex)) {
181  return m_impl.coeff(inputIndex);
182  } else {
183  return Scalar(0);
184  }
185  }
186 
187  // TODO(yangke): optimize this function so that we can detect and produce
188  // all-zero packets
189  template<int LoadMode>
190  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
191  {
192  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
193  eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
194 
196  for (int i = 0; i < PacketSize; ++i) {
197  values[i] = coeff(index+i);
198  }
199  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
200  return rslt;
201  }
202 
203  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
204  const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
205  3 * TensorOpCost::MulCost<Index>() +
206  2 * TensorOpCost::AddCost<Index>());
207  const double input_size = m_impl.dimensions().TotalSize();
208  const double output_size = m_dimensions.TotalSize();
209  if (output_size == 0)
210  return TensorOpCost();
211  return m_impl.costPerCoeff(vectorized) +
212  TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
213  compute_cost, vectorized, PacketSize);
214  }
215 
216  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
217 
218  protected:
223  const Strides m_strides;
225 };
226 
227 } // end namespace Eigen
228 
229 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:54
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorInflation.h:31
Eigen::TensorInflationOp::expression
const EIGEN_DEVICE_FUNC internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorInflation.h:69
Eigen::TensorEvaluator::device
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition: TensorEvaluator.h:114
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorInflation.h:28
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorInflation.h:190
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorInflation.h:86
Eigen
Definition: common.h:73
Eigen::TensorInflationOp::StorageKind
Eigen::internal::traits< TensorInflationOp >::StorageKind StorageKind
Definition: TensorInflation.h:58
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorInflation.h:177
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Nested
XprType::Nested Nested
Definition: TensorInflation.h:30
Eigen::array< Index, NumDims >
Eigen::TensorInflationOp::Index
Eigen::internal::traits< TensorInflationOp >::Index Index
Definition: TensorInflation.h:59
Eigen::TensorInflationOp::m_strides
const Strides m_strides
Definition: TensorInflation.h:73
Eigen::internal::nested
Definition: TensorTraits.h:170
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:42
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorInflation.h:219
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:579
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::costPerCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorInflation.h:203
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Index
XprTraits::Index Index
Definition: TensorInflation.h:29
Eigen::internal::nested< TensorInflationOp< Strides, XprType >, 1, typename eval< TensorInflationOp< Strides, XprType > >::type >::type
TensorInflationOp< Strides, XprType > type
Definition: TensorInflation.h:45
Eigen::internal::eval< TensorInflationOp< Strides, XprType >, Eigen::Dense >::type
const typedef TensorInflationOp< Strides, XprType > & type
Definition: TensorInflation.h:39
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::internal::remove_all::type
T type
Definition: Meta.h:78
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorInflation.h:84
Scalar
SCALAR Scalar
Definition: common.h:84
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: Macros.h:757
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Dimensions
DSizes< Index, NumDims > Dimensions
Definition: TensorInflation.h:83
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorInflation.h:26
Eigen::internal::TensorIntDivisor< Index >
Eigen::DSizes< Index, NumDims >
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorInflation.h:132
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::TensorEvaluator
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorInflation.h:98
Eigen::internal::true_type
Definition: Meta.h:54
Eigen::internal::remove_reference::type
T type
Definition: Meta.h:66
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorInflation.h:85
Eigen::internal::unpacket_traits
Definition: XprHelper.h:158
Eigen::TensorInflationOp::Scalar
Eigen::internal::traits< TensorInflationOp >::Scalar Scalar
Definition: TensorInflation.h:54
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::getInputIndex
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index *inputIndex) const
Definition: TensorInflation.h:142
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:41
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
Eigen::TensorInflationOp::Nested
Eigen::internal::nested< TensorInflationOp >::type Nested
Definition: TensorInflation.h:57
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_impl
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorInflation.h:222
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorInflation.h:81
Eigen::TensorInflationOp::TensorInflationOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType &expr, const Strides &strides)
Definition: TensorInflation.h:61
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::cleanup
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
Definition: TensorInflation.h:136
Eigen::TensorInflationOp::m_xpr
XprType::Nested m_xpr
Definition: TensorInflation.h:72
Eigen::Map< Matrix< Scalar, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> >
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:829
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:43
Eigen::internal::array_size
Definition: EmulateArray.h:203
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::data
EIGEN_DEVICE_FUNC Scalar * data() const
Definition: TensorInflation.h:216
Eigen::TensorInflationOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorInflation.h:55
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:124
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorInflation.h:130
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:66
Eigen::TensorInflationOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorInflation.h:56
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
Eigen::TensorEvaluator::Scalar
Derived::Scalar Scalar
Definition: TensorEvaluator.h:31
Eigen::TensorInflationOp
Definition: TensorForwardDeclarations.h:57
internal
Definition: BandTriangularSolver.h:13
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:320
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::XprType
TensorInflationOp< Strides, ArgType > XprType
Definition: TensorInflation.h:80
Eigen::internal::eval
Definition: XprHelper.h:312
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_inputStrides
array< Index, NumDims > m_inputStrides
Definition: TensorInflation.h:221
Eigen::TensorInflationOp::strides
const EIGEN_DEVICE_FUNC Strides & strides() const
Definition: TensorInflation.h:65
Eigen::TensorOpCost
Definition: TensorCostModel.h:25
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_strides
const Strides m_strides
Definition: TensorInflation.h:223
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_fastStrides
array< internal::TensorIntDivisor< Index >, NumDims > m_fastStrides
Definition: TensorInflation.h:224
Eigen::GenericNumTraits::Real
T Real
Definition: NumTraits.h:100
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_outputStrides
array< Index, NumDims > m_outputStrides
Definition: TensorInflation.h:220
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorInflation.h:27
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:06:46