You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
4.1 KiB

  1. namespace Eigen {
  2. /** \page TopicStorageOrders Storage orders
  3. There are two different storage orders for matrices and two-dimensional arrays: column-major and row-major.
  4. This page explains these storage orders and how to specify which one should be used.
  5. <b>Table of contents</b>
  6. - \ref TopicStorageOrdersIntro
  7. - \ref TopicStorageOrdersInEigen
  8. - \ref TopicStorageOrdersWhich
  9. \section TopicStorageOrdersIntro Column-major and row-major storage
  10. The entries of a matrix form a two-dimensional grid. However, when the matrix is stored in memory, the entries
  11. have to somehow be laid out linearly. There are two main ways to do this, by row and by column.
  12. We say that a matrix is stored in \b row-major order if it is stored row by row. The entire first row is
  13. stored first, followed by the entire second row, and so on. Consider for example the matrix
  14. \f[
  15. A = \begin{bmatrix}
  16. 8 & 2 & 2 & 9 \\
  17. 9 & 1 & 4 & 4 \\
  18. 3 & 5 & 4 & 5
  19. \end{bmatrix}.
  20. \f]
  21. If this matrix is stored in row-major order, then the entries are laid out in memory as follows:
  22. \code 8 2 2 9 9 1 4 4 3 5 4 5 \endcode
  23. On the other hand, a matrix is stored in \b column-major order if it is stored column by column, starting with
  24. the entire first column, followed by the entire second column, and so on. If the above matrix is stored in
  25. column-major order, it is laid out as follows:
  26. \code 8 9 3 2 1 5 2 4 4 9 4 5 \endcode
  27. This example is illustrated by the following Eigen code. It uses the PlainObjectBase::data() function, which
  28. returns a pointer to the memory location of the first entry of the matrix.
  29. <table class="example">
  30. <tr><th>Example</th><th>Output</th></tr>
  31. <tr><td>
  32. \include TopicStorageOrders_example.cpp
  33. </td>
  34. <td>
  35. \verbinclude TopicStorageOrders_example.out
  36. </td></tr></table>
  37. \section TopicStorageOrdersInEigen Storage orders in Eigen
  38. The storage order of a matrix or a two-dimensional array can be set by specifying the \c Options template
  39. parameter for Matrix or Array. As \ref TutorialMatrixClass explains, the %Matrix class template has six
  40. template parameters, of which three are compulsory (\c Scalar, \c RowsAtCompileTime and \c ColsAtCompileTime)
  41. and three are optional (\c Options, \c MaxRowsAtCompileTime and \c MaxColsAtCompileTime). If the \c Options
  42. parameter is set to \c RowMajor, then the matrix or array is stored in row-major order; if it is set to
  43. \c ColMajor, then it is stored in column-major order. This mechanism is used in the above Eigen program to
  44. specify the storage order.
  45. If the storage order is not specified, then Eigen defaults to storing the entry in column-major. This is also
  46. the case if one of the convenience typedefs (\c Matrix3f, \c ArrayXXd, etc.) is used.
  47. Matrices and arrays using one storage order can be assigned to matrices and arrays using the other storage
  48. order, as happens in the above program when \c Arowmajor is initialized using \c Acolmajor. Eigen will reorder
  49. the entries automatically. More generally, row-major and column-major matrices can be mixed in an expression
  50. as we want.
  51. \section TopicStorageOrdersWhich Which storage order to choose?
  52. So, which storage order should you use in your program? There is no simple answer to this question; it depends
  53. on your application. Here are some points to keep in mind:
  54. - Your users may expect you to use a specific storage order. Alternatively, you may use other libraries than
  55. Eigen, and these other libraries may expect a certain storage order. In these cases it may be easiest and
  56. fastest to use this storage order in your whole program.
  57. - Algorithms that traverse a matrix row by row will go faster when the matrix is stored in row-major order
  58. because of better data locality. Similarly, column-by-column traversal is faster for column-major
  59. matrices. It may be worthwhile to experiment a bit to find out what is faster for your particular
  60. application.
  61. - The default in Eigen is column-major. Naturally, most of the development and testing of the Eigen library
  62. is thus done with column-major matrices. This means that, even though we aim to support column-major and
  63. row-major storage orders transparently, the Eigen library may well work best with column-major matrices.
  64. */
  65. }