Storage orders

There are two different storage orders for matrices and two-dimensional arrays: column-major and row-major. This page explains these storage orders and how to specify which one should be used.

Column-major and row-major storage

The entries of a matrix form a two-dimensional grid. However, when the matrix is stored in memory, the entries have to somehow be laid out linearly. There are two main ways to do this, by row and by column.

We say that a matrix is stored in row-major order if it is stored row by row. The entire first row is stored first, followed by the entire second row, and so on. Consider for example the matrix

\[ A = \begin{bmatrix} 8 & 2 & 2 & 9 \\ 9 & 1 & 4 & 4 \\ 3 & 5 & 4 & 5 \end{bmatrix}. \]

If this matrix is stored in row-major order, then the entries are laid out in memory as follows:

8 2 2 9 9 1 4 4 3 5 4 5

On the other hand, a matrix is stored in column-major order if it is stored column by column, starting with the entire first column, followed by the entire second column, and so on. If the above matrix is stored in column-major order, it is laid out as follows:

8 9 3 2 1 5 2 4 4 9 4 5

This example is illustrated by the following Eigen code. It uses the PlainObjectBase::data() function, which returns a pointer to the memory location of the first entry of the matrix.

Example Output
Matrix<int, 3, 4, ColMajor> Acolmajor;
Acolmajor << 8, 2, 2, 9,
9, 1, 4, 4,
3, 5, 4, 5;
cout << "The matrix A:" << endl;
cout << Acolmajor << endl << endl;
cout << "In memory (column-major):" << endl;
for ( int i = 0; i < Acolmajor.size(); i++)
cout << *(Acolmajor.data() + i) << " ";
cout << endl << endl;
Matrix<int, 3, 4, RowMajor> Arowmajor = Acolmajor;
cout << "In memory (row-major):" << endl;
for ( int i = 0; i < Arowmajor.size(); i++)
cout << *(Arowmajor.data() + i) << " ";
cout << endl;
The matrix A:
8 2 2 9
9 1 4 4
3 5 4 5

In memory (column-major):
8  9  3  2  1  5  2  4  4  9  4  5  

In memory (row-major):
8  2  2  9  9  1  4  4  3  5  4  5  

Storage orders in Eigen

The storage order of a matrix or a two-dimensional array can be set by specifying the Options template parameter forMatrix or Array. As The Matrix class explains, the Matrix class template has six template parameters, of which three are compulsory (ScalarRowsAtCompileTime and ColsAtCompileTime) and three are optional (OptionsMaxRowsAtCompileTimeand MaxColsAtCompileTime). If the Options parameter is set to RowMajor, then the matrix or array is stored in row-major order; if it is set to ColMajor, then it is stored in column-major order. This mechanism is used in the above Eigenprogram to specify the storage order.

If the storage order is not specified, then Eigen defaults to storing the entry in column-major. This is also the case if one of the convenience typedefs (Matrix3fArrayXXd, etc.) is used.

Matrices and arrays using one storage order can be assigned to matrices and arrays using the other storage order, as happens in the above program when Arowmajor is initialized using AcolmajorEigen will reorder the entries automatically. More generally, row-major and column-major matrices can be mixed in an expression as we want.

Which storage order to choose?

So, which storage order should you use in your program? There is no simple answer to this question; it depends on your application. Here are some points to keep in mind:

  • Your users may expect you to use a specific storage order. Alternatively, you may use other libraries thanEigen, and these other libraries may expect a certain storage order. In these cases it may be easiest and fastest to use this storage order in your whole program.
  • 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. It may be worthwhile to experiment a bit to find out what is faster for your particular application.
  • 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值