Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 17, 2010 #1
    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.
     
  2. jcsd
  3. Mar 17, 2010 #2
    The performance should be the same for every case in every programming language.
     
  4. Mar 17, 2010 #3

    minger

    User Avatar
    Science Advisor

    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.
     
  5. Mar 17, 2010 #4
    Could you explain, why this is so ? Is this a special of fortran ?
     
  6. Mar 17, 2010 #5

    minger

    User Avatar
    Science Advisor

    Did some Googling:
    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.
     
  7. Mar 17, 2010 #6
    To quote Scientific Software Development with Fortran by Drew McCormack,
     
  8. Mar 17, 2010 #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.
     
  9. Mar 18, 2010 #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.
     
  10. May 15, 2012 #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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: (Fortran) Does order of accessing a matrix affect the performance?
Loading...