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