I actually have this data locality as a possible problem for why my fortran program runs somewhat slow. In one part of this program, I have nested loops and throughout these loops, a given section of a big array is referenced multiple times. The pseudocode goes like this
subroutine foo()
use mymodule, only : big_array
...
do i = 1, n1
do j = 1, n2
! invoke big_array(i,j)
end
end
end subroutine
The value of n1
and n2
can be tens of thousands. Since big_array is allocated on heap (it's an allocatable array), I have long suspected that the repeated reference to big_array elements in the above loops may contribute to the slowing down of the program. But I have never been able to set my doubt clear on this as I only have a very basic knowledge of how stack and heap memory work. If I were to utilize stack allocated data, I can declare a local automatic array before the nested loop and store the needed section of big_array in this stack-allocated local array, so that the new array is closer in memory to the nested loop but this also incures an additional cpu time when the program needs to allocate the local array. So, I don't know which one is better than which.
Can someone also give me the idea of just how much slower a program can get if it involves reading and writing to memory addresses far enough from the point where the reference is made?