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 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;
27  typedef traits<XprType> XprTraits;
28  typedef typename packet_traits<Scalar>::type Packet;
29  typedef typename XprTraits::StorageKind StorageKind;
30  typedef typename XprTraits::Index Index;
31  typedef typename XprType::Nested Nested;
32  typedef typename remove_reference<Nested>::type _Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
35 };
36 
37 template<typename Strides, typename XprType>
38 struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
39 {
40  typedef const TensorInflationOp<Strides, XprType>& type;
41 };
42 
43 template<typename Strides, typename XprType>
44 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
45 {
46  typedef TensorInflationOp<Strides, XprType> type;
47 };
48 
49 } // end namespace internal
50 
51 template<typename Strides, typename XprType>
52 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors>
53 {
54  public:
55  typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
56  typedef typename Eigen::internal::traits<TensorInflationOp>::Packet Packet;
57  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
58  typedef typename XprType::CoeffReturnType CoeffReturnType;
59  typedef typename XprType::PacketReturnType PacketReturnType;
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 
67  EIGEN_DEVICE_FUNC
68  const Strides& strides() const { return m_strides; }
69 
70  EIGEN_DEVICE_FUNC
71  const typename internal::remove_all<typename XprType::Nested>::type&
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 {
83  typedef TensorInflationOp<Strides, ArgType> XprType;
84  typedef typename XprType::Index Index;
85  static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
86  typedef DSizes<Index, NumDims> Dimensions;
87 
88  enum {
89  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
90  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
91  BlockAccess = false,
92  Layout = TensorEvaluator<ArgType, Device>::Layout,
93  CoordAccess = false, // to be implemented
94  };
95 
96  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
97  : m_impl(op.expression(), device), m_strides(op.strides())
98  {
99  m_dimensions = m_impl.dimensions();
100  // Expand each dimension to the inflated dimension.
101  for (int i = 0; i < NumDims; ++i) {
102  m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
103  }
104 
105  // Remember the strides for fast division.
106  for (int i = 0; i < NumDims; ++i) {
107  m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
108  }
109 
110  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
111  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
112  m_outputStrides[0] = 1;
113  m_inputStrides[0] = 1;
114  for (int i = 1; i < NumDims; ++i) {
115  m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
116  m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
117  }
118  } else { // RowMajor
119  m_outputStrides[NumDims-1] = 1;
120  m_inputStrides[NumDims-1] = 1;
121  for (int i = NumDims - 2; i >= 0; --i) {
122  m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
123  m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
124  }
125  }
126  }
127 
128  typedef typename XprType::Scalar Scalar;
129  typedef typename XprType::CoeffReturnType CoeffReturnType;
130  typedef typename XprType::PacketReturnType PacketReturnType;
131 
132  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
133 
134  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
135  m_impl.evalSubExprsIfNeeded(NULL);
136  return true;
137  }
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
139  m_impl.cleanup();
140  }
141 
142  // Computes the input index given the output index. Returns true if the output
143  // index doesn't fall into a hole.
144  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
145  {
146  eigen_assert(index < dimensions().TotalSize());
147  *inputIndex = 0;
148  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
149  for (int i = NumDims - 1; i > 0; --i) {
150  const Index idx = index / m_outputStrides[i];
151  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
152  return false;
153  }
154  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
155  index -= idx * m_outputStrides[i];
156  }
157  if (index != index / m_fastStrides[0] * m_strides[0]) {
158  return false;
159  }
160  *inputIndex += index / m_strides[0];
161  return true;
162  } else {
163  for (int i = 0; i < NumDims - 1; ++i) {
164  const Index idx = index / m_outputStrides[i];
165  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
166  return false;
167  }
168  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
169  index -= idx * m_outputStrides[i];
170  }
171  if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
172  return false;
173  }
174  *inputIndex += index / m_strides[NumDims - 1];
175  }
176  return true;
177  }
178 
179  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
180  {
181  Index inputIndex = 0;
182  if (getInputIndex(index, &inputIndex)) {
183  return m_impl.coeff(inputIndex);
184  } else {
185  return Scalar(0);
186  }
187  }
188 
189  // TODO(yangke): optimize this function so that we can detect and produce
190  // all-zero packets
191  template<int LoadMode>
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
193  {
194  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
195  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
196  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
197 
198  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
199  for (int i = 0; i < packetSize; ++i) {
200  values[i] = coeff(index+i);
201  }
202  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
203  return rslt;
204  }
205 
206  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
207 
208  protected:
209  Dimensions m_dimensions;
210  array<Index, NumDims> m_outputStrides;
211  array<Index, NumDims> m_inputStrides;
212  TensorEvaluator<ArgType, Device> m_impl;
213  const Strides m_strides;
214  array<internal::TensorIntDivisor<Index>, NumDims> m_fastStrides;
215 };
216 
217 } // end namespace Eigen
218 
219 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13