MPI Hello World

In this lesson, I will show you a basic MPI Hello World application and also discuss how to run an MPI program. The lesson will cover the basics of initializing MPI and running an MPI job across several processes. This lesson is intended to work with installations of MPICH2 (specifically 1.4). If you have not installed MPICH2, please refer back to the installing MPICH2 lesson.

MPI Hello World


First of all, the source code for this lesson can be downloaded here. Download it, extract it, and change to the example directory. The directory should contain three files: makefile, mpi_hello_world.c, and run.perl.

Downloading MPI Hello World application

Open the mpi_hello_world.c source code. Below are some excerpts from the code.

#include <mpi.h>
int main(int argc, char** argv) {
  // Initialize the MPI environment
  MPI_Init(NULL, NULL);
 
  // Get the number of processes
  int world_size;
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 
  // Get the rank of the process
  int world_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
  // Get the name of the processor
  char processor_name[MPI_MAX_PROCESSOR_NAME];
  int name_len;
  MPI_Get_processor_name(processor_name, &name_len);
 
  // Print off a hello world message
  printf("Hello world from processor %s, rank %d"
         " out of %d processors\n",
         processor_name, world_rank, world_size);
 
  // Finalize the MPI environment.
  MPI_Finalize();
}

You will notice that the first step to building an MPI program is including the MPI header files with #include <mpi.h>. After this, the MPI environment must be initialized with MPI_Init(NULL, NULL). During MPI_Init, all of MPI’s global and internal variables are constructed. For example, a communicator is formed around all of the processes that were spawned, and unique ranks are assigned to each process. Currently, MPI_Init takes two arguments that are not necessary, and the extra parameters are simply left as extra space in case future implementations might need them.

After MPI_Init, there are two main functions that are called. These two functions are used in almost every single MPI program that you will write.

  • MPI_Comm_size(MPI_Comm communicator, int* size) – Returns the size of a communicator. In our example, MPI_COMM_WORLD (which is constructed for us by MPI) encloses all of the processes in the job, so this call should return the amount of processes that were requested for the job.
  • MPI_Comm_rank(MPI_Comm communicator, int* rank) – Returns the rank of a process in a communicator. Each process inside of a communicator is assigned an incremental rank starting from zero. The ranks of the processes are primarily used for identification purposes when sending and receiving messages.

A miscellaneous and less-used function in this program is MPI_Get_processor_name(char* name, int* name_length), which can obtain the actual name of the processor on which the process is executing. The final call in this program, MPI_Finalize() is used to clean up the MPI environment. No more MPI calls can be made after this one.

Running MPI Hello World


Now compile the example by typing make. My makefile looks for the MPICC environment variable. If you installed MPICH2 to a local directory, set your MPICC environment variable to point to your mpicc binary. The mpicc program in your installation is really just a wrapper around gcc, and it makes compiling and linking all of the necessary MPI routines much easier.

Compiling MPI Hello World application

After your program is compiled, it is ready to be executed. Now comes the part where you might have to do some additional configuration. If you are running MPI programs on a cluster of nodes, you will have to set up a host file. If you are simply running MPI on a laptop or a single machine, disregard the next piece of information.

The host file contains names of all of the computers on which your MPI job will execute. For ease of execution, you should be sure that all of these computers have SSH access, and you should also setup an authorized keys file to avoid a password prompt for SSH. My host file looks like this.

MPI host file

For the run script that I have provided in the download, you should set an environment variable called MPI_HOSTS and have it point to your hosts file. My script will automatically include it in the command line when the MPI job is launched. If you do not need a hosts file, simply do not set the environment variable. Also, if you have a local installation of MPI, you should set the MPIRUN environment variable to point to the mpirun binary from the installation. After this, call ./run.perl mpi_hello_world to run the example application.

Running MPI Hello World application

As expected, the MPI program was launched across all of the hosts in my host file. Each process was assigned a unique rank, which was printed off along with the process name. As one can see from my example output, the output of the processes is in an arbitrary order since there is no synchronization involved before printing.

Notice how the script called mpirun. This is program that the MPI implementation uses to launch the job. Processes are spawned across all the hosts in the host file and the MPI program executes across each process. My script automatically supplies the -n flag to set the number of MPI processes to four. Try changing the run script and launching more processes! Don’t accidentally crash your system though. :-)

Now you might be asking, “My hosts are actually dual-core machines. How can I get MPI to spawn processes across the individual cores first before individual machines?” The solution is pretty simple. Just modify your hosts file and place a colon and the number of cores per processor after the host name. For example, I specified that each of my hosts has two cores.

Setting slots per MPI host

When I execute the run script again, voila!, the MPI job spawns two processes on only two of my hosts.

Running MPI Hello World application with multiple cores

Up Next

Now that you have a basic understanding of how an MPI program is executed, it is now time to learn fundamental point-to-point communication routines. In the next lesson, I cover basic sending and receiving routines in MPI. Feel free to also examine the beginner MPI tutorial for a complete reference of all of the beginning MPI lessons.

Having trouble? Confused? Feel free to leave a comment below and perhaps I or another reader can be of help.

HAS MPITUTORIAL.COM HELPED YOU?

Donate a small sum of 5 dollars today to help me out with my hosting costs. Thank you!


Related posts:

  1. MPI Introduction
  2. Installing MPICH2

4 Comments

  1. mwood says:

    Hi Wes,

    Really like the site so far! I am trying to run a simple MPI program on two computers, one at my house, the other at school. I have ssh’d into the school computer, set secret password, started mpd, and transferred the program file over. When I try to run mpirun with the above options, specifically the -f flag, it spits back errors and brings up the help screen. Seems like mpirun does not like the way I am trying to use the -f flag, perhaps.

    At any rate, I can run the program locally just fine, but have not been able to get it working over ssh. Any thoughts?

    Thanks for your time

    • Wes says:

      Hey Matt, apologies for the really late reply to your question. I have never tried to run an MPI program over a local and remote computer. I am assuming that MPI is getting confused on finding the executable though. Normally people will execute MPI programs over a networked file system of some sort so that all of the processes can find the executable in the same place.

  2. David says:

    First: Thanks for putting this tutorial together.

    But…
    I’ve set up a 4 node system using Raspberry Pi’s. The basic testing (hostname and example/cpi) work fine.

    When I run the hello world program on mpmaster, I get this:
    ————————-
    mpirun was unable to launch the specified application as it could not access
    or execute an executable:

    Executable: ./mpi_hello_world
    Node: mpnode2

    while attempting to start process rank 1.
    ———
    INFO:
    from running env:
    MPI_HOSTS=/home/sysop/dev/hostsfile

    sysop@mpmaster:~/dev$ cat hostsfile
    mpmaster
    mpnode2
    mpnode3
    mpnode4

    And I can ssh without a passphase into the other three nodes.

    One other “strange” thing: I am using MPICH2 1.5. It doesn’t like the -f parameter. I had to change that to -machinefile. Don’t know why, just the -f doesn’t work.

    I was reading somewhere else and it said to scp the executable to all the nodes. Is that correct? Does the executable have to be on all the nodes?

    • Wes says:

      Hey David, that is very strange the -f flag doesn’t work for you. I just installed MPICH2 1.5 and the -f flag still works for me. I will update all of my code to use the -machinefile flag instead.

      Regarding your question – Yes, mpirun needs to have access to the executable on all of the nodes. I’m not positive if copying the file to all of the nodes will work, but I would recommend trying that (and make sure to copy the executable to the same location across all nodes). If that doesn’t work, I would recommend setting up NFS on the nodes or setting up PVFS (http://www.pvfs.org/). Unfortunately I have never set up a networked file system and only have limited experience setting up a parallel file system on clusters. Also, in your case, I believe you are restricted to using only the SD drive on your Raspberry Pi, right? I’m really interested to know if that presents any restrictions in setting up a networked or parallel file system of some sort.

      Keep me updated on your progress, and sorry I couldn’t have been more help. I’m really interested to know how your Raspberry Pi cluster works out!

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    RECOMMENDED BOOKS

    My recommendations for great books on MPI.
    Click here for more info

    BEGINNER TUTORIAL

    A step-by-step tutorial on beginner MPI topics.
    Click here for more info