William F Mitchell wrote:
>
> Aleksandar Donev wrote:
> >
> > Hi,
> > > Although what Walt says is true
> >
> > Yes, of course, this was not the real program, just a quick and sloppy
> > example...
> >
> > > No, I don't think passing a rank 2 actual argument to a rank 1 dummy
> > > argument is "one of the main purposes of assumed-size arrays". One of the
> > main
> > > misuses of them, perhaps (if the interface is implicit).
> >
> > Well, say you are writing a routine that emulates RANDOM_NUMBER and you have
> > a serial generator. For contiguous arrays, the best thing you can do is use
> > an assumed-size array and just loop through its contents (stride 1 of
> > course) populating it with random numbers. Yes, of course, you could declare
> > its rank correctly and use nested DO loops to do the traversal but this is
> > clumsy and less efficient (the full program I am writing will be posted
> > soon!).
Would automatic arrays and reshape work? Something like
subroutine rank4(array)
real :: array(:,:,:,:)
real :: local(size(array))
do i = 1,size(local)
local(i) = .141592653 !as random as any other number!!!
enddo
array = reshape(local,(/ (size(array,i), i = 1,4) /) )
end subroutine rank4
If array is normally reasonably small then the automatic allocation
is likely to be efficiently done on the stack. The final copy with
the reshape is likely to be implemented as a nest of loops; but the
compiler might be able to do it more efficiently than you could. Also,
if the actual random generator is more complicated than the one I used
then the copy time probably won't make much difference.
Hope this helps,
Dick Hendrickson
> >
> > But anyway, assumed size arrays should really be used for efficiency, not
> > for ease-of-use, so I should just forget about my generic interface
> > (sigh...). So although I am aware of misuses and usually do not use
> > assumed-size arrays, this is not the case here...
>
> This might be a job for TRANSFER. Can't say as I employ it very often, if ever,
> but you might be able to write a specific under your generic name which takes a
> rank 2 array and passes it through transfer on the way to calling the rank 1 version.
> Oh, nope -- the argument would have to be intent(in), and yours is clearly intent(out).
> Well, maybe someone else has an idea along this line. As I said, transfer is not
> among my commonly used tools. Oh yea, I think I keep seeing arguments about whether
> it's likely to make a copy (loosing efficiency), too.
>
> Maybe I should stick to talking about things I know.
Hey Bill, it's never been a requirement for the rest of us!
> Bill
> --
> William F. Mitchell
> Mathematical and Computational Sciences Division
> National Institute of Standards and Technology
> [log in to unmask] http://math.nist.gov/~mitchell
|