chi86
chi86

Reputation: 181

Passing an array to a subroutine

When I pass an array to a subroutine, does it shift the whole array or just an pointer to the memory path? My problem looks like:

program run
   real,dimension :: p(200,200,200)
   integer :: i

   do i=0,10000000
      call sub_p(p)
   enddo
end

subroutine sub_p(rhs)
   real,dimension :: rhs(200,200,200)

   ...

end

Becuase if the whole array is passed it probably takes longer regarding runtime?

@Vladimir: So in the code above a pointer is passed but if the code looks like the following the array is copied?:

program run
   real,dimension :: p(200,200,200)
   integer,parameter :: imax = 198
   integer,parameter :: jmax = 198
   integer,parameter :: kmax = 198
   integer :: ib,ie,jb,je,kb,ke
   integer :: i

   ib=1; ie=ib+imax
   jb=1; je=jb+jmax
   kb=1; ke=kb+kmax

   do i=0,10000000
      call sub_p(p(ib:ie,jb:je,kb:ke))
   enddo
end

subroutine sub_p(rhs)
   integer,parameter :: imax = 198
   integer,parameter :: jmax = 198
   integer,parameter :: kmax = 198
   real,dimension :: rhs(imax,jmax,kmax)

   ...

end

Am I right?

Upvotes: 1

Views: 106

Answers (1)

Yes, if the interface is implicit or the dummy argument is an explicit shape array, as you have, a pointer is passed. (The standard doesn't say that explicitly, but effectively it is the only possible implementation.

But it can be a pointer to a copy! If the array is not contiguous and the interface is implicit there will be a copy!

In your case:

no copy:

call sub_p(p)

a copy required:

call similar_sub(p(1,:,:))

Upvotes: 2

Related Questions