use bempp_octree::tools::{gather_to_root, global_inclusive_cumsum};
use itertools::{izip, Itertools};
use mpi::traits::Communicator;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
pub fn main() {
let universe = mpi::initialize().unwrap();
let comm = universe.world();
let mut rng = ChaCha8Rng::seed_from_u64(comm.rank() as u64);
let nelems = 10;
let mut elems = Vec::<usize>::with_capacity(nelems);
for _ in 0..nelems {
elems.push(rng.gen_range(0..100));
}
let global_cum_sum = global_inclusive_cumsum(&elems, &comm);
if let (Some(cum_sum_root), Some(original_array)) = (
gather_to_root(&global_cum_sum, &comm),
gather_to_root(&elems, &comm),
) {
let expected_cum_sum = original_array
.iter()
.scan(0, |state, x| {
*state += *x;
Some(*state)
})
.collect_vec();
assert_eq!(
original_array.first().unwrap(),
cum_sum_root.first().unwrap()
);
for (actual, expected) in izip!(cum_sum_root.iter(), expected_cum_sum.iter()) {
assert_eq!(*actual, *expected);
}
println!("Cumulative sum computed.");
}
}