Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid mkdir race conditions during psave #64

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ LocalPreferences.toml
GridapTopOpt.so

# Ensure never to write results to git
results/*
results/*
tmp/*
2 changes: 1 addition & 1 deletion src/GridapTopOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using GridapSolvers
using GridapSolvers.LinearSolvers, GridapSolvers.NonlinearSolvers, GridapSolvers.BlockSolvers
using GridapSolvers.SolverInterfaces: SolverVerboseLevel, SOLVER_VERBOSE_NONE, SOLVER_VERBOSE_LOW, SOLVER_VERBOSE_HIGH

using JLD2: save_object, load_object
using JLD2: save_object, load_object, jldsave

import Base: +

Expand Down
5 changes: 3 additions & 2 deletions src/Io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
"""
psave(filename::AbstractString, x)

Save a partitioned object `x` to a directory `dir` as
Save a partitioned object `x` to a directory `dir` as
a set of JLD2 files corresponding to each part.
"""
function psave(dir::AbstractString, x)
ranks = get_parts(x)
i_am_main(ranks) && mkpath(dir)
PartitionedArrays.barrier(ranks)

Check warning on line 46 in src/Io.jl

View check run for this annotation

Codecov / codecov/patch

src/Io.jl#L46

Added line #L46 was not covered by tests
arr = to_local_storage(x)
map(ranks,arr) do id, arr
filename = joinpath(dir,basename(dir)*"_$id.jdl2")
save_object(filename,arr)
jldsave(filename, IOStream; arr)

Check warning on line 50 in src/Io.jl

View check run for this annotation

Codecov / codecov/patch

src/Io.jl#L50

Added line #L50 was not covered by tests
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/mpi/JLD2SaveLoadMPI.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module JLD2SaveLoadMPI
using Test

using Gridap, Gridap.MultiField, GridapDistributed, GridapPETSc, GridapSolvers,
PartitionedArrays, GridapTopOpt, SparseMatricesCSR

function main(distribute,mesh_partition)
ranks = distribute(LinearIndices((prod(mesh_partition),)))
path = joinpath("tmp","test")
i_am_main(ranks) && mkpath(path)
# setup
model = CartesianDiscreteModel(ranks,mesh_partition,(0,1,0,1),(20,20))
V_φ = TestFESpace(model,ReferenceFE(lagrangian,Float64,1))
φh = interpolate(initial_lsf(4,0.2),V_φ)

psave(path,get_free_dof_values(φh))
x = pload(path,ranks)
@assert x == get_free_dof_values(φh)
pload!(path,x)
@assert x == get_free_dof_values(φh)
true
end

with_mpi() do distribute
@test main(distribute,(2,2))
end

end # module
22 changes: 22 additions & 0 deletions test/seq/JLD2SaveLoad.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module JLD2SaveLoad
using Test

using Gridap, GridapTopOpt

function main()
path = joinpath(mktempdir(), "test.jld2")
# setup
model = CartesianDiscreteModel((0,1,0,1),(20,20))
V_φ = TestFESpace(model,ReferenceFE(lagrangian,Float64,1))
φh = interpolate(initial_lsf(4,0.2),V_φ)

save(path,get_free_dof_values(φh))
x = load(path)
@test x == get_free_dof_values(φh)
load!(path,x)
@test x == get_free_dof_values(φh)
end

main()

end # module
1 change: 1 addition & 0 deletions test/seq/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ using Test
@time @testset "Inverse Homogenisation - ALM" begin include("InverseHomogenisationALMTests.jl") end
@time @testset "Inverter - HPM" begin include("InverterHPMTests.jl") end
@time @testset "PZMultiFieldRepeatingState - ALM" begin include("PZMultiFieldRepeatingStateTests.jl") end
@time @testset "JLD2SaveLoad" begin include("JLD2SaveLoad.jl") end

end # module
Loading