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:

 

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: