chi86
chi86

Reputation: 181

Copying arrays: loops vs. array operations

I work with Fortran now quite a long time but I have a question to which I can't find a satisfying answer. If I have two arrays and I want to copy one into the other:

real,dimension(0:100,0:100) :: array1,array2
...
do i=0,100
   do j=0,100
      array1(i,j) = array2(i,j)
   enddo
enddo

But I also noticed that it works as well if I do it like that:

real,dimension(0:100,0:100) :: array1,array2
...
array1 = array2

And there is a huge difference in computational time! (The second one is much faster!) If I do it without a loop can there be a problem because I don't know maybe I'm not coping the content just the memory reference? Does it change anything if I do another mathematical step like:

array1 = array2*5

Is there a problem on a different architecture (cluster server) or on a different compiler (gfortran, ifort)?

I have to perform various computational steps on huge amounts of data so the computational time is an issue.

Upvotes: 3

Views: 2771

Answers (2)

chw21
chw21

Reputation: 8140

Everything that @Alexander_Vogt said, but also:

do i=0,100
    do j=0,100
        array1(i,j) = array2(i,j)
    enddo
enddo

will always be slower than

do j=0,100
    do i=0,100
        array1(i,j) = array2(i,j)
    enddo
enddo

(Unless the compiler notices it and reorders the loops.)

In Fortran, the first parameter is the fastest changing. That means that in the second loop, the compiler can load several elements of the array in one big swoop in the lower level caches to do operations on.

If you have multidimensional loops, always have the innermost loop loop over the first index, and so on. (If possible in any way.)

Upvotes: 4

Alexander Vogt
Alexander Vogt

Reputation: 18118

Fortran is very capable of performing vector operations. Both

array1 = array2

and

array1 = array2*5

are valid operations. This notation allows the compiler to efficiently parallelize (and/or) optimize the code, as no dependence on the order of the operations exists.

However, these construct are equivalent to the explicit loops, and it depends on the compiler which one will be faster.

Whether the memory will be copied or not depends on what further is done with the arrays and whether the compiler can optimize that. If there is no performance gain, it is safe to assume the array will be copied.

Upvotes: 1

Related Questions