Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.2-d6b3a29ca committed by Constantine Khrulev on 2025-03-28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
output_checkpoint.cc
Go to the documentation of this file.
1/* Copyright (C) 2017, 2019, 2022, 2023, 2024 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
25namespace 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",
68
69 double checkpoint_start_time = get_time(m_grid->com);
70 profiling.begin("io.checkpoint");
71 {
72 // Note: we open a new file every time we write a checkpoint, moving the old file
73 // aside if it exists.
74 File file(m_grid->com,
76 string_to_backend(m_config->get_string("output.format")),
78
81
83 }
84 profiling.end("io.checkpoint");
85 double checkpoint_end_time = get_time(m_grid->com);
86
87 // Also flush time-series:
89
90 m_log->message(2,
91 " [%s] Done saving a checkpoint in %f seconds (%f minutes).\n",
92 timestamp(m_grid->com).c_str(),
93 checkpoint_end_time - checkpoint_start_time,
94 (checkpoint_end_time - checkpoint_start_time) / 60.0);
95
96 return m_config->get_flag("output.checkpoint.exit");
97}
98
99} // end of namespace pism
High-level PISM I/O class.
Definition File.hh:55
double m_start_time
Definition IceModel.hh:474
void init_checkpoints()
Initialize checkpointing (snapshot-on-wallclock-time) mechanism.
VariableMetadata run_stats() const
Definition utilities.cc:91
const Time::Ptr m_time
Time manager.
Definition IceModel.hh:246
std::shared_ptr< Context > m_ctx
Execution context.
Definition IceModel.hh:240
double m_last_checkpoint_time
Definition IceModel.hh:458
const Logger::Ptr m_log
Logger.
Definition IceModel.hh:244
void flush_timeseries()
Flush scalar time-series.
Definition output_ts.cc:124
bool write_checkpoint()
Write a checkpoint (i.e. an intermediate result of a run).
std::set< std::string > m_checkpoint_vars
Definition IceModel.hh:459
Config::Ptr m_config
Configuration flags and parameters.
Definition IceModel.hh:238
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:150
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:457
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:236
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:74
double get_time(MPI_Comm comm)
io::Backend string_to_backend(const std::string &backend)
Definition File.cc:56
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:143