MONC
checkpointer.F90
Go to the documentation of this file.
1 
6  use datadefn_mod, only : string_length
8  use state_mod, only : model_state_type
14  implicit none
15 
16 #ifndef TEST_MODE
17  private
18 #endif
19 
20  character(len=STRING_LENGTH), save :: checkpoint_file
21  logical, save :: unique_per_dump, & !< Whether to make each model dump a unique filename
23  integer, save :: checkpoint_frequency
24 
26 
27 contains
28 
32  checkpointer_get_descriptor%name="checkpointer"
33  checkpointer_get_descriptor%version=0.1
37  end function checkpointer_get_descriptor
38 
42  subroutine initialisation_callback(current_state)
43  type(model_state_type), target, intent(inout) :: current_state
44 
45  character(len=STRING_LENGTH) :: internal_write_mode
46 
47  checkpoint_frequency=options_get_integer(current_state%options_database, "checkpoint_frequency")
48  checkpoint_file=options_get_string(current_state%options_database, "checkpoint_file")
49  unique_per_dump=options_get_logical(current_state%options_database, "checkpoint_unique_per_dump")
50  internal_write_mode=options_get_string(current_state%options_database, "checkpoint_internal_write")
51  if (trim(internal_write_mode) .eq. "always") then
52  enable_write=.true.
53  else if (trim(internal_write_mode) .eq. "never") then
54  enable_write=.false.
55  else
56  ! Auto mode
57  enable_write=.not. current_state%io_server_enabled
58  end if
59 
60  if (options_has_key(current_state%options_database, "checkpoint")) then
61  call read_checkpoint_file(current_state, options_get_string(current_state%options_database, "checkpoint"))
62  end if
63  end subroutine initialisation_callback
64 
67  subroutine timestep_callback(current_state)
68  type(model_state_type), target, intent(inout) :: current_state
69 
70  if (enable_write .and. checkpoint_frequency .gt. 0) then
71  if (mod(current_state%timestep, checkpoint_frequency) == 0) call perform_checkpoint_dump(current_state)
72  end if
73  end subroutine timestep_callback
74 
77  subroutine finalisation_callback(current_state)
78  type(model_state_type), target, intent(inout) :: current_state
79 
80  if (enable_write) call perform_checkpoint_dump(current_state)
81  end subroutine finalisation_callback
82 
86  subroutine perform_checkpoint_dump(current_state)
87  type(model_state_type), target, intent(inout) :: current_state
88 
89  character(len=STRING_LENGTH) :: unique_fn
90  real :: start_dump_time, end_dump_time
91  integer :: ierr
92 
93  call cpu_time(start_dump_time)
94  if (unique_per_dump) then
95  call generate_unique_filename(current_state, unique_fn)
96  call write_checkpoint_file(current_state, unique_fn)
97  else
98  call write_checkpoint_file(current_state, checkpoint_file)
99  end if
100  ! Barrier here to ensure all processes dumped before log_log stats (is there a better way?)
101  call mpi_barrier(current_state%parallel%monc_communicator, ierr)
102  call cpu_time(end_dump_time)
103  call log_dump_stats(current_state, start_dump_time, end_dump_time)
104  end subroutine perform_checkpoint_dump
105 
110  subroutine log_dump_stats(current_state, start_time, end_time)
111  type(model_state_type), intent(inout) :: current_state
112  real :: start_time, end_time
113 
114  call log_master_newline()
115  call log_master_log(log_info, "Model dump completed in "//trim(conv_to_string(int((end_time-start_time)*1000)))//"ms")
116  end subroutine log_dump_stats
117 
122  subroutine generate_unique_filename(current_state, new_name)
123  type(model_state_type), intent(inout) :: current_state
124  character(len=STRING_LENGTH), intent(out) :: new_name
125 
126  integer :: dot_posn
127 
128  dot_posn=index(checkpoint_file, ".")
129  if (dot_posn .gt. 0) then
130  new_name = checkpoint_file(1:dot_posn-1)
131  else
132  new_name=checkpoint_file
133  end if
134  new_name=trim(new_name)//"_"//trim(conv_to_string(current_state%timestep))
135  if (dot_posn .gt. 0) then
136  new_name=trim(new_name)//checkpoint_file(dot_posn:len(checkpoint_file))
137  end if
138  end subroutine generate_unique_filename
139 end module checkpointer_mod
subroutine, public write_checkpoint_file(current_state, filename)
Will write out the current model state_mod into a NetCDF checkpoint file.
integer, save checkpoint_frequency
subroutine, public read_checkpoint_file(current_state, filename)
Reads in a NetCDF checkpoint file and uses this to initialise the model.
character(len=string_length) function, public options_get_string(options_database, key, index)
Retrieves a string value from the database that matches the provided key.
Logging utility.
Definition: logging.F90:2
subroutine timestep_callback(current_state)
The timestep hook will dump out model state_mod to a checkpoint file.
logical, save enable_write
subroutine finalisation_callback(current_state)
Called on termination to write out the status of the model run to checkpoint.
Contains common definitions for the data and datatypes used by MONC.
Definition: datadefn.F90:2
The ModelState which represents the current state of a run.
Definition: state.F90:39
subroutine, public log_master_log(level, message)
Will log just from the master process.
Definition: logging.F90:47
Conversion between common inbuilt FORTRAN data types.
Definition: conversions.F90:5
Converts data types to strings.
Definition: conversions.F90:36
subroutine, public log_master_newline()
The master process will log a new line to stdio.
Definition: logging.F90:59
subroutine initialisation_callback(current_state)
Initialisation hook, if appropriate (depends on command line arguments) then will read in an existing...
Checkpointing NetCDF functionality.
Definition: checkpointer.F90:5
Interfaces and types that MONC components must specify.
Writes out model state_mod to a checkpoint NetCDF file.
subroutine generate_unique_filename(current_state, new_name)
Generates a unique filename based upon the base one specified and the number of completed timesteps...
integer, parameter, public string_length
Default length of strings.
Definition: datadefn.F90:10
Will read in a NetCDF checkpoint file and initialise the model state_mod based upon this...
subroutine perform_checkpoint_dump(current_state)
Performs the checkpoint dump and timings. This can be called as part of the timestep or at the end of...
type(component_descriptor_type) function, public checkpointer_get_descriptor()
Provides registry information for the component.
integer function, public options_get_integer(options_database, key, index)
Retrieves an integer value from the database that matches the provided key.
Manages the options database. Contains administration functions and deduce runtime options from the c...
integer, parameter, public log_info
Log INFO, WARNING and ERROR messages.
Definition: logging.F90:13
character(len=string_length), save checkpoint_file
The checkpoint write file base name.
logical function, public options_get_logical(options_database, key, index)
Retrieves a logical value from the database that matches the provided key.
subroutine log_dump_stats(current_state, start_time, end_time)
Will dump out the model dump statistics.
logical function, public options_has_key(options_database, key)
Determines whether a specific key is in the database.
The model state which represents the current state of a run.
Definition: state.F90:2
logical, save unique_per_dump
Whether to make each model dump a unique filename.