TensorConcatenation.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_CONCATENATION_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Axis, typename LhsXprType, typename RhsXprType>
24 struct traits<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >
25 {
26  // Type promotion to handle the case where the types of the lhs and the rhs are different.
27  typedef typename promote_storage_type<typename LhsXprType::Scalar,
28  typename RhsXprType::Scalar>::ret Scalar;
29  typedef typename packet_traits<Scalar>::type Packet;
30  typedef typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
31  typename traits<RhsXprType>::StorageKind>::ret StorageKind;
32  typedef typename promote_index_type<typename traits<LhsXprType>::Index,
33  typename traits<RhsXprType>::Index>::type Index;
34  typedef typename LhsXprType::Nested LhsNested;
35  typedef typename RhsXprType::Nested RhsNested;
36  typedef typename remove_reference<LhsNested>::type _LhsNested;
37  typedef typename remove_reference<RhsNested>::type _RhsNested;
38  static const int NumDimensions = traits<LhsXprType>::NumDimensions;
39  static const int Layout = traits<LhsXprType>::Layout;
40  enum { Flags = 0 };
41 };
42 
43 template<typename Axis, typename LhsXprType, typename RhsXprType>
44 struct eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, Eigen::Dense>
45 {
46  typedef const TensorConcatenationOp<Axis, LhsXprType, RhsXprType>& type;
47 };
48 
49 template<typename Axis, typename LhsXprType, typename RhsXprType>
50 struct nested<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, 1, typename eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >::type>
51 {
52  typedef TensorConcatenationOp<Axis, LhsXprType, RhsXprType> type;
53 };
54 
55 } // end namespace internal
56 
57 
58 template<typename Axis, typename LhsXprType, typename RhsXprType>
59 class TensorConcatenationOp : public TensorBase<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, WriteAccessors>
60 {
61  public:
62  typedef typename internal::traits<TensorConcatenationOp>::Scalar Scalar;
63  typedef typename internal::traits<TensorConcatenationOp>::Packet Packet;
64  typedef typename internal::traits<TensorConcatenationOp>::StorageKind StorageKind;
65  typedef typename internal::traits<TensorConcatenationOp>::Index Index;
66  typedef typename internal::nested<TensorConcatenationOp>::type Nested;
67  typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
68  typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
69  typedef typename internal::promote_storage_type<typename LhsXprType::PacketReturnType,
70  typename RhsXprType::PacketReturnType>::ret PacketReturnType;
71  typedef typename NumTraits<Scalar>::Real RealScalar;
72 
73  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(const LhsXprType& lhs, const RhsXprType& rhs, Axis axis)
74  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_axis(axis) {}
75 
76  EIGEN_DEVICE_FUNC
77  const typename internal::remove_all<typename LhsXprType::Nested>::type&
78  lhsExpression() const { return m_lhs_xpr; }
79 
80  EIGEN_DEVICE_FUNC
81  const typename internal::remove_all<typename RhsXprType::Nested>::type&
82  rhsExpression() const { return m_rhs_xpr; }
83 
84  EIGEN_DEVICE_FUNC const Axis& axis() const { return m_axis; }
85 
86  EIGEN_DEVICE_FUNC
87  EIGEN_STRONG_INLINE TensorConcatenationOp& operator = (const TensorConcatenationOp& other)
88  {
89  typedef TensorAssignOp<TensorConcatenationOp, const TensorConcatenationOp> Assign;
90  Assign assign(*this, other);
91  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
92  return *this;
93  }
94 
95  template<typename OtherDerived>
96  EIGEN_DEVICE_FUNC
97  EIGEN_STRONG_INLINE TensorConcatenationOp& operator = (const OtherDerived& other)
98  {
99  typedef TensorAssignOp<TensorConcatenationOp, const OtherDerived> Assign;
100  Assign assign(*this, other);
101  internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
102  return *this;
103  }
104 
105  protected:
106  typename LhsXprType::Nested m_lhs_xpr;
107  typename RhsXprType::Nested m_rhs_xpr;
108  const Axis m_axis;
109 };
110 
111 
112 // Eval as rvalue
113 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
114 struct TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
115 {
117  typedef typename XprType::Index Index;
118  static const int NumDims = internal::array_size<typename TensorEvaluator<LeftArgType, Device>::Dimensions>::value;
119  static const int RightNumDims = internal::array_size<typename TensorEvaluator<RightArgType, Device>::Dimensions>::value;
120  typedef DSizes<Index, NumDims> Dimensions;
121  typedef typename XprType::Scalar Scalar;
122  typedef typename XprType::CoeffReturnType CoeffReturnType;
123  typedef typename XprType::PacketReturnType PacketReturnType;
124  enum {
125  IsAligned = false,
128  };
129 
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
131  : m_leftImpl(op.lhsExpression(), device), m_rightImpl(op.rhsExpression(), device), m_axis(op.axis())
132  {
133  EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout) || NumDims == 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
134  EIGEN_STATIC_ASSERT(NumDims == RightNumDims, YOU_MADE_A_PROGRAMMING_MISTAKE)
135  eigen_assert(0 <= m_axis && m_axis < NumDims);
136  const Dimensions& lhs_dims = m_leftImpl.dimensions();
137  const Dimensions& rhs_dims = m_rightImpl.dimensions();
138  {
139  int i = 0;
140  for (; i < m_axis; ++i) {
141  eigen_assert(lhs_dims[i] > 0);
142  eigen_assert(lhs_dims[i] == rhs_dims[i]);
143  m_dimensions[i] = lhs_dims[i];
144  }
145  eigen_assert(lhs_dims[i] > 0); // Now i == m_axis.
146  eigen_assert(rhs_dims[i] > 0);
147  m_dimensions[i] = lhs_dims[i] + rhs_dims[i];
148  for (++i; i < NumDims; ++i) {
149  eigen_assert(lhs_dims[i] > 0);
150  eigen_assert(lhs_dims[i] == rhs_dims[i]);
151  m_dimensions[i] = lhs_dims[i];
152  }
153  }
154 
155  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
156  m_leftStrides[0] = 1;
157  m_rightStrides[0] = 1;
158  m_outputStrides[0] = 1;
159 
160  for (int j = 1; j < NumDims; ++j) {
161  m_leftStrides[j] = m_leftStrides[j-1] * lhs_dims[j-1];
162  m_rightStrides[j] = m_rightStrides[j-1] * rhs_dims[j-1];
163  m_outputStrides[j] = m_outputStrides[j-1] * m_dimensions[j-1];
164  }
165  } else {
166  m_leftStrides[NumDims - 1] = 1;
167  m_rightStrides[NumDims - 1] = 1;
168  m_outputStrides[NumDims - 1] = 1;
169 
170  for (int j = NumDims - 2; j >= 0; --j) {
171  m_leftStrides[j] = m_leftStrides[j+1] * lhs_dims[j+1];
172  m_rightStrides[j] = m_rightStrides[j+1] * rhs_dims[j+1];
173  m_outputStrides[j] = m_outputStrides[j+1] * m_dimensions[j+1];
174  }
175  }
176  }
177 
178  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
179 
180  // TODO(phli): Add short-circuit memcpy evaluation if underlying data are linear?
181  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/)
182  {
183  m_leftImpl.evalSubExprsIfNeeded(NULL);
184  m_rightImpl.evalSubExprsIfNeeded(NULL);
185  return true;
186  }
187 
188  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
189  {
190  m_leftImpl.cleanup();
191  m_rightImpl.cleanup();
192  }
193 
194  // TODO(phli): attempt to speed this up. The integer divisions and modulo are slow.
195  // See CL/76180724 comments for more ideas.
196  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
197  {
198  // Collect dimension-wise indices (subs).
199  array<Index, NumDims> subs;
200  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
201  for (int i = NumDims - 1; i > 0; --i) {
202  subs[i] = index / m_outputStrides[i];
203  index -= subs[i] * m_outputStrides[i];
204  }
205  subs[0] = index;
206  } else {
207  for (int i = 0; i < NumDims - 1; ++i) {
208  subs[i] = index / m_outputStrides[i];
209  index -= subs[i] * m_outputStrides[i];
210  }
211  subs[NumDims - 1] = index;
212  }
213 
214  const Dimensions& left_dims = m_leftImpl.dimensions();
215  if (subs[m_axis] < left_dims[m_axis]) {
216  Index left_index;
217  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
218  left_index = subs[0];
219  for (int i = 1; i < NumDims; ++i) {
220  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
221  }
222  } else {
223  left_index = subs[NumDims - 1];
224  for (int i = NumDims - 2; i >= 0; --i) {
225  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
226  }
227  }
228  return m_leftImpl.coeff(left_index);
229  } else {
230  subs[m_axis] -= left_dims[m_axis];
231  const Dimensions& right_dims = m_rightImpl.dimensions();
232  Index right_index;
233  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
234  right_index = subs[0];
235  for (int i = 1; i < NumDims; ++i) {
236  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
237  }
238  } else {
239  right_index = subs[NumDims - 1];
240  for (int i = NumDims - 2; i >= 0; --i) {
241  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
242  }
243  }
244  return m_rightImpl.coeff(right_index);
245  }
246  }
247 
248  // TODO(phli): Add a real vectorization.
249  template<int LoadMode>
250  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
251  {
252  static const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
253  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
254  eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
255 
256  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
257  for (int i = 0; i < packetSize; ++i) {
258  values[i] = coeff(index+i);
259  }
260  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
261  return rslt;
262  }
263 
264  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
265 
266  protected:
267  Dimensions m_dimensions;
268  array<Index, NumDims> m_outputStrides;
269  array<Index, NumDims> m_leftStrides;
270  array<Index, NumDims> m_rightStrides;
271  TensorEvaluator<LeftArgType, Device> m_leftImpl;
272  TensorEvaluator<RightArgType, Device> m_rightImpl;
273  const Axis m_axis;
274 };
275 
276 // Eval as lvalue
277 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
278  struct TensorEvaluator<TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
279  : public TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
280 {
281  typedef TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device> Base;
282  typedef TensorConcatenationOp<Axis, LeftArgType, RightArgType> XprType;
283  typedef typename Base::Dimensions Dimensions;
284  enum {
285  IsAligned = false,
286  PacketAccess = TensorEvaluator<LeftArgType, Device>::PacketAccess & TensorEvaluator<RightArgType, Device>::PacketAccess,
287  Layout = TensorEvaluator<LeftArgType, Device>::Layout,
288  };
289 
290  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(XprType& op, const Device& device)
291  : Base(op, device)
292  {
293  EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
294  }
295 
296  typedef typename XprType::Index Index;
297  typedef typename XprType::Scalar Scalar;
298  typedef typename XprType::CoeffReturnType CoeffReturnType;
299  typedef typename XprType::PacketReturnType PacketReturnType;
300 
301  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
302  {
303  // Collect dimension-wise indices (subs).
304  array<Index, Base::NumDims> subs;
305  for (int i = Base::NumDims - 1; i > 0; --i) {
306  subs[i] = index / this->m_outputStrides[i];
307  index -= subs[i] * this->m_outputStrides[i];
308  }
309  subs[0] = index;
310 
311  const Dimensions& left_dims = this->m_leftImpl.dimensions();
312  if (subs[this->m_axis] < left_dims[this->m_axis]) {
313  Index left_index = subs[0];
314  for (int i = 1; i < Base::NumDims; ++i) {
315  left_index += (subs[i] % left_dims[i]) * this->m_leftStrides[i];
316  }
317  return this->m_leftImpl.coeffRef(left_index);
318  } else {
319  subs[this->m_axis] -= left_dims[this->m_axis];
320  const Dimensions& right_dims = this->m_rightImpl.dimensions();
321  Index right_index = subs[0];
322  for (int i = 1; i < Base::NumDims; ++i) {
323  right_index += (subs[i] % right_dims[i]) * this->m_rightStrides[i];
324  }
325  return this->m_rightImpl.coeffRef(right_index);
326  }
327  }
328 
329  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
330  void writePacket(Index index, const PacketReturnType& x)
331  {
332  static const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
333  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
334  eigen_assert(index + packetSize - 1 < this->dimensions().TotalSize());
335 
336  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
337  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
338  for (int i = 0; i < packetSize; ++i) {
339  coeffRef(index+i) = values[i];
340  }
341  }
342 };
343 
344 } // end namespace Eigen
345 
346 #endif // EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13
The tensor evaluator classes.
Definition: TensorEvaluator.h:28
The tensor base class.
Definition: TensorForwardDeclarations.h:19
Tensor concatenation class.
Definition: TensorConcatenation.h:59