diff options
author | Rasmus Munk Larsen <rmlarsen@google.com> | 2020-10-13 21:48:31 +0000 |
---|---|---|
committer | Rasmus Munk Larsen <rmlarsen@google.com> | 2020-10-13 21:48:31 +0000 |
commit | c6953f799b01d36f4236b64f351cc1446e0abe17 (patch) | |
tree | 9abcded97c6effc010d08787c5b43ef7bb043b54 /unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h | |
parent | 807e51528d220c0efed870f0505dea81a5776085 (diff) |
Add packet generic ops `predux_fmin`, `predux_fmin_nan`, `predux_fmax`, and `predux_fmax_nan` that implement reductions with `PropagateNaN`, and `PropagateNumbers` semantics. Add (slow) generic implementations for most reductions.
Diffstat (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h index 2edc45f1a..fd8fa00fa 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h @@ -192,17 +192,19 @@ struct MinMaxBottomValue<T, false, false> { }; -template <typename T> struct MaxReducer +template <typename T, int NaNPropagation=PropagateFast> struct MaxReducer { EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const { - if (t > *accum) { *accum = t; } + scalar_max_op<T, T, NaNPropagation> op; + *accum = op(t, *accum); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet& p, Packet* accum) const { - (*accum) = pmax<Packet>(*accum, p); + scalar_max_op<T, T, NaNPropagation> op; + (*accum) = op.packetOp(*accum, p); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const { - return MinMaxBottomValue<T, true, Eigen::NumTraits<T>::IsInteger>::bottom_value(); + return MinMaxBottomValue<T, /*IsMax=*/true, Eigen::NumTraits<T>::IsInteger>::bottom_value(); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const { @@ -217,32 +219,34 @@ template <typename T> struct MaxReducer } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const { - return numext::maxi(saccum, predux_max(vaccum)); + scalar_max_op<T, T, NaNPropagation> op; + return op(saccum, op.predux(vaccum)); } }; -template <typename T, typename Device> -struct reducer_traits<MaxReducer<T>, Device> { +template <typename T, typename Device, int NaNPropagation> + struct reducer_traits<MaxReducer<T, NaNPropagation>, Device> { enum { Cost = NumTraits<T>::AddCost, PacketAccess = PacketType<T, Device>::HasMax, IsStateful = false, - IsExactlyAssociative = true + IsExactlyAssociative = (NaNPropagation!=PropagateFast) }; }; - -template <typename T> struct MinReducer +template <typename T, int NaNPropagation=PropagateFast> struct MinReducer { EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const { - if (t < *accum) { *accum = t; } + scalar_min_op<T, T, NaNPropagation> op; + *accum = op(t, *accum); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet& p, Packet* accum) const { - (*accum) = pmin<Packet>(*accum, p); + scalar_min_op<T, T, NaNPropagation> op; + (*accum) = op.packetOp(*accum, p); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const { - return MinMaxBottomValue<T, false, Eigen::NumTraits<T>::IsInteger>::bottom_value(); + return MinMaxBottomValue<T, /*IsMax=*/false, Eigen::NumTraits<T>::IsInteger>::bottom_value(); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const { @@ -257,21 +261,21 @@ template <typename T> struct MinReducer } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const { - return numext::mini(saccum, predux_min(vaccum)); + scalar_min_op<T, T, NaNPropagation> op; + return op(saccum, op.predux(vaccum)); } }; -template <typename T, typename Device> -struct reducer_traits<MinReducer<T>, Device> { +template <typename T, typename Device, int NaNPropagation> + struct reducer_traits<MinReducer<T, NaNPropagation>, Device> { enum { Cost = NumTraits<T>::AddCost, PacketAccess = PacketType<T, Device>::HasMin, IsStateful = false, - IsExactlyAssociative = true + IsExactlyAssociative = (NaNPropagation!=PropagateFast) }; }; - template <typename T> struct ProdReducer { EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const T t, T* accum) const { @@ -282,7 +286,6 @@ template <typename T> struct ProdReducer EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet& p, Packet* accum) const { (*accum) = pmul<Packet>(*accum, p); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const { internal::scalar_cast_op<int, T> conv; return conv(1); |