simple/
simple.rs

1use std::sync::Arc;
2
3use balancer::Balancer;
4use mpi::{environment::Universe, traits::Communicator};
5
6fn main() {
7    let universe = Arc::new(mpi::initialize().unwrap());
8    experiment(universe.clone());
9    experiment(universe.clone());
10    if universe.world().rank() == 0 {
11        println!("done!");
12    }
13}
14
15fn experiment(universe: Arc<Universe>) {
16    // Get relevant portion of data on this node
17    let data: Vec<f64> = (0..100_000).map(|x| x as f64 / 100_000.0).collect();
18
19    // Define task
20    let work = |x: &f64| x * x;
21
22    // Initialize balancer, work and collect
23    let verbose = false;
24    let balancer = Balancer::new(universe, verbose);
25    balancer.work_subset(&data, work);
26    let output = balancer.collect();
27
28    // That's it!
29    // Let's do some verification
30    if balancer.rank == 0 {
31        for (expected, actual) in data.iter().map(work).zip(output.as_ref().unwrap()) {
32            assert_eq!(expected, *actual);
33        }
34    }
35}