Wednesday, 7 August 2013

Installation Open MPI on Windows

Posted by Suhel Sayyad
This article explains how to setup Open MPI and how to compile and run MPI programs in the Microsoft Windows environment.
Open MPI is an implementation of the MPI standard. It is cross platform and makes use of your installed C++ or Fortran compiler. Getting everything working in a Linux environment is fairly easy provided gcc is installed. However when it comes to Windows there seems to be fairly few resources available on the internet that details the procedures to get everything up and running.
This article does not make direct use of Visual Studio. Any version of Visual Studio should do, all we really need is the Microsoft C++ compiler and related tools that Open MPI uses.
Open MPI

Step 1: Installation

Download and install the latest version of Open MPI. Note that I downloaded the 64 bit version. I also changed the installation path to “Program Files” instead of the default “Program Files (x86)”. The Open MPI website states that this a temporary issue with their installer that should be fixed in time. If the installer asks to setup PATH variables then do so for all users. If the installer reports a failure when trying to do so, then don’t worry, we can do it manually as well. 

Step 2: Open MPI PATH setup

For me the installer failed to setup any PATH variables, stating that my PATH was too long (not true). In this case we shall set it up manually. Simply add the bin directory of Open MPI to the global PATH variable. Thus in my case I added “C:\Program Files\OpenMPI_v1.6\bin” without the quotes to my PATH. 

Step 3: cl.exe PATH setup

Open MPI for Windows is pre-configured to use the Microsoft C/C++ Compiler. It thus needs access to cl.exe, which controls the Microsoft Compiler and Linker. It is important here to add the correct directory to your PATH depending on the architecture (32 bit or 64 bit) of the Open MPI that you downloaded or else you will get linker errors. Since I downloaded the x64 version, I added the following directory to my global PATH: “C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\amd64″ If you installed the x86 version of Open MPI then you can use the base bin directory (without the amd64 on the end). 

Step 4: VCVARS

cl.exe won’t work out the box in any command prompt. It is required that the Visual Studio environmental variables are set up. The good news is that Microsoft provides bat files to do this for you. The bad news is that these settings are lost when you restart your computer. Thus here you have a choice, either simply run the bat file before you wish to compile MPI programs (not that much hassle since the bat file is in the same directory as cl.exe and thus in your PATH now), or you can add the file to your task scheduler to run at system startup. The name of the file is either vcvars32.bat or vcvars64.bat for x86 and x64 architectures respectfully. I just simply type vcvars64 into my command prompt before compiling the first time. At this point everything should be setup.
 
Below is Hello World Code to Test it.
/*
   
    OpenMPI Hello World Example.
    To run it with 10 processes execute "mpirun -np 10 main.exe" where main.exe is
    the name of your executable.
   
    The output of the program should then be:
    Hello World! My rank is 0
    Hello World! My rank is 1
    Hello World! My rank is 2
    Hello World! My rank is 3
    Hello World! My rank is 4
    Hello World! My rank is 5
    Hello World! My rank is 6
    Hello World! My rank is 7
    Hello World! My rank is 8
    Hello World! My rank is 9
*/

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

using namespace std;

int main(int argc, char** argv)
{
    int baton = 1;
    int max;
    int myRank;
    MPI_Status status;
   
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
    MPI_Comm_size(MPI_COMM_WORLD, &max);
    if(myRank == 0)
    {
        cout << "Hello World! My rank is 0" << endl;
        MPI_Send(&baton, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
    }
    else
    {
        MPI_Recv(&baton, 1, MPI_INT, myRank-1, 1, MPI_COMM_WORLD, &status);
        cout << "Hello World! My rank is " << myRank << endl;
        if(myRank < max-1)
            MPI_Send(&baton, 1, MPI_INT, myRank+1, 1, MPI_COMM_WORLD);
    }

    MPI_Finalize();
}
 

3 comments :

  1. hi, i get unresolved external symbols for the functions of mpi, how should i fix it?

    ReplyDelete
    Replies
    1. Mail me the program and screenshot of error. I will look into it and get back to you.
      Mail id: suhelsayyad2006@gmail.com

      Delete