PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
utilities.cc
Go to the documentation of this file.
1 // Copyright (C) 2004-2019, 2021, 2023 Jed Brown, Ed Bueler and Constantine Khroulev
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 #include <cstring>
20 #include <petscsys.h>
21 
22 #include "pism/icemodel/IceModel.hh"
23 
24 #include "pism/util/Grid.hh"
25 #include "pism/util/ConfigInterface.hh"
26 #include "pism/util/Time.hh"
27 #include "pism/util/io/File.hh"
28 #include "pism/util/pism_utilities.hh"
29 #include "pism/util/pism_signal.h"
30 
31 namespace pism {
32 
33 //! Catch signals -USR1, -USR2 and -TERM.
34 /*!
35 Signal `SIGTERM` makes PISM end, saving state under original `-o` name
36 (or default name). We also add an indication to the history attribute
37 of the output NetCDF file.
38 
39 Signal `SIGUSR1` makes PISM save state under a filename based on the
40 the name of the executable (e.g. `pismr` or `pismv`) and the current
41 model year. In addition the time series (`-ts_file`, etc.) is flushed out
42 There is no indication of these actions in the history attribute of the output (`-o`)
43 NetCDF file because there is no effect on it, but there is an indication at `stdout`.
44 
45 Signal `SIGUSR2` makes PISM flush time-series, without saving model state.
46  */
48 
49  if (pism_signal == SIGTERM) {
50  m_log->message(1,
51  "\ncaught signal SIGTERM: EXITING EARLY and saving with original filename.\n");
52 
53  prepend_history(pism::printf("EARLY EXIT caused by signal SIGTERM. Completed timestep at time=%s.",
54  m_time->date(m_time->current()).c_str()));
55  // Tell the caller that the user requested an early termination of
56  // the run.
57  return 1;
58  }
59 
60  if (pism_signal == SIGUSR1) {
61  auto date_without_spaces = replace_character(m_time->date(m_time->current()), ' ', '_');
62  auto file_name = pism::printf("pism-%s.nc", date_without_spaces.c_str());
63  m_log->message(1,
64  "\ncaught signal SIGUSR1: Writing intermediate file `%s' and flushing time series.\n\n",
65  file_name.c_str());
66  pism_signal = 0;
67 
68  File file(m_grid->com,
69  file_name,
70  string_to_backend(m_config->get_string("output.format")),
72  m_ctx->pio_iosys_id());
74 
75  // flush all the time-series buffers:
77  }
78 
79  if (pism_signal == SIGUSR2) {
80  m_log->message(1,
81  "\ncaught signal SIGUSR2: Flushing time series.\n\n");
82  pism_signal = 0;
83 
84  // flush all the time-series buffers:
86  }
87 
88  return 0;
89 }
90 
91 
93 
94  VariableMetadata result("run_stats", m_sys);
95 
96  result["source"] = std::string("PISM ") + pism::revision;
97  result["long_name"] = "Run statistics";
98 
99  // timing stats
100  double
102  proc_hours = m_grid->size() * wall_clock_hours,
103  model_years = units::convert(m_sys, m_time->current() - m_time->start(),
104  "seconds", "years");
105 
106  result["wall_clock_hours"] = { wall_clock_hours };
107  result["processor_hours"] = { proc_hours };
108  result["model_years_per_processor_hour"] = { model_years / proc_hours };
109  result["number_of_time_steps"] = { (double)m_step_counter };
110 
111  return result;
112 }
113 
114 //! Get time and user/host name and add it to the given string.
115 void IceModel::prepend_history(const std::string &str) {
117  username_prefix(m_grid->com) + (str + "\n") +
119 }
120 
121 //! Return the grid used by this model.
122 std::shared_ptr<Grid> IceModel::grid() const {
123  return m_grid;
124 }
125 
126 //! Return the context this model is running in.
127 std::shared_ptr<Context> IceModel::ctx() const {
128  return m_ctx;
129 }
130 
131 } // end of namespace pism
High-level PISM I/O class.
Definition: File.hh:56
virtual int process_signals()
Catch signals -USR1, -USR2 and -TERM.
Definition: utilities.cc:47
double m_start_time
Definition: IceModel.hh:477
VariableMetadata run_stats() const
Definition: utilities.cc:92
const Config::Ptr m_config
Configuration flags and parameters.
Definition: IceModel.hh:243
unsigned int m_step_counter
Definition: IceModel.hh:330
const Time::Ptr m_time
Time manager.
Definition: IceModel.hh:251
std::shared_ptr< Grid > grid() const
Return the grid used by this model.
Definition: utilities.cc:122
const std::shared_ptr< Context > m_ctx
Execution context.
Definition: IceModel.hh:245
const Logger::Ptr m_log
Logger.
Definition: IceModel.hh:249
VariableMetadata m_output_global_attributes
stores global attributes saved in a PISM output file
Definition: IceModel.hh:256
void flush_timeseries()
Flush scalar time-series.
Definition: output_ts.cc:122
virtual void prepend_history(const std::string &string)
Get time and user/host name and add it to the given string.
Definition: utilities.cc:115
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
const units::System::Ptr m_sys
Unit system.
Definition: IceModel.hh:247
std::shared_ptr< Context > ctx() const
Return the context this model is running in.
Definition: utilities.cc:127
std::set< std::string > m_output_vars
Definition: IceModel.hh:424
const std::shared_ptr< Grid > m_grid
Computational grid.
Definition: IceModel.hh:241
std::string get_string(const std::string &name) const
Get a string attribute.
VariableMetadata & set_string(const std::string &name, const std::string &value)
Set a string attribute.
@ PISM_READWRITE_MOVE
create a file for writing, move foo.nc to foo.nc~ if present
Definition: IO_Flags.hh:78
double convert(System::Ptr system, double input, const std::string &spec1, const std::string &spec2)
Convert a quantity from unit1 to unit2.
Definition: Units.cc:70
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 printf(const char *format,...)
std::string username_prefix(MPI_Comm com)
Creates a string with the user name, hostname and the time-stamp (for history strings).
std::string replace_character(const std::string &input, char from, char to)
volatile sig_atomic_t pism_signal
Definition: pism_signal.c:23