Skip to content

Commit

Permalink
add test for partitioners
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs committed Jan 28, 2025
1 parent cbe8a93 commit 4498fe5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/parallel_grid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//? mpirun -n {{NPROCESSES}} --features "serde"

use mpi::{
collective::SystemOperation, environment::Universe, topology::Communicator,
traits::CommunicatorCollectives,
Expand Down
2 changes: 2 additions & 0 deletions examples/test_parallel_io.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//? mpirun -n {{NPROCESSES}} --features "serde"

#[cfg(feature = "serde")]
use mpi::{collective::CommunicatorCollectives, environment::Universe, traits::Communicator};
#[cfg(feature = "serde")]
Expand Down
111 changes: 111 additions & 0 deletions examples/test_partitioners.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//? mpirun -n {{NPROCESSES}} --features "coupe"

use mpi::{
collective::SystemOperation, environment::Universe, topology::Communicator,
traits::CommunicatorCollectives,
};
use ndelement::types::ReferenceCellType;
use ndgrid::{
grid::local_grid::SingleElementGridBuilder,
traits::{Builder, Entity, Grid, ParallelBuilder, ParallelGrid},
types::{GraphPartitioner, Ownership},

Check warning on line 11 in examples/test_partitioners.rs

View workflow job for this annotation

GitHub Actions / Rust style checks (--features "serde,strict")

Diff in /home/runner/work/ndgrid/ndgrid/examples/test_partitioners.rs

Check warning on line 11 in examples/test_partitioners.rs

View workflow job for this annotation

GitHub Actions / Rust style checks (--features "serde,strict")

Diff in /home/runner/work/ndgrid/ndgrid/examples/test_partitioners.rs
};


fn run_test<C: Communicator>(comm: &C, partitioner: GraphPartitioner) {
let n = 100;

let mut b = SingleElementGridBuilder::<f64>::new(2, (ReferenceCellType::Quadrilateral, 1));

let mut i = 0;
for y in 0..n {
for x in 0..n {
b.add_point(i, &[x as f64 / (n - 1) as f64, y as f64 / (n - 1) as f64]);
i += 1;
}
}

let mut i = 0;
for y in 0..n - 1 {
for x in 0..n - 1 {
let sw = n * y + x;
b.add_cell(i, &[sw, sw + 1, sw + n, sw + n + 1]);
i += 1;
}
}

let rank = comm.rank();
let grid = if rank == 0 {
b.create_parallel_grid_root(comm, partitioner)
} else {
b.create_parallel_grid(comm, 0)
};

// Check that owned cells are sorted ahead of ghost cells

let cell_count_owned = grid
.local_grid()
.cell_iter()
.filter(|entity| entity.is_owned())
.count();

// Now check that the first `cell_count_owned` entities are actually owned.
for cell in grid.local_grid().cell_iter().take(cell_count_owned) {
assert!(cell.is_owned())
}

// Now make sure that the indices of the global cells are in consecutive order

let mut cell_global_count = grid.cell_layout().local_range().0;

for cell in grid.local_grid().cell_iter().take(cell_count_owned) {
assert_eq!(cell.global_index(), cell_global_count);
cell_global_count += 1;
}

// Get the global indices.

let global_vertices = grid
.local_grid()
.entity_iter(0)
.filter(|e| matches!(e.ownership(), Ownership::Owned))
.map(|e| e.global_index())
.collect::<Vec<_>>();

let nvertices = global_vertices.len();

let global_cells = grid
.local_grid()
.entity_iter(2)
.filter(|e| matches!(e.ownership(), Ownership::Owned))
.map(|e| e.global_index())
.collect::<Vec<_>>();

let ncells = global_cells.len();

let mut total_cells: usize = 0;
let mut total_vertices: usize = 0;

comm.all_reduce_into(&ncells, &mut total_cells, SystemOperation::sum());
comm.all_reduce_into(&nvertices, &mut total_vertices, SystemOperation::sum());

assert_eq!(total_cells, (n - 1) * (n - 1));
assert_eq!(total_vertices, n * n);
}

fn main() {
let universe: Universe = mpi::initialize().unwrap();
let comm = universe.world();

if comm.rank() == 0 {
println!("Testing GraphPartitioner::None");
}
run_test(&comm, GraphPartitioner::None);

#[cfg(feature = "coupe")]
if comm.rank() == 0 {
println!("Testing GraphPartitioner::Coupe");
}
#[cfg(feature = "coupe")]
run_test(&comm, GraphPartitioner::Coupe);
}

0 comments on commit 4498fe5

Please sign in to comment.