Eigen-unsupported  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
24 namespace internal {
25 template<typename Axis, typename LhsXprType, typename RhsXprType>
26 struct traits<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >
27 {
28  // Type promotion to handle the case where the types of the lhs and the rhs are different.
29  typedef typename promote_storage_type<typename LhsXprType::Scalar,
30  typename RhsXprType::Scalar>::ret Scalar;
31  typedef typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
32  typename traits<RhsXprType>::StorageKind>::ret StorageKind;
33  typedef typename promote_index_type<typename traits<LhsXprType>::Index,
34  typename traits<RhsXprType>::Index>::type Index;
35  typedef typename LhsXprType::Nested LhsNested;
36  typedef typename RhsXprType::Nested RhsNested;
37  typedef std::remove_reference_t<LhsNested> LhsNested_;
38  typedef std::remove_reference_t<RhsNested> RhsNested_;
39  static constexpr int NumDimensions = traits<LhsXprType>::NumDimensions;
40  static constexpr int Layout = traits<LhsXprType>::Layout;
41  enum { Flags = 0 };
42  typedef std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
43  typename traits<LhsXprType>::PointerType, typename traits<RhsXprType>::PointerType> PointerType;
44 };
45 
46 template<typename Axis, typename LhsXprType, typename RhsXprType>
47 struct eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, Eigen::Dense>
48 {
49  typedef const TensorConcatenationOp<Axis, LhsXprType, RhsXprType>& type;
50 };
51 
52 template<typename Axis, typename LhsXprType, typename RhsXprType>
53 struct nested<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, 1, typename eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >::type>
54 {
55  typedef TensorConcatenationOp<Axis, LhsXprType, RhsXprType> type;
56 };
57 
58 } // end namespace internal
59 
60 
61 template<typename Axis, typename LhsXprType, typename RhsXprType>
62 class TensorConcatenationOp : public TensorBase<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, WriteAccessors>
63 {
64  public:
66  typedef typename internal::traits<TensorConcatenationOp>::Scalar Scalar;
67  typedef typename internal::traits<TensorConcatenationOp>::StorageKind StorageKind;
68  typedef typename internal::traits<TensorConcatenationOp>::Index Index;
69  typedef typename internal::nested<TensorConcatenationOp>::type Nested;
70  typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
71  typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
72  typedef typename NumTraits<Scalar>::Real RealScalar;
73 
74  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(const LhsXprType& lhs, const RhsXprType& rhs, Axis axis)
75  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_axis(axis) {}
76 
77  EIGEN_DEVICE_FUNC
78  const internal::remove_all_t<typename LhsXprType::Nested>&
79  lhsExpression() const { return m_lhs_xpr; }
80 
81  EIGEN_DEVICE_FUNC
82  const internal::remove_all_t<typename RhsXprType::Nested>&
83  rhsExpression() const { return m_rhs_xpr; }
84 
85  EIGEN_DEVICE_FUNC const Axis& axis() const { return m_axis; }
86 
87  EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorConcatenationOp)
88  protected:
89  typename LhsXprType::Nested m_lhs_xpr;
90  typename RhsXprType::Nested m_rhs_xpr;
91  const Axis m_axis;
92 };
93 
94 
95 // Eval as rvalue
96 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
97 struct TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
98 {
100  typedef typename XprType::Index Index;
101  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<LeftArgType, Device>::Dimensions>::value;
102  static constexpr int RightNumDims = internal::array_size<typename TensorEvaluator<RightArgType, Device>::Dimensions>::value;
103  typedef DSizes<Index, NumDims> Dimensions;
104  typedef typename XprType::Scalar Scalar;
105  typedef typename XprType::CoeffReturnType CoeffReturnType;
106  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
107  typedef StorageMemory<CoeffReturnType, Device> Storage;
108  typedef typename Storage::Type EvaluatorPointerType;
109  static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
110  enum {
111  IsAligned = false,
114  BlockAccess = false,
117  RawAccess = false
118  };
119 
120  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
121  typedef internal::TensorBlockNotImplemented TensorBlock;
122  //===--------------------------------------------------------------------===//
123 
124  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
125  : m_leftImpl(op.lhsExpression(), device), m_rightImpl(op.rhsExpression(), device), m_axis(op.axis())
126  {
127  EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout) || NumDims == 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
128  EIGEN_STATIC_ASSERT((NumDims == RightNumDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
129  EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
130 
131  eigen_assert(0 <= m_axis && m_axis < NumDims);
132  const Dimensions& lhs_dims = m_leftImpl.dimensions();
133  const Dimensions& rhs_dims = m_rightImpl.dimensions();
134  {
135  int i = 0;
136  for (; i < m_axis; ++i) {
137  eigen_assert(lhs_dims[i] > 0);
138  eigen_assert(lhs_dims[i] == rhs_dims[i]);
139  m_dimensions[i] = lhs_dims[i];
140  }
141  eigen_assert(lhs_dims[i] > 0); // Now i == m_axis.
142  eigen_assert(rhs_dims[i] > 0);
143  m_dimensions[i] = lhs_dims[i] + rhs_dims[i];
144  for (++i; i < NumDims; ++i) {
145  eigen_assert(lhs_dims[i] > 0);
146  eigen_assert(lhs_dims[i] == rhs_dims[i]);
147  m_dimensions[i] = lhs_dims[i];
148  }
149  }
150 
151  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
152  m_leftStrides[0] = 1;
153  m_rightStrides[0] = 1;
154  m_outputStrides[0] = 1;
155 
156  for (int j = 1; j < NumDims; ++j) {
157  m_leftStrides[j] = m_leftStrides[j-1] * lhs_dims[j-1];
158  m_rightStrides[j] = m_rightStrides[j-1] * rhs_dims[j-1];
159  m_outputStrides[j] = m_outputStrides[j-1] * m_dimensions[j-1];
160  }
161  } else {
162  m_leftStrides[NumDims - 1] = 1;
163  m_rightStrides[NumDims - 1] = 1;
164  m_outputStrides[NumDims - 1] = 1;
165 
166  for (int j = NumDims - 2; j >= 0; --j) {
167  m_leftStrides[j] = m_leftStrides[j+1] * lhs_dims[j+1];
168  m_rightStrides[j] = m_rightStrides[j+1] * rhs_dims[j+1];
169  m_outputStrides[j] = m_outputStrides[j+1] * m_dimensions[j+1];
170  }
171  }
172  }
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
175 
176  // TODO(phli): Add short-circuit memcpy evaluation if underlying data are linear?
177  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
178  {
179  m_leftImpl.evalSubExprsIfNeeded(NULL);
180  m_rightImpl.evalSubExprsIfNeeded(NULL);
181  return true;
182  }
183 
184  EIGEN_STRONG_INLINE void cleanup()
185  {
186  m_leftImpl.cleanup();
187  m_rightImpl.cleanup();
188  }
189 
190  // TODO(phli): attempt to speed this up. The integer divisions and modulo are slow.
191  // See CL/76180724 comments for more ideas.
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
193  {
194  // Collect dimension-wise indices (subs).
195  array<Index, NumDims> subs;
196  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
197  for (int i = NumDims - 1; i > 0; --i) {
198  subs[i] = index / m_outputStrides[i];
199  index -= subs[i] * m_outputStrides[i];
200  }
201  subs[0] = index;
202  } else {
203  for (int i = 0; i < NumDims - 1; ++i) {
204  subs[i] = index / m_outputStrides[i];
205  index -= subs[i] * m_outputStrides[i];
206  }
207  subs[NumDims - 1] = index;
208  }
209 
210  const Dimensions& left_dims = m_leftImpl.dimensions();
211  if (subs[m_axis] < left_dims[m_axis]) {
212  Index left_index;
213  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
214  left_index = subs[0];
215  EIGEN_UNROLL_LOOP
216  for (int i = 1; i < NumDims; ++i) {
217  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
218  }
219  } else {
220  left_index = subs[NumDims - 1];
221  EIGEN_UNROLL_LOOP
222  for (int i = NumDims - 2; i >= 0; --i) {
223  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
224  }
225  }
226  return m_leftImpl.coeff(left_index);
227  } else {
228  subs[m_axis] -= left_dims[m_axis];
229  const Dimensions& right_dims = m_rightImpl.dimensions();
230  Index right_index;
231  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
232  right_index = subs[0];
233  EIGEN_UNROLL_LOOP
234  for (int i = 1; i < NumDims; ++i) {
235  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
236  }
237  } else {
238  right_index = subs[NumDims - 1];
239  EIGEN_UNROLL_LOOP
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  const int packetSize = PacketType<CoeffReturnType, Device>::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  EIGEN_UNROLL_LOOP
258  for (int i = 0; i < packetSize; ++i) {
259  values[i] = coeff(index+i);
260  }
261  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
262  return rslt;
263  }
264 
265  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
266  costPerCoeff(bool vectorized) const {
267  const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
268  2 * TensorOpCost::MulCost<Index>() +
269  TensorOpCost::DivCost<Index>() +
270  TensorOpCost::ModCost<Index>());
271  const double lhs_size = m_leftImpl.dimensions().TotalSize();
272  const double rhs_size = m_rightImpl.dimensions().TotalSize();
273  return (lhs_size / (lhs_size + rhs_size)) *
274  m_leftImpl.costPerCoeff(vectorized) +
275  (rhs_size / (lhs_size + rhs_size)) *
276  m_rightImpl.costPerCoeff(vectorized) +
277  TensorOpCost(0, 0, compute_cost);
278  }
279 
280  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
281 
282  #ifdef EIGEN_USE_SYCL
283  // binding placeholder accessors to a command group handler for SYCL
284  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
285  m_leftImpl.bind(cgh);
286  m_rightImpl.bind(cgh);
287  }
288  #endif
289 
290  protected:
291  Dimensions m_dimensions;
292  array<Index, NumDims> m_outputStrides;
293  array<Index, NumDims> m_leftStrides;
294  array<Index, NumDims> m_rightStrides;
295  TensorEvaluator<LeftArgType, Device> m_leftImpl;
296  TensorEvaluator<RightArgType, Device> m_rightImpl;
297  const Axis m_axis;
298 };
299 
300 // Eval as lvalue
301 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
302  struct TensorEvaluator<TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
303  : public TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
304 {
305  typedef TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device> Base;
306  typedef TensorConcatenationOp<Axis, LeftArgType, RightArgType> XprType;
307  typedef typename Base::Dimensions Dimensions;
308  static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
309  enum {
310  IsAligned = false,
311  PacketAccess = TensorEvaluator<LeftArgType, Device>::PacketAccess &&
312  TensorEvaluator<RightArgType, Device>::PacketAccess,
313  BlockAccess = false,
314  PreferBlockAccess = TensorEvaluator<LeftArgType, Device>::PreferBlockAccess ||
315  TensorEvaluator<RightArgType, Device>::PreferBlockAccess,
316  RawAccess = false
317  };
318 
319  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
320  typedef internal::TensorBlockNotImplemented TensorBlock;
321  //===--------------------------------------------------------------------===//
322 
323  EIGEN_STRONG_INLINE TensorEvaluator(XprType& op, const Device& device)
324  : Base(op, device)
325  {
326  EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
327  }
328 
329  typedef typename XprType::Index Index;
330  typedef typename XprType::Scalar Scalar;
331  typedef typename XprType::CoeffReturnType CoeffReturnType;
332  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
333 
334  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
335  {
336  // Collect dimension-wise indices (subs).
337  array<Index, Base::NumDims> subs;
338  for (int i = Base::NumDims - 1; i > 0; --i) {
339  subs[i] = index / this->m_outputStrides[i];
340  index -= subs[i] * this->m_outputStrides[i];
341  }
342  subs[0] = index;
343 
344  const Dimensions& left_dims = this->m_leftImpl.dimensions();
345  if (subs[this->m_axis] < left_dims[this->m_axis]) {
346  Index left_index = subs[0];
347  for (int i = 1; i < Base::NumDims; ++i) {
348  left_index += (subs[i] % left_dims[i]) * this->m_leftStrides[i];
349  }
350  return this->m_leftImpl.coeffRef(left_index);
351  } else {
352  subs[this->m_axis] -= left_dims[this->m_axis];
353  const Dimensions& right_dims = this->m_rightImpl.dimensions();
354  Index right_index = subs[0];
355  for (int i = 1; i < Base::NumDims; ++i) {
356  right_index += (subs[i] % right_dims[i]) * this->m_rightStrides[i];
357  }
358  return this->m_rightImpl.coeffRef(right_index);
359  }
360  }
361 
362  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
363  void writePacket(Index index, const PacketReturnType& x)
364  {
365  const int packetSize = PacketType<CoeffReturnType, Device>::size;
366  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
367  eigen_assert(index + packetSize - 1 < this->dimensions().TotalSize());
368 
369  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
370  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
371  for (int i = 0; i < packetSize; ++i) {
372  coeffRef(index+i) = values[i];
373  }
374  }
375 };
376 
377 } // end namespace Eigen
378 
379 #endif // EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
The tensor base class.
Definition: TensorForwardDeclarations.h:58
Tensor concatenation class.
Definition: TensorConcatenation.h:63
WriteAccessors
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31