Rust MPI Hello World

MPI - std::thread - Rayon

Writing a simple Rust MPI program that will say hello from each processor

Rust MPI Hello World

MPI is a powerful tool to have in the HPC world, and Rust supports it.

You will need to install the MPI and Clang libraries on your system to use Rust MPI

sudo apt install mpich 
sudo apt install clang
	

Add mpi to your cargo.toml file

[dependencies]
mpi = "0.6"
	

main.rs

	
extern crate mpi;

use mpi::request::WaitGuard;
use mpi::traits::*;

fn main() {
    let universe = mpi::initialize().unwrap();
    let world = universe.world();
    let size = world.size();
    let rank = world.rank();

    println!("Hello, world from rank {} of {}", rank, size);
}
	

runtime output

	
me@pi:~/rust/mpi-hello$ mpirun -n 4 target/debug/mpi-hello
Hello, world from rank 2 of 4
Hello, world from rank 0 of 4
Hello, world from rank 1 of 4
Hello, world from rank 3 of 4
me@pi:~/rust/mpi-hello$
	

Mission Accomplished

And there we have it, because the rank is numbered from 0 the highest rank will be one less than the size.

When running the program we use the mpirun command with the -n flag to say how many processors to run the program on.