(Fortran) Does order of accessing a matrix affect the performance?

In summary, the performance of two algorithms will be the same if they access their arrays in the same way.
  • #1
confi999
19
0
Hello,

In Fortran 90, will the performance (in terms of computational efficiency) of the following two be the same. If not, which one will be faster. Please advise with the reasoning:

(i)
do i = imin,imax
do j = jmin,jmax
do k = kmin,kmax

Several statements (rigorous arithmetic manipulation) involving a(i,j,k) and b(1,i,j,k)

end do
end do
end do


(ii)
do k = kmin,kmax
do j = jmin,jmax
do i = imin,imax

Several statements (rigorous arithmetic manipulation) involving a(i,j,k) and b(1,i,j,k)

end do
end do
end do


Thank you very much.
 
Technology news on Phys.org
  • #2
The performance should be the same for every case in every programming language.
 
  • #3
Yes it matters a lot! Always access your arrays as in your version (ii). I work in CFD where huge arrays of data are accessed frequently, and that's one of the first things my adviser told me to do to increase computational efficiency.
 
  • #4
minger said:
Yes it matters a lot! Always access your arrays as in your version (ii). I work in CFD where huge arrays of data are accessed frequently, and that's one of the first things my adviser told me to do to increase computational efficiency.

Could you explain, why this is so ? Is this a special of fortran ?
 
  • #5
Did some Googling:
http://www.enm.bris.ac.uk/staff/pjn/Programming/C-FORTRAN.html said:
Because of this behaviour you do not want to be continually jumping between, apparently, arbitrary memory locations as the computer will need to keep swapping pages around the system in order to get the one containing the memory address you want into the cache. This is illustrated by the two pairs of nested loops in the example programs stepping through a large two dimensional array.

Although C and FORTRAN allow us to have arrays with multi-dimensional indices, the computer's memory address space is only one dimensional. If we consider a simple 3x3 array in C (A[3][3]) then this is actually laid out in memory as:

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2]

i.e. the rightmost index is the most rapidly varying.

Whereas in FORTRAN the 3x3 array A(3,3) is laid out in memory as:

A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) A(1,3) A(2,3) A(3,3)

i.e. the leftmost index is the most rapidly varying.

If we have an array which is thousands of elements in each dimension then varying the first index by 1 in a C program, or the second index by 1 in a FORTRAN program, will take you into a different page which may not currently be readily accessible, thus causing a delay in processing. Consequently, it is important that loops are nested in the correct order to ensure that, wherever practical, you process all of the elemnents in the current page before going onto the next one, rather than continually processing a single element in a page and then jumping to a completely different one.

So, the answer is in the way that Fortran stores and accesses memory. It puts the left-most column as neighbors, so to avoid skipping around the memory, vary the indices from left to right.
 
  • #6
To quote Scientific Software Development with Fortran by Drew McCormack,
Why is loop order of any importance? This has to do with the order in which array elements are stored in the memory of a computer. In Fortran — unlike the popular C programming language — elements are stored in column major order. This means that elements along the columns of a two dimensional array (ie, matrix) are consecutive in memory. To be more concrete, in the example above, the element r(5,4) is just before r(6,4), but is not next to element r(5,5). The accompanying figure may help you visualize this.

When you loop over an array, you should try to do it in small steps through memory, because this allows for the most optimal use of the computer's memory architecture. A modern computer brings chunks of memory into its cache for the CPU to operate on; if the array elements in a series of operations are close together in memory, the CPU can perform many operations before it has to write the results back out to main memory.

If, on the other hand, the operations require big strides through memory, the computer will bring in a chunk of memory — technically called a cache line — but will only be able to operate on one or a few elements before having to write the chunk back out and get a new one. Retrieving and storing cache lines is a relatively time consuming operation on a modern computer.
 
  • #7
Thanks a lot for the question and the interesting answers. I oversaw the implications for memory handling. I apologize for my first answer, that was obviously totally wrong.
 
  • #8
Thank you so much for all the answers and the questions.
All these gave me the answer and the explanation that I was looking for.
Thanks a lot to everyone.
 
  • #9
Dear Sir,

I am using FORTRAN-90.
I have little experience and i have one problem.

I want to store data (i.e. X and Y values) in an array (A(900),B(900)) in such a way that at first I used only 30 elements of each array for storage 30 initial values of X and Y and then I have some scientific calculations to change the values of X and Y and then again want to store 30 modified values of X and Y in the same array from the 31th array element of both arrays. In this way I want to store my data and finally want to print these arrays.

Could you please help me out how I can write this algorithim I FORTRAN code…..

Please help me.

Please guide me

With best regards
 

Related to (Fortran) Does order of accessing a matrix affect the performance?

1. Does the order of accessing a matrix in Fortran affect its performance?

Yes, the order of accessing a matrix can have a significant impact on the performance of a Fortran program. This is because Fortran follows a column-major order, meaning that elements in a multi-dimensional array are stored in memory in a column-by-column fashion. Therefore, accessing elements in the same column will be faster compared to accessing elements in different columns, due to the way data is stored in memory.

2. How does the order of accessing a matrix affect the performance in Fortran?

The order of accessing a matrix in Fortran can affect performance in several ways. As mentioned before, accessing elements in the same column is faster than accessing elements in different columns. This is because accessing elements in the same column utilizes the processor's cache more efficiently. Additionally, the order of accessing can also affect the effectiveness of loop unrolling and vectorization, two optimization techniques commonly used in Fortran programs.

3. Can changing the order of accessing a matrix improve the performance in Fortran?

Yes, changing the order of accessing a matrix can potentially improve the performance in Fortran programs. By rearranging the order of accessing to access elements in the same column first, the program can utilize the processor's cache more efficiently, resulting in faster execution times. Additionally, other optimization techniques such as loop unrolling and vectorization can also be more effective with a different order of accessing.

4. Does the size of the matrix affect the performance when accessing in different orders in Fortran?

Yes, the size of the matrix can also affect the performance when accessing in different orders in Fortran. For smaller matrices, the difference in performance may not be significant. However, for larger matrices, the impact of accessing elements in different orders can be more pronounced, as the memory access patterns become more important in determining the efficiency of the program.

5. Is the order of accessing a matrix the only factor affecting performance in Fortran?

No, the order of accessing a matrix is not the only factor affecting performance in Fortran. Other factors such as the complexity of the algorithm, the compiler optimization level, and the hardware architecture also play important roles. However, the order of accessing a matrix is a crucial factor that should not be overlooked, as it can have a significant impact on the overall performance of a Fortran program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
762
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
9
Views
3K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top