Hello, and welcome to the first tutorial in my MPI tutorial series! This tutorial will work fine for Visual Studio 2005 Express, Visual Studio 2005 Standard, Visual Studio 2008 Express, and Visual Studio 2008 Standard edition. Much of this tutorial can also be applied to the professional versions of Visual Studio, with some minor differences.

Step 1: Download Visual Studio 2008 Express if you do not already have it

If you don’t already have a version of Visual Studio installed on your computer, you can download a free version called Visual Studio 2008 Express. This is the most powerful developement environment available for Windows. Download Visual Studio 2008 Express here

There are multiple versions of Visual Studio 2008 Express. Because this blog focuses on high performance computing, all tutorials will be using the C++ flavour of Visual Studio.

Step 2: Download HPC Pack 2008 SDK

Microsoft has realized that virually all high performance computing clusters run on Linux. To help combat this, Microsoft realeased the High Performance Computing Pack 2008 SDK. This SDK is necessary if you want to use MPI for this tutorial and other tutorials. Please note that MPI is not platform dependent. You should write your programs so that they run on Windows, Linux, Mac OS, and any other operating system. Usually I write programs in Visual Studio, then I compile the source code with GCC in Linux when I’m finally ready to run the program on a large cluster. You can search on Google, or you can download the HPC Pack 2008 SDK here.

Step 3: Now that you have everything installed, lets go ahead and create our project!

Open up Visual Studio, go to the File menu and select New->Project. A dialog will appear:

start_new_project

After naming your project, click OK.

configure_new_project

Turn off the Precompiled Header option. This will generally make it easier to compile your source code of platforms other than Windows.

Step 4: Get Hello World Up and running

#include "stdafx.h"
#include <iostream>
using namespace std;
//int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char* argv[])
{ 
    cout << "Hello World\n";
    return 0;
}

Go ahead and modify the default source code so it looks like the code above. Compile your program, and make sure that it runs properly. There are several reasons we changed the _tmain to the standard main function declaration. The first reason is for platform compatibility reasons. The second reason is that we will need to use argv as char* later on, not as a TCHAR*.

Step 5: Configure your project to run MPI

Now that you’re program is compiling and working, we’ll need to configure your program to include the MPI libraries and header files. Right-click your project in the Solution Explorer window, and select Properties.

Additional Include Directories

You’ll need to navigate to the HPC Pack 2008 SDK directory, and make sure the include directory is included in your project’s include search path.

Additional Library Directories

Next, you’ll need to navigate to the HPC Pack 2008 SDK directory, and make sure the library directory is included in you’re project’s library search path.

Additional Dependencies

Next, you’ll need to specifically tell Visual Studio that you will be depending on msmpi.lib. Before continuing with this tutorial, it’s a good idea to compile and run your program again to make sure it’s still working.

Step 6: Modify your source code to use MPI

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

using namespace std;

//int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char* argv[])
{
 //cout << "Hello World\n";

 int  nTasks, rank; 

 MPI_Init(&argc,&argv);
 MPI_Comm_size(MPI_COMM_WORLD,&nTasks);
 MPI_Comm_rank(MPI_COMM_WORLD,&rank);

 printf ("Number of threads = %d, My rank = %d\n", nTasks, rank);

 MPI_Finalize();
 return 0;
}

There are a couple things I would like to point out. First, we include the mpi.h file. There are a total of four MPI functions that we call.

MPI_Init: This function should always be called at the start of the program. Simply pass in the address for argc and argv. Make sure that you changed the _tmain function to main as requested earlier, and make sure argv is of type char*. This function basically initializes MPI.

MPI_Comm_size: This function basically gives you the number of tasks in any communication group. For now, we’ll use MPI_COMM_WORLD, which will be a collection of all the different threads in our program. The variable nTasks will be equal to the total number of threads int he program.

MPI_Comm_rank: Each thread has a different ID, also called a rank. For example, if your program has four threads, the threads will have a rank value somewhere between 0 and 3. Again, each thread will have a different rank which distinguishes it from the rest of the threads. In order to find out which rank a thread is, simply call this function.

MPI_Finalize: This function cleans up MPI, and should be called just before the program exits.

Important Information for Windows XP users

If you are using Windows XP, you may brush up against an error like the following dialog when trying to complete step 7 of this tutorial.

XP Smpd Exe Error


The procedure entry point GetProcessIdOfThread could not be located in the dynamic link library KERNEL32.dll.

Don’t panic, this is fixable.You will need to download a Windows Hotfix for this issue. Download Hotfix here.

Please note that because this hotfix has not been fully tested, you will need to enter your email address, and Microsoft will email you a link with where you can download the file. The email may appear in your spam box, as it did for me. The email will contain information on where you can download the Hotfix. The email will also contain a password that you will need before being able to apply the hotfix. Again, the reason for making you jump through these hoops is because the Hotfix hasn’t been fully tested. However, I can tell you that it worked on my system first time, with no problems at all. I didn’t even have to restart my computer or anything. Just make sure you close all instances of your program before applying this Hotfix.

Step 7: Getting your program to run with multiple threads

If you compile and run the program now, you will find that the program only runs with 1 thread, even if you have a dual or quad-core computer. This is a problem! In order to run MPI, you’ll need to run the program through mpiexec.exe.

Run With Command Line

For now, go ahead and click the Start menu, and select Run. Type cmd and hit enter. Go ahead and browse to your project directory. If you’ve never used the command line before, you can change directories with “cd <directory name>”, list the contents of the current directory with “dir”, and go back a directory with “cd ..”.

Once you browse to your project directory, type mpiexec MPI_Tutorial_1.exe. In the picture above, you’ll see that it ran with two threads. That’s because I’m currently running on a computer with two processor cores. In the next command, you see that by specifying with an -n switch, you can force your program to start with any number of threads.

If you encounter an error when trying to run your program with mpiexe, please scroll up and read about the issue with Windows XP, and how to fix the issue by applying a hotfix.

Step 8: Getting your program to run without having to type on the command line

Wouldn’t it be nice if we didn’t have to type on the command line every time we wanted to run our program? Well, we can configure Visual Studio to do this automatically. Right click on the Solution Explorer window, select your project, right click, and select properties.

A note for Visual Studio Professional users: If you have a professional version of Visual Studio 2005 or 2008, you are in luck. by selecting the Debugging Category, you can select the “Debugger to launch” drop down list. If you see MPI Cluster Debugging, or something similar, then you should definitely use that. This offers many advantages over the express and standard versions of Visual Studio. These advantages will be covered in later tutorials. If you are using an express or standard version of Visual Studio, don’t panic. Please follow the text below in order to get your program to run with the build and run command in Visual Studio.

Configure Command Line

Make the command target mpiexec.exe. As command line options, you can specify the number of processors you want, and just as importantly, specity your executable. You may want to include your target path with quotation marks. Go ahead and click Apply, and run your project. Visual Studio should be able to compile and run your program just as it did before!

Wrapping up

This has been an exciting day! You were able to create a project from scratch, and modify it to run MPI. Sure, there’s a lot of prep work involved in getting this to work, but it was worth it. There are many things left to be discussed, including how to debug your program. This issue and more will be looked at in a following tutorial.

You can download the project files for this tutorial here:

MPI_Tutorial_1

NEXT: Learn how to attach the debugger to your MPI application

Back to the MPI tutorial landing page