Introduced NestParentByRefBit and NestByRefBit - this should fix temporaries related to nested products.

Fixed a few typos and a few warnings.
This commit is contained in:
Hauke Heibel
2010-02-06 17:43:32 +01:00
parent 6f3f857897
commit 871698d3aa
29 changed files with 84 additions and 75 deletions

View File

@@ -76,7 +76,9 @@ struct ei_traits<Block<MatrixType, BlockRows, BlockCols, _DirectAccessStatus> >
MaskPacketAccessBit = (InnerMaxSize == Dynamic || (InnerSize >= ei_packet_traits<Scalar>::size))
? PacketAccessBit : 0,
FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
Flags = (ei_traits<MatrixType>::Flags & (HereditaryBits | MaskPacketAccessBit | DirectAccessBit)) | FlagsLinearAccessBit
Flags = (ei_traits<MatrixType>::Flags & (HereditaryBits | MaskPacketAccessBit | DirectAccessBit))
| FlagsLinearAccessBit
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags)
};
};

View File

@@ -343,7 +343,7 @@ template<typename OtherDerived>
EIGEN_STRONG_INLINE void DenseBase<Derived>::copyCoeff(int index, const DenseBase<OtherDerived>& other)
{
ei_internal_assert(index >= 0 && index < size());
derived().coeffRef(index) = other.derived().coeff(index);
derived().coeffRef(index) = Scalar(other.derived().coeff(index));
}
/** \internal Copies the packet at position (row,col) of other into *this.

View File

@@ -72,6 +72,7 @@ struct ei_traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > : ei_traits<Lhs>
( AlignedBit
| (StorageOrdersAgree ? LinearAccessBit : 0)
| (ei_functor_traits<BinaryOp>::PacketAccess && StorageOrdersAgree ? PacketAccessBit : 0)
| EIGEN_PROPAGATE_NESTING_BIT(Lhs::Flags|Rhs::Flags)
)
)
),

View File

@@ -48,7 +48,8 @@ struct ei_traits<CwiseNullaryOp<NullaryOp, MatrixType> > : ei_traits<MatrixType>
& ( HereditaryBits
| (ei_functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
| (ei_functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
| (ei_functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
| (ei_functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit)
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = ei_functor_traits<NullaryOp>::Cost
};
};

View File

@@ -51,7 +51,8 @@ struct ei_traits<CwiseUnaryOp<UnaryOp, MatrixType> >
enum {
Flags = (_MatrixTypeNested::Flags & (
HereditaryBits | LinearAccessBit | AlignedBit
| (ei_functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0))),
| (ei_functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)))
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost + ei_functor_traits<UnaryOp>::Cost
};
};

View File

@@ -47,7 +47,8 @@ struct ei_traits<CwiseUnaryView<ViewOp, MatrixType> >
typedef typename MatrixType::Nested MatrixTypeNested;
typedef typename ei_cleantype<MatrixTypeNested>::type _MatrixTypeNested;
enum {
Flags = (ei_traits<_MatrixTypeNested>::Flags & (HereditaryBits | LinearAccessBit | AlignedBit)),
Flags = (ei_traits<_MatrixTypeNested>::Flags & (HereditaryBits | LinearAccessBit | AlignedBit))
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags), // if I am not wrong, I need to test this on MatrixType and not on the nested type
CoeffReadCost = ei_traits<_MatrixTypeNested>::CoeffReadCost + ei_functor_traits<ViewOp>::Cost
};
};

View File

@@ -58,7 +58,8 @@ struct ei_traits<Diagonal<MatrixType,Index> >
: Index == Dynamic ? EIGEN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime)
: (EIGEN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime) - AbsIndex),
MaxColsAtCompileTime = 1,
Flags = (unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit),
Flags = ((unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit))
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost
};
};

View File

@@ -37,7 +37,8 @@ struct ei_traits<DiagonalProduct<MatrixType, DiagonalType, ProductOrder> >
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
Flags = (HereditaryBits & (unsigned int)(MatrixType::Flags))
| (PacketAccessBit & (unsigned int)(MatrixType::Flags) & (unsigned int)(DiagonalType::DiagonalVectorType::Flags)),
| (PacketAccessBit & (unsigned int)(MatrixType::Flags) & (unsigned int)(DiagonalType::DiagonalVectorType::Flags))
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = NumTraits<Scalar>::MulCost + MatrixType::CoeffReadCost + DiagonalType::DiagonalVectorType::CoeffReadCost
};
};

View File

@@ -53,7 +53,8 @@ struct ei_traits<Minor<MatrixType> >
int(MatrixType::MaxRowsAtCompileTime) - 1 : Dynamic,
MaxColsAtCompileTime = (MatrixType::MaxColsAtCompileTime != Dynamic) ?
int(MatrixType::MaxColsAtCompileTime) - 1 : Dynamic,
Flags = _MatrixTypeNested::Flags & HereditaryBits,
Flags = (_MatrixTypeNested::Flags & HereditaryBits)
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost // minor is used typically on tiny matrices,
// where loops are unrolled and the 'if' evaluates at compile time
};

View File

@@ -42,7 +42,10 @@ struct ei_traits<ProductBase<Derived,_Lhs,_Rhs> > //: ei_traits<typename ei_clea
ColsAtCompileTime = ei_traits<Rhs>::ColsAtCompileTime,
MaxRowsAtCompileTime = ei_traits<Lhs>::MaxRowsAtCompileTime,
MaxColsAtCompileTime = ei_traits<Rhs>::MaxColsAtCompileTime,
Flags = EvalBeforeNestingBit | EvalBeforeAssigningBit,
Flags = EvalBeforeNestingBit
| EvalBeforeAssigningBit
| NestParentByRefBit
| EIGEN_PROPAGATE_NESTING_BIT(Lhs::Flags|Rhs::Flags),
CoeffReadCost = 0 // FIXME why is it needed ?
};
};

View File

@@ -355,7 +355,7 @@ template<typename Derived>
EIGEN_STRONG_INLINE typename ei_traits<Derived>::Scalar
DenseBase<Derived>::mean() const
{
return this->redux(Eigen::ei_scalar_sum_op<Scalar>()) / Scalar(this->size());
return Scalar(this->redux(Eigen::ei_scalar_sum_op<Scalar>())) / Scalar(this->size());
}
/** \returns the product of all coefficients of *this

View File

@@ -47,8 +47,9 @@ struct ei_traits<SelfAdjointView<MatrixType, TriangularPart> > : ei_traits<Matri
typedef MatrixType ExpressionType;
enum {
Mode = TriangularPart | SelfAdjoint,
Flags = _MatrixTypeNested::Flags & (HereditaryBits)
& (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)), // FIXME these flags should be preserved
Flags = (_MatrixTypeNested::Flags & (HereditaryBits)
& (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))) // FIXME these flags should be preserved
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost
};
};

View File

@@ -90,7 +90,7 @@ template<typename BinaryOp, typename MatrixType> class SelfCwiseBinaryOp
OtherDerived& _other = other.const_cast_derived();
ei_internal_assert(index >= 0 && index < m_matrix.size());
Scalar& tmp = m_matrix.coeffRef(index);
tmp = m_functor(tmp, _other.coeff(index));
tmp = m_functor(tmp, Scalar(_other.coeff(index)));
}
template<typename OtherDerived, int StoreMode, int LoadMode>

View File

@@ -118,10 +118,10 @@ MatrixBase<Derived>::blueNorm() const
iexp = - ((iemax+it)/2);
s2m = RealScalar(std::pow(RealScalar(ibeta),RealScalar(iexp))); // scaling factor for upper range
overfl = rbig*s2m; // overfow boundary for abig
overfl = rbig*s2m; // overflow boundary for abig
eps = RealScalar(std::pow(double(ibeta), 1-it));
relerr = ei_sqrt(eps); // tolerance for neglecting asml
abig = 1.0/eps - 1.0;
abig = RealScalar(1.0/eps - 1.0);
if (RealScalar(nbig)>abig) nmax = int(abig); // largest safe n
else nmax = nbig;
}

View File

@@ -50,7 +50,8 @@ struct ei_traits<Transpose<MatrixType> > : ei_traits<MatrixType>
ColsAtCompileTime = MatrixType::RowsAtCompileTime,
MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
Flags = (int(_MatrixTypeNested::Flags) ^ RowMajorBit),
Flags = (int(_MatrixTypeNested::Flags & ~NestByRefBit) ^ RowMajorBit)
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost
};
};

View File

@@ -130,7 +130,8 @@ struct ei_traits<TriangularView<MatrixType, _Mode> > : ei_traits<MatrixType>
typedef MatrixType ExpressionType;
enum {
Mode = _Mode,
Flags = (_MatrixTypeNested::Flags & (HereditaryBits) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))) | Mode,
Flags = (_MatrixTypeNested::Flags & (HereditaryBits) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))) | Mode
| EIGEN_PROPAGATE_NESTING_BIT(ei_traits<MatrixType>::Flags),
CoeffReadCost = _MatrixTypeNested::CoeffReadCost
};
};

View File

@@ -81,8 +81,10 @@ struct ei_traits<GeneralProduct<LhsNested,RhsNested,UnrolledProduct> >
Flags = ((unsigned int)(LhsFlags | RhsFlags) & HereditaryBits & RemovedBits)
| EvalBeforeAssigningBit
| EvalBeforeNestingBit
| NestParentByRefBit
| (CanVectorizeLhs || CanVectorizeRhs ? PacketAccessBit : 0)
| (LhsFlags & RhsFlags & AlignedBit),
| (LhsFlags & RhsFlags & AlignedBit)
| EIGEN_PROPAGATE_NESTING_BIT(LhsFlags|RhsFlags),
CoeffReadCost = InnerSize == Dynamic ? Dynamic
: InnerSize * (NumTraits<Scalar>::MulCost + LhsCoeffReadCost + RhsCoeffReadCost)

View File

@@ -35,7 +35,7 @@
* - It should be smaller than the sqrt of INT_MAX. Indeed, we often multiply a number of rows with a number
* of columns in order to compute a number of coefficients. Even if we guard that with an "if" checking whether
* the values are Dynamic, we still get a compiler warning "integer overflow". So the only way to get around
* it would be a meta-selector. Doing this everywhere would reduce code readability and lenghten compilation times.
* it would be a meta-selector. Doing this everywhere would reduce code readability and lengthen compilation times.
* Also, disabling compiler warnings for integer overflow, sounds like a bad idea.
* - It should be a prime number, because for example the old value 10000 led to bugs with 100x100 matrices.
*
@@ -76,7 +76,7 @@ const unsigned int EvalBeforeNestingBit = 0x2;
/** \ingroup flags
*
* means the expression should be evaluated before any assignement */
* means the expression should be evaluated before any assignment */
const unsigned int EvalBeforeAssigningBit = 0x4;
/** \ingroup flags
@@ -97,6 +97,9 @@ const unsigned int EvalBeforeAssigningBit = 0x4;
*/
const unsigned int PacketAccessBit = 0x8;
const unsigned int NestParentByRefBit = 0x80;
const unsigned int NestByRefBit = 0x100;
#ifdef EIGEN_VECTORIZE
/** \ingroup flags
*

View File

@@ -97,7 +97,7 @@ class ei_compute_matrix_flags
};
public:
enum { ret = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit | aligned_bit };
enum { ret = LinearAccessBit | DirectAccessBit | NestByRefBit | packet_access_bit | row_major_bit | aligned_bit };
};
template<int _Rows, int _Cols> struct ei_size_at_compile_time
@@ -201,6 +201,28 @@ template<typename T> struct ei_plain_matrix_type_row_major
// we should be able to get rid of this one too
template<typename T> struct ei_must_nest_by_value { enum { ret = false }; };
template<class T>
struct ei_is_reference
{
#ifndef NDEBUG
static void check() { std::cout << typeid(T).name() << std::endl; }
#else
static void check() {}
#endif
enum { ret = false };
};
template<class T>
struct ei_is_reference<T&>
{
#ifndef NDEBUG
static void check() { std::cout << typeid(T).name() << "&" << std::endl; }
#else
static void check() {}
#endif
enum { ret = true };
};
/**
* The reference selector for template expressions. The idea is that we don't
* need to use references for expressions since they are light weight proxy
@@ -209,31 +231,14 @@ template<typename T> struct ei_must_nest_by_value { enum { ret = false }; };
template <typename T>
struct ei_ref_selector
{
typedef T type;
typedef typename ei_meta_if<
bool(ei_traits<T>::Flags & NestByRefBit),
T const&,
T
>::ret type;
};
/**
* Matrices on the other hand side should only be copied, when it is sure
* we gain by copying (see arithmetic cost check and eval before nesting flag).
* Note: This is an optimization measure that comprises potential (though little)
* to create erroneous code. Any user, utilizing ei_nested outside of
* Eigen needs to take care that no references to temporaries are
* stored or that this potential danger is at least communicated
* to the user.
**/
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct ei_ref_selector< Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> MatrixType;
typedef MatrixType const& type;
};
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct ei_ref_selector< Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
{
typedef Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> ArrayType;
typedef ArrayType const& type;
};
#define EIGEN_PROPAGATE_NESTING_BIT(ReferenceFlags) ((ReferenceFlags) & NestParentByRefBit)<<1
/** \internal Determines how a given expression should be nested into another one.
* For example, when you do a * (b+c), Eigen will determine how the expression b+c should be