hehe, the complicated nesting scheme in Flagged in the previous commit

was a sign that we were doing something wrong. In fact, having
NestByValue as a special case of Flagged was wrong, and the previous
commit, while not buggy, was inefficient because then when the resulting
NestByValue xpr was nested -- hence copied -- the original xpr which was
already nested by value was copied again; hence instead of 1 copy we got
3 copies.
The solution was to ressuscitate the old Temporary.h (renamed
NestByValue.h) as it was the right approach.
This commit is contained in:
Benoit Jacob 2008-05-28 05:14:16 +00:00
parent aebecae510
commit f54760c889
10 changed files with 108 additions and 24 deletions

View File

@ -30,6 +30,7 @@ namespace Eigen {
#include "src/Core/Coeffs.h"
#include "src/Core/Assign.h"
#include "src/Core/MatrixStorage.h"
#include "src/Core/NestByValue.h"
#include "src/Core/Flagged.h"
#include "src/Core/Matrix.h"
#include "src/Core/CwiseBinaryOp.h"

View File

@ -94,11 +94,7 @@ template<typename ExpressionType, unsigned int Added, unsigned int Removed> clas
}
protected:
const typename ei_meta_if<
Added & ~Removed & NestByValueBit,
ExpressionType,
typename ExpressionType::Nested
>::ret m_matrix;
typename ExpressionType::Nested m_matrix;
};
/** \returns an expression of *this with added flags
@ -121,13 +117,4 @@ MatrixBase<Derived>::lazy() const
return derived();
}
/** \returns an expression of *this with the NestByValueBit flag added.
*/
template<typename Derived>
inline const Flagged<Derived, NestByValueBit, 0>
MatrixBase<Derived>::nestByValue() const
{
return derived();
}
#endif // EIGEN_FLAGGED_H

View File

@ -305,8 +305,8 @@ template<typename Derived> class MatrixBase
Transpose<Derived> transpose();
const Transpose<Derived> transpose() const;
const Transpose<
Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived>
, NestByValueBit, 0> >
NestByValue<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived> >
>
adjoint() const;
//@}
@ -452,7 +452,6 @@ template<typename Derived> class MatrixBase
template<unsigned int Added>
const Flagged<Derived, Added, 0> marked() const;
const Flagged<Derived, 0, EvalBeforeNestingBit | EvalBeforeAssigningBit> lazy() const;
const Flagged<Derived, NestByValueBit, 0> nestByValue() const;
/** \returns number of elements to skip to pass from one row (resp. column) to another
* for a row-major (resp. column-major) matrix.
@ -460,6 +459,8 @@ template<typename Derived> class MatrixBase
* of the underlying matrix.
*/
inline int stride(void) const { return derived()._stride(); }
inline const NestByValue<Derived> nestByValue() const;
//@}
/// \name Coefficient-wise operations

View File

@ -0,0 +1,93 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.
#ifndef EIGEN_NESTBYVALUE_H
#define EIGEN_NESTBYVALUE_H
/** \class NestByValue
*
* \brief Expression which must be nested by value
*
* \param ExpressionType the type of the object of which we are requiring nesting-by-value
*
* This class is the return type of MatrixBase::nestByValue()
* and most of the time this is the only way it is used.
*
* \sa MatrixBase::nestByValue()
*/
template<typename ExpressionType>
struct ei_traits<NestByValue<ExpressionType> >
{
typedef typename ExpressionType::Scalar Scalar;
enum {
RowsAtCompileTime = ExpressionType::RowsAtCompileTime,
ColsAtCompileTime = ExpressionType::ColsAtCompileTime,
MaxRowsAtCompileTime = ExpressionType::MaxRowsAtCompileTime,
MaxColsAtCompileTime = ExpressionType::MaxColsAtCompileTime,
Flags = ExpressionType::Flags,
CoeffReadCost = ExpressionType::CoeffReadCost
};
};
template<typename ExpressionType> class NestByValue
: public MatrixBase<NestByValue<ExpressionType> >
{
public:
EIGEN_GENERIC_PUBLIC_INTERFACE(NestByValue)
inline NestByValue(const ExpressionType& matrix) : m_expression(matrix) {}
private:
inline int _rows() const { return m_expression.rows(); }
inline int _cols() const { return m_expression.cols(); }
inline int _stride() const { return m_expression.stride(); }
inline const Scalar _coeff(int row, int col) const
{
return m_expression.coeff(row, col);
}
template<int LoadMode>
inline const PacketScalar _packetCoeff(int row, int col) const
{
return m_expression.template packetCoeff<LoadMode>(row, col);
}
protected:
const ExpressionType m_expression;
};
/** \returns an expression of the temporary version of *this.
*/
template<typename Derived>
inline const NestByValue<Derived>
MatrixBase<Derived>::nestByValue() const
{
return NestByValue<Derived>(derived());
}
#endif // EIGEN_NESTBYVALUE_H

View File

@ -165,7 +165,7 @@ template<typename T> class ei_product_eval_to_column_major
template<typename T, int n=1> struct ei_product_nested_rhs
{
typedef typename ei_meta_if<
(ei_traits<T>::Flags & NestByValueBit) && (!(ei_traits<T>::Flags & RowMajorBit)) && (int(ei_traits<T>::Flags) & DirectAccessBit),
ei_must_nest_by_value<T>::ret && (!(ei_traits<T>::Flags & RowMajorBit)) && (int(ei_traits<T>::Flags) & DirectAccessBit),
T,
typename ei_meta_if<
((ei_traits<T>::Flags & EvalBeforeNestingBit)
@ -181,7 +181,7 @@ template<typename T, int n=1> struct ei_product_nested_rhs
template<typename T, int n=1> struct ei_product_nested_lhs
{
typedef typename ei_meta_if<
ei_traits<T>::Flags & NestByValueBit && (int(ei_traits<T>::Flags) & DirectAccessBit),
ei_must_nest_by_value<T>::ret && (int(ei_traits<T>::Flags) & DirectAccessBit),
T,
typename ei_meta_if<
int(ei_traits<T>::Flags) & EvalBeforeNestingBit

View File

@ -126,8 +126,8 @@ MatrixBase<Derived>::transpose() const
* \sa transpose(), conjugate(), class Transpose, class ei_scalar_conjugate_op */
template<typename Derived>
inline const Transpose<
Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived >
, NestByValueBit, 0> >
NestByValue<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived > >
>
MatrixBase<Derived>::adjoint() const
{
return conjugate().nestByValue();

View File

@ -45,7 +45,6 @@ const unsigned int SelfAdjointBit = 0x100; ///< means the matrix is selfadjoint
const unsigned int UpperTriangularBit = 0x200; ///< means the strictly triangular lower part is 0
const unsigned int LowerTriangularBit = 0x400; ///< means the strictly triangular upper part is 0
const unsigned int DirectAccessBit = 0x800; ///< means the underlying matrix data can be direclty accessed
const unsigned int NestByValueBit = 0x1000; ///< means the expression should be copied by value when nested
// list of flags that are inherited by default
const unsigned int HereditaryBits = RowMajorBit

View File

@ -35,6 +35,7 @@ template<typename _Scalar, int _Rows, int _Cols,
class Matrix;
template<typename ExpressionType, unsigned int Added, unsigned int Removed> class Flagged;
template<typename ExpressionType> class NestByValue;
template<typename MatrixType> class Minor;
template<typename MatrixType, int BlockRows=Dynamic, int BlockCols=Dynamic> class Block;
template<typename MatrixType> class Transpose;

View File

@ -135,7 +135,6 @@ typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
typedef typename Base::PacketScalar PacketScalar; \
typedef typename Eigen::ei_nested<Derived>::type Nested; \
typedef typename Eigen::ei_eval<Derived>::type Eval; \
typedef Eigen::Flagged<Derived, NestByValueBit, 0> Temporary; \
enum { RowsAtCompileTime = Base::RowsAtCompileTime, \
ColsAtCompileTime = Base::ColsAtCompileTime, \
MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime, \

View File

@ -192,11 +192,14 @@ template<typename T> struct ei_unref<T&> { typedef T type; };
template<typename T> struct ei_unconst { typedef T type; };
template<typename T> struct ei_unconst<const T> { typedef T type; };
template<typename T> struct ei_must_nest_by_value { enum { ret = false }; };
template<typename T> struct ei_must_nest_by_value<NestByValue<T> > { enum { ret = true }; };
template<typename T, int n=1> struct ei_nested
{
typedef typename ei_meta_if<
ei_traits<T>::Flags & NestByValueBit,
ei_must_nest_by_value<T>::ret,
T,
typename ei_meta_if<
int(ei_traits<T>::Flags) & EvalBeforeNestingBit