-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from mulimoen/feature/mpi
MPI support
- Loading branch information
Showing
16 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "netcdf-examples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[features] | ||
mpi = ["dep:mpi", "netcdf/mpi", "dep:mpi-sys"] | ||
|
||
[dependencies] | ||
netcdf = { workspace = true } | ||
mpi = { version = "0.7.0", optional = true } | ||
mpi-sys = { workspace = true, optional = true } | ||
ndarray = "0.15.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#[cfg(feature = "mpi")] | ||
mod parallel; | ||
|
||
fn main() { | ||
#[cfg(feature = "mpi")] | ||
parallel::main().unwrap(); | ||
|
||
#[cfg(not(feature = "mpi"))] | ||
println!("MPI support is not included, will not run this example"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use mpi::traits::{AsRaw, Communicator}; | ||
|
||
fn target_function(rank: i32, t: usize) -> i32 { | ||
100 * (t as i32) + rank | ||
} | ||
|
||
fn mpi_null_info() -> mpi_sys::MPI_Info { | ||
let mut info = std::ptr::null_mut(); | ||
let e = unsafe { mpi_sys::MPI_Info_create(&mut info) }; | ||
assert_eq!(e, mpi_sys::MPI_SUCCESS.try_into().unwrap()); | ||
|
||
info | ||
} | ||
|
||
fn create( | ||
path: &str, | ||
communicator: impl Communicator + AsRaw<Raw = mpi_sys::MPI_Comm>, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let info = mpi_null_info(); | ||
let mut file = | ||
netcdf::create_par_with(path, communicator.as_raw(), info, netcdf::Options::NETCDF4)?; | ||
|
||
let size = communicator.size() as usize; | ||
let rank = communicator.rank(); | ||
|
||
file.add_dimension("x", size)?; | ||
file.add_unlimited_dimension("t")?; | ||
let var = file.add_variable::<i32>("output", &["t", "x"])?; | ||
var.access_collective()?; | ||
|
||
file.enddef()?; | ||
|
||
let mut var = file.variable_mut("output").unwrap(); | ||
|
||
let values = ndarray::Array1::from_shape_fn(10, |t| target_function(rank, t)); | ||
var.put((.., rank as usize), values.view())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read( | ||
path: &str, | ||
communicator: impl Communicator + AsRaw<Raw = mpi_sys::MPI_Comm>, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let info = mpi_null_info(); | ||
|
||
let file = netcdf::open_par_with(path, communicator.as_raw(), info, netcdf::Options::empty())?; | ||
|
||
let rank = communicator.rank(); | ||
let var = file.variable("output").unwrap(); | ||
var.access_collective()?; | ||
let values = var.get::<i32, _>((.., rank as usize))?; | ||
|
||
for (t, &v) in values.iter().enumerate() { | ||
assert_eq!(v, target_function(rank, t)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let universe = mpi::initialize().unwrap(); | ||
let path = "par.nc"; | ||
|
||
create(path, universe.world())?; | ||
|
||
read(path, universe.world())?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![cfg(feature = "mpi")] | ||
use std::ffi::{c_char, c_int}; | ||
|
||
use mpi_sys::{MPI_Comm, MPI_Info}; | ||
|
||
pub const NC_INDEPENDENT: c_int = 0; | ||
pub const NC_COLLECTIVE: c_int = 1; | ||
|
||
extern "C" { | ||
pub fn nc_create_par( | ||
path: *const c_char, | ||
cmode: c_int, | ||
comm: MPI_Comm, | ||
info: MPI_Info, | ||
ncidp: *mut c_int, | ||
) -> c_int; | ||
pub fn nc_open_par( | ||
path: *const c_char, | ||
mode: c_int, | ||
comm: MPI_Comm, | ||
info: MPI_Info, | ||
ncidp: *mut c_int, | ||
) -> c_int; | ||
pub fn nc_var_par_access(ncid: c_int, varid: c_int, par_access: c_int) -> c_int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.