Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorShuffling.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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_SHUFFLING_H
12#define EIGEN_TENSOR_TENSOR_SHUFFLING_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename Shuffle, typename XprType>
21struct traits<TensorShufflingOp<Shuffle, XprType> > : public traits<XprType> {
22 typedef typename XprType::Scalar Scalar;
23 typedef traits<XprType> XprTraits;
24 typedef typename XprTraits::StorageKind StorageKind;
25 typedef typename XprTraits::Index Index;
26 static constexpr int NumDimensions = XprTraits::NumDimensions;
27 static constexpr int Layout = XprTraits::Layout;
28 typedef typename XprTraits::PointerType PointerType;
29};
30
31template <typename Shuffle, typename XprType>
32struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense> {
33 typedef const TensorShufflingOp<Shuffle, XprType>& type;
34};
35
36} // end namespace internal
37
43template <typename Shuffle, typename XprType>
44class TensorShufflingOp : public TensorBase<TensorShufflingOp<Shuffle, XprType> > {
45 public:
47 typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
48 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
49 typedef typename XprType::CoeffReturnType CoeffReturnType;
50 typedef typename Eigen::internal::ref_selector<TensorShufflingOp>::type Nested;
51 typedef typename Eigen::internal::traits<TensorShufflingOp>::StorageKind StorageKind;
52 typedef typename Eigen::internal::traits<TensorShufflingOp>::Index Index;
53
54 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(const XprType& expr, const Shuffle& shfl)
55 : m_xpr(expr), m_shuffle(shfl) {}
56
57 EIGEN_DEVICE_FUNC const Shuffle& shufflePermutation() const { return m_shuffle; }
58
59 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
60
61 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorShufflingOp)
62
63 protected:
64 typename XprType::Nested m_xpr;
65 const Shuffle m_shuffle;
66};
67
68// Eval as rvalue
69template <typename Shuffle, typename ArgType, typename Device>
70struct TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> {
73 typedef typename XprType::Index Index;
74 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
75 typedef DSizes<Index, NumDims> Dimensions;
76 typedef typename XprType::Scalar Scalar;
77 typedef typename XprType::CoeffReturnType CoeffReturnType;
78 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
79 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
80 typedef StorageMemory<CoeffReturnType, Device> Storage;
81 typedef typename Storage::Type EvaluatorPointerType;
82
83 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
84 enum {
85 IsAligned = false,
86 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
88 PreferBlockAccess = true,
89 CoordAccess = false, // to be implemented
90 RawAccess = false
91 };
92
93 typedef std::remove_const_t<Scalar> ScalarNoConst;
94
95 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
96 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
97 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
98
99 typedef typename internal::TensorMaterializedBlock<ScalarNoConst, NumDims, Layout, Index> TensorBlock;
100 //===--------------------------------------------------------------------===//
101
102 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
103 : m_device(device), m_impl(op.expression(), device) {
104 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
105 const Shuffle& shuffle = op.shufflePermutation();
106 m_is_identity = true;
107 for (int i = 0; i < NumDims; ++i) {
108 m_shuffle[i] = static_cast<int>(shuffle[i]);
109 m_dimensions[i] = input_dims[shuffle[i]];
110 m_inverseShuffle[shuffle[i]] = i;
111 if (m_is_identity && shuffle[i] != i) {
112 m_is_identity = false;
113 }
114 }
115
116 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
117 m_unshuffledInputStrides[0] = 1;
118 m_outputStrides[0] = 1;
119
120 for (int i = 1; i < NumDims; ++i) {
121 m_unshuffledInputStrides[i] = m_unshuffledInputStrides[i - 1] * input_dims[i - 1];
122 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
123 m_fastOutputStrides[i] =
124 internal::TensorIntDivisor<Index>(m_outputStrides[i] > 0 ? m_outputStrides[i] : Index(1));
125 }
126 } else {
127 m_unshuffledInputStrides[NumDims - 1] = 1;
128 m_outputStrides[NumDims - 1] = 1;
129 for (int i = NumDims - 2; i >= 0; --i) {
130 m_unshuffledInputStrides[i] = m_unshuffledInputStrides[i + 1] * input_dims[i + 1];
131 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
132 m_fastOutputStrides[i] =
133 internal::TensorIntDivisor<Index>(m_outputStrides[i] > 0 ? m_outputStrides[i] : Index(1));
134 }
135 }
136
137 for (int i = 0; i < NumDims; ++i) {
138 m_inputStrides[i] = m_unshuffledInputStrides[shuffle[i]];
139 }
140 }
141
142 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
143
144 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
145 m_impl.evalSubExprsIfNeeded(nullptr);
146 return true;
147 }
148
149#ifdef EIGEN_USE_THREADS
150 template <typename EvalSubExprsCallback>
151 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
152 m_impl.evalSubExprsIfNeededAsync(nullptr, [done](bool) { done(true); });
153 }
154#endif // EIGEN_USE_THREADS
155
156 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
157
158 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
159 if (m_is_identity) {
160 return m_impl.coeff(index);
161 } else {
162 return m_impl.coeff(srcCoeff(index));
163 }
164 }
165
166 // Assembles a packet whose elements all lie in one inner-most run of the
167 // output: the input indices form an arithmetic progression starting at
168 // `base` with step `inner_stride`, so the index mapping is computed once
169 // per packet instead of once per coefficient.
170 template <int LoadMode, typename Self, bool ImplPacketAccess>
171 struct InnerRunLoader {
172 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType Run(const Self& self, Index base,
173 Index inner_stride) {
174 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
175 std::remove_const_t<CoeffReturnType> values[PacketSize];
176 EIGEN_UNROLL_LOOP
177 for (int i = 0; i < PacketSize; ++i) {
178 values[i] = self.m_impl.coeff(base + i * inner_stride);
179 }
180 return internal::pload<PacketReturnType>(values);
181 }
182 };
183
184 template <int LoadMode, typename Self>
185 struct InnerRunLoader<LoadMode, Self, true> {
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType Run(const Self& self, Index base,
187 Index inner_stride) {
188 if (inner_stride == 1) {
189 // Inner dimension not shuffled: one contiguous load.
190 return self.m_impl.template packet<Unaligned>(base);
191 }
192 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
193 std::remove_const_t<CoeffReturnType> values[PacketSize];
194 EIGEN_UNROLL_LOOP
195 for (int i = 0; i < PacketSize; ++i) {
196 values[i] = self.m_impl.coeff(base + i * inner_stride);
197 }
198 return internal::pload<PacketReturnType>(values);
199 }
200 };
201
202 template <int LoadMode, typename Self, bool ImplPacketAccess>
203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType LoadPacketViaInnerRun(const Self& self, Index index) {
204 constexpr int inner_dim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : NumDims - 1;
205 Index inner_pos;
206 const Index base = self.srcCoeffInner(index, inner_pos);
207 if (inner_pos + PacketSize <= self.m_dimensions[inner_dim]) {
208 return InnerRunLoader<LoadMode, Self, ImplPacketAccess>::Run(self, base, self.m_inputStrides[inner_dim]);
209 }
210
211 // The packet crosses an inner-run boundary: assemble it scalar by scalar.
212 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
213 std::remove_const_t<CoeffReturnType> values[PacketSize];
214 EIGEN_UNROLL_LOOP
215 for (int i = 0; i < PacketSize; ++i) {
216 values[i] = self.coeff(index + i);
217 }
218 return internal::pload<PacketReturnType>(values);
219 }
220
221 template <int LoadMode, typename Self, bool ImplPacketAccess>
222 struct PacketLoader {
223 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType Run(const Self& self, Index index) {
224 if (self.m_is_identity) {
225 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
226 std::remove_const_t<CoeffReturnType> values[PacketSize];
227 EIGEN_UNROLL_LOOP
228 for (int i = 0; i < PacketSize; ++i) {
229 values[i] = self.m_impl.coeff(index + i);
230 }
231 return internal::pload<PacketReturnType>(values);
232 }
233 return LoadPacketViaInnerRun<LoadMode, Self, ImplPacketAccess>(self, index);
234 }
235 };
236
237 template <int LoadMode, typename Self>
238 struct PacketLoader<LoadMode, Self, true> {
239 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType Run(const Self& self, Index index) {
240 if (self.m_is_identity) {
241 return self.m_impl.template packet<LoadMode>(index);
242 }
243 return LoadPacketViaInnerRun<LoadMode, Self, true>(self, index);
244 }
245 };
246
247 template <int LoadMode>
248 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
249 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
250 return PacketLoader<LoadMode, Self, TensorEvaluator<ArgType, Device>::PacketAccess>::Run(*this, index);
251 }
252
253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
254 static constexpr int inner_dim = Layout == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
255
256 const size_t target_size = m_device.firstLevelCacheSize();
257 const bool inner_dim_shuffled = m_shuffle[inner_dim] != inner_dim;
258
259 // Shuffled inner dimensions lead to a random memory access, which is not
260 // captured by default cost model bytes loaded/stored. We add this cost
261 // explicitly. The number of cycles was picked based on the benchmarks.
262 // TODO(ezhulenev): This number was picked based on very questionable
263 // benchmarks, add benchmarks that are representative of real workloads.
264 using BlockRequirements = internal::TensorBlockResourceRequirements;
265 if (inner_dim_shuffled) {
266 return BlockRequirements::uniform<Scalar>(target_size).addCostPerCoeff({0, 0, NumDims * 28});
267 } else {
268 return BlockRequirements::skewed<Scalar>(target_size);
269 }
270 }
271
272 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
273 bool root_of_expr_ast = false) const {
274 eigen_assert(m_impl.data() != nullptr);
275
276 typedef internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout> TensorBlockIO;
277 typedef typename TensorBlockIO::Dst TensorBlockIODst;
278 typedef typename TensorBlockIO::Src TensorBlockIOSrc;
279
280 const typename TensorBlock::Storage block_storage =
281 TensorBlock::prepareStorage(desc, scratch, /*allow_strided_storage=*/root_of_expr_ast);
282
283 typename TensorBlockIO::Dimensions input_strides(m_unshuffledInputStrides);
284 TensorBlockIOSrc src(input_strides, m_impl.data(), srcCoeff(desc.offset()));
285
286 TensorBlockIODst dst(block_storage.dimensions(), block_storage.strides(), block_storage.data());
287
288 typename TensorBlockIO::DimensionsMap dst_to_src_dim_map(m_shuffle);
289 TensorBlockIO::Copy(dst, src, dst_to_src_dim_map);
290
291 return block_storage.AsTensorMaterializedBlock();
292 }
293
294 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
295 const double compute_cost = m_is_identity
296 ? TensorOpCost::AddCost<Index>()
297 : NumDims * (2 * TensorOpCost::AddCost<Index>() +
298 2 * TensorOpCost::MulCost<Index>() + TensorOpCost::DivCost<Index>());
299 return m_impl.costPerCoeff(vectorized) +
300 TensorOpCost(0, 0, compute_cost, m_is_identity /* vectorized */, PacketSize);
301 }
302
303 EIGEN_DEVICE_FUNC typename Storage::Type data() const { return nullptr; }
304
305 protected:
306 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index
307 GetBlockOutputIndex(Index input_index, const DSizes<Index, NumDims>& input_block_strides,
308 const DSizes<Index, NumDims>& output_block_strides,
309 const DSizes<internal::TensorIntDivisor<Index>, NumDims>& fast_input_block_strides) const {
310 Index output_index = 0;
311 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
312 for (int i = NumDims - 1; i > 0; --i) {
313 const Index idx = input_index / fast_input_block_strides[i];
314 output_index += idx * output_block_strides[m_inverseShuffle[i]];
315 input_index -= idx * input_block_strides[i];
316 }
317 return output_index + input_index * output_block_strides[m_inverseShuffle[0]];
318 } else {
319 for (int i = 0; i < NumDims - 1; ++i) {
320 const Index idx = input_index / fast_input_block_strides[i];
321 output_index += idx * output_block_strides[m_inverseShuffle[i]];
322 input_index -= idx * input_block_strides[i];
323 }
324 return output_index + input_index * output_block_strides[m_inverseShuffle[NumDims - 1]];
325 }
326 }
327
328 // Computes the input index of output index `index` and, as a by-product of
329 // the same fast-divisor walk, the output's inner-dimension coordinate. The
330 // packet paths use the latter to test whether a whole packet stays inside
331 // one inner-most run without spending an extra division on it.
332 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeffInner(Index index, Index& inner_pos) const {
333 Index inputIndex = 0;
334 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
335 for (int i = NumDims - 1; i > 0; --i) {
336 const Index idx = index / m_fastOutputStrides[i];
337 inputIndex += idx * m_inputStrides[i];
338 index -= idx * m_outputStrides[i];
339 }
340 inner_pos = index;
341 return inputIndex + index * m_inputStrides[0];
342 } else {
343 for (int i = 0; i < NumDims - 1; ++i) {
344 const Index idx = index / m_fastOutputStrides[i];
345 inputIndex += idx * m_inputStrides[i];
346 index -= idx * m_outputStrides[i];
347 }
348 inner_pos = index;
349 return inputIndex + index * m_inputStrides[NumDims - 1];
350 }
351 }
352
353 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const {
354 Index inner_pos;
355 return srcCoeffInner(index, inner_pos);
356 }
357
358 Dimensions m_dimensions;
359 bool m_is_identity;
360 array<int, NumDims> m_shuffle;
361 array<Index, NumDims> m_inverseShuffle; // TODO(ezhulenev): Make it int type.
362 array<Index, NumDims> m_outputStrides;
363 array<internal::TensorIntDivisor<Index>, NumDims> m_fastOutputStrides;
364 array<Index, NumDims> m_inputStrides;
365 array<Index, NumDims> m_unshuffledInputStrides;
366
367 const Device EIGEN_DEVICE_REF m_device;
368 TensorEvaluator<ArgType, Device> m_impl;
369};
370
371// Eval as lvalue
372template <typename Shuffle, typename ArgType, typename Device>
373struct TensorEvaluator<TensorShufflingOp<Shuffle, ArgType>, Device>
374 : public TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> {
375 typedef TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> Base;
376
377 typedef TensorShufflingOp<Shuffle, ArgType> XprType;
378 typedef typename XprType::Index Index;
379 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
380 typedef DSizes<Index, NumDims> Dimensions;
381 typedef typename XprType::Scalar Scalar;
382 typedef typename XprType::CoeffReturnType CoeffReturnType;
383 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
384 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
385 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
386
387 enum {
388 IsAligned = false,
389 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
390 BlockAccess = TensorEvaluator<ArgType, Device>::RawAccess,
391 PreferBlockAccess = true,
392 RawAccess = false
393 };
394
395 typedef std::remove_const_t<Scalar> ScalarNoConst;
396
397 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
398 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
399 //===--------------------------------------------------------------------===//
400
401 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : Base(op, device) {}
402
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index) const {
404 return this->m_impl.coeffRef(this->srcCoeff(index));
405 }
406
407 // Contiguous store into an unshuffled inner run; only instantiated when the
408 // nested evaluator has packet access.
409 template <int StoreMode, typename Self, bool ImplPacketAccess>
410 struct InnerRunWriter {
411 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static bool Run(const Self&, Index, const PacketReturnType&) { return false; }
412 };
413
414 template <int StoreMode, typename Self>
415 struct InnerRunWriter<StoreMode, Self, true> {
416 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static bool Run(const Self& self, Index base, const PacketReturnType& x) {
417 self.m_impl.template writePacket<Unaligned>(base, x);
418 return true;
419 }
420 };
421
422 template <int StoreMode>
423 EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType& x) const {
424 typedef TensorEvaluator<TensorShufflingOp<Shuffle, ArgType>, Device> Self;
425 constexpr bool ImplPacketAccess = bool(TensorEvaluator<ArgType, Device>::PacketAccess);
426
427 // Mirrors the rvalue PacketLoader: within one inner-most run the target
428 // input indices form an arithmetic progression, so the index mapping is
429 // computed once per packet; an unshuffled inner dimension becomes a
430 // single contiguous store.
431 constexpr int inner_dim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : NumDims - 1;
432 Index inner_pos;
433 const Index base = this->srcCoeffInner(index, inner_pos);
434 if (inner_pos + PacketSize <= this->m_dimensions[inner_dim]) {
435 const Index inner_stride = this->m_inputStrides[inner_dim];
436 if (inner_stride == 1 && InnerRunWriter<StoreMode, Self, ImplPacketAccess>::Run(*this, base, x)) {
437 return;
438 }
439 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
440 std::remove_const_t<CoeffReturnType> values[PacketSize];
441 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
442 EIGEN_UNROLL_LOOP
443 for (int i = 0; i < PacketSize; ++i) {
444 this->m_impl.coeffRef(base + i * inner_stride) = values[i];
445 }
446 return;
447 }
448
449 // The packet crosses an inner-run boundary: scatter scalar by scalar.
450 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
451 std::remove_const_t<CoeffReturnType> values[PacketSize];
452 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
453 EIGEN_UNROLL_LOOP
454 for (int i = 0; i < PacketSize; ++i) {
455 this->coeffRef(index + i) = values[i];
456 }
457 }
458
459 template <typename TensorBlock>
460 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writeBlock(const TensorBlockDesc& desc, const TensorBlock& block) {
461 eigen_assert(this->m_impl.data() != nullptr);
462
463 typedef internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout> TensorBlockIO;
464 typedef typename TensorBlockIO::Dst TensorBlockIODst;
465 typedef typename TensorBlockIO::Src TensorBlockIOSrc;
466
467 const Scalar* block_buffer = block.data();
468
469 // TODO(ezhulenev): TensorBlockIO should be able to read from any Eigen
470 // expression with coefficient and packet access as `src`.
471 void* mem = nullptr;
472 if (block_buffer == nullptr) {
473 mem = this->m_device.allocate(desc.size() * sizeof(Scalar));
474 ScalarNoConst* buf = static_cast<ScalarNoConst*>(mem);
475
476 typedef internal::TensorBlockAssignment<ScalarNoConst, NumDims, typename TensorBlock::XprType, Index>
477 TensorBlockAssignment;
478
479 TensorBlockAssignment::Run(
480 TensorBlockAssignment::target(desc.dimensions(), internal::strides<Layout>(desc.dimensions()), buf),
481 block.expr());
482
483 block_buffer = buf;
484 }
485
486 // Read from block.
487 TensorBlockIOSrc src(internal::strides<Layout>(desc.dimensions()), block_buffer);
488
489 // Write to the output buffer.
490 typename TensorBlockIO::Dimensions output_strides(this->m_unshuffledInputStrides);
491 typename TensorBlockIO::Dimensions output_dimensions;
492 for (int i = 0; i < NumDims; ++i) {
493 output_dimensions[this->m_shuffle[i]] = desc.dimension(i);
494 }
495 TensorBlockIODst dst(output_dimensions, output_strides, this->m_impl.data(), this->srcCoeff(desc.offset()));
496
497 // Reorder dimensions according to the shuffle.
498 typename TensorBlockIO::DimensionsMap dst_to_src_dim_map;
499 for (int i = 0; i < NumDims; ++i) {
500 dst_to_src_dim_map[i] = static_cast<int>(this->m_inverseShuffle[i]);
501 }
502 TensorBlockIO::Copy(dst, src, dst_to_src_dim_map);
503
504 // Deallocate temporary buffer used for the block materialization.
505 if (mem != nullptr) this->m_device.deallocate(mem);
506 }
507};
508
509} // end namespace Eigen
510
511#endif // EIGEN_TENSOR_TENSOR_SHUFFLING_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor shuffling class.
Definition TensorShuffling.h:44
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47