8def helper_purge_old_dumps(dump_prefix, keep_first=1, keep_last=3, ext=".sham") -> None:
12 if shamrock.sys.world_rank() == 0:
13 res = glob.glob(dump_prefix +
"*" + ext)
17 to_remove = res[keep_first:-keep_last]
23def helper_get_last_dump(dump_prefix, ext=".sham") -> int | None:
25 Get the last dump number.
27 res = glob.glob(dump_prefix +
"*" + ext)
33 dump_num = int(f[len(dump_prefix) : -len(ext)])
34 num_max = max(num_max, dump_num)
46 Helper class to handle Shamrock checkpoint dump files.
48 When ``metadata`` is enabled at construction, a JSON companion file is written
49 and read alongside each checkpoint to store simulation metadata.
52 def __init__(self, model, dump_prefix, ext=".sham", metadata=False):
57 The Shamrock model instance used to write and load dumps.
59 Path prefix for dump files; the dump index is appended as a
60 zero-padded seven-digit number (e.g. ``prefix0000042``).
62 File extension for checkpoint dumps (default is ``".sham"``).
63 metadata : bool, optional
64 If ``True``, also write/read a ``.json`` companion with per-checkpoint
65 metadata (default is ``False``).
70 os.makedirs(os.path.dirname(self.
dump_prefix), exist_ok=
True)
74 """Get the name of the dump file with the extension"""
78 """Get the name of the dump file (extension from self.ext)"""
82 """Find the last dump number.
84 When metadata mode is enabled, validate that checkpoint dumps and JSON
85 companion files agree on the latest checkpoint index.
91 last_metadata_dump = helper_get_last_dump(self.
dump_prefix,
".json")
92 if last_dump != last_metadata_dump:
94 "Detected inconsistent checkpoint files: "
95 f
"last {self.ext} dump is {last_dump}, "
96 f
"last .json dump is {last_metadata_dump}. "
97 "This may indicate a botched checkpoint."
103 Purge old dump files.
105 When metadata mode is enabled, also purge old JSON companion files.
109 keep_first : int, optional
110 Number of oldest dump files to keep (default is 1, i.e. keep the first dump).
111 keep_last : int, optional
112 Number of newest dump files to keep (default is 3, i.e. keep the last 3 dumps).
117 This method does not return a value.
119 helper_purge_old_dumps(self.
dump_prefix, keep_first, keep_last, self.
ext)
122 helper_purge_old_dumps(self.
dump_prefix, keep_first, keep_last,
".json")
131 The dump identifier to load.
136 If ``metadata`` was enabled at construction, the JSON metadata
137 loaded from the companion file; otherwise ``None``.
140 if shamrock.sys.world_rank() == 0:
141 print(f
"Loading dump: {dump_name} i={idump}")
142 self.
model.load_from_dump(dump_name)
145 with open(dump_name,
"r")
as f:
151 self, idump, metadata=None, purge_old_dumps=False, keep_first=1, keep_last=3
159 The dump identifier to write.
160 metadata : object, optional
161 JSON-serializable metadata stored in a ``.json`` companion next to the
162 checkpoint. Required when ``metadata`` was enabled at construction.
163 purge_old_dumps : bool, optional
164 Whether to purge old dumps (default is False).
165 keep_first : int, optional
166 Number of oldest dump files to keep (default is 1, i.e. keep the first dump).
167 keep_last : int, optional
168 Number of newest dump files to keep (default is 3, i.e. keep the last 3 dumps).
173 This method does not return a value.
176 self.
model.dump(dump_name)
180 raise ValueError(
"metadata is required when metadata is enabled")
182 if shamrock.sys.world_rank() == 0:
184 json.dump(metadata, f)
191 Load the last dump or call a function if no dump is found.
195 functor_no_last_dump : callable
196 Setup function invoked when no dump exists. Must not return a value.
201 If a dump was loaded and ``metadata`` was enabled at construction,
202 the JSON metadata from the companion; otherwise ``None``.
206 result = functor_no_last_dump()
207 if result
is not None:
208 raise ValueError(
"functor_no_last_dump must not return a value")
str get_dump_name_extension(self, idump, ext)
int|None get_last_dump(self)
str get_dump_name(self, idump)
dict|None load_dump(self, idump)
None write_dump(self, idump, metadata=None, purge_old_dumps=False, keep_first=1, keep_last=3)
__init__(self, model, dump_prefix, ext=".sham", metadata=False)
None purge_old_dumps(self, keep_first=1, keep_last=3)
dict|None load_last_dump_or(self, functor_no_last_dump)