TensorConcatenation.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) 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,
33  typedef typename LhsXprType::Nested LhsNested;
34  typedef typename RhsXprType::Nested RhsNested;
37  static const int NumDimensions = traits<LhsXprType>::NumDimensions;
38  static const int Layout = traits<LhsXprType>::Layout;
39  enum { Flags = 0 };
40 };
41 
42 template<typename Axis, typename LhsXprType, typename RhsXprType>
43 struct eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, Eigen::Dense>
44 {
46 };
47 
48 template<typename Axis, typename LhsXprType, typename RhsXprType>
49 struct nested<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, 1, typename eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >::type>
50 {
52 };
53 
54 } // end namespace internal
55 
56 
57 template<typename Axis, typename LhsXprType, typename RhsXprType>
58 class TensorConcatenationOp : public TensorBase<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, WriteAccessors>
59 {
60  public:
65  typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
66  typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
68 
69  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(const LhsXprType& lhs, const RhsXprType& rhs, Axis axis)
70  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_axis(axis) {}
71 
72  EIGEN_DEVICE_FUNC
74  lhsExpression() const { return m_lhs_xpr; }
75 
76  EIGEN_DEVICE_FUNC
78  rhsExpression() const { return m_rhs_xpr; }
79 
80  EIGEN_DEVICE_FUNC const Axis& axis() const { return m_axis; }
81 
82  EIGEN_DEVICE_FUNC
84  {
86  Assign assign(*this, other);
88  return *this;
89  }
90 
91  template<typename OtherDerived>
92  EIGEN_DEVICE_FUNC
93  EIGEN_STRONG_INLINE TensorConcatenationOp& operator = (const OtherDerived& other)
94  {
96  Assign assign(*this, other);
98  return *this;
99  }
100 
101  protected:
102  typename LhsXprType::Nested m_lhs_xpr;
103  typename RhsXprType::Nested m_rhs_xpr;
104  const Axis m_axis;
105 };
106 
107 
108 // Eval as rvalue
109 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
110 struct TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
111 {
113  typedef typename XprType::Index Index;
117  typedef typename XprType::Scalar Scalar;
120  enum {
121  IsAligned = false,
124  RawAccess = false
125  };
126 
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
128  : m_leftImpl(op.lhsExpression(), device), m_rightImpl(op.rhsExpression(), device), m_axis(op.axis())
129  {
130  EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout) || NumDims == 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
131  EIGEN_STATIC_ASSERT((NumDims == RightNumDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
132  EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
133 
134  eigen_assert(0 <= m_axis && m_axis < NumDims);
135  const Dimensions& lhs_dims = m_leftImpl.dimensions();
136  const Dimensions& rhs_dims = m_rightImpl.dimensions();
137  {
138  int i = 0;
139  for (; i < m_axis; ++i) {
140  eigen_assert(lhs_dims[i] > 0);
141  eigen_assert(lhs_dims[i] == rhs_dims[i]);
142  m_dimensions[i] = lhs_dims[i];
143  }
144  eigen_assert(lhs_dims[i] > 0); // Now i == m_axis.
145  eigen_assert(rhs_dims[i] > 0);
146  m_dimensions[i] = lhs_dims[i] + rhs_dims[i];
147  for (++i; i < NumDims; ++i) {
148  eigen_assert(lhs_dims[i] > 0);
149  eigen_assert(lhs_dims[i] == rhs_dims[i]);
150  m_dimensions[i] = lhs_dims[i];
151  }
152  }
153 
154  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
155  m_leftStrides[0] = 1;
156  m_rightStrides[0] = 1;
157  m_outputStrides[0] = 1;
158 
159  for (int j = 1; j < NumDims; ++j) {
160  m_leftStrides[j] = m_leftStrides[j-1] * lhs_dims[j-1];
161  m_rightStrides[j] = m_rightStrides[j-1] * rhs_dims[j-1];
162  m_outputStrides[j] = m_outputStrides[j-1] * m_dimensions[j-1];
163  }
164  } else {
165  m_leftStrides[NumDims - 1] = 1;
166  m_rightStrides[NumDims - 1] = 1;
167  m_outputStrides[NumDims - 1] = 1;
168 
169  for (int j = NumDims - 2; j >= 0; --j) {
170  m_leftStrides[j] = m_leftStrides[j+1] * lhs_dims[j+1];
171  m_rightStrides[j] = m_rightStrides[j+1] * rhs_dims[j+1];
172  m_outputStrides[j] = m_outputStrides[j+1] * m_dimensions[j+1];
173  }
174  }
175  }
176 
177  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
178 
179  // TODO(phli): Add short-circuit memcpy evaluation if underlying data are linear?
180  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/)
181  {
182  m_leftImpl.evalSubExprsIfNeeded(NULL);
183  m_rightImpl.evalSubExprsIfNeeded(NULL);
184  return true;
185  }
186 
187  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
188  {
189  m_leftImpl.cleanup();
190  m_rightImpl.cleanup();
191  }
192 
193  // TODO(phli): attempt to speed this up. The integer divisions and modulo are slow.
194  // See CL/76180724 comments for more ideas.
195  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
196  {
197  // Collect dimension-wise indices (subs).
199  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
200  for (int i = NumDims - 1; i > 0; --i) {
201  subs[i] = index / m_outputStrides[i];
202  index -= subs[i] * m_outputStrides[i];
203  }
204  subs[0] = index;
205  } else {
206  for (int i = 0; i < NumDims - 1; ++i) {
207  subs[i] = index / m_outputStrides[i];
208  index -= subs[i] * m_outputStrides[i];
209  }
210  subs[NumDims - 1] = index;
211  }
212 
213  const Dimensions& left_dims = m_leftImpl.dimensions();
214  if (subs[m_axis] < left_dims[m_axis]) {
215  Index left_index;
216  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
217  left_index = subs[0];
218  for (int i = 1; i < NumDims; ++i) {
219  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
220  }
221  } else {
222  left_index = subs[NumDims - 1];
223  for (int i = NumDims - 2; i >= 0; --i) {
224  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
225  }
226  }
227  return m_leftImpl.coeff(left_index);
228  } else {
229  subs[m_axis] -= left_dims[m_axis];
230  const Dimensions& right_dims = m_rightImpl.dimensions();
231  Index right_index;
232  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
233  right_index = subs[0];
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  for (int i = NumDims - 2; i >= 0; --i) {
240  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
241  }
242  }
243  return m_rightImpl.coeff(right_index);
244  }
245  }
246 
247  // TODO(phli): Add a real vectorization.
248  template<int LoadMode>
249  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
250  {
252  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
253  eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
254 
255  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
256  for (int i = 0; i < packetSize; ++i) {
257  values[i] = coeff(index+i);
258  }
259  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
260  return rslt;
261  }
262 
263  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
264  costPerCoeff(bool vectorized) const {
265  const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
266  2 * TensorOpCost::MulCost<Index>() +
267  TensorOpCost::DivCost<Index>() +
268  TensorOpCost::ModCost<Index>());
269  const double lhs_size = m_leftImpl.dimensions().TotalSize();
270  const double rhs_size = m_rightImpl.dimensions().TotalSize();
271  return (lhs_size / (lhs_size + rhs_size)) *
272  m_leftImpl.costPerCoeff(vectorized) +
273  (rhs_size / (lhs_size + rhs_size)) *
274  m_rightImpl.costPerCoeff(vectorized) +
275  TensorOpCost(0, 0, compute_cost);
276  }
277 
278  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
279 
280  protected:
281  Dimensions m_dimensions;
287  const Axis m_axis;
288 };
289 
290 // Eval as lvalue
291 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
292  struct TensorEvaluator<TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
293  : public TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
294 {
297  typedef typename Base::Dimensions Dimensions;
298  enum {
299  IsAligned = false,
302  RawAccess = false
303  };
304 
305  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(XprType& op, const Device& device)
306  : Base(op, device)
307  {
308  EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
309  }
310 
311  typedef typename XprType::Index Index;
312  typedef typename XprType::Scalar Scalar;
315 
316  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
317  {
318  // Collect dimension-wise indices (subs).
320  for (int i = Base::NumDims - 1; i > 0; --i) {
321  subs[i] = index / this->m_outputStrides[i];
322  index -= subs[i] * this->m_outputStrides[i];
323  }
324  subs[0] = index;
325 
326  const Dimensions& left_dims = this->m_leftImpl.dimensions();
327  if (subs[this->m_axis] < left_dims[this->m_axis]) {
328  Index left_index = subs[0];
329  for (int i = 1; i < Base::NumDims; ++i) {
330  left_index += (subs[i] % left_dims[i]) * this->m_leftStrides[i];
331  }
332  return this->m_leftImpl.coeffRef(left_index);
333  } else {
334  subs[this->m_axis] -= left_dims[this->m_axis];
335  const Dimensions& right_dims = this->m_rightImpl.dimensions();
336  Index right_index = subs[0];
337  for (int i = 1; i < Base::NumDims; ++i) {
338  right_index += (subs[i] % right_dims[i]) * this->m_rightStrides[i];
339  }
340  return this->m_rightImpl.coeffRef(right_index);
341  }
342  }
343 
344  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
345  void writePacket(Index index, const PacketReturnType& x)
346  {
348  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
349  eigen_assert(index + packetSize - 1 < this->dimensions().TotalSize());
350 
351  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
352  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
353  for (int i = 0; i < packetSize; ++i) {
354  coeffRef(index+i) = values[i];
355  }
356  }
357 };
358 
359 } // end namespace Eigen
360 
361 #endif // EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
internal::traits< TensorConcatenationOp >::Index Index
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
TensorEvaluator< const TensorConcatenationOp< Axis, LeftArgType, RightArgType >, Device > Base
internal::traits< TensorConcatenationOp >::Scalar Scalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType & coeffRef(Index index)
leaf::MyValues values
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(XprType &op, const Device &device)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:124
vector< size_t > dimensions(L.begin(), L.end())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType &x)
EIGEN_DEVICE_FUNC const Axis & axis() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(const LhsXprType &lhs, const RhsXprType &rhs, Axis axis)
promote_storage_type< typename LhsXprType::Scalar, typename RhsXprType::Scalar >::ret Scalar
internal::nested< TensorConcatenationOp >::type Nested
#define eigen_assert(x)
Definition: Macros.h:579
NumTraits< Scalar >::Real RealScalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
static EIGEN_DEVICE_FUNC void run(const Expression &expr, const Device &device=Device())
#define NULL
Definition: ccolamd.c:609
promote_storage_type< typename traits< LhsXprType >::StorageKind, typename traits< RhsXprType >::StorageKind >::ret StorageKind
internal::traits< TensorConcatenationOp >::StorageKind StorageKind
internal::promote_storage_type< typename LhsXprType::CoeffReturnType, typename RhsXprType::CoeffReturnType >::ret CoeffReturnType
EIGEN_DEVICE_FUNC const internal::remove_all< typename RhsXprType::Nested >::type & rhsExpression() const
The tensor base class.
Definition: TensorBase.h:829
DenseIndex ret
Definition: level1_impl.h:59
#define EIGEN_ALIGN_MAX
Definition: Macros.h:757
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
EIGEN_DEVICE_FUNC const internal::remove_all< typename LhsXprType::Nested >::type & lhsExpression() const
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Tensor concatenation class.
std::ptrdiff_t j
Definition: pytypes.h:897


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:45:16