Open MPI library

This post introduces the Open MPI library. (Its official website, its GitHub Repo)

Open MPI is a popular open source MPI implementation.

Open MPI is a Message Passing Interface (MPI) library project combining technologies and resources from several other projects (FT-MPI, LA-MPI, LAM/MPI, and PACX-MPI). It is used by many TOP500 supercomputers including Roadrunner, which was the world’s fastest supercomputer from June 2008 to November 2009,[1]and K computer, the fastest supercomputer from June 2011 to June 2012.[2][3]

The Open MPI developers selected these MPI implementations as excelling in one or more areas. Open MPI aims to use the best ideas and technologies from the individual projects and create one world-class open-source MPI implementation that excels in all areas. The Open MPI project specifies several top-level goals:

  • to create a free, open source software, peer-reviewed, production-quality complete MPI-3.0 implementation
  • to provide extremely high, competitive performance (low latency or high bandwidth)
  • to involve the high-performance computing community directly with external development and feedback (vendors, 3rd party researchers, users, etc.)
  • to provide a stable platform for 3rd-party research and commercial development
  • to help prevent the “forking problem” common to other MPI projects[4]
  • to support a wide variety of high-performance computing platforms and environments

 

 

 

 

Parallel Programming using MPI in Python

This post introduces Parallel Programming using MPI in Python.

The library is mpi4py (MPI and python extensions of MPI), see here for its code repo on bitbucket.

Laurent Duchesne provides an excellent step-by-step guide for parallelizing your Python code using multiple processors and MPI. Craig Finch has a more practical example for high throughput MPI on GitHub. See here for more mpi4py examples from Craig Finch.

An example of TensorFlow using MPI can be found here.

References:

 

MPI and OpenMP with Python

This post lists resources for using MPI with Python.

An application built with the hybrid model of parallel programming can run on a computer cluster using both OpenMP and Message Passing Interface (MPI), such that OpenMP is used for parallelism within a (multi-core) node while MPI is used for parallelism between nodes. There have also been efforts to run OpenMP on software distributed shared memory systems,[6] to translate OpenMP into MPI[7][8] and to extend OpenMP for non-shared memory systems.

cython.parallel is built on top of OpenMP (see Using Parallelism)

Please read Laurent Duchesne’s excellent step-by-step guide for parallelizing your Python code using multiple processors and MPI.

On our cluster, to run MPI Python programs, mpi4py has been compiled against OpenMPI 1.10.1 therefore we need to load that additional package:

module load python/3.4.3 mpi/openmpi/1.10.1-gcc

Create the the test MPI example file as described in Laurent’s guide above, using the same name mpi.py:

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

print("I am rank", rank, "of", size)

Create the SLURM submission script submit.sh:

#SBATCH -n 4 
mpirun python mpi.py

You should get output similar to:

I am rank 3 of 4
I am rank 0 of 4
I am rank 1 of 4
I am rank 2 of 4

Craig Finch has a more practical example for high throughput MPI on GitHub.

 

References:

 

Parallel programming on Ubuntu using OpenMP with C/C++

This post introduces the basic of parallel programming on Ubuntu using OpenMP with C/C++. (MP in OpenMP refers to as Multi-processing.)

OpenMP, is an an API that enables direct multi-threaded, shared memory parallelism. See here for a nice OpenMP tutorial. A good introduction of OpenMP can be found here.

See here for wiki intro to OpenMP.

OpenMP uses a portable, scalable model that gives programmers a simple and flexible interface for developing parallel applications for platforms ranging from the standard desktop computer to the supercomputer.

An application built with the hybrid model of parallel programming can run on a computer cluster using both OpenMP and Message Passing Interface (MPI), such that OpenMP is used for parallelism within a (multi-core) node while MPI is used for parallelism between nodes. There have also been efforts to run OpenMP on software distributed shared memory systems, to translate OpenMP into MPI and to extend OpenMP for non-shared memory systems.

1: Create a text file and paste the follow code into it

Writing the hello world code in C is the same everywhere. There are only minor differences in the code.

#include<stdio.h>

int main( int ac, char **av)

{

#pragma omp parallel // specify the code between the curly brackets is part of an OpenMP parallel section.

{

printf("Hello World!!!\n");

}

return 0;

}

Save the code as anyname.c.  Now let’s compile the code.

2:  Open a terminal (CTRL + ALT + T)

3: Compile the code

In your terminal, cd to the path where the c script you just created is located.

To compile your code, simply type this in the terminal

gcc anyname.c -o anyname.out

If there is any problem in the code, you must fix it and compile again. Now to next (big) step .

4: Number of threads and running in OpenMp

When the code is compiled correctly, we can run it parallel form using OpenMP. firstly we determine number of threads we are going to use. type this in your terminal:

export OMP_NUM_THREADS=4

you can try changing the number of threads into numbers you like. (Note that how many maximum threads you can use depends on your machine’s hardware.)

Then, we are going to compile it using the OpenMP. It is similar to normal compiling but with addition of a few words. The format is like this:

gcc -fopenmp anyname.c -o anyname.out

after that, you can run the program. To run the program, type this into your terminal:

./anyname.out

you will find out that the hello world prints out as many times as your thread number.

 

Notes for c++ with OpenMP :

Here I will just talk about how to compile cpp code, the other part with OpenMP is the same as c code, except for: need to change .c to .cpp and gcc to g++.

If you don’t have C/C++ compiler then first install it by  (Normally this comes with an installed Ubuntu OS):
sudo apt-get build-essential
Then open Gedit or any editor , write your code and save it as
“myprogram.cpp”. (See below for a simple example of c++ code)

#include <iostream>
int main()
{
  #pragma omp parallel 
  {
      std::cout << "hello world \n";
    }
    return 0;
}

Next step is to compile:
Make sure your terminal is cd to the folder where you have saved your .cpp file.
cd /path to/the cpp file
Once done you can compile it using G++
g++ myprogram.cpp
This will output a.out (by default) which is your executable code that can be run as
 ./a.out
If you want to name the output instead of a.out, you can pass a parameter to G++
g++ myprogram.cpp -o myprogram
This time it will output a file “myprogram” which again can be run in the same way
./myprogram

 

Notes for OpenMP:

You don’t install OpenMP. OpenMP is a feature of the compiler. Check that the compiler you are using implements OpenMP, which is an API for parallel programming in C/C++/Fortran.

Check with the OpenMP website, openmp.org for a list of compilers and platforms that support OpenMP programming.

OpenMP Directives:

  1. Directive Format
  2. C/C++ Directive Format
  3. Directive Scoping
  4. PARALLEL Construct

(Thanks Weiming for introducing this cool feature of OpenMP to the lab.)

Posts referenced: