Skip to content

Commit

Permalink
add jld2 checkpointing for version controll
Browse files Browse the repository at this point in the history
  • Loading branch information
aelligp committed Apr 22, 2024
1 parent 04ac372 commit a7fa0e8
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/IO/JLD2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
checkpointing_jld2(dst, stokes, thermal, η, particles, phases, time)
Save necessary data in `dst` as and jld2 file to restart the model from the state at `time`.
If run in parallel, the file will be named after the corresponidng rank e.g.`checkpoint_rank_0.jld2`
and thus can be loaded by the processor while restarting the simulation.
If you want to restart your simulation from the checkpoint you can use load() and specify the MPI rank
by providing a dollar sign and the rank number.
# Example
```julia
checkpointing_jld2(
"path/to/dst",
stokes,
thermal,
η,
particles,
pPhases,
t,
)
restart = load("path/to/dst/checkpoint_rank_(igg.me).jld2")"
stokes = restart["stokes"]
thermal = restart["thermal"]
η = restart["η"]
particles = restart["particles"]
pPhases = restart["phases"]
t = restart["time"]
```
"""
function checkpointing_jld2(dst, stokes, thermal, η, particles, phases, time; igg=igg::IGG)
!isdir(dst) && mkpath(dst) # create folder in case it does not exist
fname = joinpath(dst, "checkpoint_rank_$(igg.me).jld2")
return jldsave(
fname;
stokes=_tocpu(stokes),
thermal=_tocpu(thermal),
η=_tocpu(η),
particles=_tocpu(particles),
phases=_tocpu(phases),
time,
)
end

function struct2cpu(a::T) where T<:Union{StokesArrays, SymmetricTensor, ThermalArrays, Velocity, Residual}
nfields = fieldcount(T)
cpu_fields = ntuple(Val(nfields)) do i
struct2cpu(getfield(a, i))
end
return T(cpu_fields...)
end

@inline struct2cpu(A::Array) = A
@inline struct2cpu(A::AbstractArray) = Array(A)

0 comments on commit a7fa0e8

Please sign in to comment.