Fortran and C Orders - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Understanding Fortran_order and C_order 'order' Options for Matrices and Arrays

 

Description

Examples

Description

• 

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 nxm 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 i1+nj1 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 nxm 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 mi1+j1 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 moi1+oj1+k1 from the start of the data space, while for the same Array stored in Fortran_order, the offset would be given by i1+nj1+nmk1.

• 

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.

Examples

As a simple example, consider the following two approaches to computing the Frobenius norm of a 2000 x 2000 Fortran_order Matrix A.

n2000:

ALinearAlgebra:-RandomMatrixn,generator=100...100.,outputoptions=datatype=float8,order=Fortran_order:

rtable_dimsA

1..2000,1..2000

(1)

In the first case, arrange the loop so that it accesses consecutive elements in the data space in order.

f1A,nsqrtaddaddAi,j2,i=1..n,j=1..n

f1A,naddaddAi,j2,i=1..n,j=1..n

(2)

t1timeevalhff1A,n

t10.368

(3)

In the second case, arrange the loop so that it accesses elements out of order.

f2A,nsqrtaddaddAi,j2,j=1..n,i=1..n

f2A,naddaddAi,j2,j=1..n,i=1..n

(4)

t2timeevalhff2A,n

t20.423

(5)

percent_savings100t2t1t1

percent_savings14.94565217

(6)

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.

See Also

Array

ArrayTools

Matrix

rtable

Vector