Skip to content

Commit

Permalink
enable MPI test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Apr 23, 2024
1 parent cf65b9a commit deb6950
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CommonDataModel = "0.3.4"
DataStructures = "0.17, 0.18"
DiskArrays = "0.3.22"
MPI = "0.20"
NetCDF_jll = "=400.701.400, =400.702.400, =400.902.5, =400.902.208, =400.902.209, =400.902.211"
NetCDF_jll = "=400.902.211"
NetworkOptions = "1.2"
julia = "1.9"

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ end
include("test_select.jl")
include("test_multifile_select.jl")
end

@testset "MPI" begin
include("test_mpi_netcdf.jl")
end
61 changes: 10 additions & 51 deletions test/test_mpi_netcdf.jl
Original file line number Diff line number Diff line change
@@ -1,57 +1,16 @@
# for example run as:
# $HOME/.julia/packages/MPI/z2owj/bin/mpiexecjl -n 4 julia test_mpi_netcdf.jl

using MPIPreferences
using MPI
using NCDatasets
using Test
using NCDatasets

mpiexec = realpath(joinpath(dirname(pathof(MPI)),"..","bin","mpiexecjl"))

#println("mpiexec ",mpiexec)

print("$mpiexec -n 4 julia test_mpi.jl")

MPI.Init()

mpi_comm = MPI.COMM_WORLD
mpi_comm_size = MPI.Comm_size(mpi_comm)
mpi_rank = MPI.Comm_rank(mpi_comm)

# need to be the same file for all processes
path = "/tmp/test-mpi.nc"
i = mpi_rank + 1


ds = NCDataset(mpi_comm,path,"c")

defDim(ds,"lon",10)
defDim(ds,"lat",mpi_comm_size)
ncv = defVar(ds,"temp",Float32,("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)


print("rank $(mpi_rank) writing to netCDF variable\n")
ncv[:,i] .= mpi_rank

ncv.attrib["units"] = "degree Celsius"
ds.attrib["comment"] = "MPI test"
close(ds)


ds = NCDataset(mpi_comm,path,"r")
ncv = ds["temp"]

@test size(ncv) == (10,mpi_comm_size)
print("rank $(mpi_rank) reading from netCDF variable\n")
nprocs = 4
testdir = @__DIR__
prog = joinpath(testdir, "test_mpi_script.jl")
fn = tempname()

@test all(==(mpi_rank),ncv[:,i])
@test ncv.attrib["units"] == "degree Celsius"
@test ds.attrib["comment"] == "MPI test"
run(`$(mpiexec()) -n $nprocs $(Base.julia_cmd()) --startup-file=no $prog $fn`)
# run with raise an error if prog fails

ds = NCDataset(fn)
@test ds.dim["lat"] == nprocs
@test ds["temp"][:,:] == repeat((0:(nprocs-1))',inner=(10,1))
close(ds)

MPI.Finalize()
49 changes: 49 additions & 0 deletions test/test_mpi_script.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# for example run as:
# $HOME/.julia/packages/MPI/z2owj/bin/mpiexecjl -n 4 julia test_mpi_script.jl

using MPI
using NCDatasets
using Test

MPI.Init()

mpi_comm = MPI.COMM_WORLD
mpi_comm_size = MPI.Comm_size(mpi_comm)
mpi_rank = MPI.Comm_rank(mpi_comm)

# need to be the same file for all processes
path = ARGS[1]
i = mpi_rank + 1

ds = NCDataset(mpi_comm,path,"c")

defDim(ds,"lon",10)
defDim(ds,"lat",mpi_comm_size)
ncv = defVar(ds,"temp",Float32,("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)


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

ncv.attrib["units"] = "degree Celsius"
ds.attrib["comment"] = "MPI test"
close(ds)


ds = NCDataset(mpi_comm,path,"r")
ncv = ds["temp"]

@test size(ncv) == (10,mpi_comm_size)
@debug("rank $(mpi_rank) reading from netCDF variable")

@test all(==(mpi_rank),ncv[:,i])
@test ncv.attrib["units"] == "degree Celsius"
@test ds.attrib["comment"] == "MPI test"

close(ds)

MPI.Finalize()

0 comments on commit deb6950

Please sign in to comment.