2015-02-27 08:46:04 -08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_CXX11_TENSOR_TENSOR_CONVERSION_H
|
|
|
|
|
#define EIGEN_CXX11_TENSOR_TENSOR_CONVERSION_H
|
|
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2015-02-27 08:46:04 -08:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
template <typename TargetType, typename XprType>
|
|
|
|
|
struct traits<TensorConversionOp<TargetType, XprType> > {
|
|
|
|
|
// Type promotion to handle the case where the types of the lhs and the rhs are different.
|
|
|
|
|
typedef TargetType Scalar;
|
|
|
|
|
typedef typename traits<XprType>::StorageKind StorageKind;
|
|
|
|
|
typedef typename traits<XprType>::Index Index;
|
|
|
|
|
typedef typename XprType::Nested Nested;
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef std::remove_reference_t<Nested> Nested_;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int NumDimensions = traits<XprType>::NumDimensions;
|
|
|
|
|
static constexpr int Layout = traits<XprType>::Layout;
|
2015-02-27 08:46:04 -08:00
|
|
|
enum { Flags = 0 };
|
2019-02-08 15:13:24 -08:00
|
|
|
typedef typename TypeConversion<Scalar, typename traits<XprType>::PointerType>::type PointerType;
|
2015-02-27 08:46:04 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename TargetType, typename XprType>
|
|
|
|
|
struct eval<TensorConversionOp<TargetType, XprType>, Eigen::Dense> {
|
|
|
|
|
typedef const TensorConversionOp<TargetType, XprType>& type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename TargetType, typename XprType>
|
|
|
|
|
struct nested<TensorConversionOp<TargetType, XprType>, 1,
|
|
|
|
|
typename eval<TensorConversionOp<TargetType, XprType> >::type> {
|
|
|
|
|
typedef TensorConversionOp<TargetType, XprType> type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket, int SrcCoeffRatio, int TgtCoeffRatio>
|
2020-06-25 14:31:16 -07:00
|
|
|
struct PacketConverter;
|
|
|
|
|
|
|
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
|
|
|
|
|
struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 1, 1> {
|
2015-02-27 08:46:04 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode, typename Index>
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
|
|
|
|
|
return internal::pcast<SrcPacket, TgtPacket>(m_impl.template packet<LoadMode>(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TensorEvaluator& m_impl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
|
|
|
|
|
struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 2, 1> {
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode, typename Index>
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
|
|
|
|
|
const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
|
|
|
|
|
|
|
|
|
|
SrcPacket src1 = m_impl.template packet<LoadMode>(index);
|
|
|
|
|
SrcPacket src2 = m_impl.template packet<LoadMode>(index + SrcPacketSize);
|
|
|
|
|
TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TensorEvaluator& m_impl;
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-03 18:58:29 -08:00
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
|
|
|
|
|
struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 4, 1> {
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode, typename Index>
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
|
|
|
|
|
const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
|
|
|
|
|
|
|
|
|
|
SrcPacket src1 = m_impl.template packet<LoadMode>(index);
|
|
|
|
|
SrcPacket src2 = m_impl.template packet<LoadMode>(index + SrcPacketSize);
|
|
|
|
|
SrcPacket src3 = m_impl.template packet<LoadMode>(index + 2 * SrcPacketSize);
|
|
|
|
|
SrcPacket src4 = m_impl.template packet<LoadMode>(index + 3 * SrcPacketSize);
|
|
|
|
|
TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2, src3, src4);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TensorEvaluator& m_impl;
|
|
|
|
|
};
|
2015-02-27 08:46:04 -08:00
|
|
|
|
|
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
|
2020-06-25 14:31:16 -07:00
|
|
|
struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 8, 1> {
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode, typename Index>
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
|
|
|
|
|
const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
|
|
|
|
|
|
|
|
|
|
SrcPacket src1 = m_impl.template packet<LoadMode>(index);
|
|
|
|
|
SrcPacket src2 = m_impl.template packet<LoadMode>(index + 1 * SrcPacketSize);
|
|
|
|
|
SrcPacket src3 = m_impl.template packet<LoadMode>(index + 2 * SrcPacketSize);
|
|
|
|
|
SrcPacket src4 = m_impl.template packet<LoadMode>(index + 3 * SrcPacketSize);
|
|
|
|
|
SrcPacket src5 = m_impl.template packet<LoadMode>(index + 4 * SrcPacketSize);
|
|
|
|
|
SrcPacket src6 = m_impl.template packet<LoadMode>(index + 5 * SrcPacketSize);
|
|
|
|
|
SrcPacket src7 = m_impl.template packet<LoadMode>(index + 6 * SrcPacketSize);
|
|
|
|
|
SrcPacket src8 = m_impl.template packet<LoadMode>(index + 7 * SrcPacketSize);
|
|
|
|
|
TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2, src3, src4, src5, src6, src7, src8);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TensorEvaluator& m_impl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket, int TgtCoeffRatio>
|
|
|
|
|
struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 1, TgtCoeffRatio> {
|
2015-02-27 08:46:04 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl)
|
|
|
|
|
: m_impl(impl), m_maxIndex(impl.dimensions().TotalSize()) {}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode, typename Index>
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
|
|
|
|
|
const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
|
2015-03-02 10:11:40 -08:00
|
|
|
// Only call m_impl.packet() when we have direct access to the underlying data. This
|
|
|
|
|
// ensures that we don't compute the subexpression twice. We may however load some
|
|
|
|
|
// coefficients twice, but in practice this doesn't negatively impact performance.
|
|
|
|
|
if (m_impl.data() && (index + SrcPacketSize < m_maxIndex)) {
|
2015-03-02 09:41:36 -08:00
|
|
|
// Force unaligned memory loads since we can't ensure alignment anymore
|
|
|
|
|
return internal::pcast<SrcPacket, TgtPacket>(m_impl.template packet<Unaligned>(index));
|
2015-02-27 08:46:04 -08:00
|
|
|
} else {
|
|
|
|
|
const int TgtPacketSize = internal::unpacket_traits<TgtPacket>::size;
|
2016-02-19 15:03:26 +00:00
|
|
|
typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
|
|
|
|
|
typedef typename internal::unpacket_traits<TgtPacket>::type TgtType;
|
|
|
|
|
internal::scalar_cast_op<SrcType, TgtType> converter;
|
2015-07-29 11:11:23 +02:00
|
|
|
EIGEN_ALIGN_MAX typename internal::unpacket_traits<TgtPacket>::type values[TgtPacketSize];
|
2019-06-28 10:08:23 +01:00
|
|
|
EIGEN_UNROLL_LOOP
|
2015-02-27 08:46:04 -08:00
|
|
|
for (int i = 0; i < TgtPacketSize; ++i) {
|
2016-02-19 15:03:26 +00:00
|
|
|
values[i] = converter(m_impl.coeff(index + i));
|
2015-02-27 08:46:04 -08:00
|
|
|
}
|
|
|
|
|
TgtPacket rslt = internal::pload<TgtPacket>(values);
|
|
|
|
|
return rslt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TensorEvaluator& m_impl;
|
|
|
|
|
const typename TensorEvaluator::Index m_maxIndex;
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-05 17:36:00 +00:00
|
|
|
/**
|
|
|
|
|
* \ingroup CXX11_Tensor_Module
|
|
|
|
|
*
|
|
|
|
|
* \brief Tensor conversion class. This class makes it possible to vectorize
|
|
|
|
|
* type casting operations when the number of scalars per packet in the source
|
|
|
|
|
* and the destination type differ
|
|
|
|
|
*/
|
2015-02-27 08:46:04 -08:00
|
|
|
template <typename TargetType, typename XprType>
|
|
|
|
|
class TensorConversionOp : public TensorBase<TensorConversionOp<TargetType, XprType>, ReadOnlyAccessors> {
|
|
|
|
|
public:
|
|
|
|
|
typedef typename internal::traits<TensorConversionOp>::Scalar Scalar;
|
|
|
|
|
typedef typename internal::traits<TensorConversionOp>::StorageKind StorageKind;
|
|
|
|
|
typedef typename internal::traits<TensorConversionOp>::Index Index;
|
|
|
|
|
typedef typename internal::nested<TensorConversionOp>::type Nested;
|
2015-10-20 11:37:44 -07:00
|
|
|
typedef Scalar CoeffReturnType;
|
2015-02-27 08:46:04 -08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-02-27 08:46:04 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConversionOp(const XprType& xpr) : m_xpr(xpr) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-02-27 08:46:04 -08:00
|
|
|
protected:
|
|
|
|
|
typename XprType::Nested m_xpr;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-28 10:08:23 +01:00
|
|
|
template <bool SameType, typename Eval, typename EvalPointerType>
|
|
|
|
|
struct ConversionSubExprEval {
|
2021-05-11 22:47:49 +00:00
|
|
|
static EIGEN_STRONG_INLINE bool run(Eval& impl, EvalPointerType) {
|
2016-02-21 22:43:37 -08:00
|
|
|
impl.evalSubExprsIfNeeded(NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-28 10:08:23 +01:00
|
|
|
template <typename Eval, typename EvalPointerType>
|
|
|
|
|
struct ConversionSubExprEval<true, Eval, EvalPointerType> {
|
2021-05-11 22:47:49 +00:00
|
|
|
static EIGEN_STRONG_INLINE bool run(Eval& impl, EvalPointerType data) { return impl.evalSubExprsIfNeeded(data); }
|
2016-02-21 22:43:37 -08:00
|
|
|
};
|
2015-02-27 08:46:04 -08:00
|
|
|
|
2019-09-19 13:54:49 -07:00
|
|
|
#ifdef EIGEN_USE_THREADS
|
|
|
|
|
template <bool SameType, typename Eval, typename EvalPointerType, typename EvalSubExprsCallback>
|
|
|
|
|
struct ConversionSubExprEvalAsync {
|
2021-05-11 22:47:49 +00:00
|
|
|
static EIGEN_STRONG_INLINE void run(Eval& impl, EvalPointerType, EvalSubExprsCallback done) {
|
2019-09-19 13:54:49 -07:00
|
|
|
impl.evalSubExprsIfNeededAsync(nullptr, std::move(done));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Eval, typename EvalPointerType, typename EvalSubExprsCallback>
|
|
|
|
|
struct ConversionSubExprEvalAsync<true, Eval, EvalPointerType, EvalSubExprsCallback> {
|
2021-05-11 22:47:49 +00:00
|
|
|
static EIGEN_STRONG_INLINE void run(Eval& impl, EvalPointerType data, EvalSubExprsCallback done) {
|
2019-09-19 13:54:49 -07:00
|
|
|
impl.evalSubExprsIfNeededAsync(data, std::move(done));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-02-08 15:13:24 -08:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template <typename SrcType, typename TargetType, bool IsSameT>
|
|
|
|
|
struct CoeffConv {
|
|
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetType run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
|
|
|
|
internal::scalar_cast_op<SrcType, TargetType> converter;
|
|
|
|
|
return converter(impl.coeff(index));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename SrcType, typename TargetType>
|
|
|
|
|
struct CoeffConv<SrcType, TargetType, true> {
|
|
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetType run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
|
|
|
|
return impl.coeff(index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename SrcPacket, typename TargetPacket, int LoadMode, bool ActuallyVectorize, bool IsSameT>
|
|
|
|
|
struct PacketConv {
|
|
|
|
|
typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
|
|
|
|
|
typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
|
|
|
|
|
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int PacketSize = internal::unpacket_traits<TargetPacket>::size;
|
2019-02-08 15:13:24 -08:00
|
|
|
|
|
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
|
|
|
|
internal::scalar_cast_op<SrcType, TargetType> converter;
|
2022-03-16 16:43:40 +00:00
|
|
|
EIGEN_ALIGN_MAX std::remove_const_t<TargetType> values[PacketSize];
|
2019-06-28 10:08:23 +01:00
|
|
|
EIGEN_UNROLL_LOOP
|
2019-02-08 15:13:24 -08:00
|
|
|
for (int i = 0; i < PacketSize; ++i) {
|
|
|
|
|
values[i] = converter(impl.coeff(index + i));
|
|
|
|
|
}
|
|
|
|
|
TargetPacket rslt = internal::pload<TargetPacket>(values);
|
|
|
|
|
return rslt;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename SrcPacket, typename TargetPacket, int LoadMode, bool IsSameT>
|
|
|
|
|
struct PacketConv<SrcPacket, TargetPacket, LoadMode, true, IsSameT> {
|
|
|
|
|
typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
|
|
|
|
|
typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
|
|
|
|
|
|
|
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
|
|
|
|
const int SrcCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::SrcCoeffRatio;
|
|
|
|
|
const int TgtCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::TgtCoeffRatio;
|
|
|
|
|
PacketConverter<TensorEvaluator<ArgType, Device>, SrcPacket, TargetPacket, SrcCoeffRatio, TgtCoeffRatio> converter(
|
|
|
|
|
impl);
|
|
|
|
|
return converter.template packet<LoadMode>(index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-08 15:47:25 -08:00
|
|
|
template <typename SrcPacket, typename TargetPacket, int LoadMode>
|
|
|
|
|
struct PacketConv<SrcPacket, TargetPacket, LoadMode, /*ActuallyVectorize=*/false, /*IsSameT=*/true> {
|
|
|
|
|
typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int PacketSize = internal::unpacket_traits<TargetPacket>::size;
|
2019-02-08 15:47:25 -08:00
|
|
|
|
|
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
2022-03-16 16:43:40 +00:00
|
|
|
EIGEN_ALIGN_MAX std::remove_const_t<TargetType> values[PacketSize];
|
2019-02-08 15:47:25 -08:00
|
|
|
for (int i = 0; i < PacketSize; ++i) values[i] = impl.coeff(index + i);
|
|
|
|
|
return internal::pload<TargetPacket>(values);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename SrcPacket, typename TargetPacket, int LoadMode>
|
|
|
|
|
struct PacketConv<SrcPacket, TargetPacket, LoadMode, /*ActuallyVectorize=*/true, /*IsSameT=*/true> {
|
2019-02-08 15:13:24 -08:00
|
|
|
template <typename ArgType, typename Device>
|
|
|
|
|
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
|
|
|
|
|
Index index) {
|
|
|
|
|
return impl.template packet<LoadMode>(index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace internal
|
2015-02-27 08:46:04 -08:00
|
|
|
|
|
|
|
|
// Eval as rvalue
|
|
|
|
|
template <typename TargetType, typename ArgType, typename Device>
|
|
|
|
|
struct TensorEvaluator<const TensorConversionOp<TargetType, ArgType>, Device> {
|
|
|
|
|
typedef TensorConversionOp<TargetType, ArgType> XprType;
|
|
|
|
|
typedef typename XprType::Index Index;
|
|
|
|
|
typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
|
|
|
|
|
typedef TargetType Scalar;
|
|
|
|
|
typedef TargetType CoeffReturnType;
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef internal::remove_all_t<typename internal::traits<ArgType>::Scalar> SrcType;
|
2016-03-08 12:07:33 -08:00
|
|
|
typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
|
|
|
|
|
typedef typename PacketType<SrcType, Device>::type PacketSourceType;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
|
|
|
|
|
static constexpr bool IsSameType = internal::is_same<TargetType, SrcType>::value;
|
2019-06-28 10:08:23 +01:00
|
|
|
typedef StorageMemory<CoeffReturnType, Device> Storage;
|
|
|
|
|
typedef typename Storage::Type EvaluatorPointerType;
|
2015-02-27 08:46:04 -08:00
|
|
|
|
|
|
|
|
enum {
|
2019-10-02 12:44:06 -07:00
|
|
|
IsAligned = false,
|
|
|
|
|
PacketAccess =
|
2019-06-28 10:08:23 +01:00
|
|
|
#ifndef EIGEN_USE_SYCL
|
2019-10-02 12:44:06 -07:00
|
|
|
true,
|
2019-06-28 10:08:23 +01:00
|
|
|
#else
|
2019-10-02 12:44:06 -07:00
|
|
|
TensorEvaluator<ArgType, Device>::PacketAccess &
|
|
|
|
|
internal::type_casting_traits<SrcType, TargetType>::VectorizedCast,
|
2019-06-28 10:08:23 +01:00
|
|
|
#endif
|
2019-12-10 15:40:23 -08:00
|
|
|
BlockAccess = TensorEvaluator<ArgType, Device>::BlockAccess,
|
2019-10-02 12:44:06 -07:00
|
|
|
PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
|
|
|
|
|
RawAccess = false
|
2015-02-27 08:46:04 -08:00
|
|
|
};
|
|
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int NumDims = internal::array_size<Dimensions>::value;
|
2019-10-02 12:44:06 -07:00
|
|
|
|
2019-09-24 12:52:45 -07:00
|
|
|
//===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
|
2019-10-02 12:44:06 -07:00
|
|
|
typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
|
|
|
|
|
typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
|
|
|
|
|
|
2019-12-10 15:40:23 -08:00
|
|
|
typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock ArgTensorBlock;
|
2019-10-02 12:44:06 -07:00
|
|
|
|
|
|
|
|
struct TensorConversionOpBlockFactory {
|
|
|
|
|
template <typename ArgXprType>
|
|
|
|
|
struct XprType {
|
|
|
|
|
typedef TensorConversionOp<TargetType, const ArgXprType> type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename ArgXprType>
|
|
|
|
|
typename XprType<ArgXprType>::type expr(const ArgXprType& expr) const {
|
|
|
|
|
return typename XprType<ArgXprType>::type(expr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef internal::TensorUnaryExprBlock<TensorConversionOpBlockFactory, ArgTensorBlock> TensorBlock;
|
2019-09-24 12:52:45 -07:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {}
|
2015-02-27 08:46:04 -08:00
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
|
|
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
|
2019-06-28 10:08:23 +01:00
|
|
|
return ConversionSubExprEval<IsSameType, TensorEvaluator<ArgType, Device>, EvaluatorPointerType>::run(m_impl, data);
|
2015-02-27 08:46:04 -08:00
|
|
|
}
|
|
|
|
|
|
2019-09-19 13:54:49 -07:00
|
|
|
#ifdef EIGEN_USE_THREADS
|
|
|
|
|
template <typename EvalSubExprsCallback>
|
|
|
|
|
EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType data, EvalSubExprsCallback done) {
|
|
|
|
|
ConversionSubExprEvalAsync<IsSameType, TensorEvaluator<ArgType, Device>, EvaluatorPointerType,
|
|
|
|
|
EvalSubExprsCallback>::run(m_impl, data, std::move(done));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
|
2015-02-27 08:46:04 -08:00
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
|
2019-02-08 15:13:24 -08:00
|
|
|
return internal::CoeffConv<SrcType, TargetType, IsSameType>::run(m_impl, index);
|
2015-02-27 08:46:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <int LoadMode>
|
2019-02-08 15:47:25 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
|
|
|
|
|
// If we are not going to do the cast, we just need to check that base
|
|
|
|
|
// TensorEvaluator has packet access. Otherwise we also need to make sure,
|
|
|
|
|
// that we have an implementation of vectorized cast.
|
|
|
|
|
const bool Vectorizable = IsSameType ? TensorEvaluator<ArgType, Device>::PacketAccess
|
2021-06-15 09:09:31 -07:00
|
|
|
: int(TensorEvaluator<ArgType, Device>::PacketAccess) &
|
|
|
|
|
int(internal::type_casting_traits<SrcType, TargetType>::VectorizedCast);
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2019-02-08 15:13:24 -08:00
|
|
|
return internal::PacketConv<PacketSourceType, PacketReturnType, LoadMode, Vectorizable, IsSameType>::run(m_impl,
|
|
|
|
|
index);
|
2015-02-27 08:46:04 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 13:57:35 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
|
|
|
|
|
const double cast_cost = TensorOpCost::CastCost<SrcType, TargetType>();
|
|
|
|
|
if (vectorized) {
|
|
|
|
|
const double SrcCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::SrcCoeffRatio;
|
|
|
|
|
const double TgtCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::TgtCoeffRatio;
|
|
|
|
|
return m_impl.costPerCoeff(vectorized) * (SrcCoeffRatio / PacketSize) +
|
|
|
|
|
TensorOpCost(0, 0, TgtCoeffRatio * (cast_cost / PacketSize));
|
|
|
|
|
} else {
|
|
|
|
|
return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, cast_cost);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 15:40:23 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
|
2019-12-09 16:19:38 -08:00
|
|
|
return m_impl.getResourceRequirements();
|
2019-10-02 12:44:06 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-10 15:40:23 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
|
2019-10-14 14:31:59 -07:00
|
|
|
bool /*root_of_expr_ast*/ = false) const {
|
2019-12-10 15:40:23 -08:00
|
|
|
return TensorBlock(m_impl.block(desc, scratch), TensorConversionOpBlockFactory());
|
2019-10-02 12:44:06 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:08:23 +01:00
|
|
|
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
|
2015-02-27 08:46:04 -08:00
|
|
|
|
2017-01-16 13:58:49 +00:00
|
|
|
/// required by sycl in order to extract the sycl accessor
|
|
|
|
|
const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
|
|
|
|
|
|
2019-02-08 15:13:24 -08:00
|
|
|
protected:
|
2016-05-26 14:37:09 -07:00
|
|
|
TensorEvaluator<ArgType, Device> m_impl;
|
2015-02-27 08:46:04 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_CXX11_TENSOR_TENSOR_CONVERSION_H
|