Re: ALLOCATABLE arrays
beliavsky_at_aol.com
Date: 01/05/04
- Next message: Richard Maine: "Re: ALLOCATABLE arrays"
- Previous message: Scott Robert Ladd: "What does a Fortran Datastore Need?"
- In reply to: Jugoslav Dujic: "Re: ALLOCATABLE arrays"
- Next in thread: Richard Maine: "Re: ALLOCATABLE arrays"
- Reply: Richard Maine: "Re: ALLOCATABLE arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 5 Jan 2004 08:03:41 -0800
"Jugoslav Dujic" wrote
<SNIP>
> I'd also suggest that you take a look at automatic arrays. They're
> less flexible than allocatables, but are great for stuff like
> temporary work arrays:
>
> subroutine sub1(worksize)
> integer:: worksize
> real:: work(worksize)
>
> work will get allocated on every entry but that takes (almost) a
> zilch of computing time, as they're allocated on the stack and
> about the only operation required is to increase the stack pointer.
> On the contrast, frequent allocation/deallocation of allocatables
> can yield a huge performance penalty. Of course, you have to
> know worksize on entry to sub1 (but as I've read your post, that
> is the case); worksize has to be known at entry-time either as
> a dummy argument or a member of COMMON or MODULE.
The two subroutines "xwork" and "xalloc" below are equivalent, except
that the first uses an automatic array and the second an ALLLOCATABLE
array. Why should the first one be significantly faster? In both
cases, memory is allocated at the beginning of the subroutine and
deallocated at the end. An allocatable array is more flexible than an
automatic one -- it can be allocated and deallocated many times in a
subroutine (a feature I almost never take advantage of), whereas the
automatic array has a constant size. Is this the source of the
performance difference?
I often use use allocatable arrays instead of automatic arrays as a
convenience. I can store the sizes of the dummy arguments as integers
and specify the allocatable array size using these integers. When this
is done for
several allocatable arrays, I think this is more readable than having
several
automatic arrays with syntax like
real :: yy(size(xx,1),size(xx,2)),zz(size(xx,2))
But Jugoslav Dujic seems to be saying that for procedures where speed
is critical, automatic arrays should be used when possible.
subroutine xwork(xx)
real, intent(in out) :: xx(:)
real :: yy(size(xx))
! do stuff
end subroutine xwork
subroutine xalloc(xx)
real, intent(in out) :: xx(:)
real, allocatable :: yy(:)
allocate (yy(size(xx)))
! do stuff
deallocate (yy)
end subroutine xalloc
- Next message: Richard Maine: "Re: ALLOCATABLE arrays"
- Previous message: Scott Robert Ladd: "What does a Fortran Datastore Need?"
- In reply to: Jugoslav Dujic: "Re: ALLOCATABLE arrays"
- Next in thread: Richard Maine: "Re: ALLOCATABLE arrays"
- Reply: Richard Maine: "Re: ALLOCATABLE arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|