144 std::string
const &filename,
int mode,
145 F1
const read_metadata, F2
const read_data,
146 F3
const on_success) {
148 std::string(
"Error while reading " + classname +
" checkpoint: ");
150 auto const &comm = context.
get_comm();
157 throw std::runtime_error(err_msg +
"could not open file " + filename);
161 cpfile.
stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
164 read_metadata(cpfile);
170 if (cpfile.
stream.peek() ==
'\n') {
171 static_cast<void>(cpfile.
stream.get());
174 if (cpfile.
stream.peek() != EOF) {
175 throw std::runtime_error(err_msg +
"extra data found, expected EOF.");
177 }
catch (std::ios_base::failure
const &) {
178 auto const eof_error = cpfile.
stream.eof();
182 throw std::runtime_error(err_msg +
"EOF found.");
187 throw std::runtime_error(err_msg +
"incorrectly formatted data.");
190 }
catch (std::runtime_error
const &err) {
193 throw std::runtime_error(err_msg + err.what());
201 std::string
const &filename,
int mode,
202 F1
const write_metadata, F2
const write_data,
203 F3
const on_failure) {
205 std::string(
"Error while writing " + classname +
" checkpoint: ");
207 auto const &comm = context.
get_comm();
211 auto failure =
false;
212 std::shared_ptr<CheckpointFile> cpfile;
215 std::make_shared<CheckpointFile>(filename, std::ios_base::out,
binary);
216 failure = !cpfile->stream;
217 boost::mpi::broadcast(comm, failure, 0);
219 throw std::runtime_error(err_msg +
"could not open file " + filename);
221 cpfile->stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
223 cpfile->stream.precision(16);
224 cpfile->stream << std::fixed;
227 boost::mpi::broadcast(comm, failure, 0);
234 write_metadata(cpfile, context);
235 write_data(cpfile, context);
236 }
catch (std::exception
const &error) {
237 on_failure(cpfile, context);
239 cpfile->stream.close();
240 if (
dynamic_cast<std::ios_base::failure
const *
>(&error)) {
241 throw std::runtime_error(err_msg +
"could not write to " + filename);
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::string const &filename, 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::string const &filename, int mode, F1 const read_metadata, F2 const read_data, F3 const on_success)