Skip to content

Editing Eigen alignment in aslam_cv

Fadri Furrer edited this page Sep 18, 2017 · 1 revision

Given the heavy use of vector instructions in Eigen the library requires memory to be 16 byte aligned. (On older machines the aligned load of memory was much faster than unaligned. This is only partially true for most recent processor models).

Standard Eigen procedure

Eigen make_aligned_operator new for structs

Therefore all structs that contain fixed size vectorizable Eigen members must have an aligned operator new which can be added using an Eigen macro.

#include <Eigen/Dense>
struct MyClass {
  Eigen::Matrix<double, 4, 1> vector;
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
};

STL containers and Eigen

STL containers need to use an allocator which aligns memory at 16 byte boundaries. The normal procedure is: std::vector<Eigen::Matrix<double, 4, 1>, Eigen::aligned_allocator<Eigen::Matrix<double, 4, 1> > vector;

http://eigen.tuxfamily.org/dox-devel/group__TopicStlContainers.html

Aslam cv specific helpers for STL containers

STL vector helper type trait

For STL vectors there is a type trait in aslam which you can use;

#include <aslam/common/aligned-allocation.h>
Aligned<std::vector, Eigen::Matrix<double, 4, 1> >::type vector;

STL map / unordered helper type trait

#include <aslam/common/aligned-allocation.h>
AlignedMap<int, Eigen::Matrix<double, 4, 1> >::type map_int_to_vector;
AlignedUnorderedMap<int, Eigen::Matrix<double, 4, 1> >::type unordered_map_int_to_vector;