From 249dc4f482f0427af3cb1e53b7a2ff4bbe6b54cf Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Thu, 3 Apr 2008 16:54:19 +0000 Subject: current state of the mess. One line fails in the tests, and useless copies are made when evaluating nested expressions. Changes: - kill LazyBit, introduce EvalBeforeNestingBit and EvalBeforeAssigningBit - product and random don't evaluate immediately anymore - eval() always evaluates - change the value of Dynamic to some large positive value, in preparation of future simplifications --- Eigen/src/Core/CommaInitializer.h | 7 +++-- Eigen/src/Core/ForwardDeclarations.h | 34 +++++++++++++-------- Eigen/src/Core/Lazy.h | 2 +- Eigen/src/Core/MatrixBase.h | 13 ++++---- Eigen/src/Core/Product.h | 58 ++++++++++++------------------------ Eigen/src/Core/Random.h | 16 +++++----- Eigen/src/Core/Util.h | 9 ++++-- 7 files changed, 67 insertions(+), 72 deletions(-) (limited to 'Eigen/src') diff --git a/Eigen/src/Core/CommaInitializer.h b/Eigen/src/Core/CommaInitializer.h index d7a5fcb47..21551ccf2 100644 --- a/Eigen/src/Core/CommaInitializer.h +++ b/Eigen/src/Core/CommaInitializer.h @@ -76,9 +76,10 @@ struct MatrixBase::CommaInitializer ei_assert(m_col0 && OtherDerived::ColsAtCompileTime>0) - m_matrix.block< (OtherDerived::RowsAtCompileTime>0?OtherDerived::RowsAtCompileTime:1) , - (OtherDerived::ColsAtCompileTime>0?OtherDerived::ColsAtCompileTime:1) >(m_row, m_col) = other; + if (OtherDerived::SizeAtCompileTime != Dynamic) + m_matrix.block + (m_row, m_col) = other; else m_matrix.block(m_row, m_col, other.rows(), other.cols()) = other; m_col += other.cols(); diff --git a/Eigen/src/Core/ForwardDeclarations.h b/Eigen/src/Core/ForwardDeclarations.h index ea0dd9502..e26704c07 100644 --- a/Eigen/src/Core/ForwardDeclarations.h +++ b/Eigen/src/Core/ForwardDeclarations.h @@ -69,40 +69,50 @@ template struct ei_scalar_quotient1_op; template struct ei_scalar_min_op; template struct ei_scalar_max_op; -template struct ei_xpr_copy +template struct ei_copy_unless_matrix { typedef T type; }; template -struct ei_xpr_copy > +struct ei_copy_unless_matrix > { typedef const Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> & type; }; +template struct ei_xpr_copy +{ + typedef typename ei_meta_if::type + >::ret type; +}; + template struct ei_eval { typedef Matrix::Scalar, ei_traits::RowsAtCompileTime, ei_traits::ColsAtCompileTime, - ei_traits::Flags & ~LazyBit, // unset lazy bit after evaluation + ei_traits::Flags & ~(EvalBeforeNestingBit | EvalBeforeAssigningBit), ei_traits::MaxRowsAtCompileTime, ei_traits::MaxColsAtCompileTime> type; }; -template struct ei_eval_if_expensive +template struct ei_eval_temporary { - enum { eval = n * NumTraits::ReadCost < (n-1) * T::CoeffReadCost }; - typedef typename ei_meta_if::ret type; - typedef typename ei_meta_if::ret reftype; + typedef Matrix::Scalar, + ei_traits::RowsAtCompileTime, + ei_traits::ColsAtCompileTime, + (ei_traits::Flags | TemporaryBit) & ~(EvalBeforeNestingBit | EvalBeforeAssigningBit), + ei_traits::MaxRowsAtCompileTime, + ei_traits::MaxColsAtCompileTime> type; }; -template struct ei_eval_unless_lazy +template struct ei_eval_if_needed_before_nesting { - typedef typename ei_meta_if::Flags & LazyBit, - T, - typename ei_eval::type - >::ret type; + enum { eval = T::Flags & EvalBeforeNestingBit + || n * NumTraits::ReadCost < (n-1) * T::CoeffReadCost }; + typedef typename ei_meta_if::type, T>::ret type; }; #endif // EIGEN_FORWARDDECLARATIONS_H diff --git a/Eigen/src/Core/Lazy.h b/Eigen/src/Core/Lazy.h index 0968e254f..aacc61695 100644 --- a/Eigen/src/Core/Lazy.h +++ b/Eigen/src/Core/Lazy.h @@ -46,7 +46,7 @@ struct ei_traits > ColsAtCompileTime = ExpressionType::ColsAtCompileTime, MaxRowsAtCompileTime = ExpressionType::MaxRowsAtCompileTime, MaxColsAtCompileTime = ExpressionType::MaxColsAtCompileTime, - Flags = ExpressionType::Flags | LazyBit, + Flags = ExpressionType::Flags & ~(EvalBeforeNestingBit | EvalBeforeAssigningBit), CoeffReadCost = ExpressionType::CoeffReadCost }; }; diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 77ecb2906..d0187df8f 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -252,7 +252,8 @@ template class MatrixBase */ //@{ template - const typename ei_eval_unless_lazy >::type + const Product::type, + typename ei_eval_if_needed_before_nesting::ColsAtCompileTime>::type> operator*(const MatrixBase &other) const; template @@ -312,9 +313,9 @@ template class MatrixBase /// \name Generating special matrices //@{ - static const typename ei_eval_unless_lazy >::type random(int rows, int cols); - static const typename ei_eval_unless_lazy >::type random(int size); - static const typename ei_eval_unless_lazy >::type random(); + static const Random random(int rows, int cols); + static const Random random(int size); + static const Random random(); static const Zero zero(int rows, int cols); static const Zero zero(int size); static const Zero zero(); @@ -358,9 +359,9 @@ template class MatrixBase template const CwiseUnaryOp::Scalar, NewType>, Derived> cast() const; - const typename ei_eval_unless_lazy::type eval() const EIGEN_ALWAYS_INLINE + const typename ei_eval::type eval() const EIGEN_ALWAYS_INLINE { - return typename ei_eval_unless_lazy::type(derived()); + return typename ei_eval::type(derived()); } template diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index 6f21292b1..93c064322 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -74,37 +74,26 @@ struct ei_product_unroller * * \sa class Sum, class Difference */ +template struct ei_product_eval_mode +{ + enum{ value = Lhs::MaxRowsAtCompileTime >= 8 && Rhs::MaxColsAtCompileTime >= 8 + ? CacheOptimal : UnrolledDotProduct }; +}; + template struct ei_traits > { typedef typename Lhs::Scalar Scalar; -#if 0 - typedef typename ei_meta_if< - (int)NumTraits::ReadCost < (int)Lhs::CoeffReadCost, - typename Lhs::Eval, - Lhs>::ret ActualLhs; - typedef typename ei_meta_if< - (int)NumTraits::ReadCost < (int)Lhs::CoeffReadCost, - typename Lhs::Eval, - typename Lhs::XprCopy>::ret ActualLhsXprCopy; - - typedef typename ei_meta_if< - (int)NumTraits::ReadCost < (int)Rhs::CoeffReadCost, - typename Rhs::Eval, - Rhs>::ret ActualRhs; - typedef typename ei_meta_if< - (int)NumTraits::ReadCost < (int)Rhs::CoeffReadCost, - typename Rhs::Eval, - typename Rhs::XprCopy>::ret ActualRhsXprCopy; -#endif enum { RowsAtCompileTime = Lhs::RowsAtCompileTime, ColsAtCompileTime = Rhs::ColsAtCompileTime, MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime, MaxColsAtCompileTime = Rhs::MaxColsAtCompileTime, - Flags = (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic) - ? (unsigned int)(Lhs::Flags | Rhs::Flags) - : (unsigned int)(Lhs::Flags | Rhs::Flags) & ~LargeBit, + Flags = ( (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic) + ? (unsigned int)(Lhs::Flags | Rhs::Flags) + : (unsigned int)(Lhs::Flags | Rhs::Flags) & ~LargeBit ) + | EvalBeforeAssigningBit + | (ei_product_eval_mode::value == (int)CacheOptimal ? EvalBeforeNestingBit : 0), CoeffReadCost = Lhs::ColsAtCompileTime == Dynamic ? Dynamic @@ -114,25 +103,15 @@ struct ei_traits > }; }; -template struct ei_product_eval_mode -{ - enum{ value = Lhs::MaxRowsAtCompileTime == Dynamic || Rhs::MaxColsAtCompileTime == Dynamic - ? CacheOptimal : UnrolledDotProduct }; -}; - template class Product : ei_no_assignment_operator, public MatrixBase > { public: EIGEN_GENERIC_PUBLIC_INTERFACE(Product) -#if 0 - typedef typename ei_traits::ActualLhs ActualLhs; - typedef typename ei_traits::ActualRhs ActualRhs; - typedef typename ei_traits::ActualLhsXprCopy ActualLhsXprCopy; - typedef typename ei_traits::ActualRhsXprCopy ActualRhsXprCopy; -#endif - Product(const Lhs& lhs, const Rhs& rhs) + + template + Product(const ArgLhs& lhs, const ArgRhs& rhs) : m_lhs(lhs), m_rhs(rhs) { ei_assert(lhs.cols() == rhs.rows()); @@ -181,13 +160,14 @@ template class Product : ei_no_assignm */ template template -const typename ei_eval_unless_lazy >::type +const Product::type, + typename ei_eval_if_needed_before_nesting::ColsAtCompileTime>::type> MatrixBase::operator*(const MatrixBase &other) const { - typedef ei_eval_if_expensive Lhs; - typedef ei_eval_if_expensive Rhs; + typedef ei_eval_if_needed_before_nesting Lhs; + typedef ei_eval_if_needed_before_nesting Rhs; return Product - (typename Lhs::reftype(derived()), typename Rhs::reftype(other.derived())).eval(); + (derived(), other.derived()); } /** replaces \c *this by \c *this * \a other. diff --git a/Eigen/src/Core/Random.h b/Eigen/src/Core/Random.h index 4bd482958..7ac3633fe 100644 --- a/Eigen/src/Core/Random.h +++ b/Eigen/src/Core/Random.h @@ -41,7 +41,7 @@ struct ei_traits > ColsAtCompileTime = ei_traits::ColsAtCompileTime, MaxRowsAtCompileTime = ei_traits::MaxRowsAtCompileTime, MaxColsAtCompileTime = ei_traits::MaxColsAtCompileTime, - Flags = ei_traits::Flags, + Flags = ei_traits::Flags | EvalBeforeNestingBit, CoeffReadCost = 2 * NumTraits::MulCost // FIXME: arbitrary value }; }; @@ -93,10 +93,10 @@ template class Random : ei_no_assignment_operator, * \sa ei_random(), ei_random(int) */ template -const typename ei_eval_unless_lazy >::type +const Random MatrixBase::random(int rows, int cols) { - return Random(rows, cols).eval(); + return Random(rows, cols); } /** \returns a random vector (not an expression, the vector is immediately evaluated). @@ -116,12 +116,12 @@ MatrixBase::random(int rows, int cols) * \sa ei_random(), ei_random(int,int) */ template -const typename ei_eval_unless_lazy >::type +const Random MatrixBase::random(int size) { ei_assert(IsVectorAtCompileTime); - if(RowsAtCompileTime == 1) return Random(1, size).eval(); - else return Random(size, 1).eval(); + if(RowsAtCompileTime == 1) return Random(1, size); + else return Random(size, 1); } /** \returns a fixed-size random matrix or vector @@ -136,10 +136,10 @@ MatrixBase::random(int size) * \sa ei_random(int), ei_random(int,int) */ template -const typename ei_eval_unless_lazy >::type +const Random MatrixBase::random() { - return Random(RowsAtCompileTime, ColsAtCompileTime).eval(); + return Random(RowsAtCompileTime, ColsAtCompileTime); } /** Sets all coefficients in this expression to random values. diff --git a/Eigen/src/Core/Util.h b/Eigen/src/Core/Util.h index 6166394ee..132c0ced8 100644 --- a/Eigen/src/Core/Util.h +++ b/Eigen/src/Core/Util.h @@ -127,12 +127,15 @@ friend class Eigen::MatrixBase; #define EIGEN_ENUM_MIN(a,b) (((int)a <= (int)b) ? (int)a : (int)b) -const int Dynamic = -10; +const int Dynamic = 10000; // matrix/expression flags const unsigned int RowMajorBit = 0x1; -const unsigned int LazyBit = 0x2; -const unsigned int LargeBit = 0x4; +const unsigned int EvalBeforeNestingBit = 0x2; +const unsigned int EvalBeforeAssigningBit = 0x4; +const unsigned int LargeBit = 0x8; +const unsigned int TemporaryBit = 0x10; + enum { ConditionalJumpCost = 5 }; -- cgit v1.2.3