Understanding Fortran_order and C_order 'order' Options for Matrices and Arrays
In Maple, construction of a Matrix or Array actually constructs two main objects, an rtable object, and an associated piece of one dimensional (1-D) memory (data space) used to store the contents of the rtable. The order=Fortran_order or order=C_order option describes how the rtable entries are laid out in the 1-D data space. The default is Fortran_order.
For Matrices, Fortran_order is usually referred to as column-major order. For an n⁢x⁢m Matrix A, the entries are stored as A[1,1],A[2,1],...,A[n,1],A[1,2],...,A[1,m],A[2,m],...,A[n,m], so each complete column of the Matrix is stored before the next column.
For direct access to an element of the Fortran_order Matrix A in the data space, one must know the number of rows in A, as the entry Ai,j is easily seen to be at an offset of i−1+n⁢j−1 units from the start of the data space.
C_order is usually referred to as row-major order, so each complete row of the Matrix is stored before the next row. For an n⁢x⁢m Matrix A, the entries are stored as A[1,1],A[1,2],...,A[1,m],A[2,1],...,A[n,1],A[n,2],...,A[n,m].
For direct access to an element of the C_order Matrix A in the data space, one must know the number of columns in A, as the entry Ai,j is easily seen to be at an offset of m⁢i−1+j−1 units from the start of the data space.
Note: For the special case of square matrices, the data storage of a C_order Matrix resembles the transpose of the same Matrix stored in Fortran_order, and vice versa.
The extension of these concepts to higher dimensional Arrays is the expected one, namely that Fortran_order stores data by the last dimension first, and that C_order stores data by the first dimension first.
For example, a three-dimensional C_order Array A, with dimensions 1..n,1..m,1..o stores the element Ai,j,k at an offset of m⁢o⁢i−1+o⁢j−1+k−1 from the start of the data space, while for the same Array stored in Fortran_order, the offset would be given by i−1+n⁢j−1+n⁢m⁢k−1.
Knowledge of the storage arrangement for rtable data can be essential in the design of algorithms that are efficient for large Matrices and Arrays. For efficient algorithms, you must access data in a way that consecutive data access is close to prior data access. This prevents the computer from having to swap out data to/from the memory of the system, instead allowing it to access data in the processor cache, which can be orders of magnitude faster.
Note: Use of the ArrayTools package requires knowledge of the storage arrangement, as its primary purpose is to provide methods of accessing, copying, or viewing the rtable data in an efficient and flexible manner.
As a simple example, consider the following two approaches to computing the Frobenius norm of a 2000 x 2000 Fortran_order Matrix A.
n ≔ 2000:
A ≔ LinearAlgebra:-RandomMatrix⁡n,generator=−100...100.,outputoptions=datatype=float8,order=Fortran_order:
In the first case, arrange the loop so that it accesses consecutive elements in the data space in order.
f1 ≔ A,n→add⁡add⁡Ai,j2,i=1..n,j=1..n
t1 ≔ time⁡evalhf⁡f1⁡A,n
In the second case, arrange the loop so that it accesses elements out of order.
f2 ≔ A,n→add⁡add⁡Ai,j2,j=1..n,i=1..n
t2 ≔ time⁡evalhf⁡f2⁡A,n
percent_savings ≔ 100⁢t2−t1t1
Though the time savings of accessing the data in the correct order is not substantial, it can be more pronounced for other algorithms, or if the algorithm is coded in a compiled language.
Download Help Document
What kind of issue would you like to report? (Optional)