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

Online Help

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  or  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  Matrix , the entries are stored as , so each complete column of the Matrix is stored before the next column.

  

For direct access to an element of the Fortran_order Matrix  in the data space, one must know the number of rows in , as the entry  is easily seen to be at an offset of  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  Matrix , the entries are stored as .

  

For direct access to an element of the C_order Matrix  in the data space, one must know the number of columns in , as the entry  is easily seen to be at an offset of  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 , with dimensions  stores the element  at an offset of  from the start of the data space, while for the same Array stored in Fortran_order, the offset would be given by .

• 

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 .

(1)

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

Warning, (in f1) `j` is implicitly declared local

Warning, (in f1) `i` is implicitly declared local

(2)

(3)

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

Warning, (in f2) `i` is implicitly declared local

Warning, (in f2) `j` is implicitly declared local

(4)

(5)

(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

 


Download Help Document