From 5359e5cdb2c39f948d9c446c41a27045a2bb6c00 Mon Sep 17 00:00:00 2001 From: Nicolas Mellado Date: Mon, 6 Jul 2015 20:55:01 +0200 Subject: [PATCH] Protect against compilation errors with nvcc and numext/complex. Disable functions explicitely involving std::complex when compiling with nvcc. Improve code compatilibity using the new macro EIGEN_USING_NUMEXT_MATH (same spirit than EIGEN_USING_STD_MATH but for numext functions) --- Eigen/Core | 6 +++ Eigen/src/Core/GlobalFunctions.h | 2 + Eigen/src/Core/MathFunctions.h | 57 ++++++++++++------------- Eigen/src/Core/functors/UnaryFunctors.h | 36 +++++++++++++--- 4 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Eigen/Core b/Eigen/Core index de94b5b75..7ad454c56 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -42,6 +42,12 @@ #define EIGEN_USING_STD_MATH(FUNC) using std::FUNC; #endif +#if defined(__CUDA_ARCH__) + #define EIGEN_USING_NUMEXT_MATH(FUNC) using ::FUNC; +#else + #define EIGEN_USING_NUMEXT_MATH(FUNC) using numext::FUNC; +#endif + #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(EIGEN_EXCEPTIONS) #define EIGEN_EXCEPTIONS #endif diff --git a/Eigen/src/Core/GlobalFunctions.h b/Eigen/src/Core/GlobalFunctions.h index 33151a0de..1f5caab4f 100644 --- a/Eigen/src/Core/GlobalFunctions.h +++ b/Eigen/src/Core/GlobalFunctions.h @@ -62,9 +62,11 @@ namespace Eigen EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(round,scalar_round_op) EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(floor,scalar_floor_op) EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(ceil,scalar_ceil_op) +#ifndef __CUDACC__ EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(isnan,scalar_isnan_op) EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(isinf,scalar_isinf_op) EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(isfinite,scalar_isfinite_op) +#endif template inline const Eigen::CwiseUnaryOp, const Derived> diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h index 5091e8d75..8a92ee8f1 100644 --- a/Eigen/src/Core/MathFunctions.h +++ b/Eigen/src/Core/MathFunctions.h @@ -279,7 +279,7 @@ struct norm1_default_impl EIGEN_DEVICE_FUNC static inline RealScalar run(const Scalar& x) { - using std::abs; + EIGEN_USING_STD_MATH(abs); return abs(real(x)) + abs(imag(x)); } }; @@ -290,7 +290,7 @@ struct norm1_default_impl EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x) { - using std::abs; + EIGEN_USING_STD_MATH(abs); return abs(x); } }; @@ -316,8 +316,8 @@ struct hypot_impl { EIGEN_USING_STD_MATH(max); EIGEN_USING_STD_MATH(min); - using std::abs; - using std::sqrt; + EIGEN_USING_STD_MATH(abs); + EIGEN_USING_STD_MATH(sqrt); RealScalar _x = abs(x); RealScalar _y = abs(y); Scalar p, qp; @@ -386,8 +386,8 @@ inline NewType cast(const OldType& x) static inline Scalar run(const Scalar& x) { EIGEN_STATIC_ASSERT((!NumTraits::IsComplex), NUMERIC_TYPE_MUST_BE_REAL) - using std::floor; - using std::ceil; + EIGEN_USING_STD_MATH(floor); + EIGEN_USING_STD_MATH(ceil); return (x > 0.0) ? floor(x + 0.5) : ceil(x - 0.5); } }; @@ -408,7 +408,7 @@ struct round_retval struct arg_impl { static inline Scalar run(const Scalar& x) { - using std::arg; + EIGEN_USING_STD_MATH(arg); return arg(x); } }; @@ -430,7 +430,7 @@ struct round_retval EIGEN_DEVICE_FUNC static inline RealScalar run(const Scalar& x) { - using std::arg; + EIGEN_USING_STD_MATH(arg); return arg(x); } }; @@ -454,7 +454,7 @@ struct log1p_impl { EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar) typedef typename NumTraits::Real RealScalar; - using std::log; + EIGEN_USING_STD_MATH(log); Scalar x1p = RealScalar(1) + x; return ( x1p == Scalar(1) ) ? x : x * ( log(x1p) / (x1p - RealScalar(1)) ); } @@ -488,7 +488,7 @@ struct pow_default_impl typedef Scalar retval; static inline Scalar run(const Scalar& x, const Scalar& y) { - using std::pow; + EIGEN_USING_STD_MATH(pow); return pow(x, y); } }; @@ -794,13 +794,26 @@ bool (isfinite)(const T& x) #endif } +#ifndef __CUDACC__ template -EIGEN_DEVICE_FUNC bool (isfinite)(const std::complex& x) { return numext::isfinite(numext::real(x)) && numext::isfinite(numext::imag(x)); } +template +bool (isnan)(const std::complex& x) +{ + return numext::isnan(numext::real(x)) || numext::isnan(numext::imag(x)); +} + +template +bool (isinf)(const std::complex& x) +{ + return (numext::isinf(numext::real(x)) || numext::isinf(numext::imag(x))) && (!numext::isnan(x)); +} +#endif + template EIGEN_DEVICE_FUNC bool (isnan)(const T& x) @@ -813,13 +826,6 @@ bool (isnan)(const T& x) #endif } -template -EIGEN_DEVICE_FUNC -bool (isnan)(const std::complex& x) -{ - return numext::isnan(numext::real(x)) || numext::isnan(numext::imag(x)); -} - template EIGEN_DEVICE_FUNC bool (isinf)(const T& x) @@ -832,13 +838,6 @@ bool (isinf)(const T& x) #endif } -template -EIGEN_DEVICE_FUNC -bool (isinf)(const std::complex& x) -{ - return (numext::isinf(numext::real(x)) || numext::isinf(numext::imag(x))) && (!numext::isnan(x)); -} - template EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(round, Scalar) round(const Scalar& x) @@ -850,7 +849,7 @@ template EIGEN_DEVICE_FUNC T (floor)(const T& x) { - using std::floor; + EIGEN_USING_STD_MATH(floor); return floor(x); } @@ -858,7 +857,7 @@ template EIGEN_DEVICE_FUNC T (ceil)(const T& x) { - using std::ceil; + EIGEN_USING_STD_MATH(ceil); return ceil(x); } @@ -897,14 +896,14 @@ struct scalar_fuzzy_default_impl template EIGEN_DEVICE_FUNC static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec) { - using std::abs; + EIGEN_USING_STD_MATH(abs); return abs(x) <= abs(y) * prec; } EIGEN_DEVICE_FUNC static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec) { EIGEN_USING_STD_MATH(min); - using std::abs; + EIGEN_USING_STD_MATH(abs); return abs(x - y) <= (min)(abs(x), abs(y)) * prec; } EIGEN_DEVICE_FUNC diff --git a/Eigen/src/Core/functors/UnaryFunctors.h b/Eigen/src/Core/functors/UnaryFunctors.h index 2fc84e01f..c61a899c5 100644 --- a/Eigen/src/Core/functors/UnaryFunctors.h +++ b/Eigen/src/Core/functors/UnaryFunctors.h @@ -533,7 +533,11 @@ struct functor_traits > */ template struct scalar_round_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_round_op) - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { using numext::round; return round(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(round); + return round(a); + } typedef typename packet_traits::type Packet; inline Packet packetOp(const Packet& a) const { return internal::pround(a); } }; @@ -552,7 +556,11 @@ struct functor_traits > */ template struct scalar_floor_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_floor_op) - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return numext::floor(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(floor); + return floor(a); + } typedef typename packet_traits::type Packet; inline Packet packetOp(const Packet& a) const { return internal::pfloor(a); } }; @@ -571,7 +579,11 @@ struct functor_traits > */ template struct scalar_ceil_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_ceil_op) - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const { return numext::ceil(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(ceil); + return ceil(a); + } typedef typename packet_traits::type Packet; inline Packet packetOp(const Packet& a) const { return internal::pceil(a); } }; @@ -591,7 +603,11 @@ struct functor_traits > template struct scalar_isnan_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_isnan_op) typedef bool result_type; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return numext::isnan(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(isnan); + return isnan(a); + } }; template struct functor_traits > @@ -609,7 +625,11 @@ struct functor_traits > template struct scalar_isinf_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_isinf_op) typedef bool result_type; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return numext::isinf(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(isinf); + return isinf(a); + } }; template struct functor_traits > @@ -627,7 +647,11 @@ struct functor_traits > template struct scalar_isfinite_op { EIGEN_EMPTY_STRUCT_CTOR(scalar_isfinite_op) typedef bool result_type; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const { return numext::isfinite(a); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const Scalar& a) const + { + EIGEN_USING_NUMEXT_MATH(isfinite); + return isfinite(a); + } }; template struct functor_traits >