gather/
gather.rs

1#![deny(warnings)]
2extern crate mpi_fork_fnsp as mpi;
3
4use mpi::datatype::{MutView, UserDatatype, View};
5use mpi::traits::*;
6use mpi::Count;
7
8fn main() {
9    let universe = mpi::initialize().unwrap();
10    let world = universe.world();
11    let root_rank = 0;
12    let root_process = world.process_at_rank(root_rank);
13
14    let count = world.size() as usize;
15    let i = 2_u64.pow(world.rank() as u32 + 1);
16
17    if world.rank() == root_rank {
18        let mut a = vec![0u64; count];
19        root_process.gather_into_root(&i, &mut a[..]);
20        println!("Root gathered sequence: {:?}.", a);
21        assert!(a
22            .iter()
23            .enumerate()
24            .all(|(a, &b)| b == 2u64.pow(a as u32 + 1)));
25    } else {
26        root_process.gather_into(&i);
27    }
28
29    let factor = world.rank() as u64 + 1;
30    let a = (1_u64..)
31        .take(count)
32        .map(|x| x * factor)
33        .collect::<Vec<_>>();
34
35    if world.rank() == root_rank {
36        let mut t = vec![0u64; count * count];
37        root_process.gather_into_root(&a[..], &mut t[..]);
38        println!("Root gathered table:");
39        for r in t.chunks(count) {
40            println!("{:?}", r);
41        }
42        assert!((0_u64..)
43            .zip(t.iter())
44            .all(|(a, &b)| b == (a / count as u64 + 1) * (a % count as u64 + 1)));
45    } else {
46        root_process.gather_into(&a[..]);
47    }
48
49    let d = UserDatatype::contiguous(count as Count, &u64::equivalent_datatype());
50    let sv = unsafe { View::with_count_and_datatype(&a[..], 1, &d) };
51
52    if world.rank() == root_rank {
53        let mut t = vec![0u64; count * count];
54
55        {
56            let mut rv =
57                unsafe { MutView::with_count_and_datatype(&mut t[..], count as Count, &d) };
58            root_process.gather_into_root(&sv, &mut rv);
59        }
60
61        println!("Root gathered table:");
62        for r in t.chunks(count) {
63            println!("{:?}", r);
64        }
65        assert!((0_u64..)
66            .zip(t.iter())
67            .all(|(a, &b)| b == (a / count as u64 + 1) * (a % count as u64 + 1)));
68    } else {
69        root_process.gather_into(&sv);
70    }
71}