|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Observer that auto-saves the engine state every every steps to a templated path.
More...
#include <ca-checkpoint.H>
Public Member Functions | |
| Periodic_Checkpoint_Observer (Engine &engine, const std::size_t every, std::string path_pattern, const std::size_t zero_pad=6) | |
| Build a synchronous periodic checkpoint observer. | |
| Periodic_Checkpoint_Observer (Engine &engine, const std::size_t every, std::string path_pattern, Async_Checkpoint_Writer< Engine > &writer, Checkpoint_Options options={}, const std::size_t zero_pad=6) | |
Build a periodic observer routing writes through an Async_Checkpoint_Writer. | |
| template<typename Lattice > | |
| void | on_step_begin (const std::size_t step, const Lattice &frame) noexcept |
| Pre-step hook. | |
| template<typename Lattice > | |
| void | on_step_end (const std::size_t step, const Lattice &) |
Snapshot when step % every == 0. | |
| const std::filesystem::path & | last_path () const noexcept |
| Path of the most recent checkpoint scheduled by this observer. | |
Private Member Functions | |
| std::filesystem::path | format_path (const std::size_t step) const |
Replace the first {step} placeholder with step. | |
Private Attributes | |
| Engine * | engine_ |
| std::size_t | every_ |
| std::string | pattern_ |
| std::size_t | zero_pad_ |
| std::filesystem::path | last_path_ |
| Async_Checkpoint_Writer< Engine > * | async_writer_ = nullptr |
| Checkpoint_Options | options_ {} |
Observer that auto-saves the engine state every every steps to a templated path.
The path pattern accepts a single {step} token which is replaced with the zero-padded step index. Snapshots are written after on_step_end, so the file always reflects the state after the step has completed.
When constructed with an Async_Checkpoint_Writer, every snapshot is forwarded to the writer's worker thread, keeping the engine's step loop free of disk I/O. Otherwise save_checkpoint is invoked synchronously.
Typical usage:
Or, with the async writer:
Async_Checkpoint_Writer reference must outlive the observer. every >= 1 (enforced by the constructor).on_step_end from multiple workers.| Engine | engine type to checkpoint. |
Definition at line 1563 of file ca-checkpoint.H.
|
inline |
Build a synchronous periodic checkpoint observer.
| [in,out] | engine | engine to checkpoint (must outlive the observer). |
| [in] | every | steps between snapshots (must be ≥ 1). |
| [in] | path_pattern | path with a single {step} placeholder. |
| [in] | zero_pad | minimum width for the step number when substituting {step}. |
| std::domain_error | if every == 0. |
Definition at line 1597 of file ca-checkpoint.H.
References ah_domain_error_if.
|
inline |
Build a periodic observer routing writes through an Async_Checkpoint_Writer.
| [in,out] | engine | engine to checkpoint (must outlive the observer). |
| [in] | every | steps between snapshots (must be ≥ 1). |
| [in] | path_pattern | path with a single {step} placeholder. |
| [in,out] | writer | async writer (must outlive the observer). |
| [in] | options | compression / fsync knobs forwarded to writes. |
| [in] | zero_pad | minimum width for the step number when substituting {step}. |
| std::domain_error | if every == 0. |
Definition at line 1619 of file ca-checkpoint.H.
|
inlineprivate |
Replace the first {step} placeholder with step.
Definition at line 1574 of file ca-checkpoint.H.
|
inlinenoexcept |
Path of the most recent checkpoint scheduled by this observer.
Empty when no snapshot has been taken yet (e.g. because the total number of completed steps is still below every, or because on_step_end was never invoked). When the observer routes writes through Async_Checkpoint_Writer, this is the path the worker thread will (or has) written to — not a guarantee the file exists on disk; callers may need to call writer.flush() first.
Definition at line 1688 of file ca-checkpoint.H.
|
inlinenoexcept |
Pre-step hook.
No-op for this observer.
Snapshots are taken at end-of-step so the pre-step callback is intentionally empty. Provided to satisfy the ObserverLike concept and to keep the observer composable inside a Composite_Observer.
| Lattice | lattice type of the engine being observed. |
| [in] | step | step that is about to start (unused). |
| [in] | frame | current frame (unused). |
Definition at line 1648 of file ca-checkpoint.H.
|
inline |
Snapshot when step % every == 0.
Writes through Async_Checkpoint_Writer when one was supplied at construction; otherwise calls save_checkpoint synchronously.
| Lattice | lattice type. |
| [in] | step | step that just completed. |
| Any | exception propagated by save_checkpoint. |
Definition at line 1664 of file ca-checkpoint.H.
References Aleph::CA::save_checkpoint(), and Aleph::CA::Async_Checkpoint_Writer< Engine >::submit().
|
private |
Definition at line 1570 of file ca-checkpoint.H.
|
private |
Definition at line 1565 of file ca-checkpoint.H.
|
private |
Definition at line 1566 of file ca-checkpoint.H.
|
private |
Definition at line 1569 of file ca-checkpoint.H.
|
private |
Definition at line 1571 of file ca-checkpoint.H.
|
private |
Definition at line 1567 of file ca-checkpoint.H.
|
private |
Definition at line 1568 of file ca-checkpoint.H.