-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jld2 checkpointing for version controll
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |