Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
Explanation of the assertion on unaligned arrays

Hello! You are seeing this webpage because your program terminated on an assertion failure like this one:

my_program: path/to/eigen/Eigen/src/Core/DenseStorage.h:44:
Eigen::internal::matrix_array<T, Size, MatrixOptions, Align>::internal::matrix_array()
[with T = double, int Size = 2, int MatrixOptions = 2, bool Align = true]:
Assertion `(reinterpret_cast<size_t>(array) & (sizemask)) == 0 && "this assertion
is explained here: http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html
     READ THIS WEB PAGE !!! ****"' failed.

There are 4 known causes for this issue. If you can target [c++17] only with a recent compiler (e.g., GCC>=7, clang>=5, MSVC>=19.12), then you're lucky: enabling c++17 should be enough (if not, please report to us). Otherwise, please read on to understand those issues and learn how to fix them.

Where in my own code is the cause of the problem?

First of all, you need to find out where in your own code this assertion was triggered from. At first glance, the error message doesn't look helpful, as it refers to a file inside Eigen! However, since your program crashed, if you can reproduce the crash, you can get a backtrace using any debugger. For example, if you're using GCC, you can use the GDB debugger as follows:

$ gdb ./my_program # Start GDB on your program
> run # Start running your program
... # Now reproduce the crash!
> bt # Obtain the backtrace

Now that you know precisely where in your own code the problem is happening, read on to understand what you need to change.

Cause 1: Structures having Eigen objects as members

If you have code like this,

class Foo
{
//...
//...
};
//...
Foo *foo = new Foo;
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186

then you need to read this separate page: Structures Having Eigen Members.

Note that here, Eigen::Vector4d is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.

Cause 2: STL Containers or manual memory allocation

If you use STL Containers such as std::vector, std::map, ..., with Eigen objects, or with classes containing Eigen objects, like this,

std::vector<Eigen::Matrix2d> my_vector;
struct my_class { ... Eigen::Matrix2d m; ... };
std::map<int, my_class> my_map;

then you need to read this separate page: Using STL Containers with Eigen.

Note that here, Eigen::Matrix2d is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types and structures having such Eigen objects as member.

The same issue will be exhibited by any classes/functions by-passing operator new to allocate memory, that is, by performing custom memory allocation followed by calls to the placement new operator. This is for instance typically the case of `stdmake_shared` or std::allocate_shared for which is the solution is to use an aligned allocator as detailed in the solution for STL containers.

Cause 3: Passing Eigen objects by value

If some function in your code is getting an Eigen object passed by value, like this,

void func(Eigen::Vector4d v);

then you need to read this separate page: Passing Eigen objects by value to functions.

Note that here, Eigen::Vector4d is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.

Cause 4: Compiler making a wrong assumption on stack alignment (for instance GCC on Windows)

This is a must-read for people using GCC on Windows (like MinGW or TDM-GCC). If you have this assertion failure in an innocent function declaring a local variable like this:

void foo()
{
//...
}
The quaternion class used to represent 3D orientations and rotations.
Definition Quaternion.h:285

then you need to read this separate page: Compiler making a wrong assumption on stack alignment.

Note that here, Eigen::Quaternionf is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.

General explanation of this assertion

Fixed-size vectorizable Eigen objects must absolutely be created at properly aligned locations, otherwise SIMD instructions addressing them will crash. For instance, SSE/NEON/MSA/Altivec/VSX targets will require 16-byte-alignment, whereas AVX and AVX512 targets may require up to 32 and 64 byte alignment respectively.

Eigen normally takes care of these alignment issues for you, by setting an alignment attribute on them and by overloading their operator new.

However there are a few corner cases where these alignment settings get overridden: they are the possible causes for this assertion.

I don't care about optimal vectorization, how do I get rid of that stuff?

Three possibilities: