Understanding Fortran_order and C_order 'order' Options for Matrices and Arrays
|
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.
|
|
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.
|
•
|
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.
>
|
|
| (2) |
>
|
|
| (3) |
In the second case, arrange the loop so that it accesses elements out of order.
>
|
|
| (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.
|
|