1

I have the code kind of like this in F90:

real(8), dimension(10,10,10) :: A

do i = 1, 1000

print*,A(i,1,1)

enddo

I'm very surprised this worked and it's faster than simply looping over 3 dimensions by i,j,k.

Can someone please explain why this works?

2
  • Faster than what exactly? What exactly do you not understand in the code? Commented Sep 2, 2017 at 21:17
  • the array A has the dimension of 10X10X10, so why does the code I provided above work when the index i has exceeded 10? And it's faster than below, do i=1,10 do j =1,10 do k=1,10 print*,A(i,j,k) enddo enddo enddo Commented Sep 2, 2017 at 21:25

1 Answer 1

5

Your code is illegal. But under the hood the memory layout of the array happens to be in the column major order as in the triple loop k,j,i, so the code appears to work. But it is illegal.

If you enable runtime error checks in your compiler (see the manual), it will find the error and report it.

It may be slightly faster, if you do not enable compiler optimizations, because there is some overhead in nested loops, but an optimizing compiler will optimize the code to one loop.

If you actually did (you should always show your code!!!)

do i=1,10
  do j=1,10
    do k=1,10
      something with A(i,j,k)

then please note that that is the wrong order and you should loop in the k,j,i order.

Also please note that measuring the speed of printing to the screen is not useful and can be very tricky. Some mathematical operations are more useful.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for you answer! Although I complied with flag mpif90 -O3 -heaparrays -cpp and the situation with A(i,1,1) is almost 10 times faster than A(i,j,k). And what I did with A is some computation but print. The code above is what I learned from an open source code. Very confused by it
Printing 1000 lines is not a very good example of a real bottleneck. Trying to run timings on something that small and convoluted can be misleading.
In the 3D case which order did you perform the loops in? As Vladimir says this is very important and can in many cases lead to an order of magnitude difference in performance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.