In FORTRAN, arrays are continuously stored in the memory by column; an array is also continuous in the memory. For three-dimensional array (x, y, z), the rows, columns, and tabs of arrays x, y, and z are stored consecutively by column in the memory, the addresses of elements on each page in the memory are also consecutive. Taking a (, 2) as an example, its storage structure in the memory is as follows:
The current computer hardware architecture determines that when reading a large volume of data, if the batch of data is located in the near memory, the read operation will be executed quickly. Therefore, in specific circumstances, we may verify the storage structure of the array in the memory through the execution time of the program.
Module variable <br/> integer, parameter: Imax = 800, Jmax = 800, Kmax = 800 <br/> integer a (IMAX, Jmax, Kmax), B (IMAX, jmax, Kmax) <br/> end module variable <br/> Program Main <br/> use variable <br/> integer I, j, k <br/> real (kind = 8) Start, end <br/> call cpu_time (start) <br/> do 10 k = 1, kmax <br/> do 10 I = 1, IMAX <br/> do 10 j = 1, Jmax <br/> A (I, j, k) = I + J + k <br/> 10 continue <br/> call cpu_time (end) <br/> Print *, end-start <br/> call cpu_time (start) <br/> do 20 k = 1, Kmax <br/> do 20 j = 1, Jmax <br/> do 20 I = 1, IMAX <br/> B (I, j, k) = I + J + k <br/> 20 continue <br/> call cpu_time (end) <br/> Print *, end-start <br/> call cpu_time (start) <br/> do 30 j = 1, Jmax <br/> do 30 I = 1, IMAX <br/> do 30 k = 1, Kmax <br/> B (I, j, k) = I + J + k <br/> 30 continue <br/> call cpu_time (end) <br/> Print *, end-start <br/> call cpu_time (start) <br/> do 40 I = 1, IMAX <br/> do 40 j = 1, Jmax <br/> do 40 k = 1, kmax <br/> B (I, j, k) = I + J + k <br/> 40 continue <br/> call cpu_time (end) <br/> Print *, end-start <br/> end <br/>
Result:
[Root @ c0109 zlt] # gfortran hello. f-O hello <br/> [root @ c0109 zlt] #. /Hello <br/> 7.25245300000000 <br/> 6.89643000000000 <br/> 12.9048070000000 <br/> 66.9721850000000 <br/> [root @ c0109 zlt] # <br/>
Appendix: Verify the storage by page of the array
Program pack <br/> integer, parameter: Imax = 2, Jmax = 2, Kmax = 4 <br/> integer Arr (IMAX, Jmax, Kmax), I, J, k <br/> do 10 k = 1, Kmax <br/> do 10 j = 1, Jmax <br/> do 10 I = 1, IMAX <br/> Arr (I, j, k) = k <br/> 10 continue <br/> Print *, arr <br/> stop <br/> end <br/>
Result:
[Root @ c0109 zlt] # gfortran ABC. f-o ABC <br/> [root @ c0109 zlt] #. /ABC <br/> 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 <br/> [root @ c0109 zlt] # <br/>