For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > Subroutine arguments: arrays vs array slices









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Subroutine arguments: arrays vs array slices
r08n

2004-07-15, 3:59 pm

If you need to pass two slices of a single bigger array to a
subroutine, what is
more efficient: to pass the slices, or to pass the whole array and the
corresponding indices? Smth like this:

1)

subroutine somesub1 (array1, array2)
real, intent(in) :: array1(:), array2(:)
...
end subroutine somesub1

call somesub1 (big_array(i:j), big_array(k:l))

2)

subroutine somesub2 (whole_array, i,j,k,l)
real, intent(in) :: whole_array(:)
integer, intent(in) :: i,j,k,l
! use whole_array(i:j)
! use whole_array(k:l)
end subroutine somesub2

call somesub2 (big_array, i,j,k,l)

Maybe it's a stupid question, but, considering various optimizations
performed by a compiler, the answer is not obvious for me. On the
other hand, because of
the same reason, is there much difference between 1) and 2)?
glen herrmannsfeldt

2004-07-15, 3:59 pm

r08n wrote:

> If you need to pass two slices of a single bigger array to a
> subroutine, what is
> more efficient: to pass the slices, or to pass the whole array and the
> corresponding indices? Smth like this:


It depends.

Under some conditions, a copy of the array will need to be
made, and not under other conditions.

Depending on how much you use it and the access patterns,
it may be more efficient, because of the way the processor
cache works, to use a contiguous copy.

In any case, be sure that you access the arrays as much
as possible such that the first subscript varies fastest.
(When possible, order the loops such that the inner loops
correspond to the leftmost subscripts.)

-- glen

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com