HILA
Loading...
Searching...
No Matches
libraries/tools/checkpoint.h
1#ifndef CHECKPOINT_H
2#define CHECKPOINT_H
3
4#include "hila.h"
5
6// <filesystem> can be in different locations, check...
7#if __has_include (<filesystem>)
8#include <filesystem>
9namespace filesys_ns = std::filesystem;
10
11#elif __has_include (<experimental/filesystem>)
12#include <experimental/filesystem>
13namespace filesys_ns = std::experimental::filesystem;
14
15#else
16static_assert(0,"Neither <filesystem> nor <experimental/filesystem> found!");
17
18#endif
19
20/// Functions checkpoint / restore_checkpoint allow one to save lattice config periodically
21/// Checkpoint keeps file "run_status" which holds the current trajectory.
22/// By modifying "run status" the number of trajectories can be changed
23
24/// parameters-type must
25
26
27template <typename group>
28void checkpoint(const GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
29 int trajectory, bool save_old = true) {
30
31 double t = hila::gettime();
32
33 if (save_old && hila::myrank() == 0 && filesys_ns::exists(config_file)) {
34 filesys_ns::rename(config_file, config_file + ".prev");
35 // rename config to config.prev
36 }
37
38 // save config
39 U.config_write(config_file);
40
41 // check if n_trajectories has changed
42 hila::input status;
43 status.quiet();
44 if (status.open("run_status", false, false)) {
45 int ntraj = status.get("trajectories");
46 if (ntraj != n_trajectories) {
47 hila::out0 << "* NUMBER OF TRAJECTORIES " << n_trajectories << " -> " << ntraj << '\n';
48 }
49 n_trajectories = ntraj;
50 status.close();
51 }
52
53 if (hila::myrank() == 0) {
54 std::fstream statfile;
55 statfile.open("run_status", std::ios::in);
56
57 std::ofstream outf;
58 outf.open("run_status", std::ios::out | std::ios::trunc);
59 outf << "trajectories " << n_trajectories
60 << " # CHANGE TO ADJUST NUMBER OF TRAJECTORIES IN THIS RUN\n";
61 outf << "trajectory " << trajectory + 1 << '\n';
62 outf << "seed " << static_cast<uint64_t>(hila::random() * (1UL << 61)) << '\n';
63 outf << "time " << hila::gettime() << '\n';
64 outf.close();
65 }
66 std::stringstream msg;
67 msg << "Checkpointing, time " << hila::gettime() - t;
68 hila::timestamp(msg.str());
69}
70
71template <typename group>
72bool restore_checkpoint(GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
73 int &trajectory) {
74 uint64_t seed;
75 bool ok = true;
76 hila::input status;
77 if (status.open("run_status", false, false)) {
78 hila::out0 << "RESTORING FROM CHECKPOINT:\n";
79 int traj = status.get("trajectories");
80 if (traj != n_trajectories) {
81 hila::out0 << "Number of trajectories set in 'run_status' " << n_trajectories
82 << " -> " << traj << '\n';
83 n_trajectories = traj;
84 }
85
86 trajectory = status.get("trajectory");
87 seed = status.get("seed");
88 // p.time_offset = status.get("time");
89
90 status.close();
92
93 U.config_read(config_file);
94 ok = true;
95
96 } else {
97
98 bool exists = hila::myrank() == 0 && filesys_ns::exists(config_file);
99 hila::broadcast(exists);
100 if (exists) {
101 hila::out0 << "READING initial config\n";
102 U.config_read(config_file);
103 ok = true;
104 } else {
105 ok = false;
106 }
107 }
108 return ok;
109}
110
111#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
int myrank()
rank of this node
Definition com_mpi.cpp:235
double random()
Real valued uniform random number generator.
Definition hila_gpu.cpp:118
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:153
double gettime()
Definition timing.cpp:163