Skip to content

Commit

Permalink
use mktempdir for regridding
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Oct 2, 2024
1 parent b0ca47c commit ac47351
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 243 deletions.
16 changes: 7 additions & 9 deletions experiments/ClimaEarth/user_io/io_helpers.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# helpers with boiler plate code for IO operations, useful for all ClimaEarth drivers.

"""
setup_output_dirs(; output_dir = nothing, regrid_dir = nothing, artifacts_dir = nothing, comms_ctx)
setup_output_dirs(; output_dir = nothing, artifacts_dir = nothing, comms_ctx)
Create output directories for the experiment. If `comms_ctx` is provided, only the root process will create the directories.
By default, the regrid directory is created as a temporary directory inside the output directory,
and the artifacts directory is created inside the output directory with the suffix `_artifacts`.
# Arguments
- `output_dir::String`: The directory where the output files will be stored. Default is the current directory.
Expand All @@ -14,28 +16,24 @@ Create output directories for the experiment. If `comms_ctx` is provided, only t
# Returns
- A tuple with the paths to the output, regrid, and artifacts directories.
"""
function setup_output_dirs(; output_dir = nothing, regrid_dir = nothing, artifacts_dir = nothing, comms_ctx)
function setup_output_dirs(; output_dir = nothing, artifacts_dir = nothing, comms_ctx)
if output_dir === nothing
output_dir = "."
end
if regrid_dir === nothing
regrid_dir = joinpath(output_dir, "regrid_tmp/")
end
if artifacts_dir === nothing
artifacts_dir = output_dir * "_artifacts"
end

@info(output_dir)
regrid_dir = nothing
if ClimaComms.iamroot(comms_ctx)
mkpath(output_dir)
mkpath(regrid_dir)
mkpath(artifacts_dir)
regrid_dir = mktempdir(output_dir, prefix = "regrid_tmp_")
end

ClimaComms.barrier(comms_ctx)
regrid_dir = ClimaComms.bcast(comms_ctx, regrid_dir)

return (; output = output_dir, artifacts = artifacts_dir, regrid = regrid_dir)

end

"""
Expand Down
2 changes: 0 additions & 2 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ function hdwrite_regridfile_rll_to_cgll(
space_undistributed = space2d_undistributed
end
if isfile(datafile_cgll) == false
isdir(REGRID_DIR) ? nothing : mkpath(REGRID_DIR)

nlat, nlon = NCDatasets.NCDataset(datafile_rll) do ds
(ds.dim["lat"], ds.dim["lon"])
end
Expand Down
18 changes: 8 additions & 10 deletions test/mpi_tests/regridder_mpi_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import ClimaCoupler: Regridder
include(joinpath("..", "TestHelper.jl"))
import .TestHelper

REGRID_DIR = @isdefined(REGRID_DIR) ? REGRID_DIR : joinpath("", "regridder_test_tmp/")

# Set up MPI communications context
# Note that runs will hang if a context is initialized twice in the same file,
# so this context should be shared among all tests in this file.
Expand All @@ -26,8 +24,12 @@ pid, nprocs = ClimaComms.init(comms_ctx)

@testset "test write_to_hdf5 and read_from_hdf5 with MPI" begin
for FT in (Float32, Float64)
# Set up testing directory
mkpath(REGRID_DIR)
# Create temporary regrid directory on root process and broadcast
regrid_dir = nothing
if ClimaComms.iamroot(comms_ctx)
regrid_dir = mktempdir(pwd(), prefix = "regrid_tmp_")
end
regrid_dir = ClimaComms.bcast(comms_ctx, regrid_dir)

hd_outfile_root = "hdf5_out_test"
tx = Dates.DateTime(1979, 01, 01, 01, 00, 00)
Expand All @@ -36,14 +38,10 @@ pid, nprocs = ClimaComms.init(comms_ctx)
varname = "testdata"

ClimaComms.barrier(comms_ctx)
Regridder.write_to_hdf5(REGRID_DIR, hd_outfile_root, tx, input_field, varname, comms_ctx)
Regridder.write_to_hdf5(regrid_dir, hd_outfile_root, tx, input_field, varname, comms_ctx)

ClimaComms.barrier(comms_ctx)
output_field = Regridder.read_from_hdf5(REGRID_DIR, hd_outfile_root, tx, varname, comms_ctx)
output_field = Regridder.read_from_hdf5(regrid_dir, hd_outfile_root, tx, varname, comms_ctx)
@test parent(input_field) == parent(output_field)

ClimaComms.barrier(comms_ctx)
ClimaComms.iamroot(comms_ctx) && rm(REGRID_DIR; recursive = true, force = true)
ClimaComms.barrier(comms_ctx)
end
end
Loading

0 comments on commit ac47351

Please sign in to comment.