# Compiling and running MPI codes

<div id="bkmrk-to-illustrate-the-pr">To illustrate the procedure we will compile and run a MPI hello world example from [mpitutorial.com](https://mpitutorial.com/). First we download the source code:</div><div id="bkmrk-"></div><div id="bkmrk--0"></div>```
$ wget https://raw.githubusercontent.com/mpitutorial/mpitutorial/gh-pages/tutorials/mpi-hello-world/code/mpi_hello_world.c
```

### Compiling with GCC

To compile the code, we first need to load the gcc and mvapich2 modules:

```
$ module load mvapich2                                                                                                                                       
```

<div id="bkmrk-then-we-can-produce-">Then we can produce the executable called `mpi_hello_world` by compiling the source code `mpi_hello_world.c`:</div><div id="bkmrk--1"></div><div id="bkmrk--2"></div>```
$ mpicc mpi_hello_world.c -o mpi_hello_world
```

<div id="bkmrk-the%C2%A0mpicc-tool-is-a-">The `mpicc` tool is a wrapper around the gcc compiler that adds the correct options for linking MPI codes and if you are curious you can run `mpicc -show` to see what it does.</div><div id="bkmrk--3"></div><div id="bkmrk--4"></div><div id="bkmrk-to-run-the-executabl">To run the executable we create a Slurm submission script called `run_mpi_hello_world.sh`, where we ask to run a total of 4 MPI tasks with (at max) 2 tasks per node:</div><div id="bkmrk--5"></div>```
#!/bin/bash

#SBATCH --time 00-00:05:00
#SBATCH --mem=2G
#SBATCH --ntasks 4
#SBATCH --ntasks-per-node 2
#SBATCH --cpus-per-task 1

module purge
module load gcc
module load mvapich2
module list

EXE=mpi_hello_world
[ ! -f  $EXE ] && echo "EXE $EXE not found." && exit 1

srun  $EXE
```

<div id="bkmrk-finally%2C-we-submit-o">Finally, we submit our MPI job with:</div><div id="bkmrk--6"></div>```
$ sbatch run_mpi_hello_world.sh
```

<div id="bkmrk--7"></div><div id="bkmrk-upon-completion-you-">  
Upon completion you should get something like:</div><div id="bkmrk--8"></div>```
...

Hello world from processor dna001.curnagl, rank 1 out of 4 processors
Hello world from processor dna001.curnagl, rank 3 out of 4 processors
Hello world from processor dna004.curnagl, rank 0 out of 4 processors
Hello world from processor dna004.curnagl, rank 2 out of 4 processors
```

It is important to check is that you have a single group of 4 processors and not 4 groups of 1 processor. If that's the case, you can now compile and run your own MPI application.

The important bit of the script is the `srun $EXE` as MPI jobs but be started with a job launcher in order to run multiple processes on multiple nodes.