12#ifndef EIGEN_TENSOR_TENSOR_REVERSE_H
13#define EIGEN_TENSOR_TENSOR_REVERSE_H
15#include "./InternalHeaderCheck.h"
20template <
typename ReverseDimensions,
typename XprType>
21struct traits<TensorReverseOp<ReverseDimensions, 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 ReverseDimensions,
typename XprType>
32struct eval<TensorReverseOp<ReverseDimensions, XprType>, Eigen::Dense> {
33 typedef const TensorReverseOp<ReverseDimensions, XprType>& type;
44template <
typename ReverseDimensions,
typename XprType>
45class TensorReverseOp :
public TensorBase<TensorReverseOp<ReverseDimensions, XprType>, WriteAccessors> {
48 typedef typename Eigen::internal::traits<TensorReverseOp>::Scalar Scalar;
50 typedef typename XprType::CoeffReturnType CoeffReturnType;
51 typedef typename Eigen::internal::ref_selector<TensorReverseOp>::type Nested;
52 typedef typename Eigen::internal::traits<TensorReverseOp>::StorageKind StorageKind;
53 typedef typename Eigen::internal::traits<TensorReverseOp>::Index Index;
55 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorReverseOp(
const XprType& expr,
const ReverseDimensions& reverse_dims)
56 : m_xpr(expr), m_reverse_dims(reverse_dims) {}
58 EIGEN_DEVICE_FUNC
const ReverseDimensions& reverse()
const {
return m_reverse_dims; }
60 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
62 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorReverseOp)
65 typename XprType::Nested m_xpr;
66 const ReverseDimensions m_reverse_dims;
70template <
typename ReverseDimensions,
typename ArgType,
typename Device>
73 typedef typename XprType::Index Index;
74 static constexpr int NumDims = internal::array_size<ReverseDimensions>::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 = TensorEvaluator<ArgType, Device>::PacketAccess,
87 BlockAccess = NumDims > 0,
88 PreferBlockAccess =
true,
93 typedef internal::TensorIntDivisor<Index> IndexDivisor;
96 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
97 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
99 typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock ArgTensorBlock;
101 typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index> TensorBlock;
104 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
105 : m_impl(op.expression(), device), m_reverse(op.reverse()), m_device(device) {
107 EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
110 m_dimensions = m_impl.dimensions();
111 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
113 for (
int i = 1; i < NumDims; ++i) {
114 m_strides[i] = m_strides[i - 1] * m_dimensions[i - 1];
115 if (m_strides[i] > 0) m_fastStrides[i] = IndexDivisor(m_strides[i]);
118 m_strides[NumDims - 1] = 1;
119 for (
int i = NumDims - 2; i >= 0; --i) {
120 m_strides[i] = m_strides[i + 1] * m_dimensions[i + 1];
121 if (m_strides[i] > 0) m_fastStrides[i] = IndexDivisor(m_strides[i]);
126 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
128 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType) {
129 m_impl.evalSubExprsIfNeeded(
nullptr);
133#ifdef EIGEN_USE_THREADS
134 template <
typename EvalSubExprsCallback>
135 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
136 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
140 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
142 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index reverseIndex(Index index)
const {
143 eigen_assert(index < dimensions().TotalSize());
144 Index inputIndex = 0;
145 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
147 for (
int i = NumDims - 1; i > 0; --i) {
148 Index idx = index / m_fastStrides[i];
149 index -= idx * m_strides[i];
151 idx = m_dimensions[i] - idx - 1;
153 inputIndex += idx * m_strides[i];
156 inputIndex += m_dimensions[0] - index - 1;
162 for (
int i = 0; i < NumDims - 1; ++i) {
163 Index idx = index / m_fastStrides[i];
164 index -= idx * m_strides[i];
166 idx = m_dimensions[i] - idx - 1;
168 inputIndex += idx * m_strides[i];
170 if (m_reverse[NumDims - 1]) {
171 inputIndex += m_dimensions[NumDims - 1] - index - 1;
179 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
180 return m_impl.coeff(reverseIndex(index));
183 template <
int LoadMode>
184 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
185 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
190 constexpr int inner_dim = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? 0 : NumDims - 1;
191 const Index inner_size = m_dimensions[inner_dim];
192 const Index inner_pos = index % inner_size;
193 if (inner_pos + PacketSize <= inner_size) {
194 if (m_reverse[inner_dim]) {
195 const Index input_index = reverseIndex(index + PacketSize - 1);
196 return internal::preverse(m_impl.template packet<Unaligned>(input_index));
198 return m_impl.template packet<Unaligned>(reverseIndex(index));
203 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
204 std::remove_const_t<CoeffReturnType> values[PacketSize];
206 for (
int i = 0; i < PacketSize; ++i) {
207 values[i] = coeff(index + i);
209 return internal::pload<PacketReturnType>(values);
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
213 const size_t target_size = m_device.lastLevelCacheSize();
216 return internal::TensorBlockResourceRequirements::skewed<Scalar>(target_size).addCostPerCoeff({0, 0, 24});
219 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
220 bool =
false)
const {
224 static const bool isColMajor =
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor);
226 static constexpr Index inner_dim_idx = isColMajor ? 0 : NumDims - 1;
227 const bool inner_dim_reversed = m_reverse[inner_dim_idx];
230 Index block_offset = 0;
233 Index input_offset = reverseIndex(desc.offset());
237 array<BlockIteratorState, NumDims> it;
238 for (
int i = 0; i < NumDims; ++i) {
239 const int dim = isColMajor ? i : NumDims - 1 - i;
240 it[i].size = desc.dimension(dim);
242 it[i].reverse = m_reverse[dim];
244 it[i].block_stride = i == 0 ? 1 : (it[i - 1].size * it[i - 1].block_stride);
245 it[i].block_span = it[i].block_stride * (it[i].size - 1);
247 it[i].input_stride = m_strides[dim];
248 it[i].input_span = it[i].input_stride * (it[i].size - 1);
251 it[i].input_stride = -1 * it[i].input_stride;
252 it[i].input_span = -1 * it[i].input_span;
258 int effective_inner_dim = 0;
259 for (
int i = 1; i < NumDims; ++i) {
260 if (it[i].reverse != it[effective_inner_dim].reverse)
break;
261 if (it[i].block_stride != it[effective_inner_dim].size)
break;
262 if (it[i].block_stride != numext::abs(it[i].input_stride))
break;
264 it[i].size = it[effective_inner_dim].size * it[i].size;
266 it[i].block_stride = 1;
267 it[i].input_stride = (inner_dim_reversed ? -1 : 1);
269 it[i].block_span = it[i].block_stride * (it[i].size - 1);
270 it[i].input_span = it[i].input_stride * (it[i].size - 1);
272 effective_inner_dim = i;
275 eigen_assert(it[effective_inner_dim].block_stride == 1);
276 eigen_assert(it[effective_inner_dim].input_stride == (inner_dim_reversed ? -1 : 1));
278 const Index inner_dim_size = it[effective_inner_dim].size;
281 const typename TensorBlock::Storage block_storage = TensorBlock::prepareStorage(desc, scratch);
282 CoeffReturnType* block_buffer = block_storage.data();
284 while (it[NumDims - 1].count < it[NumDims - 1].size) {
286 Index dst = block_offset;
287 Index src = input_offset;
291 if (inner_dim_reversed) {
292 for (Index i = 0; i < inner_dim_size; ++i) {
293 block_buffer[dst] = m_impl.coeff(src);
298 for (Index i = 0; i < inner_dim_size; ++i) {
299 block_buffer[dst] = m_impl.coeff(src);
306 if ((NumDims - effective_inner_dim) == 1)
break;
309 for (Index i = effective_inner_dim + 1; i < NumDims; ++i) {
310 if (++it[i].count < it[i].size) {
311 block_offset += it[i].block_stride;
312 input_offset += it[i].input_stride;
315 if (i != NumDims - 1) it[i].count = 0;
316 block_offset -= it[i].block_span;
317 input_offset -= it[i].input_span;
321 return block_storage.AsTensorMaterializedBlock();
324 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
325 double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() + 2 * TensorOpCost::MulCost<Index>() +
326 TensorOpCost::DivCost<Index>());
327 for (
int i = 0; i < NumDims; ++i) {
329 compute_cost += 2 * TensorOpCost::AddCost<Index>();
334 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
337 EIGEN_DEVICE_FUNC
typename Storage::Type data()
const {
return nullptr; }
340 Dimensions m_dimensions;
341 array<Index, NumDims> m_strides;
342 array<IndexDivisor, NumDims> m_fastStrides;
343 TensorEvaluator<ArgType, Device> m_impl;
344 ReverseDimensions m_reverse;
345 const Device EIGEN_DEVICE_REF m_device;
348 struct BlockIteratorState {
350 : size(0), count(0), reverse(false), block_stride(0), block_span(0), input_stride(0), input_span(0) {}
364template <
typename ReverseDimensions,
typename ArgType,
typename Device>
366 :
public TensorEvaluator<const TensorReverseOp<ReverseDimensions, ArgType>, Device> {
367 typedef TensorEvaluator<const TensorReverseOp<ReverseDimensions, ArgType>, Device> Base;
368 typedef TensorReverseOp<ReverseDimensions, ArgType> XprType;
369 typedef typename XprType::Index Index;
370 static constexpr int NumDims = internal::array_size<ReverseDimensions>::value;
371 typedef DSizes<Index, NumDims> Dimensions;
373 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
376 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
379 BlockAccess = TensorEvaluator<ArgType, Device>::RawAccess,
383 PreferBlockAccess =
false,
387 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device) : Base(op, device) {}
389 typedef typename XprType::Scalar Scalar;
390 typedef typename XprType::CoeffReturnType CoeffReturnType;
391 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
392 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
393 typedef std::remove_const_t<Scalar> ScalarNoConst;
396 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
399 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return this->m_dimensions; }
401 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
const {
402 return this->m_impl.coeffRef(this->reverseIndex(index));
405 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
418 return internal::TensorBlockResourceRequirements::merge(
419 this->m_impl.getResourceRequirements(),
420 internal::TensorBlockResourceRequirements::uniform<Scalar>(this->m_device.firstLevelCacheSize()));
423 template <
int StoreMode>
424 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index index,
const PacketReturnType& x)
const {
425 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
431 constexpr int inner_dim = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? 0 : NumDims - 1;
432 const Index inner_size = this->m_dimensions[inner_dim];
433 const Index inner_pos = index % inner_size;
434 if (inner_pos + PacketSize <= inner_size) {
435 if (this->m_reverse[inner_dim]) {
436 const Index input_index = this->reverseIndex(index + PacketSize - 1);
437 this->m_impl.template writePacket<Unaligned>(input_index, internal::preverse(x));
439 this->m_impl.template writePacket<Unaligned>(this->reverseIndex(index), x);
445 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment) CoeffReturnType values[PacketSize];
446 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
448 for (
int i = 0; i < PacketSize; ++i) {
449 this->coeffRef(index + i) = values[i];
453 template <
typename TensorBlock>
454 EIGEN_STRONG_INLINE
void writeBlock(
const TensorBlockDesc& desc,
const TensorBlock& block) {
455 eigen_assert(this->m_impl.data() !=
nullptr);
461 Index input_corner = this->reverseIndex(desc.offset());
462 for (
int i = 0; i < NumDims; ++i) {
463 if (this->m_reverse[i]) input_corner -= (desc.dimension(i) - 1) * this->m_strides[i];
470 typedef TensorReverseOp<const ReverseDimensions, const typename TensorBlock::XprType> RevBlockExpr;
471 const RevBlockExpr reversed_block(block.expr(), this->m_reverse);
473 typedef internal::TensorBlockAssignment<ScalarNoConst, NumDims, RevBlockExpr, Index> TensorBlockAssign;
474 TensorBlockAssign::Run(TensorBlockAssign::target(desc.dimensions(), DSizes<Index, NumDims>(this->m_strides),
475 this->m_impl.data(), input_corner),
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor reverse elements class.
Definition TensorReverse.h:45
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47