Blog

Posted 2011/09/22

MPI where am I?

Here is a simple MPI program which will print out the hostname of the node it is running on. By printing the hostname you can see where the process is being executed.

whereami.c
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "mpi.h"

main(int argc, char **argv ) {
    int rank, size;
    int buflen = 512;
    char name[buflen];
    
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    gethostname(name, buflen);
    
    printf( "P rank %d of %d, host %s\n", rank, size, name);

    MPI_Finalize();
}

You can compile whereami.c the normal MPI way:

$ mpicc -o whereami whereami.c 

MPI programs need a host list of the nodes that can be used to run the job. You can use command line options to wrangle exactly how processes are spread between nodes. This is what people have trouble with. You can download my host_list.txt for the rlogin cluster. I set it up with the names for 10 nodes.

Where are some examples where I tell MPI how to schedule my job across the nodes.

# 10 processes, 1 process per node, 10 nodes
$ mpirun -mca btl ^openib -np 10 -npernode 1 --hostfile host_list.txt ./whereami
P rank 7 of 10, host hawthorn.rlogin
P rank 0 of 10, host ash.rlogin
P rank 2 of 10, host birch.rlogin
P rank 1 of 10, host beech.rlogin
P rank 8 of 10, host hemlock.rlogin
P rank 9 of 10, host hickory.rlogin
P rank 6 of 10, host hackberry.rlogin
P rank 4 of 10, host chinkapin.rlogin
P rank 3 of 10, host boxelder.rlogin
P rank 5 of 10, host gum.rlogin

# 10 processes, 10 processes per node, 1 node
$ mpirun -mca btl ^openib -np 10 -npernode 10 --hostfile host_list.txt ./whereami
P rank 1 of 10, host ash.rlogin
P rank 6 of 10, host ash.rlogin
P rank 5 of 10, host ash.rlogin
P rank 2 of 10, host ash.rlogin
P rank 0 of 10, host ash.rlogin
P rank 3 of 10, host ash.rlogin
P rank 4 of 10, host ash.rlogin
P rank 9 of 10, host ash.rlogin
P rank 7 of 10, host ash.rlogin
P rank 8 of 10, host ash.rlogin

# 10 processes, 2 processes per node, 5 nodes
$ mpirun -mca btl ^openib -np 10 -npernode 2 --hostfile host_list.txt ./whereami
P rank 8 of 10, host chinkapin.rlogin
P rank 9 of 10, host chinkapin.rlogin
P rank 0 of 10, host ash.rlogin
P rank 1 of 10, host ash.rlogin
P rank 2 of 10, host beech.rlogin
P rank 6 of 10, host boxelder.rlogin
P rank 3 of 10, host beech.rlogin
P rank 7 of 10, host boxelder.rlogin
P rank 4 of 10, host birch.rlogin
P rank 5 of 10, host birch.rlogin