11#ifndef EIGEN_TENSOR_TENSOR_SHUFFLING_H
12#define EIGEN_TENSOR_TENSOR_SHUFFLING_H
15#include "./InternalHeaderCheck.h"
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;
31template <
typename Shuffle,
typename XprType>
32struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense> {
33 typedef const TensorShufflingOp<Shuffle, XprType>& type;
43template <
typename Shuffle,
typename XprType>
44class TensorShufflingOp :
public TensorBase<TensorShufflingOp<Shuffle, XprType> > {
47 typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
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;
54 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(
const XprType& expr,
const Shuffle& shfl)
55 : m_xpr(expr), m_shuffle(shfl) {}
57 EIGEN_DEVICE_FUNC
const Shuffle& shufflePermutation()
const {
return m_shuffle; }
59 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
61 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorShufflingOp)
64 typename XprType::Nested m_xpr;
65 const Shuffle m_shuffle;
69template <
typename Shuffle,
typename ArgType,
typename Device>
73 typedef typename XprType::Index Index;
74 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
76 typedef typename XprType::Scalar Scalar;
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;
83 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
86 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
88 PreferBlockAccess =
true,
93 typedef std::remove_const_t<Scalar> ScalarNoConst;
96 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
97 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
99 typedef typename internal::TensorMaterializedBlock<ScalarNoConst, NumDims, Layout, Index> TensorBlock;
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;
116 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
117 m_unshuffledInputStrides[0] = 1;
118 m_outputStrides[0] = 1;
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));
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));
137 for (
int i = 0; i < NumDims; ++i) {
138 m_inputStrides[i] = m_unshuffledInputStrides[shuffle[i]];
142 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
144 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
145 m_impl.evalSubExprsIfNeeded(
nullptr);
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); });
156 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
158 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
160 return m_impl.coeff(index);
162 return m_impl.coeff(srcCoeff(index));
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];
177 for (
int i = 0; i < PacketSize; ++i) {
178 values[i] = self.m_impl.coeff(base + i * inner_stride);
180 return internal::pload<PacketReturnType>(values);
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) {
190 return self.m_impl.template packet<Unaligned>(base);
192 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
193 std::remove_const_t<CoeffReturnType> values[PacketSize];
195 for (
int i = 0; i < PacketSize; ++i) {
196 values[i] = self.m_impl.coeff(base + i * inner_stride);
198 return internal::pload<PacketReturnType>(values);
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;
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]);
212 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
213 std::remove_const_t<CoeffReturnType> values[PacketSize];
215 for (
int i = 0; i < PacketSize; ++i) {
216 values[i] = self.coeff(index + i);
218 return internal::pload<PacketReturnType>(values);
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];
228 for (
int i = 0; i < PacketSize; ++i) {
229 values[i] = self.m_impl.coeff(index + i);
231 return internal::pload<PacketReturnType>(values);
233 return LoadPacketViaInnerRun<LoadMode, Self, ImplPacketAccess>(self, index);
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);
243 return LoadPacketViaInnerRun<LoadMode, Self, true>(self, index);
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);
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;
256 const size_t target_size = m_device.firstLevelCacheSize();
257 const bool inner_dim_shuffled = m_shuffle[inner_dim] != inner_dim;
264 using BlockRequirements = internal::TensorBlockResourceRequirements;
265 if (inner_dim_shuffled) {
266 return BlockRequirements::uniform<Scalar>(target_size).addCostPerCoeff({0, 0, NumDims * 28});
268 return BlockRequirements::skewed<Scalar>(target_size);
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);
276 typedef internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout> TensorBlockIO;
277 typedef typename TensorBlockIO::Dst TensorBlockIODst;
278 typedef typename TensorBlockIO::Src TensorBlockIOSrc;
280 const typename TensorBlock::Storage block_storage =
281 TensorBlock::prepareStorage(desc, scratch, root_of_expr_ast);
283 typename TensorBlockIO::Dimensions input_strides(m_unshuffledInputStrides);
284 TensorBlockIOSrc src(input_strides, m_impl.data(), srcCoeff(desc.offset()));
286 TensorBlockIODst dst(block_storage.dimensions(), block_storage.strides(), block_storage.data());
288 typename TensorBlockIO::DimensionsMap dst_to_src_dim_map(m_shuffle);
289 TensorBlockIO::Copy(dst, src, dst_to_src_dim_map);
291 return block_storage.AsTensorMaterializedBlock();
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 , PacketSize);
303 EIGEN_DEVICE_FUNC
typename Storage::Type data()
const {
return nullptr; }
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];
317 return output_index + input_index * output_block_strides[m_inverseShuffle[0]];
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];
324 return output_index + input_index * output_block_strides[m_inverseShuffle[NumDims - 1]];
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];
341 return inputIndex + index * m_inputStrides[0];
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];
349 return inputIndex + index * m_inputStrides[NumDims - 1];
353 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index)
const {
355 return srcCoeffInner(index, inner_pos);
358 Dimensions m_dimensions;
360 array<int, NumDims> m_shuffle;
361 array<Index, NumDims> m_inverseShuffle;
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;
367 const Device EIGEN_DEVICE_REF m_device;
368 TensorEvaluator<ArgType, Device> m_impl;
372template <
typename Shuffle,
typename ArgType,
typename Device>
374 :
public TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> {
375 typedef TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> Base;
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;
389 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
390 BlockAccess = TensorEvaluator<ArgType, Device>::RawAccess,
391 PreferBlockAccess =
true,
395 typedef std::remove_const_t<Scalar> ScalarNoConst;
398 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
401 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device) : Base(op, device) {}
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
const {
404 return this->m_impl.coeffRef(this->srcCoeff(index));
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; }
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);
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);
431 constexpr int inner_dim = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? 0 : NumDims - 1;
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)) {
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);
443 for (
int i = 0; i < PacketSize; ++i) {
444 this->m_impl.coeffRef(base + i * inner_stride) = values[i];
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);
454 for (
int i = 0; i < PacketSize; ++i) {
455 this->coeffRef(index + i) = values[i];
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);
463 typedef internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout> TensorBlockIO;
464 typedef typename TensorBlockIO::Dst TensorBlockIODst;
465 typedef typename TensorBlockIO::Src TensorBlockIOSrc;
467 const Scalar* block_buffer = block.data();
472 if (block_buffer ==
nullptr) {
473 mem = this->m_device.allocate(desc.size() *
sizeof(Scalar));
474 ScalarNoConst* buf =
static_cast<ScalarNoConst*
>(mem);
476 typedef internal::TensorBlockAssignment<ScalarNoConst, NumDims, typename TensorBlock::XprType, Index>
477 TensorBlockAssignment;
479 TensorBlockAssignment::Run(
480 TensorBlockAssignment::target(desc.dimensions(), internal::strides<Layout>(desc.dimensions()), buf),
487 TensorBlockIOSrc src(internal::strides<Layout>(desc.dimensions()), block_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);
495 TensorBlockIODst dst(output_dimensions, output_strides, this->m_impl.data(), this->srcCoeff(desc.offset()));
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]);
502 TensorBlockIO::Copy(dst, src, dst_to_src_dim_map);
505 if (mem !=
nullptr) this->m_device.deallocate(mem);
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