From Eigen
Revision as of 13:57, 15 December 2016 by Ggael (Talk | contribs)

Jump to: navigation, search
Owl faq.jpg



Disabling non MPL2 features


Eigen and other libraries

Should I use Eigen?

Probably, but check pit falls first.

Why another matrix library? What is the need for Eigen?





The state of existing matrix libraries before Eigen is that:

  • some are Free Software
  • some are fast
  • some have a decent API

However Eigen is the first library to satisfy all these criteria.

How does Eigen compare to BLAS/LAPACK?

Eigen covers many things that BLAS/LAPACK don't:





Eigen has an incomparably better API than BLAS and LAPACK.

  • See the API Showcase.

Miscellaneous advantages (not specifically against BLAS/LAPACK):


Eigen and IDEs

For details on how to correctly use Eigen with IDEs (such as Eclipse CDT), see IDEs.


I need help with compiler errors!


Known MSVC issues



I need help with Assert crashes!






How do I get good performance?



Where in my program are temporary objects created?




#include <Eigen/Dense>
int main(int argc, char** argv)
  // It's OK to allocate here
  // It's NOT OK to allocate here
  // It's OK to allocate again


Which SIMD instruction sets are supported by Eigen?

Eigen supports SSE, AVX, AVX512, AltiVec/VSX (On Power7/8 systems in both little and big-endian mode), ARM NEON for 32 and 64-bit ARM SoCs, and now S390x SIMD (ZVector).


Of course vectorization is not mandatory -- you can use Eigen on any CPU.

Note: For S390x SIMD, due to lack of hardware support for 32-bit vector float types, only 32-bit ints and 64-bit double support has been added.

How can I enable vectorization?

You just need to tell your compiler to enable the corresponding instruction set, and Eigen will then detect it. If it is enabled by default, then you don't need to do anything. On GCC and clang you can simply pass -march=native to let the compiler enables all instruction set that are supported by your CPU.


On the x86-64 architecture, SSE2 is generally enabled by default, but you can enable AVX and FMA for better performance




On S390X SIMD (ZVector), you have to use a recent gcc (version >5.2.1) compiler, and add the following flags: -march=z13 -mzvector.

How can I disable vectorization?

You can disable Eigen's vectorization by defining the EIGEN_DONT_VECTORIZE preprocessor symbol.


Also notice that your compiler may still be auto-vectorizing.



If you want to get rid of it, follow this link.

How does vectorization depend on the compiler?



Eigen vectorization supports the following compilers:

  • GCC 4.2 and newer,
  • MSVC 2008 and newer,
  • All other compilers (for example it works with clang and ICC).


What can't be vectorized?

SSE, AltiVec, NEON and S390x work with packets of 128 bits, or 16 bytes. This means 4 ints, or 4 floats, or 2 doubles, except for S390x which doesn't support 32-bit floats in hardware. Even though it is usually preferable to work on packets that are 128-bit aligned, starting from Eigen 3.3, Eigen will also vectorize unaligned vectors and matrices at a last resort. So only tiny vectors of matrices that are smaller than a packet won't be vectorized. This is for instance the case of Vector3f which is smaller than a packet. However, you may be able to use Vector4f class to perform Vector3f operations and make use of vectorization if you can carefully ensure that the last component is always zero. . Moreover, Matrix2f internally stores 4 floats, and vectorization will automatically take place if the expression can be seen as a 1D vector of 4 elements (e.g., adding two Matrix2f).

The vectorization of unaligned matrices or vectors can be disabled by defining EIGEN_UNALIGNED_VECTORIZE=0. In this case fixed-size vectors and matrices whose size in bytes is are not a multiple of 16 bytes won't be vectorized. Vectors and matrices of dynamic-sizes will still be vectorized by enforcing aligned stores at runtime.

The same remarks apply for AVX which operates on packets of 32 bytes for which 32 bytes alignment is required. Since AVX can also operate with 16 bytes packets, the optimal packet size and alignment is automatically figured-out for fixed-size vectors and matrices. For instance, 16bytes alignement and packets will be used for Vector2d, Vector4f, Vector6d, etc. For dynamic-sizes vectors and matrices, only 32 bytes packets will be used, meaning that VectorXf(4) won't be vectorized with AVX whereas it will be vectorized with SSE only.


How can I check that vectorization is actually being used?



Vector4f a, b;
asm("#it begins here!")
a += b;
asm("#it ends here!")

See Studying assembly output for more information.


Why Eigen API is using signed integers for sizes, indices, etc.?

At a first glance it is indeed very tempting to use an unsigned type for values that are expected to be positive or nul. However, unsigned types relies on modulo 2^k arithmetic, which is definitely not what we want here. Moreover, in many places we can have negative indices and offsets. Mixing signed and unsigned integers is costly, error prone, and highly correlated to bugs. As a result, it is thus much simpler and safer to use signed integers everywhere, and reserve the usage of unsigned integers for bitwise manipulation and modulo 2^k arithmetic.

Hm... but the STL use unsigned integers everywhere. Right, but this has been acknowledge as a mistake by several members of the C++ standardization committee and C++ experts (Bjarne Stroustrup, Herb Sutter, Chandler Carruth, Eric Niebler, etc.). Sources:

* http://channel9.msdn.com/Events/GoingNative/2013/Interactive-Panel-Ask-Us-Anything (at 12mn, 42,mn and 63mn)
* http://ericniebler.com/2014/12/07/a-slice-of-python-in-c/#comment-92581




   void pinv( MatrixType& pinvmat) const
     SingularValuesType singularValues_inv=m_singularValues;
        if ( m_singularValues(i) > pinvtoler )
       else singularValues_inv(i)=0;



How to implement IIR filters?


Other languages

Can I use Eigen with Python?

Can I use Eigen with R?

  • The RcppEigen package provides bindings and more for R.

Can I use Eigen with Java?

  • jeigen, a Java wrapper for Eigen.