MOVE_ALLOC(3) - [MEMORY] Move allocation from one object to another
call move_alloc(from, to [,stat] [,errmsg] )
subroutine move_alloc(from, to)type(TYPE(kind=**)),intent(inout),allocatable :: from(..) type(TYPE(kind=**)),intent(out),allocatable :: to(..) integer(kind=**),intent(out) :: stat character(len=*),intent(inout) :: errmsg
o FROM may be of any type and kind. o TO shall be of the same type, kind and rank as FROM.
MOVE_ALLOC(3) moves the allocation from FROM to TO. FROM will become deallocated in the process.
This is potentially more efficient than other methods of assigning the values in FROM to TO and explicitly deallocating FROM, which are far more likely to require a temporary object or a copy of the elements of the array.
o FROM : The data object to be moved to TO and deallocated. o TO : The destination data object to move the allocated data object FROM to. Typically, it is a different shape than FROM. o STAT : If STAT is present and execution is successful, it is assigned the value zero. Otherwise, if an error condition occurs:
o if STAT is absent, error termination is initiated; o otherwise, if FROM is a coarray and the current team contains a stopped image, STAT is assigned the value STAT_STOPPED_IMAGE from the intrinsic module ISO_FORTRAN_ENV; o otherwise, if FROM is a coarray and the current team contains a failed image, and no other error condition occurs, STAT is assigned the value STAT_FAILED_IMAGE from the intrinsic module ISO_FORTRAN_ENV; o otherwise, STAT is assigned a processor-dependent positive value that differs from that of STAT_STOPPED_IMAGE or STAT_FAILED_IMAGE. o ERRMSG : If the ERRMSG argument is present and an error condition occurs, it is assigned an explanatory message. If no error condition occurs, the definition status and value of ERRMSG are unchanged.
Basic sample program to allocate a bigger grid
program demo_move_alloc implicit none ! Example to allocate a bigger GRID real, allocatable :: grid(:), tempgrid(:) integer :: n, iResults:! initialize small GRID n = 3 allocate (grid(1:n)) grid = [ (real (i), i=1,n) ]
! initialize TEMPGRID which will be used to replace GRID allocate (tempgrid(1:2*n)) ! Allocate bigger grid tempgrid(::2) = grid ! Distribute values to new locations tempgrid(2::2) = grid + 0.5 ! initialize other values
! move TEMPGRID to GRID call MOVE_ALLOC (from=tempgrid, to=grid)
! TEMPGRID should no longer be allocated ! and GRID should be the size TEMPGRID was if (size (grid) /= 2*n .or. allocated (tempgrid)) then print *, "Failure in move_alloc!" endif print *, allocated(grid), allocated(tempgrid) print (99f8.3), grid end program demo_move_alloc
> T F > 1.000 1.500 2.000 2.500 3.000 3.500
Fortran 2003, STAT and ERRMSG options added 2018
ALLOCATED(3)
Fortran intrinsic descriptions
Nemo Release 3.1 | move_alloc (3fortran) | November 02, 2024 |