Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CA::Periodic_Checkpoint_Observer< Engine > Class Template Reference

Observer that auto-saves the engine state every every steps to a templated path. More...

#include <ca-checkpoint.H>

Collaboration diagram for Aleph::CA::Periodic_Checkpoint_Observer< Engine >:
[legend]

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_ {}
 

Detailed Description

template<typename Engine>
class Aleph::CA::Periodic_Checkpoint_Observer< Engine >

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:

"checkpoints/run_{step}.bin");
Observer that auto-saves the engine state every every steps to a templated path.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
static mt19937 engine

Or, with the async writer:

"ckpt/run_{step}.bin",
Background writer pumping snapshots onto a dedicated thread.
Precondition
The engine reference passed to the constructor must outlive the observer.
The optional Async_Checkpoint_Writer reference must outlive the observer.
every >= 1 (enforced by the constructor).
Note
This class is not thread-safe. The engine, the observer and the underlying filesystem are assumed to be driven by a single thread. Use external synchronisation if a parallel engine fires on_step_end from multiple workers.
Template Parameters
Engineengine type to checkpoint.

Definition at line 1563 of file ca-checkpoint.H.

Constructor & Destructor Documentation

◆ Periodic_Checkpoint_Observer() [1/2]

template<typename Engine >
Aleph::CA::Periodic_Checkpoint_Observer< Engine >::Periodic_Checkpoint_Observer ( Engine &  engine,
const std::size_t  every,
std::string  path_pattern,
const std::size_t  zero_pad = 6 
)
inline

Build a synchronous periodic checkpoint observer.

Parameters
[in,out]engineengine to checkpoint (must outlive the observer).
[in]everysteps between snapshots (must be ≥ 1).
[in]path_patternpath with a single {step} placeholder.
[in]zero_padminimum width for the step number when substituting {step}.
Exceptions
std::domain_errorif every == 0.

Definition at line 1597 of file ca-checkpoint.H.

References ah_domain_error_if.

◆ Periodic_Checkpoint_Observer() [2/2]

template<typename Engine >
Aleph::CA::Periodic_Checkpoint_Observer< Engine >::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 
)
inline

Build a periodic observer routing writes through an Async_Checkpoint_Writer.

Parameters
[in,out]engineengine to checkpoint (must outlive the observer).
[in]everysteps between snapshots (must be ≥ 1).
[in]path_patternpath with a single {step} placeholder.
[in,out]writerasync writer (must outlive the observer).
[in]optionscompression / fsync knobs forwarded to writes.
[in]zero_padminimum width for the step number when substituting {step}.
Exceptions
std::domain_errorif every == 0.

Definition at line 1619 of file ca-checkpoint.H.

Member Function Documentation

◆ format_path()

template<typename Engine >
std::filesystem::path Aleph::CA::Periodic_Checkpoint_Observer< Engine >::format_path ( const std::size_t  step) const
inlineprivate

Replace the first {step} placeholder with step.

Definition at line 1574 of file ca-checkpoint.H.

◆ last_path()

template<typename Engine >
const std::filesystem::path & Aleph::CA::Periodic_Checkpoint_Observer< Engine >::last_path ( ) const
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.

Returns
const reference to the cached path, or an empty path when none was produced.

Definition at line 1688 of file ca-checkpoint.H.

◆ on_step_begin()

template<typename Engine >
template<typename Lattice >
void Aleph::CA::Periodic_Checkpoint_Observer< Engine >::on_step_begin ( const std::size_t  step,
const Lattice &  frame 
)
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.

Template Parameters
Latticelattice type of the engine being observed.
Parameters
[in]stepstep that is about to start (unused).
[in]framecurrent frame (unused).

Definition at line 1648 of file ca-checkpoint.H.

◆ on_step_end()

template<typename Engine >
template<typename Lattice >
void Aleph::CA::Periodic_Checkpoint_Observer< Engine >::on_step_end ( const std::size_t  step,
const Lattice &   
)
inline

Snapshot when step % every == 0.

Writes through Async_Checkpoint_Writer when one was supplied at construction; otherwise calls save_checkpoint synchronously.

Template Parameters
Latticelattice type.
Parameters
[in]stepstep that just completed.
Exceptions
Anyexception 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().

Member Data Documentation

◆ async_writer_

Definition at line 1570 of file ca-checkpoint.H.

◆ engine_

Definition at line 1565 of file ca-checkpoint.H.

◆ every_

template<typename Engine >
std::size_t Aleph::CA::Periodic_Checkpoint_Observer< Engine >::every_
private

Definition at line 1566 of file ca-checkpoint.H.

◆ last_path_

template<typename Engine >
std::filesystem::path Aleph::CA::Periodic_Checkpoint_Observer< Engine >::last_path_
private

Definition at line 1569 of file ca-checkpoint.H.

◆ options_

Definition at line 1571 of file ca-checkpoint.H.

◆ pattern_

template<typename Engine >
std::string Aleph::CA::Periodic_Checkpoint_Observer< Engine >::pattern_
private

Definition at line 1567 of file ca-checkpoint.H.

◆ zero_pad_

template<typename Engine >
std::size_t Aleph::CA::Periodic_Checkpoint_Observer< Engine >::zero_pad_
private

Definition at line 1568 of file ca-checkpoint.H.


The documentation for this class was generated from the following file: