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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
24 namespace internal {
25 template<typename Strides, typename XprType>
26 struct traits<TensorInflationOp<Strides, XprType> > : public traits<XprType>
27 {
28  typedef typename XprType::Scalar Scalar;
29  typedef traits<XprType> XprTraits;
30  typedef typename XprTraits::StorageKind StorageKind;
31  typedef typename XprTraits::Index Index;
32  typedef typename XprType::Nested Nested;
33  typedef std::remove_reference_t<Nested> Nested_;
34  static constexpr int NumDimensions = XprTraits::NumDimensions;
35  static constexpr int Layout = XprTraits::Layout;
36  typedef typename XprTraits::PointerType PointerType;
37 };
38 
39 template<typename Strides, typename XprType>
40 struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
41 {
42  typedef const TensorInflationOp<Strides, XprType>& type;
43 };
44 
45 template<typename Strides, typename XprType>
46 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
47 {
48  typedef TensorInflationOp<Strides, XprType> type;
49 };
50 
51 } // end namespace internal
52 
53 template<typename Strides, typename XprType>
54 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors>
55 {
56  public:
57  typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
59  typedef typename XprType::CoeffReturnType CoeffReturnType;
60  typedef typename Eigen::internal::nested<TensorInflationOp>::type Nested;
61  typedef typename Eigen::internal::traits<TensorInflationOp>::StorageKind StorageKind;
62  typedef typename Eigen::internal::traits<TensorInflationOp>::Index Index;
63 
64  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
65  : m_xpr(expr), m_strides(strides) {}
66 
68  const Strides& strides() const { return m_strides; }
69 
72  expression() const { return m_xpr; }
73 
74  protected:
75  typename XprType::Nested m_xpr;
76  const Strides m_strides;
77 };
78 
79 // Eval as rvalue
80 template<typename Strides, typename ArgType, typename Device>
81 struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
82 {
84  typedef typename XprType::Index Index;
85  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
87  typedef typename XprType::Scalar Scalar;
93 
95  enum {
96  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
98  BlockAccess = false,
99  PreferBlockAccess = false,
100  CoordAccess = false, // to be implemented
101  RawAccess = false
102  };
103 
104  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
105  typedef internal::TensorBlockNotImplemented TensorBlock;
106  //===--------------------------------------------------------------------===//
107 
108  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
109  : m_impl(op.expression(), device), m_strides(op.strides())
110  {
111  m_dimensions = m_impl.dimensions();
112  // Expand each dimension to the inflated dimension.
113  for (int i = 0; i < NumDims; ++i) {
114  m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
115  }
116 
117  // Remember the strides for fast division.
118  for (int i = 0; i < NumDims; ++i) {
119  m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
120  }
121 
122  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
123  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
124  m_outputStrides[0] = 1;
125  m_inputStrides[0] = 1;
126  for (int i = 1; i < NumDims; ++i) {
127  m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
128  m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
129  }
130  } else { // RowMajor
131  m_outputStrides[NumDims-1] = 1;
132  m_inputStrides[NumDims-1] = 1;
133  for (int i = NumDims - 2; i >= 0; --i) {
134  m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
135  m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
136  }
137  }
138  }
139 
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
141 
142  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
143  m_impl.evalSubExprsIfNeeded(NULL);
144  return true;
145  }
146  EIGEN_STRONG_INLINE void cleanup() {
147  m_impl.cleanup();
148  }
149 
150  // Computes the input index given the output index. Returns true if the output
151  // index doesn't fall into a hole.
152  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
153  {
154  eigen_assert(index < dimensions().TotalSize());
155  *inputIndex = 0;
156  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
158  for (int i = NumDims - 1; i > 0; --i) {
159  const Index idx = index / m_outputStrides[i];
160  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
161  return false;
162  }
163  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
164  index -= idx * m_outputStrides[i];
165  }
166  if (index != index / m_fastStrides[0] * m_strides[0]) {
167  return false;
168  }
169  *inputIndex += index / m_strides[0];
170  return true;
171  } else {
173  for (int i = 0; i < NumDims - 1; ++i) {
174  const Index idx = index / m_outputStrides[i];
175  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
176  return false;
177  }
178  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
179  index -= idx * m_outputStrides[i];
180  }
181  if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
182  return false;
183  }
184  *inputIndex += index / m_strides[NumDims - 1];
185  }
186  return true;
187  }
188 
189  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
190  {
191  Index inputIndex = 0;
192  if (getInputIndex(index, &inputIndex)) {
193  return m_impl.coeff(inputIndex);
194  } else {
195  return Scalar(0);
196  }
197  }
198 
199  // TODO(yangke): optimize this function so that we can detect and produce
200  // all-zero packets
201  template<int LoadMode>
202  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
203  {
204  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
205  eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
206 
207  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[PacketSize];
209  for (int i = 0; i < PacketSize; ++i) {
210  values[i] = coeff(index+i);
211  }
212  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
213  return rslt;
214  }
215 
216  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
217  const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
218  3 * TensorOpCost::MulCost<Index>() +
219  2 * TensorOpCost::AddCost<Index>());
220  const double input_size = m_impl.dimensions().TotalSize();
221  const double output_size = m_dimensions.TotalSize();
222  if (output_size == 0)
223  return TensorOpCost();
224  return m_impl.costPerCoeff(vectorized) +
225  TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
226  compute_cost, vectorized, PacketSize);
227  }
228 
229  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
230 
231  protected:
236  const Strides m_strides;
238 };
239 
240 } // end namespace Eigen
241 
242 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
int i
#define EIGEN_ALIGN_MAX
#define EIGEN_UNROLL_LOOP
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_STATIC_ASSERT(X, MSG)
The tensor base class.
Eigen::internal::traits< TensorInflationOp >::Scalar Scalar
Eigen::NumTraits< Scalar >::Real RealScalar
XprType::CoeffReturnType CoeffReturnType
const internal::remove_all_t< typename XprType::Nested > & expression() const
Eigen::internal::nested< TensorInflationOp >::type Nested
TensorInflationOp(const XprType &expr, const Strides &strides)
const Strides & strides() const
Eigen::internal::traits< TensorInflationOp >::StorageKind StorageKind
Eigen::internal::traits< TensorInflationOp >::Index Index
typename remove_all< T >::type remove_all_t
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:28
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
array< internal::TensorIntDivisor< Index >, NumDims > m_fastStrides
A cost model used to limit the number of threads used for evaluating tensor expression.
const Dimensions & dimensions() const
static constexpr int Layout
Derived::Scalar Scalar
CoeffReturnType coeff(Index index) const
static constexpr int PacketSize