Re: [eigen] Alignment

[ Thread Index | Date Index | More Archives ]

Hi Benoit,

On Sun, 2009-02-22 at 16:04 +0100, Benoit Jacob wrote:
> Are you using 2.0, or trunk?

2.0 at the moment.

> We know about the std::vector::resize() problem.
> We made a fix,
> basically you just #include<Eigen/StdVector> and then you're able to
> do std::vector<Eigen::Vector2d>.

Like I said, I realized that but also noticed that it doesn't cover the
custom class with Eigen members case.

> However, in Eigen 2.0 our fix doesn't support the case of
> std::vector<T> with T a custom class having an Eigen member.

Thanks for confirming. It might be worth adding that to the
documentation to avoid further stupid questions on the subject ;)

> Gael fixed that in trunk but that isn't backported to 2.0 (big change;
> Gael: of course it's your choice whether this is backportable).

Sounds great!

> So can you try with trunk and see if it makes std::vector work for
> you? (do  #include<Eigen/StdVector>).

Will do as I get around to it... I need to get my code to work first :)

> > My understanding is that the std::vector variant should be safe unless
> > resize() is called.
> Indeed, but that's quite an incomfortable assumption, that's why we
> went further and made Eigen/StdVector.

I agree it's not great, but since I don't call resize() it's good enough
for me right now.

> > What about boost::array? Given that it's a thin wrapper around a plain
> > array (see [1]) is the compiler guaranteed to figure out how to lay
> > things out in memory correctly?
> If boost::array<T> honors T::operator new[] then I'd say it should
> work fine, because EIGEN_MAKE_ALIGNED_OPERATOR_NEW overloads also this
> form to make it return aligned pointers, and the EIGEN_ALIGN_128 on
> Eigen's internal fixed-size arrays should also be honored.

boost::array uses a plain "on-the-stack" array internally. So, as far as
storage is concerned

boost::array<T,size> a;

is equivalent to

T a[size];

If sizeof(T) doesn't happen to be a multiple of 16 the compiler would
have to perform some sort of automagic padding (i.e. adjust sizeof(T)
artificially) to ensure alignment in this case. G++ does that in the
simple test below but I was wondering whether that's just coincidence or
whether it's guaranteed to do that.

I'm sorry to be a bit dense about this but I'd hate to see code
randomly blow up at runtime ;)

#include <boost/array.hpp>

#include <Eigen/Core>
#include <Eigen/LU> 

#include <Eigen/StdVector>
#include <Eigen/Cholesky>

using namespace Eigen;

class foo
    Matrix<double, 4, 4> A;
    Vector3d v;
    char c;


int main()
    std::cout << "sizeof(foo) = " << sizeof(foo) << std::endl;
    boost::array<foo, 20> a;

    std::cout << std::hex << &a[1] << std::endl;

Generated output:

sizeof(foo) = 160



Mail converted by MHonArc 2.6.19+