Why Eigen use Column-Major by default instead of Row-Major?
Asked Answered
L

3

8

Although Eigen is C++ library and C/C++ use row-major storage structure, why Eigen prefers to use column-major storage order? From Why does MATLAB use column-major order? post, I understand that MATLAB use it because of historical (due to FORTRAN) reasons. But Eigen is built on C++ at the beginning as far as I know.

In Eigen Library website it says:

The default in Eigen is column-major. Naturally, most of the development and testing of the Eigen library is thus done with column-major matrices. This means that, even though we aim to support column-major and row-major storage orders transparently, the Eigen library may well work best with column-major matrices. (retrieved from https://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html)

Maybe due to using C++ and OpenCV very frequently, my brain tends to think in row-major form. Are there any performance (cache locality, load/store speed etc.) reasons of making development and testing of Eigen library with column-major matrices?

Loot answered 10/4, 2020 at 12:48 Comment(1)
This might be related. Short answer is it doesn't matter.Reel
M
2

Actually, you are right, originally, for row-major stored data, row-major traversal has better cache locality, thus a higher cache hit rate.

Eigen stores data in col-major order by default. It seems a little bit counterintuitive,however, Eigen also traverses data in col-major order by default.

Also, you can find the official document here Eigen storage order: Algorithms that traverse a matrix row by row will go faster when the matrix is stored in row-major order because of better data locality. Similarly, column-by-column traversal is faster for column-major matrices.

Metatarsal answered 9/8, 2021 at 12:49 Comment(0)
P
2

In many domains, including Signal Processing, Vectors are considered to be Column vectors.

As a consequence, most of the signal-processing algorithms operate on the columns of the matrices. When someone implements such algorithms in C++ (which is row-major) the computational performance is hurt badly due to the heavy cache misses.

The default column-major order of Eigen is very handy on such occasions. This is the same reason why Matlab chose to be column-major.

Presocratic answered 31/10, 2023 at 15:17 Comment(0)
W
0

Eigen, as a linear algebra library, follows some mathematical conventions. The appearance of matrices and vectors arises from the need to solve linear systems of equations. Starting from this very primitive problem, mathematically, all vectors should be column vectors, so defining matrices as column matrices also aligns well with intuition. In the context of Eigen's applications, which are linear algebra calculations, using column vectors makes it more convenient for users.

Woermer answered 20/3 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.