This forum has been archived. All content is frozen. Please use KDE Discuss instead.

About the alignment of fixed-size vectorizable Eigen types

Tags: None
(comma "," separated)
updogliu
Registered Member
Posts
13
Karma
0
OS
The example given at last in the page below is confusing.
http://eigen.tuxfamily.org/dox-devel/TopicStructHavingEigenMembers.html
Should it have been "enum { NeedsToAlign = (sizeof(Vector)%16)==0 };" ?

I have to say manually forcing alignment is really annoying! The users need to add EIGEN_MAKE_ALIGNED_OPERATOR_NEW to every and each class in the inheritance/composition trees --- even if they are not direct users of Eigen library. :(

P.S. Are the new features "C++11 alignment specifiers" relevant to this problem?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
you can declare your attributes with Eigen::DontAlign, e.g.:

Matrix<float,4,4,DontAlign> m_mat;

If a function use m_mat several times, then you can copy it at the beginning of the function into an aligned Matrix4f to take advantage of SSE.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
another solution is to store your fixed size object into a private struct which is dynamically allocated, e.g.:


struct Foo_d
{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Matrix4f m_mat;
Vector2d m_vec;
etc.
};


class Foo {
Foo() { init_d(); }
~Foo() { delete d; }
private:
void init_d() { d = new Foo_d; }
Foo_d* d;
};
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
for the record these two options have been added to the documentation
updogliu
Registered Member
Posts
13
Karma
0
OS
Thank you for your reply. I know some of those workaround. But what I'd like to know is that is there a schedule or plan of fixing this problem completely -- I mean a solution that completely encapsulates codes for alignment inside Eigen and thus won't entail any further work on the users.
The present situation is that whenever a downstream library uses Eigen, it has to tell its users how to take care of this alignment issue. That's really not a satisfying solution in my view. :-(
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
well this is a limitation of C++ which apparently is not solved by the new standard.

Note that with the two above solutions, the user of library using Eigen will be affected only if he/she directly manipulated Eigen's objects.
updogliu
Registered Member
Posts
13
Karma
0
OS
Sorry for digging this old post out. Usually, I prefer a boost::shared_ptr<Foo_d> to a raw Foo_d*. Boost provides a method to make shared object --- shared_ptr<T> make_shared( Args && ... args ) and shared_ptr<T> allocate_shared( A const & a, Args && ... args ) (http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/make_shared.html#functions)

I have confirmed that make_shared(...) won't meet the alignment requirement of Foo_d. allocate_shared(...) will use an user-supplied allocator, so I wonder can it make shared Foo_d objects? If so, would you please show me the code (I guess one or two lines only) doing that? I'm really a layman of allocator stuff :( . Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
you need to pass this allocator:

Eigen::aligned_allocator<Foo_d>

to allocate_shared(...);
updogliu
Registered Member
Posts
13
Karma
0
OS
Thanks. Normally speaking, should/may I save and use a named allocator multiple times? For example:
Assume the constructor of Foo_d takes a std::string argument
Code: Select all
Eigen::aligned_allocator<Foo_d> a;
shared_ptr p1 = allocate_shared<const Foo_d>(a, string("abc"));
shared_ptr p2 = allocate_shared<const Foo_d>(a, string("def"));

or should I change these to
Code: Select all
shared_ptr p1 = allocate_shared<const Foo_d>(Eigen::aligned_allocator<Foo_d>(), string("abc"));
shared_ptr p2 = allocate_shared<const Foo_d>(Eigen::aligned_allocator<Foo_d>(), string("def"));
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
That depends on the allocator and on the application. In this case since Eigen::aligned_allocator is empty this does not really matter.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]