HILA
Loading...
Searching...
No Matches
libraries/tools/checkpoint.h
Go to the documentation of this file.
1/**
2 * @file checkpoint.h
3 * @brief Checkpointing functions for saving and restoring lattice configurations
4 *
5 */
6#ifndef CHECKPOINT_H
7#define CHECKPOINT_H
8
9#include "hila.h"
10
11// <filesystem> can be in different locations, check...
12#if __has_include(<filesystem>)
13#include <filesystem>
14namespace filesys_ns = std::filesystem;
15
16#elif __has_include(<experimental/filesystem>)
17#include <experimental/filesystem>
18namespace filesys_ns = std::experimental::filesystem;
19
20#else
21static_assert(0, "Neither <filesystem> nor <experimental/filesystem> found!");
22
23#endif
24
25/// Functions checkpoint / restore_checkpoint allow one to save lattice config periodically
26/// Checkpoint keeps file "run_status" which holds the current trajectory.
27/// By modifying "run status" the number of trajectories can be changed
28
29/// parameters-type must
30
31
32template <typename group>
33void checkpoint(const GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
34 int trajectory, bool save_old = true) {
35
36 double t = hila::gettime();
37
38 if (save_old && hila::myrank() == 0 && filesys_ns::exists(config_file)) {
39 filesys_ns::rename(config_file, config_file + ".prev");
40 // rename config to config.prev
41 }
42
43 // save config
44 U.config_write(config_file);
45
46 // check if n_trajectories has changed
47 hila::input status;
48 status.quiet();
49 if (status.open("run_status", false, false)) {
50 int ntraj = status.get("trajectories");
51 if (ntraj != n_trajectories) {
52 hila::out0 << "* NUMBER OF TRAJECTORIES " << n_trajectories << " -> " << ntraj << '\n';
53 }
54 n_trajectories = ntraj;
55 status.close();
56 }
57
58 if (hila::myrank() == 0) {
59 std::fstream statfile;
60 statfile.open("run_status", std::ios::in);
61
62 std::ofstream outf;
63 outf.open("run_status", std::ios::out | std::ios::trunc);
64 outf << "trajectories " << n_trajectories
65 << " # CHANGE TO ADJUST NUMBER OF TRAJECTORIES IN THIS RUN\n";
66 outf << "trajectory " << trajectory + 1 << '\n';
67 outf << "seed " << static_cast<uint64_t>(hila::random() * (1UL << 61)) << '\n';
68 outf << "time " << hila::gettime() << '\n';
69 outf.close();
70 }
71 std::stringstream msg;
72 msg << "Checkpointing, time " << hila::gettime() - t;
73 hila::timestamp(msg.str());
74}
75
76template <typename group>
77bool restore_checkpoint(GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
78 int &trajectory) {
79 uint64_t seed;
80 bool ok = true;
81 hila::input status;
82 if (status.open("run_status", false, false)) {
83 hila::out0 << "RESTORING FROM CHECKPOINT:\n";
84 int traj = status.get("trajectories");
85 if (traj != n_trajectories) {
86 hila::out0 << "Number of trajectories set in 'run_status' " << n_trajectories << " -> "
87 << traj << '\n';
88 n_trajectories = traj;
89 }
90
91 trajectory = status.get("trajectory");
92 seed = status.get("seed");
93 // p.time_offset = status.get("time");
94
95 status.close();
97
98 U.config_read(config_file);
99 ok = true;
100
101 } else {
102
103 bool exists = hila::myrank() == 0 && filesys_ns::exists(config_file);
104 hila::broadcast(exists);
105 if (exists) {
106 hila::out0 << "READING initial config\n";
107 U.config_read(config_file);
108 ok = true;
109 } else {
110 ok = false;
111 }
112 }
113 return ok;
114}
115
116#endif
Gauge field class.
Definition gaugefield.h:22
void config_write(const std::string &filename) const
config_write writes the gauge field to file, with additional "verifying" header
Definition gaugefield.h:155
hila::input - Class for parsing runtime parameter files.
Definition input.h:52
void close()
Closes input parameter file.
Definition input.cpp:79
returntype get(const std::string &key)
Get next value in stack of read in input string from parameters file.
Definition input.h:269
bool open(const std::string &fname, bool use_cmdline=true, bool exit_on_error=true)
Open file that parameters are read from.
Definition input.cpp:22
void quiet(bool really=true)
Silence print output during file reading.
Definition input.h:112
void checkpoint(const GaugeField< group > &U, const std::string &config_file, int &n_trajectories, int trajectory, bool save_old=true)
parameters-type must
double random()
Real valued uniform random number generator.
Definition hila_gpu.cpp:121
int myrank()
rank of this node
Definition com_mpi.cpp:234
std::ostream out0
This writes output only from main process (node 0)
void seed_random(uint64_t seed, bool device_rng=true)
Seed random generators with 64-bit unsigned value. On MPI shuffles the seed so that different MPI ran...
Definition random.cpp:86
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
Definition com_mpi.h:152
double gettime()
Definition timing.cpp:163