* extend Map to allow the user to specify whether the mapped data

is aligned or not. This is done using the Aligned constant:
  Map<MatrixType,Aligned>::Map(data);
* rename ForceAligned to EnforceAlignedAccess, and update its doc,
  and emphasize this is mainly an internal stuff.
This commit is contained in:
Gael Guennebaud 2009-10-23 14:26:14 +02:00
parent 83a7b7c44c
commit a382963b04
6 changed files with 63 additions and 43 deletions

View File

@ -33,10 +33,10 @@
* \param MatrixType the type of the object in which we are taking a block * \param MatrixType the type of the object in which we are taking a block
* \param BlockRows the number of rows of the block we are taking at compile time (optional) * \param BlockRows the number of rows of the block we are taking at compile time (optional)
* \param BlockCols the number of columns of the block we are taking at compile time (optional) * \param BlockCols the number of columns of the block we are taking at compile time (optional)
* \param _PacketAccess allows to enforce aligned loads and stores if set to \b ForceAligned. * \param _PacketAccess \internal used to enforce aligned loads in expressions such as
* The default is \b AsRequested. This parameter is internaly used by Eigen * \code mat.block() += other; \endcode. Possible values are
* in expressions such as \code mat.block() += other; \endcode and most of * \c AsRequested (default) and \c EnforceAlignedAccess.
* the time this is the only way it is used. * See class MapBase for more details.
* \param _DirectAccessStatus \internal used for partial specialization * \param _DirectAccessStatus \internal used for partial specialization
* *
* This class represents an expression of either a fixed-size or dynamic-size block. It is the return * This class represents an expression of either a fixed-size or dynamic-size block. It is the return
@ -84,9 +84,9 @@ struct ei_traits<Block<MatrixType, BlockRows, BlockCols, _PacketAccess, _DirectA
CoeffReadCost = ei_traits<MatrixType>::CoeffReadCost, CoeffReadCost = ei_traits<MatrixType>::CoeffReadCost,
PacketAccess = _PacketAccess PacketAccess = _PacketAccess
}; };
typedef typename ei_meta_if<int(PacketAccess)==ForceAligned, typedef typename ei_meta_if<int(PacketAccess)==EnforceAlignedAccess,
Block<MatrixType, BlockRows, BlockCols, _PacketAccess, _DirectAccessStatus>&, Block<MatrixType, BlockRows, BlockCols, _PacketAccess, _DirectAccessStatus>&,
Block<MatrixType, BlockRows, BlockCols, ForceAligned, _DirectAccessStatus> >::ret AlignedDerivedType; Block<MatrixType, BlockRows, BlockCols, EnforceAlignedAccess, _DirectAccessStatus> >::ret AlignedDerivedType;
}; };
template<typename MatrixType, int BlockRows, int BlockCols, int PacketAccess, int _DirectAccessStatus> class Block template<typename MatrixType, int BlockRows, int BlockCols, int PacketAccess, int _DirectAccessStatus> class Block
@ -228,13 +228,13 @@ class Block<MatrixType,BlockRows,BlockCols,PacketAccess,HasDirectAccess>
class InnerIterator; class InnerIterator;
typedef typename ei_traits<Block>::AlignedDerivedType AlignedDerivedType; typedef typename ei_traits<Block>::AlignedDerivedType AlignedDerivedType;
friend class Block<MatrixType,BlockRows,BlockCols,PacketAccess==AsRequested?ForceAligned:AsRequested,HasDirectAccess>; friend class Block<MatrixType,BlockRows,BlockCols,PacketAccess==EnforceAlignedAccess?AsRequested:EnforceAlignedAccess,HasDirectAccess>;
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
AlignedDerivedType _convertToForceAligned() AlignedDerivedType _convertToEnforceAlignedAccess()
{ {
return Block<MatrixType,BlockRows,BlockCols,ForceAligned,HasDirectAccess> return Block<MatrixType,BlockRows,BlockCols,EnforceAlignedAccess,HasDirectAccess>
(m_matrix, Base::m_data, Base::m_rows.value(), Base::m_cols.value()); (m_matrix, Base::m_data, Base::m_rows.value(), Base::m_cols.value());
} }

View File

@ -31,16 +31,14 @@
* \brief A matrix or vector expression mapping an existing array of data. * \brief A matrix or vector expression mapping an existing array of data.
* *
* \param MatrixType the equivalent matrix type of the mapped data * \param MatrixType the equivalent matrix type of the mapped data
* \param _PacketAccess allows to enforce aligned loads and stores if set to ForceAligned. * \param PointerAlignment specifies whether the pointer is \c Aligned, or \c Unaligned.
* The default is AsRequested. This parameter is internaly used by Eigen * The default is \c Unaligned.
* in expressions such as \code Map<...>(...) += other; \endcode and most
* of the time this is the only way it is used.
* *
* This class represents a matrix or vector expression mapping an existing array of data. * This class represents a matrix or vector expression mapping an existing array of data.
* It can be used to let Eigen interface without any overhead with non-Eigen data structures, * It can be used to let Eigen interface without any overhead with non-Eigen data structures,
* such as plain C arrays or structures from other libraries. * such as plain C arrays or structures from other libraries.
* *
* \b Tips: to change the array of data mapped by a Map object, you can use the C++ * \b Tip: to change the array of data mapped by a Map object, you can use the C++
* placement new syntax: * placement new syntax:
* *
* Example: \include Map_placement_new.cpp * Example: \include Map_placement_new.cpp
@ -48,22 +46,27 @@
* *
* This class is the return type of Matrix::Map() but can also be used directly. * This class is the return type of Matrix::Map() but can also be used directly.
* *
* \b Note \b to \b Eigen \b developers: The template parameter \c PointerAlignment
* can also be or-ed with \c EnforceAlignedAccess in order to enforce aligned read
* in expressions such as \code A += B; \endcode. See class MapBase for further details.
*
* \sa Matrix::Map() * \sa Matrix::Map()
*/ */
template<typename MatrixType, int _PacketAccess> template<typename MatrixType, int Options>
struct ei_traits<Map<MatrixType, _PacketAccess> > : public ei_traits<MatrixType> struct ei_traits<Map<MatrixType, Options> > : public ei_traits<MatrixType>
{ {
enum { enum {
PacketAccess = _PacketAccess, PacketAccess = Options & EnforceAlignedAccess,
Flags = ei_traits<MatrixType>::Flags & ~AlignedBit Flags = (Options&Aligned)==Aligned ? ei_traits<MatrixType>::Flags | AlignedBit
: ei_traits<MatrixType>::Flags & ~AlignedBit
}; };
typedef typename ei_meta_if<int(PacketAccess)==ForceAligned, typedef typename ei_meta_if<int(PacketAccess)==EnforceAlignedAccess,
Map<MatrixType, _PacketAccess>&, Map<MatrixType, Options>&,
Map<MatrixType, ForceAligned> >::ret AlignedDerivedType; Map<MatrixType, Options|EnforceAlignedAccess> >::ret AlignedDerivedType;
}; };
template<typename MatrixType, int PacketAccess> class Map template<typename MatrixType, int Options> class Map
: public MapBase<Map<MatrixType, PacketAccess> > : public MapBase<Map<MatrixType, Options> >
{ {
public: public:
@ -72,9 +75,9 @@ template<typename MatrixType, int PacketAccess> class Map
inline int stride() const { return this->innerSize(); } inline int stride() const { return this->innerSize(); }
AlignedDerivedType _convertToForceAligned() AlignedDerivedType _convertToEnforceAlignedAccess()
{ {
return Map<MatrixType,ForceAligned>(Base::m_data, Base::m_rows.value(), Base::m_cols.value()); return AlignedDerivedType(Base::m_data, Base::m_rows.value(), Base::m_cols.value());
} }
inline Map(const Scalar* data) : Base(data) {} inline Map(const Scalar* data) : Base(data) {}

View File

@ -32,11 +32,17 @@
* *
* Expression classes inheriting MapBase must define the constant \c PacketAccess, * Expression classes inheriting MapBase must define the constant \c PacketAccess,
* and type \c AlignedDerivedType in their respective ei_traits<> specialization structure. * and type \c AlignedDerivedType in their respective ei_traits<> specialization structure.
* The value of \c PacketAccess can be either: * The value of \c PacketAccess can be either \b AsRequested, or set to \b EnforceAlignedAccess which
* - \b ForceAligned which enforces both aligned loads and stores * enforces both aligned loads and stores.
* - \b AsRequested which is the default behavior *
* \c EnforceAlignedAccess is automatically set in expressions such as
* \code A += B; \endcode where A is either a Block or a Map. Here,
* this expression is transfomed into \code A = A_with_EnforceAlignedAccess + B; \endcode
* avoiding unaligned loads from A. Indeed, since Eigen's packet evaluation mechanism
* automatically align to the destination matrix, we know that loads to A will be aligned too.
*
* The type \c AlignedDerivedType should correspond to the equivalent expression type * The type \c AlignedDerivedType should correspond to the equivalent expression type
* with \c PacketAccess being \c ForceAligned. * with \c PacketAccess set to \c EnforceAlignedAccess.
* *
* \sa class Map, class Block * \sa class Map, class Block
*/ */
@ -79,19 +85,19 @@ template<typename Derived> class MapBase
* \sa MapBase::stride() */ * \sa MapBase::stride() */
inline const Scalar* data() const { return m_data; } inline const Scalar* data() const { return m_data; }
template<bool IsForceAligned,typename Dummy> struct force_aligned_impl { template<bool IsEnforceAlignedAccess,typename Dummy> struct force_aligned_impl {
static AlignedDerivedType run(MapBase& a) { return a.derived(); } static AlignedDerivedType run(MapBase& a) { return a.derived(); }
}; };
template<typename Dummy> struct force_aligned_impl<false,Dummy> { template<typename Dummy> struct force_aligned_impl<false,Dummy> {
static AlignedDerivedType run(MapBase& a) { return a.derived()._convertToForceAligned(); } static AlignedDerivedType run(MapBase& a) { return a.derived()._convertToEnforceAlignedAccess(); }
}; };
/** \returns an expression equivalent to \c *this but having the \c PacketAccess constant /** \returns an expression equivalent to \c *this but having the \c PacketAccess constant
* set to \c ForceAligned. Must be reimplemented by the derived class. */ * set to \c EnforceAlignedAccess. Must be reimplemented by the derived class. */
AlignedDerivedType forceAligned() AlignedDerivedType forceAligned()
{ {
return force_aligned_impl<int(PacketAccess)==int(ForceAligned),Derived>::run(*this); return force_aligned_impl<int(PacketAccess)==int(EnforceAlignedAccess),Derived>::run(*this);
} }
inline const Scalar& coeff(int row, int col) const inline const Scalar& coeff(int row, int col) const
@ -131,7 +137,7 @@ template<typename Derived> class MapBase
template<int LoadMode> template<int LoadMode>
inline PacketScalar packet(int row, int col) const inline PacketScalar packet(int row, int col) const
{ {
return ei_ploadt<Scalar, int(PacketAccess) == ForceAligned ? Aligned : LoadMode> return ei_ploadt<Scalar, int(PacketAccess) == EnforceAlignedAccess ? Aligned : LoadMode>
(m_data + (IsRowMajor ? col + row * stride() (m_data + (IsRowMajor ? col + row * stride()
: row + col * stride())); : row + col * stride()));
} }
@ -139,13 +145,13 @@ template<typename Derived> class MapBase
template<int LoadMode> template<int LoadMode>
inline PacketScalar packet(int index) const inline PacketScalar packet(int index) const
{ {
return ei_ploadt<Scalar, int(PacketAccess) == ForceAligned ? Aligned : LoadMode>(m_data + index); return ei_ploadt<Scalar, int(PacketAccess) == EnforceAlignedAccess ? Aligned : LoadMode>(m_data + index);
} }
template<int StoreMode> template<int StoreMode>
inline void writePacket(int row, int col, const PacketScalar& x) inline void writePacket(int row, int col, const PacketScalar& x)
{ {
ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == ForceAligned ? Aligned : StoreMode> ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == EnforceAlignedAccess ? Aligned : StoreMode>
(const_cast<Scalar*>(m_data) + (IsRowMajor ? col + row * stride() (const_cast<Scalar*>(m_data) + (IsRowMajor ? col + row * stride()
: row + col * stride()), x); : row + col * stride()), x);
} }
@ -153,13 +159,14 @@ template<typename Derived> class MapBase
template<int StoreMode> template<int StoreMode>
inline void writePacket(int index, const PacketScalar& x) inline void writePacket(int index, const PacketScalar& x)
{ {
ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == ForceAligned ? Aligned : StoreMode> ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == EnforceAlignedAccess ? Aligned : StoreMode>
(const_cast<Scalar*>(m_data) + index, x); (const_cast<Scalar*>(m_data) + index, x);
} }
inline MapBase(const Scalar* data) : m_data(data), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime) inline MapBase(const Scalar* data) : m_data(data), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
{ {
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
checkDataAlignment();
} }
inline MapBase(const Scalar* data, int size) inline MapBase(const Scalar* data, int size)
@ -170,6 +177,7 @@ template<typename Derived> class MapBase
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
ei_assert(size >= 0); ei_assert(size >= 0);
ei_assert(data == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == size); ei_assert(data == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == size);
checkDataAlignment();
} }
inline MapBase(const Scalar* data, int rows, int cols) inline MapBase(const Scalar* data, int rows, int cols)
@ -178,6 +186,7 @@ template<typename Derived> class MapBase
ei_assert( (data == 0) ei_assert( (data == 0)
|| ( rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) || ( rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
&& cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols))); && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
checkDataAlignment();
} }
Derived& operator=(const MapBase& other) Derived& operator=(const MapBase& other)
@ -215,6 +224,13 @@ template<typename Derived> class MapBase
{ return derived() = forceAligned() / other; } { return derived() = forceAligned() / other; }
protected: protected:
void checkDataAlignment() const
{
ei_assert( ((!(ei_traits<Derived>::Flags&AlignedBit))
|| ((std::size_t(m_data)&0xf)==0)) && "data is not aligned");
}
const Scalar* EIGEN_RESTRICT m_data; const Scalar* EIGEN_RESTRICT m_data;
const ei_int_if_dynamic<RowsAtCompileTime> m_rows; const ei_int_if_dynamic<RowsAtCompileTime> m_rows;
const ei_int_if_dynamic<ColsAtCompileTime> m_cols; const ei_int_if_dynamic<ColsAtCompileTime> m_cols;

View File

@ -59,7 +59,7 @@ MatrixBase<Derived>::stableNorm() const
RealScalar invScale = 1; RealScalar invScale = 1;
RealScalar ssq = 0; // sum of square RealScalar ssq = 0; // sum of square
enum { enum {
Alignment = (int(Flags)&DirectAccessBit) || (int(Flags)&AlignedBit) ? ForceAligned : AsRequested Alignment = (int(Flags)&DirectAccessBit) || (int(Flags)&AlignedBit) ? EnforceAlignedAccess : AsRequested
}; };
int n = size(); int n = size();
int bi=0; int bi=0;

View File

@ -196,8 +196,8 @@ const unsigned int UnitLowerTriangular = LowerTriangularBit | UnitDiagBit;
enum { DiagonalOnTheLeft, DiagonalOnTheRight }; enum { DiagonalOnTheLeft, DiagonalOnTheRight };
enum { Aligned, Unaligned }; enum { Unaligned=0, Aligned=1 };
enum { ForceAligned, AsRequested }; enum { AsRequested=0, EnforceAlignedAccess=2 };
enum { ConditionalJumpCost = 5 }; enum { ConditionalJumpCost = 5 };
enum CornerType { TopLeft, TopRight, BottomLeft, BottomRight }; enum CornerType { TopLeft, TopRight, BottomLeft, BottomRight };
enum DirectionType { Vertical, Horizontal, BothDirections }; enum DirectionType { Vertical, Horizontal, BothDirections };

View File

@ -37,13 +37,14 @@ template<typename VectorType> void map_class(const VectorType& m)
Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3; Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3;
Map<VectorType, Aligned>(array1, size) = VectorType::Random(size); Map<VectorType, Aligned>(array1, size) = VectorType::Random(size);
Map<VectorType>(array2, size) = Map<VectorType>(array1, size); Map<VectorType, Aligned>(array2, size) = Map<VectorType,Aligned>(array1, size);
Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size); Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
VectorType ma1 = Map<VectorType>(array1, size); VectorType ma1 = Map<VectorType, Aligned>(array1, size);
VectorType ma2 = Map<VectorType, Aligned>(array2, size); VectorType ma2 = Map<VectorType, Aligned>(array2, size);
VectorType ma3 = Map<VectorType>(array3unaligned, size); VectorType ma3 = Map<VectorType>(array3unaligned, size);
VERIFY_IS_APPROX(ma1, ma2); VERIFY_IS_APPROX(ma1, ma2);
VERIFY_IS_APPROX(ma1, ma3); VERIFY_IS_APPROX(ma1, ma3);
VERIFY_RAISES_ASSERT((Map<VectorType,Aligned>(array3unaligned, size)));
ei_aligned_delete(array1, size); ei_aligned_delete(array1, size);
ei_aligned_delete(array2, size); ei_aligned_delete(array2, size);