Skip to main content

ring/
ring.rs

1//! Non-blocking ring exchange, adapted from the rsmpi README: each rank sends a
2//! message to its successor while receiving from its predecessor.
3
4use mpi::request::WaitGuard;
5use mpi::traits::*;
6
7fn main() {
8    let universe = mpi::initialize().unwrap();
9    let world = universe.world();
10    let size = world.size();
11    let rank = world.rank();
12
13    let next_rank = (rank + 1) % size;
14    let previous_rank = (rank - 1 + size) % size;
15
16    let msg = [rank, 2 * rank, 4 * rank];
17    mpi::request::scope(|scope| {
18        let _sreq = WaitGuard::from(
19            world
20                .process_at_rank(next_rank)
21                .immediate_send(scope, &msg[..]),
22        );
23
24        let (msg, status) = world.any_process().receive_vec::<i32>();
25
26        println!(
27            "Rank {rank} received {msg:?} from rank {} (tag {}).",
28            status.source_rank(),
29            status.tag()
30        );
31        assert_eq!(status.source_rank(), previous_rank);
32        let x = previous_rank;
33        assert_eq!(vec![x, 2 * x, 4 * x], msg);
34    });
35
36    world.barrier();
37    if rank == 0 {
38        println!("Ring exchange completed successfully.");
39    }
40}