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/// Functions checkpoint / restore_checkpoint allow one to save lattice config periodically
12/// Checkpoint keeps file "run_status" which holds the current trajectory.
13/// By modifying "run status" the number of trajectories can be changed
14
15/// parameters-type must
16
17
18template <typename group>
19void checkpoint(const GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
20 int trajectory, bool save_old = true) {
21
22 double t = hila::gettime();
23
24 if (save_old && hila::myrank() == 0 && filesys_ns::exists(config_file)) {
25 filesys_ns::rename(config_file, config_file + ".prev");
26 // rename config to config.prev
27 }
28
29 // save config
30 U.config_write(config_file);
31
32
33 // check if n_trajectories has changed
34 // NOTE: all ranks must call hila::input routines!
35
36 hila::input status;
37 status.quiet();
38 if (status.open("run_status", false, false)) {
39 int ntraj = status.get("trajectories");
40 if (ntraj != n_trajectories) {
41 hila::out0 << "* NUMBER OF TRAJECTORIES " << n_trajectories << " -> " << ntraj << '\n';
42 n_trajectories = ntraj;
43 }
44 status.close();
45 }
46
47 if (hila::myrank() == 0) {
48
49 // write the status file
50 std::ofstream outf;
51 outf.open("run_status", std::ios::out | std::ios::trunc);
52 outf << "trajectories " << n_trajectories
53 << " # CHANGE TO ADJUST NUMBER OF TRAJECTORIES IN THIS RUN\n";
54 outf << "trajectory " << trajectory + 1 << '\n';
55 outf << "seed " << static_cast<uint64_t>(hila::random() * (1UL << 61)) << '\n';
56 outf << "time " << hila::gettime() << '\n';
57 outf.close();
58
59 std::stringstream msg;
60 msg << "Checkpointing, time " << hila::gettime() - t;
61 hila::timestamp(msg.str());
62 }
63
64}
65
66
67template <typename group>
68bool restore_checkpoint(GaugeField<group> &U, const std::string &config_file, int &n_trajectories,
69 int &trajectory) {
70 uint64_t seed;
71 bool ok = true;
72 hila::input status;
73 if (status.open("run_status", false, false)) {
74 hila::out0 << "RESTORING FROM CHECKPOINT:\n";
75 int traj = status.get("trajectories");
76 if (traj != n_trajectories) {
77 hila::out0 << "Number of trajectories set in 'run_status' " << n_trajectories << " -> "
78 << traj << '\n';
79 n_trajectories = traj;
80 }
81
82 trajectory = status.get("trajectory");
83 seed = status.get("seed");
84 // p.time_offset = status.get("time");
85
86 status.close();
88
89 U.config_read(config_file);
90 ok = true;
91
92 } else {
93
94 bool exists = hila::myrank() == 0 && filesys_ns::exists(config_file);
95 hila::broadcast(exists);
96 if (exists) {
97 hila::out0 << "READING initial config\n";
98 U.config_read(config_file);
99 ok = true;
100 } else {
101 ok = false;
102 }
103 }
104 return ok;
105}
106
107#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:94
returntype get(const std::string &key)
Get next value in stack of read in input string from parameters file.
Definition input.h:272
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:27
void quiet(bool really=true)
Silence print output during file reading.
Definition input.h:115
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:168
double gettime()
Definition timing.cpp:167