Checkpointing

Writing checkpoint files

Long-running simulations commonly write checkpoint files so that runs can be restarted from a recent saved state. In JustPIC, checkpoints are written in JLD2 format.

At the lowest level, you can serialize arrays manually:

jldsave(
    "my_file.jld2";
    particles     = Array(particles),
    phases        = to_cpu(phases),
    phase_ratios  = Array(phase_ratios),
    particle_args = to_cpu.(particle_args),
)

This saves particle information to my_file.jld2, ready to be reloaded later.

If file size matters more than exact restart reproducibility, you can downcast to Float32 before writing:

jldsave(
    "my_file.jld2";
    particles     = Array(Float32, particles),
    phases        = to_cpu(Float32, phases),
    phase_ratios  = Array(Float32, phase_ratios),
    particle_args = to_cpu.(Float32, particle_args),
)

For routine use, prefer the built-in helper:

checkpointing_particles(
    dst,
    particles;
    phases = nothing,
    phase_ratios = nothing,
    chain = nothing,
    t = nothing,
    dt = nothing,
    particle_args = nothing,
    kwargs...,
)

On multiple MPI ranks, pass the rank id to get rank-local filenames:

checkpointing_particles(
    dst,
    particles,
    me;
    phases = nothing,
    phase_ratios = nothing,
    chain = nothing,
    t = nothing,
    dt = nothing,
    particle_args = nothing,
    kwargs...,
)

Any additional keyword arguments are written into the checkpoint as extra fields.

Loading a checkpoint file

To restart a simulation, load the file and cast the stored arrays back to the active backend:

data          = load("my_file.jld2")
particles     = TA(backend)(Float64, data["particles"])
phases        = TA(backend)(Float64, data["phases"])
phase_ratios  = TA(backend)(Float64, data["phase_ratios"])
particle_args = TA(backend).(Float64, data["particle_args"])

TA(backend) selects the backend-appropriate array type, so the same checkpoint can be restored onto CPU or accelerator arrays.

API

JustPIC.checkpointing_particlesFunction
checkpointing_particles(dst, particles; phases=nothing, phase_ratios=nothing, chain=nothing, t=nothing, dt=nothing, particle_args=nothing)
checkpointing_particles(dst, particles, me; phases=nothing, phase_ratios=nothing, chain=nothing, t=nothing, dt=nothing, particle_args=nothing)

Write particle state and optional companion data to a JLD2 checkpoint.

By default the file is saved as particles_checkpoint.jld2 in dst. Additional keyword arguments are serialized into the checkpoint after being converted to plain Julia arrays where needed.

Common keywords

  • phases: per-particle phase labels.
  • phase_ratios: PhaseRatios container to checkpoint.
  • chain: marker-chain state.
  • t: simulation time.
  • dt: timestep size.
  • particle_args: tuple of extra particle-carried fields.

Notes

  • Arrays are converted to plain Julia arrays before serialization so the checkpoint can be reloaded independently of the active backend.
  • Passing me writes rank-local files named after the zero-based MPI rank: particles_checkpoint0000.jld2, particles_checkpoint0001.jld2, and so on.
source