Creating Directory
Creating a directory for running samples.
Copying-Samples
Copying Samples to current directory.
Showing Source
Showing Source code with cat.
Loading Modules
Loading the openmpi/intel-12 module.
Compiling Code
Compiling the source code.
Submit Job
Showing jobscript and submitting the job.
Showing Output
Showing the output files.

Compiling C++ Example Using the openmpi modules

All the sample code can be found on the cluster at /share/apps/samples/openmpi-c++ you can copy them to your home folder using:
$ cp -r /share/apps/samples/openmpi-c++ ./

 

First you need to create your source code. See example mpi_hello.cpp  below:

// Filename: mpi_hello.cpp
// Description: A parallel hello world program

#include <iostream>
#include <mpi.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        MPI::Init(argc, argv);

        int rank = MPI::COMM_WORLD.Get_rank();
        int size = MPI::COMM_WORLD.Get_size();

        std::cout<<"Returned: "<<system("sleep 2")<<" ";
        std::cout << "Hello World! I am " << rank << " of " << size <<
        std::endl;

        MPI::Finalize();
        return(0);
}
 

Then you must load the correct openmpi module

$ module load openmpi-intel (Loads the openmpi/intel-12 module)
or
$ module load openmpi/intel-11
or
$ module load openmpi/intel-12
or
$ module load openmpi-gnu
or
$ module load openmpi/gcc45
or
$ module load openmpi/gcc46
or
$ module load openmpi/gcc47
 

Then compile the code

$ mpiCC -o openmpi-hello mpi_hello.cpp
 
You should not see any error output, and should have an executable file called lam-hello in current your directory.
CAUTION: If you do not load the module your code will not compile correctly!

Write the jobfile (jobfile.job)

#PBS -N openmpi-hello
#PBS -q @nic-cluster.mst.edu
#PBS -l nodes=2
#PBS -l walltime=01:00:00
#PBS -m abe
#PBS -M joeminer@mst.edu
#PBS -V
cd $PBS_O_WORKDIR
mpirun -np 2 ./openmpi-hello

Submit the Job

If you have logged out since you last loaded the module you will need to load the module again.
$ qsub jobfile.job

Output

openmpi-hello.o####
Returned: 0 Hello World! I am 1 of 2
Returned: 0 Hello World! I am 0 of 2

Errors

If the output is not what you expected check the openmpi-hello.e#### file.