Home > Archive > Fortran > April 2007 > how to "rename" an 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 "rename" an array
|
|
| Liang-Chun Liu 2007-04-14, 8:03 am |
| My apologies if this question is too stupid... :-P
I am wondering if there is any command offering the array"renaming"
capability
(something like the rename command in DOS operation)
Currently I just create an empty array and then feed the original
array into the new array....
But this looks clumsy and doubles the memory usage temporarily....
I could not locate this command in visual fortran....Can anyone give a
hand?
Thanks in advance.
| |
| Steve Lionel 2007-04-14, 8:03 am |
| On Apr 14, 7:42 am, "Liang-Chun Liu" <lancy...@gmail.com> wrote:
> I am wondering if there is any command offering the array"renaming"
> capability
> (something like the rename command in DOS operation)
>
> Currently I just create an empty array and then feed the original
> array into the new array....
> But this looks clumsy and doubles the memory usage temporarily....
I'm not entirely sure about your reference to "renaming". Your
description makes me think of the common desire to expand the size of
an allocatable array without doing two copies. Is that what you want?
The closest thing Fortran has to "renaming" is the MOVE_ALLOC
intrinsic of Fortran 2003. This lets you move the "allocation" of one
array (with its data) to another, the original array now being
unallocated. With this, expanding an array requires one copy and two
arrays allocated for a while, but it saves a second copy that would be
required without it.
Let's say you have an array A that you want to double its size while
preserving the original data.
Without MOVE_ALLOC you'd do this:
OLD_SIZE = SIZE(A)
ALLOCATE (TEMP_ARRAY(OLD_SIZE)
TEMP_ARRAY = A ! First copy
DEALLOCATE (A)
ALLOCATE (A(2*OLD_SIZE))
A(1:OLD_SIZE) = TEMP_ARRAY ! Second copy
DEALLOCATE (TEMP_ARRAY)
With MOVE_ALLOC you can do this:
ALLOCATE(TEMP_ARRAY(2*SIZE(A))
TEMP_ARRAY(1:SIZE(A)) = A
CALL MOVE_ALLOC (TEMP_ARRAY, A)
! TEMP_ARRAYis now unallocated
You say you are using "Visual Fortran" but don't provide further
details. MOVE_ALLOC is supported in Intel Visual Fortran 9.1.028 and
higher, not in Compaq or Digital Visual Fortran. For Intel Visual
Fortran, you'll currently find it described only in the Release Notes
and not the regular manuals.
Steve
| |
| Tobias Burnus 2007-04-14, 8:03 am |
| Hi,
On Apr 14, 1:42 pm, "Liang-Chun Liu" <lancy...@gmail.com> wrote:
> My apologies if this question is too stupid... :-P
> I am wondering if there is any command offering the array"renaming"
> capability
> (something like the rename command in DOS operation)
>
> Currently I just create an empty array and then feed the original
> array into the new array....
> But this looks clumsy and doubles the memory usage temporarily...
Depending what you want to do, you could use a pointer:
real, dimension(88) :: myArray
real, pointer, dimension(:) :: ptr
nullify(ptr)
myArray = initializeMyArray()
ptr => myArray
prt(5) ! accesses myArray(5)
Tobias
| |
| Liang-Chun Liu 2007-04-14, 7:04 pm |
| On 4=A4=EB14=A4=E9, =A4U=A4=C88=AE=C940=A4=C0, "Steve Lionel" <steve.lio...=
@intel.com> wrote:
> On Apr 14, 7:42 am, "Liang-Chun Liu" <lancy...@gmail.com> wrote:
>
>
>
> I'm not entirely sure about your reference to "renaming". Your
> description makes me think of the common desire to expand the size of
> an allocatable array without doing two copies. Is that what you want?
>
> The closest thing Fortran has to "renaming" is the MOVE_ALLOC
> intrinsic of Fortran 2003. This lets you move the "allocation" of one
> array (with its data) to another, the original array now being
> unallocated. With this, expanding an array requires one copy and two
> arrays allocated for a while, but it saves a second copy that would be
> required without it.
>
> Let's say you have an array A that you want to double its size while
> preserving the original data.
>
> Without MOVE_ALLOC you'd do this:
>
> OLD_SIZE =3D SIZE(A)
> ALLOCATE (TEMP_ARRAY(OLD_SIZE)
> TEMP_ARRAY =3D A ! First copy
> DEALLOCATE (A)
> ALLOCATE (A(2*OLD_SIZE))
> A(1:OLD_SIZE) =3D TEMP_ARRAY ! Second copy
> DEALLOCATE (TEMP_ARRAY)
>
> With MOVE_ALLOC you can do this:
>
> ALLOCATE(TEMP_ARRAY(2*SIZE(A))
> TEMP_ARRAY(1:SIZE(A)) =3D A
> CALL MOVE_ALLOC (TEMP_ARRAY, A)
> ! TEMP_ARRAYis now unallocated
>
> You say you are using "Visual Fortran" but don't provide further
> details. MOVE_ALLOC is supported in Intel Visual Fortran 9.1.028 and
> higher, not in Compaq or Digital Visual Fortran. For Intel Visual
> Fortran, you'll currently find it described only in the Release Notes
> and not the regular manuals.
>
> Steve
Thank you so much for the detailed answer.
In this post I was asking how to change the name of an array while
retain the content of it
i=2Ee., change a=3D[1,2,3] to b=3D[1,2,3]....
but actually your answer solved my problem.
My original question is: I need to dynamically maintain my array
for example: [1,4,4]=3D=3D>[1,4,4,6]=3D=3D>[1,4]
in the 1st process I need to add an element
in the 2nd process I need to remove 2 elements
for now I use a temp array as you described in the "without MOVE_ALLOC
process"
I'll check out Intel fortran later for this function.
| |
| Patrick Thrapp 2007-04-14, 7:04 pm |
| OK I may be dating myself here, but I would do...
PROGRAM MyProgram
INTEGER max
PARAMETER (max = 1000)
INTEGER A(max), B(max), C(max), me(max)
COMMON /rename/ A, B, C, me
CALL initialize(A)
CALL transforms(B)
CALL skew(me)
STOP
END
This is more like aliases, but might accomplish the same semantic as your
"rename"
"Liang-Chun Liu" <lancyliu@gmail.com> wrote in message
news:1176550975.002621.64910@e65g2000hsc.googlegroups.com...
> My apologies if this question is too stupid... :-P
>
> I am wondering if there is any command offering the array"renaming"
> capability
> (something like the rename command in DOS operation)
>
> Currently I just create an empty array and then feed the original
> array into the new array....
> But this looks clumsy and doubles the memory usage temporarily....
>
> I could not locate this command in visual fortran....Can anyone give a
> hand?
>
> Thanks in advance.
>
| |
| Jim Backus 2007-04-16, 10:04 pm |
| On Sat, 14 Apr 2007 19:22:16 UTC, "Patrick Thrapp"
<collector@garbage.dump> wrote:
> OK I may be dating myself here, but I would do...
>
> PROGRAM MyProgram
>
> INTEGER max
> PARAMETER (max = 1000)
>
> INTEGER A(max), B(max), C(max), me(max)
>
> COMMON /rename/ A, B, C, me
>
So would I ... COMMON is probably deprecated these days!
--
Jim Backus running OS/2 Warp 3 & 4, Debian Linux and Win98SE
bona fide replies to j <dot> backus <the circle thingy> jita <dot>
demon <dot> co <dot> uk
|
|
|
|
|