145 std::filesystem::path
const &path,
int mode,
146 F1
const read_metadata, F2
const read_data,
147 F3
const on_success) {
149 std::string(
"Error while reading " + classname +
" checkpoint: ");
151 auto const &comm = context.
get_comm();
158 throw std::runtime_error(err_msg +
"could not open file " +
163 cpfile.
stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
166 read_metadata(cpfile);
172 if (cpfile.
stream.peek() ==
'\n') {
173 static_cast<void>(cpfile.
stream.get());
176 if (cpfile.
stream.peek() != EOF) {
177 throw std::runtime_error(err_msg +
"extra data found, expected EOF.");
179 }
catch (std::ios_base::failure
const &) {
180 auto const eof_error = cpfile.
stream.eof();
184 throw std::runtime_error(err_msg +
"EOF found.");
189 throw std::runtime_error(err_msg +
"incorrectly formatted data.");
192 }
catch (std::runtime_error
const &err) {
195 throw std::runtime_error(err_msg + err.what());
203 std::filesystem::path
const &path,
int mode,
204 F1
const write_metadata, F2
const write_data,
205 F3
const on_failure) {
207 std::string(
"Error while writing " + classname +
" checkpoint: ");
209 auto const &comm = context.
get_comm();
213 auto failure =
false;
214 std::shared_ptr<CheckpointFile> cpfile;
216 cpfile = std::make_shared<CheckpointFile>(path, std::ios_base::out,
binary);
217 failure = !cpfile->stream;
218 boost::mpi::broadcast(comm, failure, 0);
220 throw std::runtime_error(err_msg +
"could not open file " +
223 cpfile->stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
225 cpfile->stream.precision(16);
226 cpfile->stream << std::fixed;
229 boost::mpi::broadcast(comm, failure, 0);
236 write_metadata(cpfile, context);
237 write_data(cpfile, context);
238 }
catch (std::exception
const &error) {
239 on_failure(cpfile, context);
241 cpfile->stream.close();
242 if (
dynamic_cast<std::ios_base::failure
const *
>(&error)) {
243 throw std::runtime_error(err_msg +
"could not write to " +
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Context of an object handle.
virtual bool is_head_node() const =0
virtual boost::mpi::communicator const & get_comm() const =0
void save_checkpoint_common(Context const &context, std::string const classname, std::filesystem::path const &path, int mode, F1 const write_metadata, F2 const write_data, F3 const on_failure)
void load_checkpoint_common(Context const &context, std::string const classname, std::filesystem::path const &path, int mode, F1 const read_metadata, F2 const read_data, F3 const on_success)