PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
output_checkpoint.cc
Go to the documentation of this file.
1 /* Copyright (C) 2017, 2019, 2022, 2023 PISM Authors
2  *
3  * This file is part of PISM.
4  *
5  * PISM is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 3 of the License, or (at your option) any later
8  * version.
9  *
10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with PISM; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "pism/icemodel/IceModel.hh"
21 
22 #include "pism/util/pism_utilities.hh"
23 #include "pism/util/Profiling.hh"
24 
25 namespace pism {
26 
27 //! Initialize checkpointing (snapshot-on-wallclock-time) mechanism.
29 
30  m_checkpoint_filename = m_config->get_string("output.checkpoint.file");
31 
32  if (m_checkpoint_filename.empty()) {
33  std::string output_file = m_config->get_string("output.file");
34  if (not output_file.empty()) {
35  m_checkpoint_filename = filename_add_suffix(output_file, "_checkpoint", "");
36  } else {
37  m_checkpoint_filename = "pism_checkpoint.nc";
38  }
39  }
40 
41  m_checkpoint_vars = output_variables(m_config->get_string("output.checkpoint.size"));
43 }
44 
45 //! Write a checkpoint (i.e. an intermediate result of a run).
46 /*!
47  * Returns `true` if PISM has to stop, `false` otherwise.
48  */
50 
51  double checkpoint_interval = m_config->get_number("output.checkpoint.interval");
52 
54 
55  if (wall_clock_hours - m_last_checkpoint_time < checkpoint_interval) {
56  return false;
57  }
58 
59  const Profiling &profiling = m_ctx->profiling();
60 
62 
63  // create a history string:
64 
65  m_log->message(2,
66  " [%s] Saving a checkpoint to '%s' (%1.3f hours after the beginning of the run)\n",
67  timestamp(m_grid->com).c_str(), m_checkpoint_filename.c_str(), wall_clock_hours);
68 
69  double checkpoint_start_time = get_time(m_grid->com);
70  profiling.begin("io.checkpoint");
71  {
72  File file(m_grid->com,
74  string_to_backend(m_config->get_string("output.format")),
76  m_ctx->pio_iosys_id());
77 
79  write_run_stats(file, run_stats());
80 
82  }
83  profiling.end("io.checkpoint");
84  double checkpoint_end_time = get_time(m_grid->com);
85 
86  // Also flush time-series:
88 
89  m_log->message(2,
90  " [%s] Done saving a checkpoint in %f seconds (%f minutes).\n",
91  timestamp(m_grid->com).c_str(),
92  checkpoint_end_time - checkpoint_start_time,
93  (checkpoint_end_time - checkpoint_start_time) / 60.0);
94 
95  return m_config->get_flag("output.checkpoint.exit");
96 }
97 
98 } // end of namespace pism
High-level PISM I/O class.
Definition: File.hh:56
double m_start_time
Definition: IceModel.hh:477
void init_checkpoints()
Initialize checkpointing (snapshot-on-wallclock-time) mechanism.
VariableMetadata run_stats() const
Definition: utilities.cc:92
const Config::Ptr m_config
Configuration flags and parameters.
Definition: IceModel.hh:243
const Time::Ptr m_time
Time manager.
Definition: IceModel.hh:251
double m_last_checkpoint_time
Definition: IceModel.hh:460
const std::shared_ptr< Context > m_ctx
Execution context.
Definition: IceModel.hh:245
const Logger::Ptr m_log
Logger.
Definition: IceModel.hh:249
void flush_timeseries()
Flush scalar time-series.
Definition: output_ts.cc:122
bool write_checkpoint()
Write a checkpoint (i.e. an intermediate result of a run).
std::set< std::string > m_checkpoint_vars
Definition: IceModel.hh:461
virtual void save_variables(const File &file, OutputKind kind, const std::set< std::string > &variables, double time, io::Type default_diagnostics_type=io::PISM_FLOAT) const
Definition: output.cc:169
virtual std::set< std::string > output_variables(const std::string &keyword)
Assembles a list of diagnostics corresponding to an output file size.
std::string m_checkpoint_filename
Definition: IceModel.hh:459
virtual void write_metadata(const File &file, MappingTreatment mapping_flag, HistoryTreatment history_flag) const
Write time-independent metadata to a file.
Definition: output.cc:72
const std::shared_ptr< Grid > m_grid
Computational grid.
Definition: IceModel.hh:241
void begin(const char *name) const
Definition: Profiling.cc:75
void end(const char *name) const
Definition: Profiling.cc:91
@ PISM_READWRITE_MOVE
create a file for writing, move foo.nc to foo.nc~ if present
Definition: IO_Flags.hh:78
double get_time(MPI_Comm comm)
io::Backend string_to_backend(const std::string &backend)
Definition: File.cc:57
double wall_clock_hours(MPI_Comm com, double start_time)
Return time since the beginning of the run, in hours.
std::string filename_add_suffix(const std::string &filename, const std::string &separator, const std::string &suffix)
Adds a suffix to a filename.
std::string timestamp(MPI_Comm com)
Creates a time-stamp used for the history NetCDF attribute.
void write_run_stats(const File &file, const pism::VariableMetadata &stats)
Definition: output.cc:162