Using STL Containers with Eigen

Table of contents

Summary

Using STL containers on fixed-size vectorizable Eigen types requires taking the following two steps:

These issues arise only with fixed-size vectorizable Eigen types. For other Eigen types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.

Using an aligned allocator

STL containers take an optional template parameter, the allocator type. When using STL containers on fixed-size vectorizable Eigen types, you need tell the container to use an allocator that will always allocate memory at 16-byte-aligned locations. Fortunately, Eigen does provide such an allocator: Eigen::aligned_allocator.

For example, instead of

std::map<int, Eigen::Vector4f>
you need to use
std::map<int, Eigen::Vector4f, std::less<int>, Eigen::aligned_allocator<Eigen::Vector4f> >
Note that here, the 3rd parameter "std::less<int>" is just the default value, we only had to specify it because we needed to specify the allocator type, that is the 4th parameter.

The case of std::vector

The situation with std::vector was even worse (explanation below) so we had to specialize it for Eigen types. The upside is that our specialization takes care of specifying the aligned allocator, so you don't need to worry about it. All you need to do is to #include <Eigen/StdVector>.

So as soon as you have

#include<Eigen/StdVector>
you can simply use
std::vector<Eigen::Vector4f>
without having to worry about anything.

Explanation: The resize() method of std::vector takes a value_type argument (defaulting to value_type()). So with std::vector<Eigen::Vector4f>, some Eigen::Vector4f objects will be passed by value, which discards any alignment modifiers, so a Eigen::Vector4f can be created at an unaligned location. In order to avoid that, the only solution we saw was to specialize std::vector to make it work on a slight modification of, here, Eigen::Vector4f, that is able to deal properly with this situation.


Generated on Thu Apr 9 10:10:01 2009 for Eigen by  doxygen 1.5.5