18 May 2011

Open MPI on Ubuntu

A. Install OpenMPI

$ sudo apt-get install libopenmpi-dev openmpi-bin openmpi-doc
$ sudo apt-get install ssh

B. Configure SSH

$ ssh-keygen -t dsa
$ cd ~/.ssh
$ cat id_dsa.pub >> authorized_keys

C. MPI Program Example

Filename: MPI_Hello.c

#include <stdio.h>
#include <mpi.h>

int main(int argc, char** argv) 
    int myrank, nprocs;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    printf("Hello from processor %d of %d\n", myrank, nprocs);

    return 0;

D. Compilation

$ mpicc MPI_Hello.c -o MPI_Hello

E. Execution

$ mpiexec -n 5 MPI_Hello

F. Integrated Development Environment

We will use Geany as an IDE to make compilation process easier and faster. To compile MPI program with Geany, follow these steps:
  1. Install Geany.
    $ sudo apt-get install geany
  2. Download MPI tags file and copy to ~/.config/geany/tags/
    $ wget http://auriza.site40.net/docs/paralel/openmpi.c.tags
    $ cp openmpi.c.tags ~/.config/geany/tags/
  3. Start Geany, and write MPI program example above.                                             
  4. Compilation setting: Build - Set Includes and Arguments.
    Compile: mpicc -Wall "%f" -o "%e"                                                                  Execute: mpiexec -n 5 "%e"
   5.To compile program: click Compile
   6. To execute program: click Execute.



  1. Hey
    Thanks for the tutorial but when i compiled my program i got the following error Plz help

    /usr/bin/ld: cannot find -lcr
    collect2: ld returned 1 exit status

    1. sorry for the delay...i was caught up in lot of things...
      This error shows that you are not able to link with the 'cr' library.
      Try installing it by -
      $ sudo apt-get install libcr-dev

      After installation is complete. Try to compile your mpi code again.

  2. Thanks for the all-encompassing tutorial. It took me about 30 seconds as opposed to the hours I spent trying to get it working the other day.

    1. Thanks...really glad to hear that I could be of help. :-)

  3. Thanks buddy. IT works.

  4. Dear Gaurav,

    Thank you very much!

    You've done a marvelous job! Thanks once again!

    OpenMPI was successfully installed in my machine running Ubuntu 12.04.

    By the way, there are a couple of problems in my installation as bellow

    1) SSH configuration:

    vishal@vishal-Inspiron-1525:~/.ssh$ cat id_dsa.pub >> authorized_keys
    cat: id_dsa.pub: No such file or directory

    2) Geany configuration (Site seems down)

    /.ssh$ wget http://auriza.site40.net/docs/paralel/openmpi.c.tags
    --2013-04-13 16:44:53-- http://auriza.site40.net/docs/paralel/openmpi.c.tags
    Resolving auriza.site40.net (auriza.site40.net)...
    Connecting to auriza.site40.net (auriza.site40.net)||:80...
    failed: Connection timed out.

    --2013-04-13 16:45:58-- (try: 2) http://auriza.site40.net/docs/paralel/openmpi.c.tags
    Connecting to auriza.site40.net (auriza.site40.net)||:80... failed: Connection timed out.

    Pl help.

    Thanks in advance.
    Vishal V. Parkar

    1. Hi vishal,
      1. "cat: id_dsa.pub: No such file or directory" - This error indicates that the cat command cannot find your specified file. So check your current ssh directory for id_dsa.pub file. If its not generated, then generate it using ssh.

      2. http://auriza.site40.net site seem to be down. So you are not getting the tag files. Actually these files are just required for auto completion feature in geany. So you don't necessarily need them. The main compilation and execution is done by the mpicc and mpiexec command which can be done without geany as well.

    2. Let me know if there is any further complication.

  5. Thank you for your tutorial. It resulted in my very first MPI program. I hope you have a great weekend!

    Just for anyone that might have any problems I found the following two terminal commands helpful.
    $ mpicc --showme:link
    $ mpicc --showme:compile

    Also mpic++ can be used for fileName.cpp programs

  6. Thanks a lot, it's really helpful artical