> For the complete documentation index, see [llms.txt](https://eric-lo.gitbook.io/mpi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eric-lo.gitbook.io/mpi/master.md).

# Running MPICH program

MPICH is one of the most popular implementations of MPI.&#x20;

In this tutorial, we aim to introduce that in a CSCI4160 cluster with 3 machines: `node-1`, `node-2`, and `node-3`.

![](https://469522351-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq54LMdFfPQqmC8BHp2%2Fuploads%2FfJ9IzCau91TxhLpU0ZOV%2Fimage.png?alt=media\&token=c97064dc-92b4-4644-91f1-aaca8844c95a)

## How to compile MPI program

Here we will use `mpi_hello_world` in [https://mpitutorial.com/tutorials/](https://mpitutorial.com/tutorials/mpi-send-and-receive/) as an example.&#x20;

### SSH to node-1

You can login `node-1` by using your CSE account just like `linux9.`

### Clone the mpitutorial git repository

```bash
cd ~ # make sure that your are now in the home directory 
git clone https://github.com/wesleykendall/mpitutorial
cd  ~/mpitutorial/tutorials/mpi-hello-world/code
```

### Compile the mpi\_hello\_world using mpicc

Instead of using `gcc`, we use `mpicc` such that the mpich library is linked.&#x20;

```
mpicc -o mpi_hello_world mpi_hello_world.c
```

Now you should get the executable `mpi_hello_world`

## Run MPI program in CSCI4160 cluster using SLURM&#x20;

We have setup a small SLURM cluster consists of `node-1`, `node-2` and `node-3`.  Use `salloc` command to launch your mpi program on that cluster:

`salloc -N 3 mpirun ./mpi_hello_world`

`-N 3`: request 3 nodes from the cluster

```
node-1:~/mpitutorial/tutorials/mpi-hello-world/code$ salloc -N 3 mpirun ./mpi_hello_world
salloc: Granted job allocation 23695
Hello world from processor node-3.cse.cuhk.edu.hk, rank 2 out of 3 processors
Hello world from processor node-1.cse.cuhk.edu.hk, rank 0 out of 3 processors
Hello world from processor node-2.cse.cuhk.edu.hk, rank 1 out of 3 processors
salloc: Relinquishing job allocation 23695
```

You can see that there are 3 Hello world messages from 3 processors with different hostnames: `node-1 node-2 node-3`

## MPICH Programming

I suggest you go through the tutorials below, which will help you a lot in Asgn1c.

| Name                                                                                                      | link                                                                                       |
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| MPI Send and Receive                                                                                      | [link](https://mpitutorial.com/tutorials/mpi-send-and-receive/)                            |
| Dynamic Receiving with MPI\_Probe (can help to query the number of permissible points on another machine) | [link](https://mpitutorial.com/tutorials/dynamic-receiving-with-mpi-probe-and-mpi-status/) |
| Custom MPI data type (you may use this to ship `struct point` datatype)                                   | [link](https://www.codingame.com/playgrounds/349/introduction-to-mpi/custom-types)         |

{% hint style="info" %}
In the links above, they launch MPI using a script`run.py` .  Using salloc to launch MPI is also OK.
{% endhint %}
