TensorChipping.h
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_CHIPPING_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_CHIPPING_H
12 
13 namespace Eigen {
14 
23 namespace internal {
24 template<DenseIndex DimId, typename XprType>
25 struct traits<TensorChippingOp<DimId, XprType> > : public traits<XprType>
26 {
27  typedef typename XprType::Scalar Scalar;
28  typedef traits<XprType> XprTraits;
29  typedef typename packet_traits<Scalar>::type Packet;
30  typedef typename XprTraits::StorageKind StorageKind;
31  typedef typename XprTraits::Index Index;
32  typedef typename XprType::Nested Nested;
33  typedef typename remove_reference<Nested>::type _Nested;
34  static const int NumDimensions = XprTraits::NumDimensions - 1;
35  static const int Layout = XprTraits::Layout;
36 };
37 
38 template<DenseIndex DimId, typename XprType>
39 struct eval<TensorChippingOp<DimId, XprType>, Eigen::Dense>
40 {
41  typedef const TensorChippingOp<DimId, XprType>& type;
42 };
43 
44 template<DenseIndex DimId, typename XprType>
45 struct nested<TensorChippingOp<DimId, XprType>, 1, typename eval<TensorChippingOp<DimId, XprType> >::type>
46 {
47  typedef TensorChippingOp<DimId, XprType> type;
48 };
49 
50 template <DenseIndex DimId>
51 struct DimensionId
52 {
53  DimensionId(DenseIndex dim) {
54  eigen_assert(dim == DimId);
55  }
56  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex actualDim() const {
57  return DimId;
58  }
59 };
60 template <>
61 struct DimensionId<Dynamic>
62 {
63  DimensionId(DenseIndex dim) : actual_dim(dim) {
64  eigen_assert(dim >= 0);
65  }
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex actualDim() const {
67  return actual_dim;
68  }
69  private:
70  const DenseIndex actual_dim;
71 };
72 
73 
74 } // end namespace internal
75 
76 
77 
78 template<DenseIndex DimId, typename XprType>
79 class TensorChippingOp : public TensorBase<TensorChippingOp<DimId, XprType> >
80 {
81  public:
82  typedef typename Eigen::internal::traits<TensorChippingOp>::Scalar Scalar;
83  typedef typename Eigen::internal::traits<TensorChippingOp>::Packet Packet;
84  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
85  typedef typename XprType::CoeffReturnType CoeffReturnType;
86  typedef typename XprType::PacketReturnType PacketReturnType;
87  typedef typename Eigen::internal::nested<TensorChippingOp>::type Nested;
88  typedef typename Eigen::internal::traits<TensorChippingOp>::StorageKind StorageKind;
89  typedef typename Eigen::internal::traits<TensorChippingOp>::Index Index;
90 
91  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorChippingOp(const XprType& expr, const Index offset, const Index dim)
92  : m_xpr(expr), m_offset(offset), m_dim(dim) {
93  }
94 
95  EIGEN_DEVICE_FUNC
96  const Index offset() const { return m_offset; }
97  EIGEN_DEVICE_FUNC
98  const Index dim() const { return m_dim.actualDim(); }
99 
100  EIGEN_DEVICE_FUNC
101  const typename internal::remove_all<typename XprType::Nested>::type&
102  expression() const { return m_xpr; }
103 
104  EIGEN_DEVICE_FUNC
105  EIGEN_STRONG_INLINE TensorChippingOp& operator = (const TensorChippingOp& other)
106  {
107  typedef TensorAssignOp<TensorChippingOp, const TensorChippingOp> Assign;
108  Assign assign(*this, other);
109  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
110  return *this;
111  }
112 
113  template<typename OtherDerived>
114  EIGEN_DEVICE_FUNC
115  EIGEN_STRONG_INLINE TensorChippingOp& operator = (const OtherDerived& other)
116  {
117  typedef TensorAssignOp<TensorChippingOp, const OtherDerived> Assign;
118  Assign assign(*this, other);
119  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
120  return *this;
121  }
122 
123  protected:
124  typename XprType::Nested m_xpr;
125  const Index m_offset;
126  const internal::DimensionId<DimId> m_dim;
127 };
128 
129 
130 // Eval as rvalue
131 template<DenseIndex DimId, typename ArgType, typename Device>
132 struct TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device>
133 {
134  typedef TensorChippingOp<DimId, ArgType> XprType;
135  static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
136  static const int NumDims = NumInputDims-1;
137  typedef typename XprType::Index Index;
138  typedef DSizes<Index, NumDims> Dimensions;
139  typedef typename XprType::Scalar Scalar;
140 
141  enum {
142  // Alignment can't be guaranteed at compile time since it depends on the
143  // slice offsets.
144  IsAligned = false,
145  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
146  Layout = TensorEvaluator<ArgType, Device>::Layout,
147  CoordAccess = false, // to be implemented
148  };
149 
150  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
151  : m_impl(op.expression(), device), m_dim(op.dim()), m_device(device)
152  {
153  // We could also support the case where NumInputDims==1 if needed.
154  EIGEN_STATIC_ASSERT(NumInputDims >= 2, YOU_MADE_A_PROGRAMMING_MISTAKE);
155  eigen_assert(NumInputDims > m_dim.actualDim());
156 
157  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
158  eigen_assert(op.offset() < input_dims[m_dim.actualDim()]);
159 
160  int j = 0;
161  for (int i = 0; i < NumInputDims; ++i) {
162  if (i != m_dim.actualDim()) {
163  m_dimensions[j] = input_dims[i];
164  ++j;
165  }
166  }
167 
168  m_stride = 1;
169  m_inputStride = 1;
170  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
171  for (int i = 0; i < m_dim.actualDim(); ++i) {
172  m_stride *= input_dims[i];
173  m_inputStride *= input_dims[i];
174  }
175  } else {
176  for (int i = NumInputDims-1; i > m_dim.actualDim(); --i) {
177  m_stride *= input_dims[i];
178  m_inputStride *= input_dims[i];
179  }
180  }
181  m_inputStride *= input_dims[m_dim.actualDim()];
182  m_inputOffset = m_stride * op.offset();
183  }
184 
185  typedef typename XprType::CoeffReturnType CoeffReturnType;
186  typedef typename XprType::PacketReturnType PacketReturnType;
187 
188  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
189 
190  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
191  m_impl.evalSubExprsIfNeeded(NULL);
192  return true;
193  }
194 
195  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
196  m_impl.cleanup();
197  }
198 
199  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
200  {
201  return m_impl.coeff(srcCoeff(index));
202  }
203 
204  template<int LoadMode>
205  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
206  {
207  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
208  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
209  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
210 
211  if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == 0) ||
212  (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == NumInputDims-1)) {
213  // m_stride is equal to 1, so let's avoid the integer division.
214  eigen_assert(m_stride == 1);
215  Index inputIndex = index * m_inputStride + m_inputOffset;
216  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
217  for (int i = 0; i < packetSize; ++i) {
218  values[i] = m_impl.coeff(inputIndex);
219  inputIndex += m_inputStride;
220  }
221  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
222  return rslt;
223  } else if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumInputDims - 1) ||
224  (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) {
225  // m_stride is aways greater than index, so let's avoid the integer division.
226  eigen_assert(m_stride > index);
227  return m_impl.template packet<LoadMode>(index + m_inputOffset);
228  } else {
229  const Index idx = index / m_stride;
230  const Index rem = index - idx * m_stride;
231  if (rem + packetSize <= m_stride) {
232  Index inputIndex = idx * m_inputStride + m_inputOffset + rem;
233  return m_impl.template packet<LoadMode>(inputIndex);
234  } else {
235  // Cross the stride boundary. Fallback to slow path.
236  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
237  for (int i = 0; i < packetSize; ++i) {
238  values[i] = coeff(index);
239  ++index;
240  }
241  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
242  return rslt;
243  }
244  }
245  }
246 
247  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar* data() const {
248  Scalar* result = m_impl.data();
249  if (((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumDims) ||
250  (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) &&
251  result) {
252  return result + m_inputOffset;
253  } else {
254  return NULL;
255  }
256  }
257 
258  protected:
259  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
260  {
261  Index inputIndex;
262  if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == 0) ||
263  (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == NumInputDims-1)) {
264  // m_stride is equal to 1, so let's avoid the integer division.
265  eigen_assert(m_stride == 1);
266  inputIndex = index * m_inputStride + m_inputOffset;
267  } else if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumInputDims-1) ||
268  (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) {
269  // m_stride is aways greater than index, so let's avoid the integer division.
270  eigen_assert(m_stride > index);
271  inputIndex = index + m_inputOffset;
272  } else {
273  const Index idx = index / m_stride;
274  inputIndex = idx * m_inputStride + m_inputOffset;
275  index -= idx * m_stride;
276  inputIndex += index;
277  }
278  return inputIndex;
279  }
280 
281  Dimensions m_dimensions;
282  Index m_stride;
283  Index m_inputOffset;
284  Index m_inputStride;
285  TensorEvaluator<ArgType, Device> m_impl;
286  const internal::DimensionId<DimId> m_dim;
287  const Device& m_device;
288 };
289 
290 
291 // Eval as lvalue
292 template<DenseIndex DimId, typename ArgType, typename Device>
293 struct TensorEvaluator<TensorChippingOp<DimId, ArgType>, Device>
294  : public TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device>
295 {
296  typedef TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device> Base;
297  typedef TensorChippingOp<DimId, ArgType> XprType;
298  static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
299  static const int NumDims = NumInputDims-1;
300  typedef typename XprType::Index Index;
301  typedef DSizes<Index, NumDims> Dimensions;
302  typedef typename XprType::Scalar Scalar;
303 
304  enum {
305  IsAligned = false,
306  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
307  };
308 
309  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
310  : Base(op, device)
311  { }
312 
313  typedef typename XprType::CoeffReturnType CoeffReturnType;
314  typedef typename XprType::PacketReturnType PacketReturnType;
315 
316  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
317  {
318  return this->m_impl.coeffRef(this->srcCoeff(index));
319  }
320 
321  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
322  void writePacket(Index index, const PacketReturnType& x)
323  {
324  static const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
325  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
326 
327  if ((static_cast<int>(this->Layout) == static_cast<int>(ColMajor) && this->m_dim.actualDim() == 0) ||
328  (static_cast<int>(this->Layout) == static_cast<int>(RowMajor) && this->m_dim.actualDim() == NumInputDims-1)) {
329  // m_stride is equal to 1, so let's avoid the integer division.
330  eigen_assert(this->m_stride == 1);
331  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
332  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
333  Index inputIndex = index * this->m_inputStride + this->m_inputOffset;
334  for (int i = 0; i < packetSize; ++i) {
335  this->m_impl.coeffRef(inputIndex) = values[i];
336  inputIndex += this->m_inputStride;
337  }
338  } else if ((static_cast<int>(this->Layout) == static_cast<int>(ColMajor) && this->m_dim.actualDim() == NumInputDims-1) ||
339  (static_cast<int>(this->Layout) == static_cast<int>(RowMajor) && this->m_dim.actualDim() == 0)) {
340  // m_stride is aways greater than index, so let's avoid the integer division.
341  eigen_assert(this->m_stride > index);
342  this->m_impl.template writePacket<StoreMode>(index + this->m_inputOffset, x);
343  } else {
344  const Index idx = index / this->m_stride;
345  const Index rem = index - idx * this->m_stride;
346  if (rem + packetSize <= this->m_stride) {
347  const Index inputIndex = idx * this->m_inputStride + this->m_inputOffset + rem;
348  this->m_impl.template writePacket<StoreMode>(inputIndex, x);
349  } else {
350  // Cross stride boundary. Fallback to slow path.
351  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
352  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
353  for (int i = 0; i < packetSize; ++i) {
354  this->coeffRef(index) = values[i];
355  ++index;
356  }
357  }
358  }
359  }
360 };
361 
362 
363 } // end namespace Eigen
364 
365 #endif // EIGEN_CXX11_TENSOR_TENSOR_CHIPPING_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13