Skip to content

Commit

Permalink
change parallel access to whole dataset (issue #122, thanks @pgf)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Apr 25, 2024
1 parent 66ab25c commit d792d8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions ext/NCDatasetsMPIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ using MPI
using NCDatasets
using NCDatasets:
NC_COLLECTIVE,
NC_FORMAT_NETCDF4,
NC_FORMAT_NETCDF4_CLASSIC,
NC_GLOBAL,
NC_INDEPENDENT,
Variable,
_dataset_ncmode,
check,
dataset,
libnetcdf
libnetcdf,
nc_inq_format

import NCDatasets:
NCDataset,
Expand Down Expand Up @@ -58,14 +61,27 @@ Change the parallel access mode of the variable `ncv` or all variables of the da
More information is available in the [NetCDF documentation](https://web.archive.org/web/20240414204638/https://docs.unidata.ucar.edu/netcdf-c/current/parallel_io.html).
"""
function access(ncv::Variable,par_access::Symbol)
ds = dataset(ncv)
ncid = ds.ncid
varid = ncv.varid
ncid = dataset(ncv).ncid
nc_var_par_access(ncid,varid,parallel_access_mode(par_access))

if nc_inq_format(ncid) in (NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC)
nc_var_par_access(ncid,varid,parallel_access_mode(par_access))
else
error("The netCDF 3 and 5 formats do not allow different access methods per variable. You need to call this function for the whole data set: NCDatasets.access(ds,$par_access)")
end
end

# set collective or independent IO globally (for all variables)
function access(ds::NCDataset,par_access::Symbol)
nc_var_par_access(ds.ncid,NC_GLOBAL,parallel_access_mode(par_access))
if nc_inq_format(ds.ncid) in (NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC)
for (varname,ncv) in ds
access(ncv.var,par_access)
end
else
# only for PnetCDF
nc_var_par_access(ds.ncid,NC_GLOBAL,parallel_access_mode(par_access))
end
end

"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_mpi_script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ncv = defVar(ds,"temp",Int32,("lon","lat"))
# see
# https://web.archive.org/web/20240414204638/https://docs.unidata.ucar.edu/netcdf-c/current/parallel_io.html
NCDatasets.access(ncv.var,:collective)

NCDatasets.access(ds,:collective)

@debug("rank $(mpi_rank) writing to netCDF variable")
ncv[:,i] .= mpi_rank
Expand Down

0 comments on commit d792d8a

Please sign in to comment.