$darkmode
Eigen-unsupported  5.0.1-dev
TensorInflation.h
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename Strides, typename XprType>
20 struct traits<TensorInflationOp<Strides, 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 = XprTraits::NumDimensions;
28  static constexpr int Layout = XprTraits::Layout;
29  typedef typename XprTraits::PointerType PointerType;
30 };
31 
32 template <typename Strides, typename XprType>
33 struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense> {
34  typedef const TensorInflationOp<Strides, XprType>& type;
35 };
36 
37 template <typename Strides, typename XprType>
38 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type> {
39  typedef TensorInflationOp<Strides, XprType> type;
40 };
41 
42 } // end namespace internal
43 
49 template <typename Strides, typename XprType>
50 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors> {
51  public:
52  typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
53  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
54  typedef typename XprType::CoeffReturnType CoeffReturnType;
55  typedef typename Eigen::internal::nested<TensorInflationOp>::type Nested;
56  typedef typename Eigen::internal::traits<TensorInflationOp>::StorageKind StorageKind;
57  typedef typename Eigen::internal::traits<TensorInflationOp>::Index Index;
58 
59  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
60  : m_xpr(expr), m_strides(strides) {}
61 
62  EIGEN_DEVICE_FUNC const Strides& strides() const { return m_strides; }
63 
64  EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
65 
66  protected:
67  typename XprType::Nested m_xpr;
68  const Strides m_strides;
69 };
70 
71 // Eval as rvalue
72 template <typename Strides, typename ArgType, typename Device>
73 struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device> {
74  typedef TensorInflationOp<Strides, ArgType> XprType;
75  typedef typename XprType::Index Index;
76  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
77  typedef DSizes<Index, NumDims> Dimensions;
78  typedef typename XprType::Scalar Scalar;
79  typedef typename XprType::CoeffReturnType CoeffReturnType;
80  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
81  static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
82  typedef StorageMemory<CoeffReturnType, Device> Storage;
83  typedef typename Storage::Type EvaluatorPointerType;
84 
85  static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
86  enum {
87  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
88  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
89  BlockAccess = false,
90  PreferBlockAccess = false,
91  CoordAccess = false, // to be implemented
92  RawAccess = false
93  };
94 
95  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
96  typedef internal::TensorBlockNotImplemented TensorBlock;
97  //===--------------------------------------------------------------------===//
98 
99  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
100  : m_impl(op.expression(), device), m_strides(op.strides()) {
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_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
133  m_impl.evalSubExprsIfNeeded(NULL);
134  return true;
135  }
136  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
137 
138  // Computes the input index given the output index. Returns true if the output
139  // index doesn't fall into a hole.
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const {
141  eigen_assert(index < dimensions().TotalSize());
142  *inputIndex = 0;
143  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
144  EIGEN_UNROLL_LOOP
145  for (int i = NumDims - 1; i > 0; --i) {
146  const Index idx = index / m_outputStrides[i];
147  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
148  return false;
149  }
150  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
151  index -= idx * m_outputStrides[i];
152  }
153  if (index != index / m_fastStrides[0] * m_strides[0]) {
154  return false;
155  }
156  *inputIndex += index / m_strides[0];
157  return true;
158  } else {
159  EIGEN_UNROLL_LOOP
160  for (int i = 0; i < NumDims - 1; ++i) {
161  const Index idx = index / m_outputStrides[i];
162  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
163  return false;
164  }
165  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
166  index -= idx * m_outputStrides[i];
167  }
168  if (index != index / m_fastStrides[NumDims - 1] * m_strides[NumDims - 1]) {
169  return false;
170  }
171  *inputIndex += index / m_strides[NumDims - 1];
172  }
173  return true;
174  }
175 
176  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
177  Index inputIndex = 0;
178  if (getInputIndex(index, &inputIndex)) {
179  return m_impl.coeff(inputIndex);
180  } else {
181  return Scalar(0);
182  }
183  }
184 
185  // TODO(yangke): optimize this function so that we can detect and produce
186  // all-zero packets
187  template <int LoadMode>
188  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
189  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
190  eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
191 
192  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[PacketSize];
193  EIGEN_UNROLL_LOOP
194  for (int i = 0; i < PacketSize; ++i) {
195  values[i] = coeff(index + i);
196  }
197  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
198  return rslt;
199  }
200 
201  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
202  const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() + 3 * TensorOpCost::MulCost<Index>() +
203  2 * TensorOpCost::AddCost<Index>());
204  const double input_size = m_impl.dimensions().TotalSize();
205  const double output_size = m_dimensions.TotalSize();
206  if (output_size == 0) return TensorOpCost();
207  return m_impl.costPerCoeff(vectorized) +
208  TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0, compute_cost, vectorized, PacketSize);
209  }
210 
211  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
212 
213  protected:
214  Dimensions m_dimensions;
215  array<Index, NumDims> m_outputStrides;
216  array<Index, NumDims> m_inputStrides;
217  TensorEvaluator<ArgType, Device> m_impl;
218  const Strides m_strides;
219  array<internal::TensorIntDivisor<Index>, NumDims> m_fastStrides;
220 };
221 
222 } // end namespace Eigen
223 
224 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index