Swap the layout from col-major to row-major, or row-major to col-major, and invert the order of the dimensions.
Swap the layout from col-major to row-major, or row-major to col-major, and invert the order of the dimensions.Beware: the dimensions are reversed by this operation. If you want to preserve the ordering of the dimensions, you need to combine this operation with a shuffle.
Tensor<float, 2, ColMajor> input(2, 4); Tensor<float, 2, RowMajor> output = input.swap_layout(); eigen_assert(output.dimension(0) == 4); eigen_assert(output.dimension(1) == 2);
array<int, 2> shuffle(1, 0); output = input.swap_layout().shuffle(shuffle); eigen_assert(output.dimension(0) == 2); eigen_assert(output.dimension(1) == 4);
#ifndef EIGEN_TENSOR_TENSOR_LAYOUT_SWAP_H
#define EIGEN_TENSOR_TENSOR_LAYOUT_SWAP_H
#include "./InternalHeaderCheck.h"
namespace internal {
template <typename XprType>
struct traits<TensorLayoutSwapOp<XprType> > : public traits<XprType> {
typedef typename XprType::Scalar Scalar;
typedef traits<XprType> XprTraits;
typedef typename XprTraits::StorageKind StorageKind;
typedef typename XprTraits::Index Index;
static constexpr int NumDimensions = traits<XprType>::NumDimensions;
static constexpr int Layout = (traits<XprType>::Layout == ColMajor) ? RowMajor : ColMajor;
typedef typename XprTraits::PointerType PointerType;
};
template <typename XprType>
struct eval<TensorLayoutSwapOp<XprType>,
Eigen::Dense> {
typedef const TensorLayoutSwapOp<XprType>& type;
};
}
template <typename XprType>
class TensorLayoutSwapOp : public TensorBase<TensorLayoutSwapOp<XprType>, WriteAccessors> {
public:
typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Scalar Scalar;
typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
typedef typename Eigen::internal::ref_selector<TensorLayoutSwapOp>::type Nested;
typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::StorageKind StorageKind;
typedef typename Eigen::internal::traits<TensorLayoutSwapOp>::Index Index;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorLayoutSwapOp(const XprType& expr) : m_xpr(expr) {}
EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorLayoutSwapOp)
protected:
typename XprType::Nested m_xpr;
};
template <typename ArgType, typename Device>
struct TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> {
typedef TensorLayoutSwapOp<ArgType> XprType;
typedef typename XprType::Index Index;
static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
typedef DSizes<Index, NumDims> Dimensions;
static constexpr int Layout =
enum {
IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
BlockAccess =
(TensorEvaluator<ArgType, Device>::RawAccess || TensorEvaluator<ArgType, Device>::BlockAccess) && NumDims > 0,
PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
CoordAccess = false,
RawAccess = TensorEvaluator<ArgType, Device>::RawAccess
};
static constexpr bool ForwardBlocksToArg =
TensorEvaluator<ArgType, Device>::BlockAccess && !TensorEvaluator<ArgType, Device>::RawAccess;
static constexpr int ArgLayout = TensorEvaluator<ArgType, Device>::Layout;
typedef typename XprType::Scalar Scalar;
typedef typename XprType::CoeffReturnType CoeffReturnType;
typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
typedef StorageMemory<CoeffReturnType, Device> Storage;
typedef typename Storage::Type EvaluatorPointerType;
typedef std::remove_const_t<Scalar> ScalarNoConst;
typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
typedef typename internal::TensorMaterializedBlock<ScalarNoConst, NumDims, Layout, Index> TensorBlock;
typedef typename TensorEvaluator<ArgType, Device>::TensorBlock ArgTensorBlock;
EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {
for (int i = 0; i < NumDims; ++i) {
m_dimensions[i] = m_impl.dimensions()[NumDims - 1 - i];
}
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) { return m_impl.evalSubExprsIfNeeded(data); }
EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_impl.coeff(index); }
template <int LoadMode>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
return m_impl.template packet<LoadMode>(index);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
return m_impl.costPerCoeff(vectorized);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
return getResourceRequirementsImpl(std::integral_constant<bool, ForwardBlocksToArg>());
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
bool root_of_expr_ast = false) const {
return blockImpl(desc, scratch, root_of_expr_ast, std::integral_constant<bool, ForwardBlocksToArg>());
}
EIGEN_DEVICE_FUNC typename Storage::Type data() const { return constCast(m_impl.data()); }
const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
protected:
EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE DSizes<Index, NumDims> reversed(const DSizes<Index, NumDims>& sizes) {
DSizes<Index, NumDims> result;
for (int i = 0; i < NumDims; ++i) result[i] = sizes[NumDims - 1 - i];
return result;
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirementsImpl(
std::true_type ) const {
return m_impl.getResourceRequirements();
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirementsImpl(
std::false_type ) const {
return internal::TensorBlockResourceRequirements::any();
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock blockImpl(TensorBlockDesc& desc, TensorBlockScratch& scratch,
bool ,
std::false_type ) const {
eigen_assert(m_impl.data() != nullptr);
return TensorBlock::materialize(m_impl.data(), m_dimensions, desc, scratch);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock blockImpl(TensorBlockDesc& desc, TensorBlockScratch& scratch,
bool root_of_expr_ast,
std::true_type ) const {
const DSizes<Index, NumDims> arg_dims = reversed(desc.dimensions());
TensorBlockDesc arg_desc(desc.offset(), arg_dims);
typedef typename TensorBlockDesc::DestinationBuffer DestinationBuffer;
const bool strided_destination = desc.destination().kind() == DestinationBuffer::kStrided;
if (desc.destination().kind() == DestinationBuffer::kContiguous || (strided_destination && root_of_expr_ast)) {
arg_desc.template AddDestinationBuffer<ArgLayout>(desc.destination().template data<ScalarNoConst>(),
reversed(desc.destination().strides()));
}
ArgTensorBlock arg_block = m_impl.block(arg_desc, scratch, root_of_expr_ast);
if (arg_block.data() != NULL) {
const bool materialized_in_output = arg_block.kind() == internal::TensorBlockKind::kMaterializedInOutput;
if (materialized_in_output) desc.DropDestinationBuffer();
return TensorBlock(arg_block.kind(), arg_block.data(), desc.dimensions(),
!(materialized_in_output && strided_destination));
}
typedef internal::TensorBlockAssignment<ScalarNoConst, NumDims, typename ArgTensorBlock::XprType, Index>
ArgBlockAssign;
typename TensorBlock::Storage storage =
TensorBlock::prepareStorage(desc, scratch, root_of_expr_ast);
ArgBlockAssign::Run(ArgBlockAssign::target(arg_dims, reversed(storage.strides()), storage.data()),
arg_block.expr());
arg_block.cleanup();
return storage.AsTensorMaterializedBlock();
}
TensorEvaluator<ArgType, Device> m_impl;
Dimensions m_dimensions;
};
template <typename ArgType, typename Device>
struct TensorEvaluator<TensorLayoutSwapOp<ArgType>, Device>
: public TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> {
typedef TensorEvaluator<const TensorLayoutSwapOp<ArgType>, Device> Base;
typedef TensorLayoutSwapOp<ArgType> XprType;
static constexpr int NumDims = Base::NumDims;
static constexpr int Layout = Base::Layout;
enum {
IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
BlockAccess = TensorEvaluator<ArgType, Device>::RawAccess && NumDims > 0,
PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
CoordAccess = false
};
typedef typename XprType::Index Index;
typedef typename XprType::Scalar Scalar;
typedef typename XprType::CoeffReturnType CoeffReturnType;
typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
typedef typename Base::TensorBlockDesc TensorBlockDesc;
EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : Base(op, device) {}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index) const {
return this->m_impl.coeffRef(index);
}
template <int StoreMode>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType& x) const {
this->m_impl.template writePacket<StoreMode>(index, x);
}
template <typename TensorBlock>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writeBlock(const TensorBlockDesc& desc, const TensorBlock& block) {
eigen_assert(this->m_impl.data() != NULL);
typedef typename TensorBlock::XprType TensorBlockExpr;
typedef internal::TensorBlockAssignment<Scalar, NumDims, TensorBlockExpr, Index> TensorBlockAssign;
TensorBlockAssign::Run(TensorBlockAssign::target(desc.dimensions(), internal::strides<Layout>(this->dimensions()),
this->m_impl.data(), desc.offset()),
block.expr());
}
};
}
#endif
Namespace containing all symbols from the Eigen library.