Skip to main content

mpi_complete_tree/
mpi_complete_tree.rs

1//! Demonstrate the instantiation of a complete octree using MPI.
2
3use std::time::Instant;
4
5use bempp_octree::{generate_random_points, Octree};
6use mpi::traits::Communicator;
7use rand::prelude::*;
8use rand_chacha::ChaCha8Rng;
9
10pub fn main() {
11    // Initialise MPI
12    let universe = mpi::initialize().unwrap();
13
14    // Get the world communicator
15    let comm = universe.world();
16
17    // Initialise a seeded Rng.
18    let mut rng = ChaCha8Rng::seed_from_u64(comm.rank() as u64);
19
20    // Create `npoints` per rank.
21    let npoints = 1000000;
22
23    // Generate random points on the positive octant of the unit sphere.
24
25    let mut points = generate_random_points(npoints, &mut rng, &comm);
26    // Make sure that the points live on the unit sphere.
27    for point in points.iter_mut() {
28        let len = point.coords()[0] * point.coords()[0]
29            + point.coords()[1] * point.coords()[1]
30            + point.coords()[2] * point.coords()[2];
31        let len = len.sqrt();
32        point.coords_mut()[0] /= len;
33        point.coords_mut()[1] /= len;
34        point.coords_mut()[2] /= len;
35    }
36
37    let start = Instant::now();
38    // The following code will create a complete octree with a maximum level of 16.
39    let octree = Octree::new(&points, 16, 50, &comm);
40    let duration = start.elapsed();
41
42    let global_number_of_points = octree.global_number_of_points();
43    let global_max_level = octree.global_max_level();
44
45    // We now check that each node of the tree has all its neighbors available.
46
47    if comm.rank() == 0 {
48        println!(
49            "Setup octree with {} points and maximum level {} in {} ms",
50            global_number_of_points,
51            global_max_level,
52            duration.as_millis()
53        );
54    }
55}