Subroutine arguments: arrays vs array slices 
Author Message
 Subroutine arguments: arrays vs array slices

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)?



Mon, 01 Jan 2007 21:38:08 GMT  
 Subroutine arguments: arrays vs array slices

Quote:

> 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



Mon, 01 Jan 2007 23:50:17 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Detecting uninitialized subroutine argument used as array bound of another argument

2. Multidimensional array vs. array of array

3. Whole arrays, array slices, and file I/O

4. how to use an array as subroutine arguments?

5. declaring array arguments in subroutines

6. Finding the size of array arguments in subroutines

7. A problem about using array as subroutine argument in F77

8. passing array arguments to subroutines

9. Allocatable arrays as arguments to subroutine questions

10. array/scalar subroutine arguments

11. Array intrinsics with zero-size array argument

12. assumed-size array arguments vs generic interfaces

 

 
Powered by phpBB® Forum Software