The following is a simple example of taking a copy and then resizing, releasing memory for later use.
MODULE ARRAY_STORAGE
integer4, parameter :: million = 1000000
integer4 :: num = 150million ! 1.9gb
REAL8, ALLOCATABLE :: ARRAY(:)
integer4 :: test_count = 0
END MODULE ARRAY_STORAGE
!--------------------------------------------------------------------
PROGRAM MAIN
USE ARRAY_STORAGE
!
call test_allocate
call sub_allocate
call test_allocate
call sub_set
call test_allocate
call sub_print
call test_allocate
!
call sub_resize (num/2)
call test_allocate
!
call sub_print
call sub_deallocate
call test_allocate
!
END MAIN
!--------------------------------------------------------------------
subroutine sub_allocate
USE ARRAY_STORAGE
integer4 stat
!
ALLOCATE (ARRAY(num),stat=stat)
if (stat/=0) then
write (,) 'problem allocating array : stat=',stat
stop
else
write (*,11) ' array allocated as size ', size(array)*8./(2.**20),' mb at ',loc(array)
end if
11 format (a,f0.3,a,b'z,zzz,zzz,zz#')
end subroutine sub_allocate
subroutine sub_resize (num_new)
USE ARRAY_STORAGE
integer*4 num_new
!
integer*4 n
integer*4 stat
REAL*8, ALLOCATABLE :: temp_array(:)
!
write (*,*) 'Resizing array'
num = size (array)
n = min (num, num_new)
allocate (temp_array(n), stat=stat)
if (stat/=0) then
write (*,*) 'problem allocating temp_array : stat=',stat
stop
else
write (*,11) ' temp_array allocated as size ', size(temp_array)*8./(2.**20),' mb at ', loc(temp_array)
end if
11 format (a,f0.3,a,b'z,zzz,zzz,zz#')
!
temp_array(1:n) = array(1:n)
deallocate (array)
!
num = num_new
call sub_allocate
!
array(n+1:num) = 0
array(1:n) = temp_array
deallocate (temp_array)
!
end subroutine sub_resize
SUBROUTINE SUB_set
USE ARRAY_STORAGE
INTEGER*4 I
!
write (*,*) 'Initialising array'
DO I=1,num
ARRAY(I) = REAL(I,KIND=2)
END DO
RETURN
END SUBROUTINE SUB_set
subroutine sub_print
USE ARRAY_STORAGE
integer*4 i
!
write (*,*) 'First 10 values of array'
DO I=1,10
WRITE (*,*) ARRAY(I)
END DO
end subroutine sub_print
subroutine sub_deallocate
USE ARRAY_STORAGE
!
write (*,*) 'Releasing array'
DEALLOCATE (ARRAY)
end subroutine sub_deallocate
subroutine test_allocate
USE ARRAY_STORAGE
!
test_count = test_count+1
if (ALLOCATED (ARRAY) ) then
write (*,11) 'Test ',test_count,' ARRAY is allocated as ', size(array)*8./(2.**30),' gb at ',loc(array)
else
write (*,11) 'Test ',test_count,' ARRAY is NOT allocated'
end if
11 format (20x,a,i0,a,f0.3,a,b'z,zzz,zzz,zz#')
end subroutine test_allocate