Compiling and Running

From BCCD 3.0

Jump to: navigation, search

This tutorial will walk you through compiling and running an MPI program using either OpenMPI or MPICH. If you're looking for an introduction to MPI programming, please see Hello World for MPI. For a slightly more advanced example, see the Simpson Rule MPI Example.

The tutorial assumes that you have an environment ready to run MPI in parallel. This means that you have booted multiple machines and run bccd-snarfhosts to generate a machines file.

The BCCD ships with multiple implementations of MPI. You can switch between the installations using Modules.


Running MPI

Typically, running an MPI program will consist of three steps:

  1. Compiling the code into an executable
  2. Copying the code across all the nodes on which you would like to run the executable
  3. Running the executable


Assuming you have code to compile (if you have binary executables only, proceed to step 2: copy) you need to create an executable. This involves compiling your code with the appropriate compiler, linked against the MPI libraries.

Suppose you have an MPI C file named hello.c. You can compile the program using the mpicc command:

mpicc -o hello.exe hello.c<ENTER>

The "-o" option allows you to provide an output file name, otherwise your executable would be saved as "a.out". The executable name is NOT required to end with .exe, but it can help you remember what type of file it is. Be careful to put a filename after the -o option, and make sure it is not the same name as your source code! Many programmers have deleted part of their source code by accidentally giving their source code the same name as their output file name.

If the compilation was successful, there will be no output. If you run ls, it should show that you have created the file hello.exe.


In order for your program to run on each node, the executable must exist on each node. If your nodes are sharing a networked filesystem (i.e. if your head node is in Liberated mode and the other nodes booted via PXE), you can skip this step. If you are instead running in Live mode and not sharing a networked filesystem, the following command will copy the current directory to each node. Make sure to replace openmpi with mpich2 if you are using MPICH instead of the default OpenMPI:

bccd-syncdir . ~/machines-openmpi<ENTER>

This creates a directory on all the nodes in /tmp. The name of this directory will be based on the hostname of the node on which you ran the bccd-syncdir command, for example, /tmp/node000-bccd


Once you have compiled the code and copied the executable file to all of the nodes, you can run the executable using the command below. If you are using MPICH, replace openmpi with mpich2. Replace 2 with the number of MPI processes with which you want to run. Replace node000 with the hostname of the node on which you ran the bccd-syncdir command:

mpirun -machinefile ~/machines-openmpi -np 2 /tmp/node000-bccd/hello.exe<ENTER>

Personal tools