From ddf775363147fc7ee778b42c21b642f085193f55 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 1 Aug 2013 16:26:57 +0200 Subject: [PATCH] Add nvcc support for small eigenvalues decompositions and workaround lack of support for std::swap and std::numeric_limits --- Eigen/Core | 2 +- Eigen/src/Core/Assign.h | 8 ++ Eigen/src/Core/CwiseNullaryOp.h | 2 + Eigen/src/Core/DenseBase.h | 6 +- Eigen/src/Core/Matrix.h | 1 + Eigen/src/Core/MatrixBase.h | 40 +++++++--- Eigen/src/Core/NumTraits.h | 12 ++- Eigen/src/Core/PlainObjectBase.h | 11 +++ Eigen/src/Core/SelfAdjointView.h | 24 ++++++ Eigen/src/Core/Swap.h | 14 ++++ Eigen/src/Core/TriangularMatrix.h | 76 ++++++++++++++++++- Eigen/src/Core/util/Meta.h | 24 ++++++ .../src/Eigenvalues/SelfAdjointEigenSolver.h | 40 ++++++++-- Eigen/src/Geometry/OrthoMethods.h | 3 + 14 files changed, 234 insertions(+), 29 deletions(-) diff --git a/Eigen/Core b/Eigen/Core index d15795aed..1aaac5ae5 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -30,7 +30,7 @@ #endif #if defined(__CUDA_ARCH__) - #define EIGEN_USING_STD_MATH(FUNC) + #define EIGEN_USING_STD_MATH(FUNC) using ::FUNC; #else #define EIGEN_USING_STD_MATH(FUNC) using std::FUNC; #endif diff --git a/Eigen/src/Core/Assign.h b/Eigen/src/Core/Assign.h index b5a5a9fdb..906adcf82 100644 --- a/Eigen/src/Core/Assign.h +++ b/Eigen/src/Core/Assign.h @@ -532,6 +532,7 @@ struct assign_selector { EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.derived()); } template + EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& evalTo(ActualDerived& dst, const ActualOtherDerived& other) { other.evalTo(dst); return dst; } }; template @@ -544,6 +545,7 @@ struct assign_selector { EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.transpose()); } template + EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& evalTo(ActualDerived& dst, const ActualOtherDerived& other) { Transpose dstTrans(dst); other.evalTo(dstTrans); return dst; } }; template @@ -556,18 +558,21 @@ struct assign_selector { template template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase::operator=(const DenseBase& other) { return internal::assign_selector::run(derived(), other.derived()); } template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase::operator=(const DenseBase& other) { return internal::assign_selector::run(derived(), other.derived()); } template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const MatrixBase& other) { return internal::assign_selector::run(derived(), other.derived()); @@ -575,6 +580,7 @@ EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const MatrixBase& ot template template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const DenseBase& other) { return internal::assign_selector::run(derived(), other.derived()); @@ -582,6 +588,7 @@ EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const DenseBase template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const EigenBase& other) { return internal::assign_selector::evalTo(derived(), other.derived()); @@ -589,6 +596,7 @@ EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const EigenBase template +EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase::operator=(const ReturnByValue& other) { return internal::assign_selector::evalTo(derived(), other.derived()); diff --git a/Eigen/src/Core/CwiseNullaryOp.h b/Eigen/src/Core/CwiseNullaryOp.h index 14c78cb85..1d4ee50a8 100644 --- a/Eigen/src/Core/CwiseNullaryOp.h +++ b/Eigen/src/Core/CwiseNullaryOp.h @@ -746,6 +746,7 @@ namespace internal { template=16)> struct setIdentity_impl { + EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) { return m = Derived::Identity(m.rows(), m.cols()); @@ -756,6 +757,7 @@ template struct setIdentity_impl { typedef typename Derived::Index Index; + EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) { m.setZero(); diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index 097717075..889e2eafb 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -391,7 +391,8 @@ template class DenseBase /** swaps *this with the expression \a other. * */ - template EIGEN_DEVICE_FUNC + template + EIGEN_DEVICE_FUNC void swap(const DenseBase& other, int = OtherDerived::ThisConstantIsPrivateInPlainObjectBase) { @@ -401,7 +402,8 @@ template class DenseBase /** swaps *this with the matrix or array \a other. * */ - template EIGEN_DEVICE_FUNC + template + EIGEN_DEVICE_FUNC void swap(PlainObjectBase& other) { SwapWrapper(derived()).lazyAssign(other.derived()); diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index 8c3780ca2..b80d97c8c 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -331,6 +331,7 @@ class Matrix * of same type it is enough to swap the data pointers. */ template + EIGEN_DEVICE_FUNC void swap(MatrixBase const & other) { this->_swap(other.derived()); } diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 8b8a868b7..ff4526d75 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -278,31 +278,41 @@ template class MatrixBase template struct TriangularViewReturnType { typedef TriangularView Type; }; template struct ConstTriangularViewReturnType { typedef const TriangularView Type; }; - template typename TriangularViewReturnType::Type triangularView(); - template typename ConstTriangularViewReturnType::Type triangularView() const; + template + EIGEN_DEVICE_FUNC + typename TriangularViewReturnType::Type triangularView(); + template + EIGEN_DEVICE_FUNC + typename ConstTriangularViewReturnType::Type triangularView() const; template struct SelfAdjointViewReturnType { typedef SelfAdjointView Type; }; template struct ConstSelfAdjointViewReturnType { typedef const SelfAdjointView Type; }; - template typename SelfAdjointViewReturnType::Type selfadjointView(); - template typename ConstSelfAdjointViewReturnType::Type selfadjointView() const; + template + EIGEN_DEVICE_FUNC + typename SelfAdjointViewReturnType::Type selfadjointView(); + template + EIGEN_DEVICE_FUNC + typename ConstSelfAdjointViewReturnType::Type selfadjointView() const; const SparseView sparseView(const Scalar& m_reference = Scalar(0), const typename NumTraits::Real& m_epsilon = NumTraits::dummy_precision()) const; - static const IdentityReturnType Identity(); - static const IdentityReturnType Identity(Index rows, Index cols); - static const BasisReturnType Unit(Index size, Index i); - static const BasisReturnType Unit(Index i); - static const BasisReturnType UnitX(); - static const BasisReturnType UnitY(); - static const BasisReturnType UnitZ(); - static const BasisReturnType UnitW(); + EIGEN_DEVICE_FUNC static const IdentityReturnType Identity(); + EIGEN_DEVICE_FUNC static const IdentityReturnType Identity(Index rows, Index cols); + EIGEN_DEVICE_FUNC static const BasisReturnType Unit(Index size, Index i); + EIGEN_DEVICE_FUNC static const BasisReturnType Unit(Index i); + EIGEN_DEVICE_FUNC static const BasisReturnType UnitX(); + EIGEN_DEVICE_FUNC static const BasisReturnType UnitY(); + EIGEN_DEVICE_FUNC static const BasisReturnType UnitZ(); + EIGEN_DEVICE_FUNC static const BasisReturnType UnitW(); EIGEN_DEVICE_FUNC const DiagonalWrapper asDiagonal() const; const PermutationWrapper asPermutation() const; + EIGEN_DEVICE_FUNC Derived& setIdentity(); + EIGEN_DEVICE_FUNC Derived& setIdentity(Index rows, Index cols); bool isIdentity(const RealScalar& prec = NumTraits::dummy_precision()) const; @@ -430,11 +440,17 @@ template class MatrixBase }; #endif // EIGEN_PARSED_BY_DOXYGEN template + EIGEN_DEVICE_FUNC typename cross_product_return_type::type cross(const MatrixBase& other) const; + template + EIGEN_DEVICE_FUNC PlainObject cross3(const MatrixBase& other) const; + + EIGEN_DEVICE_FUNC PlainObject unitOrthogonal(void) const; + Matrix eulerAngles(Index a0, Index a1, Index a2) const; #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS diff --git a/Eigen/src/Core/NumTraits.h b/Eigen/src/Core/NumTraits.h index eb5306bf2..2b6633c9c 100644 --- a/Eigen/src/Core/NumTraits.h +++ b/Eigen/src/Core/NumTraits.h @@ -68,16 +68,22 @@ template struct GenericNumTraits >::type NonInteger; typedef T Nested; - static inline Real epsilon() { return std::numeric_limits::epsilon(); } + EIGEN_DEVICE_FUNC + static inline Real epsilon() + { + #if defined(__CUDA_ARCH__) + return internal::device::numeric_limits::epsilon(); + #else + return std::numeric_limits::epsilon(); + #endif + } EIGEN_DEVICE_FUNC static inline Real dummy_precision() { // make sure to override this for floating-point types return Real(0); } - EIGEN_DEVICE_FUNC static inline T highest() { return (std::numeric_limits::max)(); } - EIGEN_DEVICE_FUNC static inline T lowest() { return IsInteger ? (std::numeric_limits::min)() : (-(std::numeric_limits::max)()); } #ifdef EIGEN2_SUPPORT diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h index 39d422f78..618e00e02 100644 --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -128,7 +128,9 @@ class PlainObjectBase : public internal::dense_xpr_base::type enum { NeedsToAlign = SizeAtCompileTime != Dynamic && (internal::traits::Flags & AlignedBit) != 0 }; EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) + EIGEN_DEVICE_FUNC Base& base() { return *static_cast(this); } + EIGEN_DEVICE_FUNC const Base& base() const { return *static_cast(this); } EIGEN_DEVICE_FUNC @@ -351,6 +353,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * Matrices are resized relative to the top-left element. In case values need to be * appended to the matrix they will be uninitialized. */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(Index nbRows, Index nbCols) { internal::conservative_resize_like_impl::run(*this, nbRows, nbCols); @@ -363,6 +366,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * * In case the matrix is growing, new rows will be uninitialized. */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(Index nbRows, NoChange_t) { // Note: see the comment in conservativeResize(Index,Index) @@ -376,6 +380,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * * In case the matrix is growing, new columns will be uninitialized. */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, Index nbCols) { // Note: see the comment in conservativeResize(Index,Index) @@ -390,6 +395,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * * When values are appended, they will be uninitialized. */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(Index size) { internal::conservative_resize_like_impl::run(*this, size); @@ -405,6 +411,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * appended to the matrix they will copied from \c other. */ template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResizeLike(const DenseBase& other) { internal::conservative_resize_like_impl::run(*this, other); @@ -647,6 +654,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type } template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void _init2(Index nbRows, Index nbCols, typename internal::enable_if::type* = 0) { EIGEN_STATIC_ASSERT(bool(NumTraits::IsInteger) && @@ -670,6 +678,7 @@ class PlainObjectBase : public internal::dense_xpr_base::type * data pointers. */ template + EIGEN_DEVICE_FUNC void _swap(DenseBase const & other) { enum { SwapPointers = internal::is_same::value && Base::SizeAtCompileTime==Dynamic }; @@ -790,6 +799,7 @@ struct conservative_resize_like_impl template struct matrix_swap_impl { + EIGEN_DEVICE_FUNC static inline void run(MatrixTypeA& a, MatrixTypeB& b) { a.base().swap(b); @@ -799,6 +809,7 @@ struct matrix_swap_impl template struct matrix_swap_impl { + EIGEN_DEVICE_FUNC static inline void run(MatrixTypeA& a, MatrixTypeB& b) { static_cast(a).m_storage.swap(static_cast(b).m_storage); diff --git a/Eigen/src/Core/SelfAdjointView.h b/Eigen/src/Core/SelfAdjointView.h index 6fa7cd15e..8231e3f5c 100644 --- a/Eigen/src/Core/SelfAdjointView.h +++ b/Eigen/src/Core/SelfAdjointView.h @@ -69,17 +69,23 @@ template class SelfAdjointView }; typedef typename MatrixType::PlainObject PlainObject; + EIGEN_DEVICE_FUNC inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {} + EIGEN_DEVICE_FUNC inline Index rows() const { return m_matrix.rows(); } + EIGEN_DEVICE_FUNC inline Index cols() const { return m_matrix.cols(); } + EIGEN_DEVICE_FUNC inline Index outerStride() const { return m_matrix.outerStride(); } + EIGEN_DEVICE_FUNC inline Index innerStride() const { return m_matrix.innerStride(); } /** \sa MatrixBase::coeff() * \warning the coordinates must fit into the referenced triangular part */ + EIGEN_DEVICE_FUNC inline Scalar coeff(Index row, Index col) const { Base::check_coordinates_internal(row, col); @@ -89,6 +95,7 @@ template class SelfAdjointView /** \sa MatrixBase::coeffRef() * \warning the coordinates must fit into the referenced triangular part */ + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) { Base::check_coordinates_internal(row, col); @@ -96,13 +103,17 @@ template class SelfAdjointView } /** \internal */ + EIGEN_DEVICE_FUNC const MatrixTypeNestedCleaned& _expression() const { return m_matrix; } + EIGEN_DEVICE_FUNC const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; } + EIGEN_DEVICE_FUNC MatrixTypeNestedCleaned& nestedExpression() { return *const_cast(&m_matrix); } /** Efficient self-adjoint matrix times vector/matrix product */ template + EIGEN_DEVICE_FUNC SelfadjointProductMatrix operator*(const MatrixBase& rhs) const { @@ -113,6 +124,7 @@ template class SelfAdjointView /** Efficient vector/matrix times self-adjoint matrix product */ template friend + EIGEN_DEVICE_FUNC SelfadjointProductMatrix operator*(const MatrixBase& lhs, const SelfAdjointView& rhs) { @@ -132,6 +144,7 @@ template class SelfAdjointView * \sa rankUpdate(const MatrixBase&, Scalar) */ template + EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase& u, const MatrixBase& v, const Scalar& alpha = Scalar(1)); /** Perform a symmetric rank K update of the selfadjoint matrix \c *this: @@ -145,6 +158,7 @@ template class SelfAdjointView * \sa rankUpdate(const MatrixBase&, const MatrixBase&, Scalar) */ template + EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase& u, const Scalar& alpha = Scalar(1)); /////////// Cholesky module /////////// @@ -159,11 +173,14 @@ template class SelfAdjointView /** Return type of eigenvalues() */ typedef Matrix::ColsAtCompileTime, 1> EigenvaluesReturnType; + EIGEN_DEVICE_FUNC EigenvaluesReturnType eigenvalues() const; + EIGEN_DEVICE_FUNC RealScalar operatorNorm() const; #ifdef EIGEN2_SUPPORT template + EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const MatrixBase& other) { enum { @@ -174,6 +191,7 @@ template class SelfAdjointView return *this; } template + EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const TriangularView& other) { enum { @@ -209,6 +227,7 @@ struct triangular_assignment_selector::run(dst, src); @@ -223,6 +242,7 @@ struct triangular_assignment_selector struct triangular_assignment_selector { + EIGEN_DEVICE_FUNC static inline void run(Derived1 &, const Derived2 &) {} }; @@ -234,6 +254,7 @@ struct triangular_assignment_selector::run(dst, src); @@ -248,6 +269,7 @@ struct triangular_assignment_selector struct triangular_assignment_selector { + EIGEN_DEVICE_FUNC static inline void run(Derived1 &, const Derived2 &) {} }; @@ -255,6 +277,7 @@ template struct triangular_assignment_selector { typedef typename Derived1::Index Index; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { for(Index j = 0; j < dst.cols(); ++j) @@ -272,6 +295,7 @@ struct triangular_assignment_selector struct triangular_assignment_selector { + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { typedef typename Derived1::Index Index; diff --git a/Eigen/src/Core/Swap.h b/Eigen/src/Core/Swap.h index bf58bd599..d602fba65 100644 --- a/Eigen/src/Core/Swap.h +++ b/Eigen/src/Core/Swap.h @@ -33,11 +33,16 @@ template class SwapWrapper EIGEN_DENSE_PUBLIC_INTERFACE(SwapWrapper) typedef typename internal::packet_traits::type Packet; + EIGEN_DEVICE_FUNC inline SwapWrapper(ExpressionType& xpr) : m_expression(xpr) {} + EIGEN_DEVICE_FUNC inline Index rows() const { return m_expression.rows(); } + EIGEN_DEVICE_FUNC inline Index cols() const { return m_expression.cols(); } + EIGEN_DEVICE_FUNC inline Index outerStride() const { return m_expression.outerStride(); } + EIGEN_DEVICE_FUNC inline Index innerStride() const { return m_expression.innerStride(); } typedef typename internal::conditional< @@ -46,30 +51,37 @@ template class SwapWrapper const Scalar >::type ScalarWithConstIfNotLvalue; + EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); } + EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_expression.data(); } + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index rowId, Index colId) { return m_expression.const_cast_derived().coeffRef(rowId, colId); } + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index index) { return m_expression.const_cast_derived().coeffRef(index); } + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index rowId, Index colId) const { return m_expression.coeffRef(rowId, colId); } + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index index) const { return m_expression.coeffRef(index); } template + EIGEN_DEVICE_FUNC void copyCoeff(Index rowId, Index colId, const DenseBase& other) { OtherDerived& _other = other.const_cast_derived(); @@ -81,6 +93,7 @@ template class SwapWrapper } template + EIGEN_DEVICE_FUNC void copyCoeff(Index index, const DenseBase& other) { OtherDerived& _other = other.const_cast_derived(); @@ -115,6 +128,7 @@ template class SwapWrapper _other.template writePacket(index, tmp); } + EIGEN_DEVICE_FUNC ExpressionType& expression() const { return m_expression; } protected: diff --git a/Eigen/src/Core/TriangularMatrix.h b/Eigen/src/Core/TriangularMatrix.h index fba07365f..1d6e34650 100644 --- a/Eigen/src/Core/TriangularMatrix.h +++ b/Eigen/src/Core/TriangularMatrix.h @@ -44,29 +44,39 @@ template class TriangularBase : public EigenBase typedef typename internal::traits::DenseMatrixType DenseMatrixType; typedef DenseMatrixType DenseType; + EIGEN_DEVICE_FUNC inline TriangularBase() { eigen_assert(!((Mode&UnitDiag) && (Mode&ZeroDiag))); } + EIGEN_DEVICE_FUNC inline Index rows() const { return derived().rows(); } + EIGEN_DEVICE_FUNC inline Index cols() const { return derived().cols(); } + EIGEN_DEVICE_FUNC inline Index outerStride() const { return derived().outerStride(); } + EIGEN_DEVICE_FUNC inline Index innerStride() const { return derived().innerStride(); } + EIGEN_DEVICE_FUNC inline Scalar coeff(Index row, Index col) const { return derived().coeff(row,col); } + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) { return derived().coeffRef(row,col); } /** \see MatrixBase::copyCoeff(row,col) */ template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void copyCoeff(Index row, Index col, Other& other) { derived().coeffRef(row, col) = other.coeff(row, col); } + EIGEN_DEVICE_FUNC inline Scalar operator()(Index row, Index col) const { check_coordinates(row, col); return coeff(row,col); } + EIGEN_DEVICE_FUNC inline Scalar& operator()(Index row, Index col) { check_coordinates(row, col); @@ -74,15 +84,20 @@ template class TriangularBase : public EigenBase } #ifndef EIGEN_PARSED_BY_DOXYGEN + EIGEN_DEVICE_FUNC inline const Derived& derived() const { return *static_cast(this); } + EIGEN_DEVICE_FUNC inline Derived& derived() { return *static_cast(this); } #endif // not EIGEN_PARSED_BY_DOXYGEN template + EIGEN_DEVICE_FUNC void evalTo(MatrixBase &other) const; template + EIGEN_DEVICE_FUNC void evalToLazy(MatrixBase &other) const; + EIGEN_DEVICE_FUNC DenseMatrixType toDenseMatrix() const { DenseMatrixType res(rows(), cols()); @@ -189,36 +204,52 @@ template class TriangularView | (Mode & (ZeroDiag)) }; + EIGEN_DEVICE_FUNC inline TriangularView(const MatrixType& matrix) : m_matrix(matrix) {} + EIGEN_DEVICE_FUNC inline Index rows() const { return m_matrix.rows(); } + EIGEN_DEVICE_FUNC inline Index cols() const { return m_matrix.cols(); } + EIGEN_DEVICE_FUNC inline Index outerStride() const { return m_matrix.outerStride(); } + EIGEN_DEVICE_FUNC inline Index innerStride() const { return m_matrix.innerStride(); } - /** \sa MatrixBase::operator+=() */ - template TriangularView& operator+=(const DenseBase& other) { return *this = m_matrix + other.derived(); } + /** \sa MatrixBase::operator+=() */ + template + EIGEN_DEVICE_FUNC + TriangularView& operator+=(const DenseBase& other) { return *this = m_matrix + other.derived(); } /** \sa MatrixBase::operator-=() */ - template TriangularView& operator-=(const DenseBase& other) { return *this = m_matrix - other.derived(); } + template + EIGEN_DEVICE_FUNC + TriangularView& operator-=(const DenseBase& other) { return *this = m_matrix - other.derived(); } /** \sa MatrixBase::operator*=() */ + EIGEN_DEVICE_FUNC TriangularView& operator*=(const typename internal::traits::Scalar& other) { return *this = m_matrix * other; } /** \sa MatrixBase::operator/=() */ + EIGEN_DEVICE_FUNC TriangularView& operator/=(const typename internal::traits::Scalar& other) { return *this = m_matrix / other; } /** \sa MatrixBase::fill() */ + EIGEN_DEVICE_FUNC void fill(const Scalar& value) { setConstant(value); } /** \sa MatrixBase::setConstant() */ + EIGEN_DEVICE_FUNC TriangularView& setConstant(const Scalar& value) { return *this = MatrixType::Constant(rows(), cols(), value); } /** \sa MatrixBase::setZero() */ + EIGEN_DEVICE_FUNC TriangularView& setZero() { return setConstant(Scalar(0)); } /** \sa MatrixBase::setOnes() */ + EIGEN_DEVICE_FUNC TriangularView& setOnes() { return setConstant(Scalar(1)); } /** \sa MatrixBase::coeff() * \warning the coordinates must fit into the referenced triangular part */ + EIGEN_DEVICE_FUNC inline Scalar coeff(Index row, Index col) const { Base::check_coordinates_internal(row, col); @@ -228,49 +259,62 @@ template class TriangularView /** \sa MatrixBase::coeffRef() * \warning the coordinates must fit into the referenced triangular part */ + EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) { Base::check_coordinates_internal(row, col); return m_matrix.const_cast_derived().coeffRef(row, col); } + EIGEN_DEVICE_FUNC const MatrixTypeNestedCleaned& nestedExpression() const { return m_matrix; } + EIGEN_DEVICE_FUNC MatrixTypeNestedCleaned& nestedExpression() { return *const_cast(&m_matrix); } /** Assigns a triangular matrix to a triangular part of a dense matrix */ template + EIGEN_DEVICE_FUNC TriangularView& operator=(const TriangularBase& other); template + EIGEN_DEVICE_FUNC TriangularView& operator=(const MatrixBase& other); + EIGEN_DEVICE_FUNC TriangularView& operator=(const TriangularView& other) { return *this = other.nestedExpression(); } template + EIGEN_DEVICE_FUNC void lazyAssign(const TriangularBase& other); template + EIGEN_DEVICE_FUNC void lazyAssign(const MatrixBase& other); /** \sa MatrixBase::conjugate() */ + EIGEN_DEVICE_FUNC inline TriangularView conjugate() { return m_matrix.conjugate(); } /** \sa MatrixBase::conjugate() const */ + EIGEN_DEVICE_FUNC inline const TriangularView conjugate() const { return m_matrix.conjugate(); } /** \sa MatrixBase::adjoint() const */ + EIGEN_DEVICE_FUNC inline const TriangularView adjoint() const { return m_matrix.adjoint(); } /** \sa MatrixBase::transpose() */ + EIGEN_DEVICE_FUNC inline TriangularView,TransposeMode> transpose() { EIGEN_STATIC_ASSERT_LVALUE(MatrixType) return m_matrix.const_cast_derived().transpose(); } /** \sa MatrixBase::transpose() const */ + EIGEN_DEVICE_FUNC inline const TriangularView,TransposeMode> transpose() const { return m_matrix.transpose(); @@ -278,6 +322,7 @@ template class TriangularView /** Efficient triangular matrix times vector/matrix product */ template + EIGEN_DEVICE_FUNC TriangularProduct operator*(const MatrixBase& rhs) const { @@ -288,6 +333,7 @@ template class TriangularView /** Efficient vector/matrix times triangular matrix product */ template friend + EIGEN_DEVICE_FUNC TriangularProduct operator*(const MatrixBase& lhs, const TriangularView& rhs) { @@ -326,26 +372,32 @@ template class TriangularView #endif // EIGEN2_SUPPORT template + EIGEN_DEVICE_FUNC inline const internal::triangular_solve_retval solve(const MatrixBase& other) const; template + EIGEN_DEVICE_FUNC void solveInPlace(const MatrixBase& other) const; template + EIGEN_DEVICE_FUNC inline const internal::triangular_solve_retval solve(const MatrixBase& other) const { return solve(other); } template + EIGEN_DEVICE_FUNC void solveInPlace(const MatrixBase& other) const { return solveInPlace(other); } + EIGEN_DEVICE_FUNC const SelfAdjointView selfadjointView() const { EIGEN_STATIC_ASSERT((Mode&UnitDiag)==0,PROGRAMMING_ERROR); return SelfAdjointView(m_matrix); } + EIGEN_DEVICE_FUNC SelfAdjointView selfadjointView() { EIGEN_STATIC_ASSERT((Mode&UnitDiag)==0,PROGRAMMING_ERROR); @@ -353,18 +405,21 @@ template class TriangularView } template + EIGEN_DEVICE_FUNC void swap(TriangularBase const & other) { TriangularView,Mode>(const_cast(m_matrix)).lazyAssign(other.derived()); } template + EIGEN_DEVICE_FUNC void swap(MatrixBase const & other) { SwapWrapper swaper(const_cast(m_matrix)); TriangularView,Mode>(swaper).lazyAssign(other.derived()); } + EIGEN_DEVICE_FUNC Scalar determinant() const { if (Mode & UnitDiag) @@ -377,6 +432,7 @@ template class TriangularView // TODO simplify the following: template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator=(const ProductBase& other) { setZero(); @@ -384,12 +440,14 @@ template class TriangularView } template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator+=(const ProductBase& other) { return assignProduct(other,1); } template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator-=(const ProductBase& other) { return assignProduct(other,-1); @@ -397,6 +455,7 @@ template class TriangularView template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator=(const ScaledProduct& other) { setZero(); @@ -404,12 +463,14 @@ template class TriangularView } template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator+=(const ScaledProduct& other) { return assignProduct(other,other.alpha()); } template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& operator-=(const ScaledProduct& other) { return assignProduct(other,-other.alpha()); @@ -418,6 +479,7 @@ template class TriangularView protected: template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularView& assignProduct(const ProductBase& prod, const Scalar& alpha); MatrixTypeNested m_matrix; @@ -439,6 +501,7 @@ struct triangular_assignment_selector typedef typename Derived1::Scalar Scalar; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { triangular_assignment_selector::run(dst, src); @@ -467,6 +530,7 @@ struct triangular_assignment_selector template struct triangular_assignment_selector { + EIGEN_DEVICE_FUNC static inline void run(Derived1 &, const Derived2 &) {} }; @@ -475,6 +539,7 @@ struct triangular_assignment_selector struct triangular_assignment_selector { typedef typename Derived1::Index Index; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { for(Index j = 0; j < dst.cols(); ++j) @@ -512,6 +578,7 @@ struct triangular_assignment_selector struct triangular_assignment_selector { typedef typename Derived1::Index Index; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { for(Index j = 0; j < dst.cols(); ++j) @@ -548,6 +616,7 @@ template struct triangular_assignment_selector { typedef typename Derived1::Index Index; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { for(Index j = 0; j < dst.cols(); ++j) @@ -568,6 +637,7 @@ template struct triangular_assignment_selector { typedef typename Derived1::Index Index; + EIGEN_DEVICE_FUNC static inline void run(Derived1 &dst, const Derived2 &src) { for(Index j = 0; j < dst.cols(); ++j) diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h index 71d587108..aea168b46 100644 --- a/Eigen/src/Core/util/Meta.h +++ b/Eigen/src/Core/util/Meta.h @@ -88,7 +88,31 @@ template struct enable_if; template struct enable_if { typedef T type; }; +#if defined(__CUDA_ARCH__) +template EIGEN_DEVICE_FUNC void swap(T &a, T &b) { T tmp = b; b = a; a = tmp; } +namespace device { +template struct numeric_limits +{ + EIGEN_DEVICE_FUNC + static T epsilon() { return 0; } +}; +template<> struct numeric_limits +{ + EIGEN_DEVICE_FUNC + static float epsilon() { return __FLT_EPSILON__; } +}; +template<> struct numeric_limits +{ + EIGEN_DEVICE_FUNC + static double epsilon() { return __DBL_EPSILON__; } +}; + +} + +#else +template EIGEN_STRONG_INLINE void swap(T &a, T &b) { std::swap(a,b); } +#endif /** \internal * A base class do disable default copy ctor and copy assignement operator. diff --git a/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h b/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h index 3993046a8..23a334b5c 100644 --- a/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h +++ b/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h @@ -109,6 +109,7 @@ template class SelfAdjointEigenSolver * Example: \include SelfAdjointEigenSolver_SelfAdjointEigenSolver.cpp * Output: \verbinclude SelfAdjointEigenSolver_SelfAdjointEigenSolver.out */ + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver() : m_eivec(), m_eivalues(), @@ -128,6 +129,7 @@ template class SelfAdjointEigenSolver * * \sa compute() for an example */ + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver(Index size) : m_eivec(size, size), m_eivalues(size), @@ -150,6 +152,7 @@ template class SelfAdjointEigenSolver * * \sa compute(const MatrixType&, int) */ + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver(const MatrixType& matrix, int options = ComputeEigenvectors) : m_eivec(matrix.rows(), matrix.cols()), m_eivalues(matrix.cols()), @@ -189,6 +192,7 @@ template class SelfAdjointEigenSolver * * \sa SelfAdjointEigenSolver(const MatrixType&, int) */ + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver& compute(const MatrixType& matrix, int options = ComputeEigenvectors); /** \brief Computes eigendecomposition of given matrix using a direct algorithm @@ -205,6 +209,7 @@ template class SelfAdjointEigenSolver * * \sa compute(const MatrixType&, int options) */ + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver& computeDirect(const MatrixType& matrix, int options = ComputeEigenvectors); /** \brief Returns the eigenvectors of given matrix. @@ -225,6 +230,7 @@ template class SelfAdjointEigenSolver * * \sa eigenvalues() */ + EIGEN_DEVICE_FUNC const MatrixType& eigenvectors() const { eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized."); @@ -247,6 +253,7 @@ template class SelfAdjointEigenSolver * * \sa eigenvectors(), MatrixBase::eigenvalues() */ + EIGEN_DEVICE_FUNC const RealVectorType& eigenvalues() const { eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized."); @@ -271,6 +278,7 @@ template class SelfAdjointEigenSolver * \sa operatorInverseSqrt(), * \ref MatrixFunctions_Module "MatrixFunctions Module" */ + EIGEN_DEVICE_FUNC MatrixType operatorSqrt() const { eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized."); @@ -296,6 +304,7 @@ template class SelfAdjointEigenSolver * \sa operatorSqrt(), MatrixBase::inverse(), * \ref MatrixFunctions_Module "MatrixFunctions Module" */ + EIGEN_DEVICE_FUNC MatrixType operatorInverseSqrt() const { eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized."); @@ -307,6 +316,7 @@ template class SelfAdjointEigenSolver * * \returns \c Success if computation was succesful, \c NoConvergence otherwise. */ + EIGEN_DEVICE_FUNC ComputationInfo info() const { eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized."); @@ -321,6 +331,7 @@ template class SelfAdjointEigenSolver static const int m_maxIterations = 30; #ifdef EIGEN2_SUPPORT + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver(const MatrixType& matrix, bool computeEigenvectors) : m_eivec(matrix.rows(), matrix.cols()), m_eivalues(matrix.cols()), @@ -330,6 +341,7 @@ template class SelfAdjointEigenSolver compute(matrix, computeEigenvectors); } + EIGEN_DEVICE_FUNC SelfAdjointEigenSolver(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true) : m_eivec(matA.cols(), matA.cols()), m_eivalues(matA.cols()), @@ -339,11 +351,13 @@ template class SelfAdjointEigenSolver static_cast*>(this)->compute(matA, matB, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly); } + EIGEN_DEVICE_FUNC void compute(const MatrixType& matrix, bool computeEigenvectors) { compute(matrix, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly); } + EIGEN_DEVICE_FUNC void compute(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true) { compute(matA, matB, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly); @@ -377,10 +391,12 @@ template class SelfAdjointEigenSolver */ namespace internal { template +EIGEN_DEVICE_FUNC static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end, Scalar* matrixQ, Index n); } template +EIGEN_DEVICE_FUNC SelfAdjointEigenSolver& SelfAdjointEigenSolver ::compute(const MatrixType& matrix, int options) { @@ -481,6 +497,7 @@ namespace internal { template struct direct_selfadjoint_eigenvalues { + EIGEN_DEVICE_FUNC static inline void run(SolverType& eig, const typename SolverType::MatrixType& A, int options) { eig.compute(A,options); } }; @@ -491,12 +508,13 @@ template struct direct_selfadjoint_eigenvalues struct direct_selfadjoint_eigenvalues= roots(1)) - std::swap(roots(0),roots(1)); + internal::swap(roots(0),roots(1)); if (roots(1) >= roots(2)) { - std::swap(roots(1),roots(2)); + internal::swap(roots(1),roots(2)); if (roots(0) >= roots(1)) - std::swap(roots(0),roots(1)); + internal::swap(roots(0),roots(1)); } } + EIGEN_DEVICE_FUNC static inline void run(SolverType& solver, const MatrixType& mat, int options) { using std::sqrt; @@ -660,12 +679,14 @@ template struct direct_selfadjoint_eigenvalues struct direct_selfadjoint_eigenvalues +template +struct direct_selfadjoint_eigenvalues { typedef typename SolverType::MatrixType MatrixType; typedef typename SolverType::RealVectorType VectorType; typedef typename SolverType::Scalar Scalar; + EIGEN_DEVICE_FUNC static inline void computeRoots(const MatrixType& m, VectorType& roots) { using std::sqrt; @@ -675,6 +696,7 @@ template struct direct_selfadjoint_eigenvalues struct direct_selfadjoint_eigenvalues +EIGEN_DEVICE_FUNC SelfAdjointEigenSolver& SelfAdjointEigenSolver ::computeDirect(const MatrixType& matrix, int options) { @@ -737,6 +760,7 @@ SelfAdjointEigenSolver& SelfAdjointEigenSolver namespace internal { template +EIGEN_DEVICE_FUNC static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end, Scalar* matrixQ, Index n) { using std::abs; diff --git a/Eigen/src/Geometry/OrthoMethods.h b/Eigen/src/Geometry/OrthoMethods.h index 556bc8160..26be3ee5b 100644 --- a/Eigen/src/Geometry/OrthoMethods.h +++ b/Eigen/src/Geometry/OrthoMethods.h @@ -132,6 +132,7 @@ struct unitOrthogonal_selector typedef typename NumTraits::Real RealScalar; typedef typename Derived::Index Index; typedef Matrix Vector2; + EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) { VectorType perp = VectorType::Zero(src.size()); @@ -154,6 +155,7 @@ struct unitOrthogonal_selector typedef typename plain_matrix_type::type VectorType; typedef typename traits::Scalar Scalar; typedef typename NumTraits::Real RealScalar; + EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) { VectorType perp; @@ -192,6 +194,7 @@ template struct unitOrthogonal_selector { typedef typename plain_matrix_type::type VectorType; + EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) { return VectorType(-numext::conj(src.y()), numext::conj(src.x())).normalized(); } };