Skip to content

Commit

Permalink
loop output (#32)
Browse files Browse the repository at this point in the history
* add optional output

* fix version

* typo
  • Loading branch information
nhz2 authored Dec 12, 2023
1 parent c9fd427 commit f19312f
Show file tree
Hide file tree
Showing 28 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MEDYANSimRunner"
uuid = "b58a3b99-22e3-44d1-b5ea-258f082a6fe8"
authors = ["nhz2 <[email protected]>"]
version = "0.5.2"
version = "0.5.3"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ Also return the expected value of step when done will first be true, used for di

This function should not mutate `state`

#### `loop(step::Int, state; kwargs...) -> state`
Return the state that gets passed to `save`
#### `loop(step::Int, state; output::SmallZarrGroups.ZGroup, kwargs...) -> state`
Return the state that gets passed to `save` and `load`

Optionally, mutate the `output` keyword argument.
When saving the snapshot, this group will get saved as `"out"`


### Main loop pseudo code
Expand All @@ -99,9 +102,10 @@ group = ZGroup(childern=Dict("snap" => save(step, state))
SmallZarrGroups.save_zip(snapshot_zip_file, group)
state = load(step, SmallZarrGroups.load_zip(snapshot_zip_file)["snap"], state)
while true
state = loop(step, state)
output = ZGroup()
state = loop(step, state; output)
step = step + 1
group = ZGroup(childern=Dict("snap" => save(step, state))
group = ZGroup(childern=Dict("snap"=>save(step, state), "out"=>output)
SmallZarrGroups.save_zip(snapshot_zip_file, group)
state = load(step, SmallZarrGroups.load_zip(snapshot_zip_file)["snap"], state)
if done(step::Int, state)[1]
Expand Down
14 changes: 10 additions & 4 deletions src/run-sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ function start_job(out_dir, job::String;
state, prev_sha256 = save_load_state!(rng_state, step, state, traj, save, load, prev_sha256)
@info "Simulation started."
while true
output = ZGroup()
copy!(Random.default_rng(), rng_state)
state = loop(step, state)
state = loop(step, state; output)
copy!(rng_state, Random.default_rng())

step += 1
state, prev_sha256 = save_load_state!(rng_state, step, state, traj, save, load, prev_sha256)
state, prev_sha256 = save_load_state!(rng_state, step, state, traj, save, load, prev_sha256, output)

copy!(Random.default_rng(), rng_state)
isdone::Bool, expected_final_step::Int64 = done(step::Int, state)
Expand Down Expand Up @@ -259,12 +260,13 @@ function continue_job(out_dir, job;
end
end
while true
output = ZGroup()
copy!(Random.default_rng(), rng_state)
state = loop(step, state)
state = loop(step, state; output)
copy!(rng_state, Random.default_rng())

step += 1
state, prev_sha256 = save_load_state!(rng_state, step, state, traj, save, load, prev_sha256)
state, prev_sha256 = save_load_state!(rng_state, step, state, traj, save, load, prev_sha256, output)

copy!(Random.default_rng(), rng_state)
isdone, expected_final_step = done(step::Int, state)
Expand All @@ -291,6 +293,7 @@ function save_load_state!(
save,
load,
prev_sha256::String,
output::Union{Nothing, ZGroup}=nothing,
)
snapshot_group = ZGroup()

Expand All @@ -299,6 +302,9 @@ function save_load_state!(
copy!(rng_state, Random.default_rng())

snapshot_group["snap"] = sub_snapshot_group
if !isnothing(output)
snapshot_group["out"] = output
end
attrs(snapshot_group)["rng_state"] = rng_2_str(rng_state)
attrs(snapshot_group)["step"] = step
attrs(snapshot_group)["prev_sha256"] = prev_sha256
Expand Down
3 changes: 2 additions & 1 deletion test/example/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ function done(step::Int, state; kwargs...)
step > 10, 11
end

function loop(step::Int, state; kwargs...)
function loop(step::Int, state; output::ZGroup, kwargs...)
a = sum(state) + rand(0:1)
state[1] = state[2]
state[2] = a
attrs(output)["sum"] = sum(state)
state
end

Expand Down
2 changes: 1 addition & 1 deletion test/example/output-ref-1_10/a/traj/footer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"steps": 11,
"prev_sha256": "22fa73eaa3944debf0d26bb9d9cebcecc5f3047a5ffed0c42e622003e562c71c"
"prev_sha256": "858a821e8b5c67b9c55e77064443bea55cf8be8d06fa745f21433e0664992966"
}
Binary file modified test/example/output-ref-1_10/a/traj/snap1.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap10.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap11.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap2.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap3.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap4.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap5.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap6.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap7.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap8.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_10/a/traj/snap9.zarr.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion test/example/output-ref-1_9/a/traj/footer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"steps": 11,
"prev_sha256": "127baaf98475d3f598ec5e78b1a6ab23462c214cfd60c7354e9b56260410b8b2"
"prev_sha256": "028031634b968ea546b390ae2fe13d1b2ebb36d03383f93998affe212d398809"
}
Binary file modified test/example/output-ref-1_9/a/traj/snap1.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap10.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap11.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap2.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap3.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap4.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap5.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap6.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap7.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap8.zarr.zip
Binary file not shown.
Binary file modified test/example/output-ref-1_9/a/traj/snap9.zarr.zip
Binary file not shown.

2 comments on commit f19312f

@nhz2
Copy link
Member Author

@nhz2 nhz2 commented on f19312f Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96965

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" f19312fa423244e1f775aaa84e84da7847f00b4b
git push origin v0.5.3

Please sign in to comment.