(last update: Wed Dec 16 09:43:00 CET 2015)
In Debian or Ubuntu, we will use the OpenMPI implementation of the MPI standard. OpenMPI is installed on all computers at the room G207.
If you wish to install it at home or in your laptop, you will need to following packages:
libopenmpi-dev openmpi-bin openmpi-doc openmpi-checkpoint
The following program says hello from each process (files found here: examples/hello-world):
#include <stdio.h> #include <mpi.h> int main (int argc, char ** argv) { int rank; MPI_Init (&argc, & argv); MPI_Comm_rank (MPI_COMM_WORLD, &rank); printf ("p%d: hello\n", rank); MPI_Finalize (); return 0; }
To compile it, execute the following command:
mpicc -Wall hello.c -o hello
To run it, using 4 processes, all of them running on the local host:
mpirun -n 4 ./hello
It will output something similar to this:
p0: hello p1: hello p3: hello p2: hello
The following Makefile could be useful for your projects (obseve that the target all not only compiles the program hello.c but it also executes it):
CFLAGS=-Wall CC=mpicc all : hello mpirun -n 4 ./hello clean : rm -f hello *.o
The tool mpirun can be instructed to launch processes on multiple machines.
It will use ssh to connect to the other machines. It will be convenient to add your public key to the ~/.ssh/authorized_keys file. In the lab, run:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
(If you don't have a key pair, you will need to create it, run the command ssh-keygen.)
mpirun does not distribute the executable (this is done using the NFS)
Important options:
-n N: run N processes
-H HOST1,HOST2,...: run one process in HOST1, one in HOST2, and so on (round-robin assignment)
-hostfile FILE: read from FILE the list of hosts to run the processes. Example:
$ cat FILE G207-1 slots=5 G207-6 slots=2
Up to 5 processes will execute in G207-1, and up to 2 processes in G207-6. If more than 7 processes are requested via -n, the remaining ones will be assigned round-robin.
For additional details, refer to the mpirun(1) man page.
Running 4 processes, all of them on the local host:
mpirun -n 4 ./hello
Equivalenly:
mpirun -n 4 -H localhost ./hello
Running 3 processes, two of them in machine G207-1 and one in G207-10 (the assignment of processes (ranks) to hosts is done round-robin):
mpirun -n 3 -H G207-1,G207-10 ./hello
Running 5 processes, 3 on G207-1 and 2 on G207-2 (note that there is no -n option):
$ cat hosts G207-1 slots=3 G207-2 slots=2 $ mpirun -hostfile hosts ./hello
Running 3 processes, all of them in G207-1:
mpirun -n 3 -hostfile hosts ./hello
Running 9 processes (3 + 3) in G207-1, (2 + 1) in G207-2 (two rounds of assignment, as first one "completed" the slots):
mpirun -n 9 -hostfile hosts ./hello