finish making use of CoeffReadCost and the new XprCopy everywhere

seems appropriate to me.
This commit is contained in:
Benoit Jacob 2008-04-08 14:15:01 +00:00
parent 371d302efb
commit 4920f2011e
4 changed files with 75 additions and 38 deletions

View File

@ -72,18 +72,25 @@ template<typename OtherDerived>
typename ei_traits<Derived>::Scalar typename ei_traits<Derived>::Scalar
MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
{ {
typename Derived::XprCopy xprCopy(derived()); typedef typename Derived::XprCopy XprCopy;
typename OtherDerived::XprCopy otherXprCopy(other.derived()); typedef typename OtherDerived::XprCopy OtherXprCopy;
typedef typename ei_unref<XprCopy>::type _XprCopy;
typedef typename ei_unref<OtherXprCopy>::type _OtherXprCopy;
XprCopy xprCopy(derived());
OtherXprCopy otherXprCopy(other.derived());
ei_assert(IsVectorAtCompileTime ei_assert(_XprCopy::IsVectorAtCompileTime
&& OtherDerived::IsVectorAtCompileTime && _OtherXprCopy::IsVectorAtCompileTime
&& xprCopy.size() == otherXprCopy.size()); && xprCopy.size() == otherXprCopy.size());
Scalar res; Scalar res;
if(SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) const bool unroll = SizeAtCompileTime
* (_XprCopy::CoeffReadCost + _OtherXprCopy::CoeffReadCost + NumTraits<Scalar>::MulCost)
+ (SizeAtCompileTime - 1) * NumTraits<Scalar>::AddCost
<= EIGEN_UNROLLING_LIMIT;
if(unroll)
ei_dot_unroller<SizeAtCompileTime-1, ei_dot_unroller<SizeAtCompileTime-1,
SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT ? SizeAtCompileTime : Dynamic, unroll ? SizeAtCompileTime : Dynamic,
typename ei_unref<typename Derived::XprCopy>::type, _XprCopy, _OtherXprCopy>
typename ei_unref<typename OtherDerived::XprCopy>::type>
::run(xprCopy, otherXprCopy, res); ::run(xprCopy, otherXprCopy, res);
else else
{ {
@ -142,8 +149,8 @@ template<typename OtherDerived>
bool MatrixBase<Derived>::isOrtho bool MatrixBase<Derived>::isOrtho
(const MatrixBase<OtherDerived>& other, RealScalar prec) const (const MatrixBase<OtherDerived>& other, RealScalar prec) const
{ {
typename Derived::XprCopy xprCopy(derived()); typename ei_xpr_copy<Derived,2>::type xprCopy(derived());
typename OtherDerived::XprCopy otherXprCopy(other.derived()); typename ei_xpr_copy<OtherDerived,2>::type otherXprCopy(other.derived());
return ei_abs2(xprCopy.dot(otherXprCopy)) <= prec * prec * xprCopy.norm2() * otherXprCopy.norm2(); return ei_abs2(xprCopy.dot(otherXprCopy)) <= prec * prec * xprCopy.norm2() * otherXprCopy.norm2();
} }

View File

@ -86,13 +86,13 @@ struct ei_traits<Product<Lhs, Rhs, EvalMode> >
typedef typename Lhs::Scalar Scalar; typedef typename Lhs::Scalar Scalar;
typedef typename ei_xpr_copy<Lhs,Rhs::ColsAtCompileTime>::type LhsXprCopy; typedef typename ei_xpr_copy<Lhs,Rhs::ColsAtCompileTime>::type LhsXprCopy;
typedef typename ei_xpr_copy<Rhs,Lhs::RowsAtCompileTime>::type RhsXprCopy; typedef typename ei_xpr_copy<Rhs,Lhs::RowsAtCompileTime>::type RhsXprCopy;
typedef typename ei_unref<LhsXprCopy>::type ActualLhs; typedef typename ei_unref<LhsXprCopy>::type _LhsXprCopy;
typedef typename ei_unref<RhsXprCopy>::type ActualRhs; typedef typename ei_unref<RhsXprCopy>::type _RhsXprCopy;
enum { enum {
LhsCoeffReadCost = ActualLhs::CoeffReadCost, LhsCoeffReadCost = _LhsXprCopy::CoeffReadCost,
RhsCoeffReadCost = ActualRhs::CoeffReadCost, RhsCoeffReadCost = _RhsXprCopy::CoeffReadCost,
LhsFlags = ActualLhs::Flags, LhsFlags = _LhsXprCopy::Flags,
RhsFlags = ActualRhs::Flags, RhsFlags = _RhsXprCopy::Flags,
RowsAtCompileTime = Lhs::RowsAtCompileTime, RowsAtCompileTime = Lhs::RowsAtCompileTime,
ColsAtCompileTime = Rhs::ColsAtCompileTime, ColsAtCompileTime = Rhs::ColsAtCompileTime,
MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime, MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime,
@ -117,9 +117,10 @@ template<typename Lhs, typename Rhs, int EvalMode> class Product : ei_no_assignm
public: public:
EIGEN_GENERIC_PUBLIC_INTERFACE(Product) EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
typedef typename ei_traits<Product>::LhsXprCopy LhsXprCopy; typedef typename ei_traits<Product>::LhsXprCopy LhsXprCopy;
typedef typename ei_traits<Product>::RhsXprCopy RhsXprCopy; typedef typename ei_traits<Product>::RhsXprCopy RhsXprCopy;
typedef typename ei_traits<Product>::_LhsXprCopy _LhsXprCopy;
typedef typename ei_traits<Product>::_RhsXprCopy _RhsXprCopy;
Product(const Lhs& lhs, const Rhs& rhs) Product(const Lhs& lhs, const Rhs& rhs)
: m_lhs(lhs), m_rhs(rhs) : m_lhs(lhs), m_rhs(rhs)
@ -144,8 +145,7 @@ template<typename Lhs, typename Rhs, int EvalMode> class Product : ei_no_assignm
{ {
ei_product_unroller<Lhs::ColsAtCompileTime-1, ei_product_unroller<Lhs::ColsAtCompileTime-1,
unroll ? Lhs::ColsAtCompileTime : Dynamic, unroll ? Lhs::ColsAtCompileTime : Dynamic,
typename ei_unref<LhsXprCopy>::type, _LhsXprCopy, _RhsXprCopy>
typename ei_unref<RhsXprCopy>::type>
::run(row, col, m_lhs, m_rhs, res); ::run(row, col, m_lhs, m_rhs, res);
} }
else else

View File

@ -87,15 +87,19 @@ struct ei_traits<PartialRedux<Direction, BinaryOp, MatrixType> >
typedef typename ei_result_of< typedef typename ei_result_of<
BinaryOp(typename MatrixType::Scalar) BinaryOp(typename MatrixType::Scalar)
>::type Scalar; >::type Scalar;
typedef typename ei_xpr_copy<MatrixType>::type MatrixTypeXprCopy;
typedef typename ei_unref<MatrixTypeXprCopy>::type _MatrixTypeXprCopy;
enum { enum {
RowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::RowsAtCompileTime, RowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::RowsAtCompileTime,
ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime, ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxRowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::MaxRowsAtCompileTime,
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime, MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
Flags = (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic) Flags = (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic)
? (unsigned int)MatrixType::Flags ? (unsigned int)_MatrixTypeXprCopy::Flags
: (unsigned int)MatrixType::Flags & ~LargeBit, : (unsigned int)_MatrixTypeXprCopy::Flags & ~LargeBit,
CoeffReadCost = 1 //FIXME -- unimplemented! TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime,
CoeffReadCost = TraversalSize * _MatrixTypeXprCopy::CoeffReadCost
+ (TraversalSize - 1) * ei_functor_traits<BinaryOp>::Cost
}; };
}; };
@ -106,6 +110,8 @@ class PartialRedux : ei_no_assignment_operator,
public: public:
EIGEN_GENERIC_PUBLIC_INTERFACE(PartialRedux) EIGEN_GENERIC_PUBLIC_INTERFACE(PartialRedux)
typedef typename ei_traits<PartialRedux>::MatrixTypeXprCopy MatrixTypeXprCopy;
typedef typename ei_traits<PartialRedux>::_MatrixTypeXprCopy _MatrixTypeXprCopy;
PartialRedux(const MatrixType& mat, const BinaryOp& func = BinaryOp()) PartialRedux(const MatrixType& mat, const BinaryOp& func = BinaryOp())
: m_matrix(mat), m_functor(func) {} : m_matrix(mat), m_functor(func) {}
@ -124,7 +130,7 @@ class PartialRedux : ei_no_assignment_operator,
} }
protected: protected:
const typename MatrixType::XprCopy m_matrix; const MatrixTypeXprCopy m_matrix;
const BinaryOp m_functor; const BinaryOp m_functor;
}; };
@ -171,10 +177,13 @@ template<typename BinaryOp>
typename ei_result_of<BinaryOp(typename ei_traits<Derived>::Scalar)>::type typename ei_result_of<BinaryOp(typename ei_traits<Derived>::Scalar)>::type
MatrixBase<Derived>::redux(const BinaryOp& func) const MatrixBase<Derived>::redux(const BinaryOp& func) const
{ {
if(SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) const bool unroll = SizeAtCompileTime * CoeffReadCost
+ (SizeAtCompileTime-1) * ei_functor_traits<BinaryOp>::Cost
<= EIGEN_UNROLLING_LIMIT;
if(unroll)
return ei_redux_unroller<BinaryOp, Derived, 0, return ei_redux_unroller<BinaryOp, Derived, 0,
(SizeAtCompileTime>0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? unroll ? SizeAtCompileTime : Dynamic>
SizeAtCompileTime : Dynamic>::run(derived(), func); ::run(derived(), func);
else else
{ {
Scalar res; Scalar res;
@ -291,10 +300,12 @@ struct ei_any_unroller<Derived, Dynamic>
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::all(void) const bool MatrixBase<Derived>::all(void) const
{ {
if(SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost)
<= EIGEN_UNROLLING_LIMIT;
if(unroll)
return ei_all_unroller<Derived, return ei_all_unroller<Derived,
(SizeAtCompileTime>0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? unroll ? SizeAtCompileTime : Dynamic
SizeAtCompileTime : Dynamic>::run(derived()); >::run(derived());
else else
{ {
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
@ -311,10 +322,12 @@ bool MatrixBase<Derived>::all(void) const
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::any(void) const bool MatrixBase<Derived>::any(void) const
{ {
if(SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost)
<= EIGEN_UNROLLING_LIMIT;
if(unroll)
return ei_any_unroller<Derived, return ei_any_unroller<Derived,
(SizeAtCompileTime>0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? unroll ? SizeAtCompileTime : Dynamic
SizeAtCompileTime : Dynamic>::run(derived()); >::run(derived());
else else
{ {
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)

View File

@ -74,10 +74,13 @@ template<typename Derived>
template<typename Visitor> template<typename Visitor>
void MatrixBase<Derived>::visit(Visitor& visitor) const void MatrixBase<Derived>::visit(Visitor& visitor) const
{ {
if(SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) const bool unroll = SizeAtCompileTime * CoeffReadCost
+ (SizeAtCompileTime-1) * ei_functor_traits<Visitor>::Cost
<= EIGEN_UNROLLING_LIMIT;
if(unroll)
return ei_visitor_unroller<Visitor, Derived, return ei_visitor_unroller<Visitor, Derived,
(SizeAtCompileTime>0 && SizeAtCompileTime <= EIGEN_UNROLLING_LIMIT) ? unroll ? SizeAtCompileTime : Dynamic
SizeAtCompileTime : Dynamic>::run(derived(), visitor); >::run(derived(), visitor);
else else
{ {
visitor.init(coeff(0,0), 0, 0); visitor.init(coeff(0,0), 0, 0);
@ -124,6 +127,13 @@ struct ei_min_coeff_visitor : ei_coeff_visitor<Scalar>
} }
}; };
template<typename Scalar>
struct ei_functor_traits<ei_min_coeff_visitor<Scalar> > {
enum {
Cost = NumTraits<Scalar>::AddCost
};
};
/** \internal /** \internal
* \brief Visitor computing the max coefficient with its value and coordinates * \brief Visitor computing the max coefficient with its value and coordinates
* *
@ -143,6 +153,13 @@ struct ei_max_coeff_visitor : ei_coeff_visitor<Scalar>
} }
}; };
template<typename Scalar>
struct ei_functor_traits<ei_max_coeff_visitor<Scalar> > {
enum {
Cost = NumTraits<Scalar>::AddCost
};
};
/** \returns the minimum of all coefficients of *this /** \returns the minimum of all coefficients of *this
* and puts in *row and *col its location. * and puts in *row and *col its location.
* *