For Programmers: Free Programming Magazines  


Home > Archive > Fortran > June 2007 > How to enlarge an defined array?









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 How to enlarge an defined array?
li.simula@gmail.com

2007-06-18, 4:15 am

if I have defined an array of dimention n*n. Than I want enlarge it to
n+1*n+1 at next computation step. How can I do it with minmum use of
the memory???

Ian Bush

2007-06-18, 4:15 am

As if by magic, li.simula@gmail.com appeared !

> if I have defined an array of dimention n*n. Than I want enlarge it to
> n+1*n+1 at next computation step. How can I do it with minmum use of
> the memory???


By far the best way is to allocate it to ( n + 1 ) * ( n + 1 ) to begin
with,
and at the earlier step use only that portion which you require. This
obviously generalizes if you know the maximum number of steps you will ever
need.

If you don't know that you will have to allocate a new patch of memory,
copy the data over and deallocate, at least in Fortran95, In F2k you
can probably use move_alloc, but as I don't know too much about
this I won't say more,

Ian
paul.richard.thomas@gmail.com

2007-06-18, 8:06 am


>
> If you don't know that you will have to allocate a new patch of memory,
> copy the data over and deallocate, at least in Fortran95, In F2k you
> can probably use move_alloc, but as I don't know too much about
> this I won't say more,


Yes, indeed. There is an example in Metcalfe, Reid and Cohen that
explains this.

Otherwise, move_alloc.f90 from the gfortran testsuite also illustrates
this:

program test_move_alloc

implicit none
integer, allocatable :: x(:), y(:), temp(:)
character(4), allocatable :: a(:), b(:)
integer :: i

allocate (x(2))
allocate (a(2))

x = [ 42, 77 ]

call move_alloc (x, y)
if (allocated(x)) call abort()
if (.not.allocated(y)) call abort()
if (any(y /= [ 42, 77 ])) call abort()

a = [ "abcd", "efgh" ]
call move_alloc (a, b)
if (allocated(a)) call abort()
if (.not.allocated(b)) call abort()
if (any(b /= [ "abcd", "efgh" ])) call abort()

! Now one of the intended applications of move_alloc; resizing

call move_alloc (y, temp)
allocate (y(6), stat=i)
if (i /= 0) call abort()
y(1:2) = temp
y(3:) = 99
deallocate(temp)
if (any(y /= [ 42, 77, 99, 99, 99, 99 ])) call abort()
end program test_move_alloc

As Ian said, the cost is an extra allocation, so it is better to get
it right in the first place:)

Paul


glen herrmannsfeldt

2007-06-18, 8:06 am

paul.richard.thomas@gmail.com wrote:


(snip)
[color=darkred]
> As Ian said, the cost is an extra allocation, so it is better to get
> it right in the first place:)


And if you really do have to do an incremental growth, do it in fairly
large steps. For 1D arrays I usually increase in factors of 2 or 5/3.
For 2D you might want to increase the total size by a similar factor,
maybe increase each dimension by a factor of 1.4 or 1.3.

-- glen

li.simula@gmail.com

2007-06-18, 8:06 am

Thanks,
But my case is that when I analyse one step,the array(or matrix) will
be enlarge for the next step.
E.g:
the the first step:
K(10:10)
the second:
K(12:12)
and,the third step K became:
K(16:16)

Any good advise?


Ian Bush

2007-06-18, 10:08 pm

As if by magic, li.simula@gmail.com appeared !

> Thanks,
> But my case is that when I analyse one step,the array(or matrix) will
> be enlarge for the next step.
> E.g:
> the the first step:
> K(10:10)
> the second:
> K(12:12)
> and,the third step K became:
> K(16:16)
>
> Any good advise?


By far the best way is to allocate it to 16 * 16 to begin with,
and at the earlier steps use only that portion which you require. This
obviously generalizes if you know the maximum size you will ever
need.

If you don't know that you will have to allocate a new patch of memory,
copy the data over and deallocate, at least in Fortran95. In F2k you
can probably use move_alloc, but as I don't know too much about
this, but more than I did, I won't say more,

Ian

Michael Metcalf

2007-06-18, 10:08 pm


<li.simula@gmail.com> wrote in message
news:1182171958.747672.60050@e9g2000prf.googlegroups.com...
> Thanks,
> But my case is that when I analyse one step,the array(or matrix) will
> be enlarge for the next step.
> E.g:
> the the first step:
> K(10:10)
> the second:
> K(12:12)
> and,the third step K became:
> K(16:16)
>
> Any good advise?
>

Maybe define it as K(100, 100), or whatever, and address it as K(:n, :n),
gradually increasing n. Alternatively, define it as K-target(100, 100), or
whatever, and make K a pointer into it, as in

integer, dimension(100, 100), target :: K_target
integer, dimension(:, :), pointer :: K
:
K => K_target(:10, :10)
etc.

Regards,

Mike Metcalf



James Giles

2007-06-18, 10:08 pm

paul.richard.thomas@gmail.com wrote:
....
> ! Now one of the intended applications of move_alloc; resizing
>
> call move_alloc (y, temp)
> allocate (y(6), stat=i)
> if (i /= 0) call abort()
> y(1:2) = temp
> y(3:) = 99
> deallocate(temp)
> if (any(y /= [ 42, 77, 99, 99, 99, 99 ])) call abort()



For this purpose, another feature of F2003 might seem preferable.
Assuming your same variable declarations, the following works
without any use of the TEMP variable:

y = [y, (99,i=1,4)]

Indeed, to extend Y with N elements with value minus one at the
beginning and M elements with value 99 at the end:

y = [(-1,i=1,N), y, (99, j=1,M)]

There's no reason that the reallocation on assignment need be slower
than the use of move-alloc. Indeed, the assignment form seems to me
to be easier to optimize (with such things as reallocation in-place).
I've heard compiler people claim one is faster than the other (both
ways).

Reallocation of two-d arrays is harder to get the order right. But it's
still easier to read and verify in the assignment form.

The olnly real diffedrence between the two methods is tha the one
using move-alloc permits you to resize an array without specifying
values for the newly created space. I suppose it's possible to argue
that's an advantage.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


Ansterdick

2007-06-19, 12:31 am

Alyssa Milano and Helen Hunt XXXXing In Sauna!
http://www.shockingonline.com/e?movie=1673286
Sponsored Links







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

Copyright 2008 codecomments.com