diff options
Diffstat (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h | 114 |
1 files changed, 63 insertions, 51 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h index a8e48fced..fc75dbb5c 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h @@ -25,7 +25,7 @@ struct scalar_mod_op { }; template <typename Scalar> struct functor_traits<scalar_mod_op<Scalar> > -{ enum { Cost = NumTraits<Scalar>::template Div<false>::Cost, PacketAccess = false }; }; +{ enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess = false }; }; /** \internal @@ -38,7 +38,7 @@ struct scalar_mod2_op { }; template <typename Scalar> struct functor_traits<scalar_mod2_op<Scalar> > -{ enum { Cost = NumTraits<Scalar>::template Div<false>::Cost, PacketAccess = false }; }; +{ enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess = false }; }; template <typename Scalar> struct scalar_fmod_op { @@ -188,6 +188,32 @@ struct reducer_traits<MeanReducer<T>, Device> { }; +template <typename T, bool IsMax = true, bool IsInteger = true> +struct MinMaxBottomValue { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static T bottom_value() { + return Eigen::NumTraits<T>::lowest(); + } +}; +template <typename T> +struct MinMaxBottomValue<T, true, false> { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static T bottom_value() { + return -Eigen::NumTraits<T>::infinity(); + } +}; +template <typename T> +struct MinMaxBottomValue<T, false, true> { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static T bottom_value() { + return Eigen::NumTraits<T>::highest(); + } +}; +template <typename T> +struct MinMaxBottomValue<T, false, false> { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static T bottom_value() { + return Eigen::NumTraits<T>::infinity(); + } +}; + + template <typename T> struct MaxReducer { static const bool PacketAccess = packet_traits<T>::HasMax; @@ -200,9 +226,8 @@ template <typename T> struct MaxReducer EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet& p, Packet* accum) const { (*accum) = pmax<Packet>(*accum, p); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const { - return Eigen::NumTraits<T>::lowest(); + return MinMaxBottomValue<T, true, Eigen::NumTraits<T>::IsInteger>::bottom_value(); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const { @@ -242,9 +267,8 @@ template <typename T> struct MinReducer EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reducePacket(const Packet& p, Packet* accum) const { (*accum) = pmin<Packet>(*accum, p); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T initialize() const { - return Eigen::NumTraits<T>::highest(); + return MinMaxBottomValue<T, false, Eigen::NumTraits<T>::IsInteger>::bottom_value(); } template <typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet initializePacket() const { @@ -454,12 +478,11 @@ template <typename T> class UniformRandomGenerator { m_deterministic = other.m_deterministic; } - template<typename Index> - T operator()(Index) const { + T operator()() const { return random<T>(); } - template<typename Index, typename PacketType> - PacketType packetOp(Index) const { + template<typename PacketType> + PacketType packetOp() const { const int packetSize = internal::unpacket_traits<PacketType>::size; EIGEN_ALIGN_MAX T values[packetSize]; for (int i = 0; i < packetSize; ++i) { @@ -484,23 +507,22 @@ template <> class UniformRandomGenerator<float> { } UniformRandomGenerator(const UniformRandomGenerator<float>& other) { m_generator = new std::mt19937(); - m_generator->seed(other(0) * UINT_MAX); + m_generator->seed(other() * UINT_MAX); m_deterministic = other.m_deterministic; } ~UniformRandomGenerator() { delete m_generator; } - template<typename Index> - float operator()(Index) const { + float operator()() const { return m_distribution(*m_generator); } - template<typename Index, typename PacketType> - PacketType packetOp(Index i) const { + template<typename PacketType> + PacketType packetOp() const { const int packetSize = internal::unpacket_traits<PacketType>::size; EIGEN_ALIGN_MAX float values[packetSize]; for (int k = 0; k < packetSize; ++k) { - values[k] = this->operator()(i); + values[k] = this->operator()(); } return internal::pload<PacketType>(values); } @@ -525,23 +547,22 @@ template <> class UniformRandomGenerator<double> { } UniformRandomGenerator(const UniformRandomGenerator<double>& other) { m_generator = new std::mt19937(); - m_generator->seed(other(0) * UINT_MAX); + m_generator->seed(other() * UINT_MAX); m_deterministic = other.m_deterministic; } ~UniformRandomGenerator() { delete m_generator; } - template<typename Index> - double operator()(Index) const { + double operator()() const { return m_distribution(*m_generator); } - template<typename Index, typename PacketType> - PacketType packetOp(Index i) const { + template<typename PacketType> + PacketType packetOp() const { const int packetSize = internal::unpacket_traits<PacketType>::size; EIGEN_ALIGN_MAX double values[packetSize]; for (int k = 0; k < packetSize; ++k) { - values[k] = this->operator()(i); + values[k] = this->operator()(); } return internal::pload<PacketType>(values); } @@ -578,12 +599,11 @@ template <> class UniformRandomGenerator<float> { curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ float operator()(Index) const { + __device__ float operator()() const { return curand_uniform(&m_state); } - template<typename Index, typename PacketType> - __device__ float4 packetOp(Index) const { + template<typename PacketType> + __device__ float4 packetOp() const { EIGEN_STATIC_ASSERT((is_same<PacketType, float4>::value), YOU_MADE_A_PROGRAMMING_MISTAKE); return curand_uniform4(&m_state); } @@ -608,12 +628,11 @@ template <> class UniformRandomGenerator<double> { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ double operator()(Index) const { + __device__ double operator()() const { return curand_uniform_double(&m_state); } - template<typename Index, typename PacketType> - __device__ double2 packetOp(Index) const { + template<typename PacketType> + __device__ double2 packetOp() const { EIGEN_STATIC_ASSERT((is_same<PacketType, double2>::value), YOU_MADE_A_PROGRAMMING_MISTAKE); return curand_uniform2_double(&m_state); } @@ -638,8 +657,7 @@ template <> class UniformRandomGenerator<std::complex<float> > { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ std::complex<float> operator()(Index) const { + __device__ std::complex<float> operator()() const { float4 vals = curand_uniform4(&m_state); return std::complex<float>(vals.x, vals.y); } @@ -664,8 +682,7 @@ template <> class UniformRandomGenerator<std::complex<double> > { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ std::complex<double> operator()(Index) const { + __device__ std::complex<double> operator()() const { double2 vals = curand_uniform2_double(&m_state); return std::complex<double>(vals.x, vals.y); } @@ -701,17 +718,16 @@ template <typename T> class NormalRandomGenerator { } NormalRandomGenerator(const NormalRandomGenerator& other) : m_deterministic(other.m_deterministic), m_distribution(other.m_distribution), m_generator(new std::mt19937()) { - m_generator->seed(other(0) * UINT_MAX); + m_generator->seed(other() * UINT_MAX); } ~NormalRandomGenerator() { delete m_generator; } - template<typename Index> - T operator()(Index) const { + T operator()() const { return m_distribution(*m_generator); } - template<typename Index, typename PacketType> - PacketType packetOp(Index) const { + template<typename PacketType> + PacketType packetOp() const { const int packetSize = internal::unpacket_traits<PacketType>::size; EIGEN_ALIGN_MAX T values[packetSize]; for (int i = 0; i < packetSize; ++i) { @@ -749,12 +765,11 @@ template <> class NormalRandomGenerator<float> { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ float operator()(Index) const { + __device__ float operator()() const { return curand_normal(&m_state); } - template<typename Index, typename PacketType> - __device__ float4 packetOp(Index) const { + template<typename PacketType> + __device__ float4 packetOp() const { EIGEN_STATIC_ASSERT((is_same<PacketType, float4>::value), YOU_MADE_A_PROGRAMMING_MISTAKE); return curand_normal4(&m_state); } @@ -779,12 +794,11 @@ template <> class NormalRandomGenerator<double> { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ double operator()(Index) const { + __device__ double operator()() const { return curand_normal_double(&m_state); } - template<typename Index, typename PacketType> - __device__ double2 packetOp(Index) const { + template<typename PacketType> + __device__ double2 packetOp() const { EIGEN_STATIC_ASSERT((is_same<PacketType, double2>::value), YOU_MADE_A_PROGRAMMING_MISTAKE); return curand_normal2_double(&m_state); } @@ -809,8 +823,7 @@ template <> class NormalRandomGenerator<std::complex<float> > { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ std::complex<float> operator()(Index) const { + __device__ std::complex<float> operator()() const { float4 vals = curand_normal4(&m_state); return std::complex<float>(vals.x, vals.y); } @@ -835,8 +848,7 @@ template <> class NormalRandomGenerator<std::complex<double> > { const int seed = m_deterministic ? 0 : get_random_seed(); curand_init(seed, tid, 0, &m_state); } - template<typename Index> - __device__ std::complex<double> operator()(Index) const { + __device__ std::complex<double> operator()() const { double2 vals = curand_normal2_double(&m_state); return std::complex<double>(vals.x, vals.y); } |