MPI - From serial to parallel: Hello world!

Objectives

  • Know how a serial program looks like and how to compile and run it

  • Know how to compile and run an MPI program (from the outside)

  • Understand what makes an MPI program (how to activate MPI inside of the program)

Instructor note

  • 15 min teaching

  • 10 min exercises

 

Serial “Hello world!” program

To warm up, let’s start with a serial “Hello world!” program, no changes/editing needed:

#include <stdio.h>

int main(int argc, char *argv[])
{
   printf ("Hello world!\n");
}

Compile the serial program:

gcc hello_serial.c -o hello_serial

Run the serial program (serial run on 1 core):

./hello_serial

Expected output:

Hello world!

 

MPI process model

Note

  • An MPI program must be linked with an MPI library (compiler wrapper) –> mpicc, …

  • An MPI program must be started with an MPI startup tool –> mpirun, mpiexec, srun, …

    • Usage:   mpirun -np # ./a.out   (# … number of MPI processes)

  • An MPI program must use the include file of this MPI library –> #include <mpi.h>

  • MPI must be initialized and finalized –> MPI_Init and MPI_Finalize

    • Usage:   MPI_Init(&argc, &argv);   OR   MPI_Init(NULL, NULL);

    • Usage:   MPI_Finalize();

    • Note:     This applies for the World Model and for starting a single-threaded MPI application. MPI offers different thread-level support, but that’s an advanced topic to be covered later.

Exercise

Write a minimal MPI program that prints “Hello world!” by each MPI process:

What happens if you do NOT modify the code below? Try it out!

You can compile and run without modifying the serial code below.
Give it a try before you actually modify.
What happens here? Why is this possible at all?
Of course, before you can proceed to the next step (2), you have to modify the code below.

#include <stdio.h>

int main(int argc, char *argv[])
{
   printf ("Hello world!\n");
}

In C and Fortran compile it as an MPI program (you need to change the code below):

_____ hello.c -o hello

Run it in parallel with several (e.g., 4) MPI processes (you need to change the code below):

__________ ./hello

Expected output with 4 MPI processes:

Hello world!
Hello world!
Hello world!
Hello world!

Got stuck? Checkout the solution:

The correct MPI program:

How to compile an MPI programm in C and Fortran:

How to run an MPI programm:

Exercise

Play around with different numbers of MPI processes Run with different numbers of MPI processes:
E.g., in the VSC JupyterHub, there are 4 physical cores available for the MPI course.
If you want to run with more MPI processes you have to add the option: –oversubscribe
Note: For performance runs you want to avoid oversubscribing, but it’s okay for our little examples.
Note: You can not do oversubscribing and pinning (see 4. Demo) at the same time.

Keypoints

  • Know how to compile and run an MPI program

  • Understand what makes an MPI program

See also

Details and recommended reading MPI 5.0 Chapter 11 - Process Initialization, Creation, and Management

  • pages 477-478: 11.1 Introduction

  • pages 478-479: 11.2 The World Model, 11.2.1 Starting MPI Processes (until Example 11.1.)

  • pages 484: 11.2.2 Finalizing MPI (until Example 11.4.)

Further reading - deep dive (not necessary for the course)

  • Multi-threaded MPI applications - MPI_Init_thread et al. - pages 481-484

  • Is MPI already initialized/finalized? - MPI_Initialized, MPI_Finalized - pages 487-489

  • The Sessions Model (11.3) - pages 489-502

  • Common Elements of Both Process Models (11.4) - MPI_Abort - pages 502-507

  • Portable MPI Process Startup (11.5) - mpirun, mpiexec - pages 509-511

  • MPI and Threads (11.6) - pages 511-516

  • The Dynamic Process Model (11.7) - not useful with static job environments - pages 516-519

  • Process Manager Interface (11.8) - not useful with static job environments - pages 519-526

  • Establishing Communication (11.9) - pages 526-536

  • Other Functionality (11.10) - pages 536-542