OAOzden Akinci
MPI lecture notes / Chapter 1 of 7

Getting started with MPI

The message-passing model, the six calls every program uses, and how to compile and run your first MPI program.

The message-passing model

An MPI program is started as several copies of the same executable, called processes or ranks. Each one has its own memory, and none can read another’s variables. They cooperate only by sending each other messages through the MPI library. This is the model behind almost every large simulation code, because it works the same way on a laptop, a single server and a cluster with thousands of nodes.

Three ideas carry through everything that follows:

  • Rank. Every process has an integer id, from 0 to size − 1. Programs use it to decide which part of the work to do.
  • Communicator. A group of processes that can talk to each other. MPI_COMM_WORLD contains all of them. Later chapters create smaller ones.
  • SPMD. Single program, multiple data: all ranks run the same code, and if (rank == 0) style branches give them different roles.

Six calls go a long way

Call What it does
MPI_Init Starts MPI. Must come before any other MPI call.
MPI_Comm_size Number of processes in a communicator.
MPI_Comm_rank This process’s rank in a communicator.
MPI_Send Sends a message to one process.
MPI_Recv Receives a message from one process.
MPI_Finalize Shuts MPI down. No MPI calls after it.

With these six you can write any parallel program. Everything else in MPI exists to make programs shorter, faster or easier to get right.

Hello, MPI

#include <stdio.h>
#include <mpi.h>

int main(int argc, char **argv)
{
    int rank, size, len;
    char host[MPI_MAX_PROCESSOR_NAME];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Get_processor_name(host, &len);

    printf("Hello from rank %d of %d on %s\n", rank, size, host);

    MPI_Finalize();
    return 0;
}

Compiling and running

MPI libraries ship wrapper compilers that add the right include paths and libraries: mpicc for C, mpicxx for C++ and mpif90 for Fortran. The launcher, mpirun or mpiexec, starts the processes:

mpicc -O2 -o 01_hello 01_hello.c
mpirun -np 4 ./01_hello
Hello from rank 2 of 4 on node01
Hello from rank 0 of 4 on node01
Hello from rank 3 of 4 on node01
Hello from rank 1 of 4 on node01

The order of the lines changes from run to run. The processes run independently, and their output is merged by the launcher in whatever order it arrives. Never rely on the order of printf output from different ranks.

On a cluster you normally start MPI programs through the batch scheduler. With Slurm, for example:

#!/bin/bash
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=32
#SBATCH --time=00:10:00
srun ./01_hello

srun knows from the job which nodes and how many tasks it may use, so you don’t pass -np yourself.

Things to keep in mind

  • Every rank runs main from the start. Code before MPI_Init runs on every process too, so keep it to plain declarations.
  • Variables are not shared. If rank 0 reads an input file, the other ranks do not see its contents until rank 0 sends them. Chapter 3 shows the usual way to do that with MPI_Bcast.
  • Return codes. By default, MPI aborts the whole job when an error occurs, so most codes don’t check return values. You can change that with MPI_Comm_set_errhandler if you need to recover from errors.

Exercises

  1. Run the program with 1, 2 and 8 processes. What happens if you start more processes than the machine has cores? (Open MPI refuses unless you add --oversubscribe.)
  2. Make only rank 0 print the number of processes, and every other rank print nothing.
  3. Measure the time between MPI_Init and MPI_Finalize with MPI_Wtime() on rank 0. How long does starting MPI take on your system with 2 and with 64 processes?