[Dart-dev] [5634] DART/branches/development/ensemble_manager/ensemble_manager_mod.f90: both write_ensemble_restart() and read_ensemble_restart() used

nancy at ucar.edu nancy at ucar.edu
Fri Mar 30 11:44:17 MDT 2012

Revision: 5634
Author:   nancy
Date:     2012-03-30 11:44:17 -0600 (Fri, 30 Mar 2012)
Log Message:
both write_ensemble_restart() and read_ensemble_restart() used
to declare a state-vector length array on the stack.  with very
large state vectors this could cause stack overflows.  write was 
changed a year ago to allocate the space only when it was needed
and not have a big array on the stack.  for some reason at that
time i didn't change read to do the same thing.  this update
makes both read and write use allocatable variables. i also changed
the write routine from using a pointer to an allocatable array;
functionally the same, but the compiler may be able to do slightly 
better optimization if it knows the array base can't change once 
allocated.   also move the random number type from module global 
storage into the single subroutine that uses it.  this code has 
an advantage in that the global ensemble number is available at 
the time the random number generator is initialized.  that value
is used as the seed, so in this subroutine the random perturbations 
are MPI task-count invariant. 

Modified Paths:

-------------- next part --------------
Modified: DART/branches/development/ensemble_manager/ensemble_manager_mod.f90
--- DART/branches/development/ensemble_manager/ensemble_manager_mod.f90	2012-03-30 17:13:55 UTC (rev 5633)
+++ DART/branches/development/ensemble_manager/ensemble_manager_mod.f90	2012-03-30 17:44:17 UTC (rev 5634)
@@ -68,9 +68,6 @@
 ! Logical flag for initialization of module
 logical              :: module_initialized = .false.
-! Module storage for a random sequence for perturbing a single initial state
-type(random_seq_type) :: random_seq
 ! Module storage for writing error messages
 character(len = 129) :: errstring
@@ -164,9 +161,8 @@
 ! goes in copy 6, the second in copy 7, etc. This lets higher level determine
 ! where other copies like mean, inflation, etc., are stored.
-! Would like to avoid num_vars size storage
-! LARGE ARRAY ON STACK -- make this a pointer and allocate it from heap?
-real(r8)                            :: ens(ens_handle%num_vars)
+! Avoid num_vars size storage on stack; make this allocatable from heap
+real(r8), allocatable               :: ens(:)
 integer                             :: iunit, i, j
 character(len = LEN(file_name) + 5) :: this_file_name
 character(len = 4)                  :: extension
@@ -174,6 +170,7 @@
 integer                             :: global_copy_index
 logical                             :: interf_provided
 logical                             :: single_file_override
+type(random_seq_type)               :: random_seq
 ! Does not make sense to have start_from_restart and single_restart_file_in BOTH false
 if(.not. start_from_restart .and. .not. single_restart_file_in) then
@@ -200,6 +197,7 @@
    single_file_override) then 
    ! Single restart file is read only by master_pe and then distributed
    if(my_pe == 0) iunit = open_restart_read(file_name)
+   allocate(ens(ens_handle%num_vars))   ! used to be on stack.
    ! Loop through the total number of copies
    do i = start_copy, end_copy
@@ -216,7 +214,8 @@
       call put_copy(0, ens_handle, i, ens, ens_time)
    end do
+   deallocate(ens)
    ! Master pe must close the file it's been reading
    if(my_pe == 0) call close_restart(iunit)
@@ -277,7 +276,7 @@
 logical, optional,    intent(in)    :: force_single_file
 ! Large temporary storage to be avoided if possible
-real(r8), pointer                   :: ens(:)
+real(r8), allocatable               :: ens(:)
 type(time_type)                     :: ens_time
 integer                             :: iunit, i, global_index
 integer                             :: owner, owners_index

More information about the Dart-dev mailing list