# How-To: Compile, Link, Run and Debug FORTRAN MPI Programs Using Microsoft Visual Studio

Categories: Software Use

Test-system(s) used: VMware virtual machine of WinXP x64 Pro, MS Visual Studio 2008 with Intel Visual Fortran 11.0.047 running mpich2

### Installation

For the 64 bit test system, care has to be taken regarding compatibility of components one wishes to use together. Given only a 32 bit Visual Studio (VS) 2008 is available for free to Univ of Manchester researchers, a 32 bit version of mpich2 is used. We downloaded the 32 bit Windows binary from the mpich2 web site. (Using 64 bit mpich2 gives link time errors.)

Following the mpich2 for MS Windows instructions (see README.winbin.rtf) we add the relevant mpich2 include and library components to a VS project’s build. For VS 2008 this is as follows; for other versions the actual locations vary. We presume you’re aware of how to set parameters for your chosen build/project/configuration.

• add the full path of mpich2 include directory (eg C:Program Files (x86)MPICH2include) to the VS include path (Configuration Properties – Fortran – General – Additional Include Directories)
• add the full path of mpich2 library directory (eg C:Program Files (x86)MPICH2lib) to the VS library path (Configuration Properties – Linker – General – Additional Library Directories)
• add the name of the relevant mpich2 library file to the VS link command using the Additional Dependencies (Configuration Properties – Linker – Input – Additional Dependencies). The mpich2 library file to use for the Intel Fortran compiler is fmpich2.lib

You should now be able to compile and link, or to build, a Fortran MPI program.

### Launching MPI from Visual Studio

Rather than running the newly created executable, known to VS as $(TargetPath), we wish to launch mpiexec.exe and pass that the required mpich2 parameters and the executable. This can be achieved by • setting the launch command (Configuration Properties – Debugging – Command) to the full path for mpiexec.exe (eg C:Program Files (x86)MPICH2binmpiexec.exe) • setting the arguments (Configuration Properties – Debugging – Command Arguments) to -n 2$(TargetPath)

Then the MPI executable can be launched from VS by Debug – Start Without Debugging (Ctrl+F5). (Debug – Start Debugging (F5) also works but VS complains there’s no debugging information for the mpiexec.exe itself)

NB if you try and use breakpoints etc it appears not to honour these – see below for how to debug.

Troubleshooting

These are just some of the problems you may see. For an exhaustive list please search the Internet.

You can now run your MPI program. If you see only one MPI process then you may not have launched the MPI processes correctly. You can check whether you program behaves as expecting by opening a terminal window (eg Start – Run – cmd) and then manually launching using {fullpath/}mpiexec.exe -n {num} {fullpath/}your.exe

If you get an error regarding inablity to connect it may due to MS Windows (or other) firewall settings. You may also occasionally need to start the mpich2 process manager (smpd.exe in the mpich2 bin directory) manually. As per Windows, a restart may help.

The VS Output window may occasionally show “First-chance exception at 0x7d4e237e in mpiexec.exe: 0x000006C5: The tag is invalid.” but it appears these can be ignored.

### by attaching to a currently running process (one VS window for all selected MPI processes)

This is a generic method and gives you one VS debugging window but you can move between MPI processes

• compile, link and start running your MPI program (you may wish to put a read statement early on to hold the program while you do the next steps)
• attach to one of the currently running MPI processes: Debug – Attach to Process brings up a dialogue box which lists Available Processes. You should see NUM instances (where N is from mpiexec -n NUM) of your executable. Select all of these and click on Attach. You can now debug by adding breakpoints etc. To move between MPI processes use the Process drop-down menu just above the code listing.

### launching VS from mpiexec (one VS window per MPI process)

Firstly, having compiled and linked (or built) your executable (project), close all VS application windows. From a terminal window

• stop any running mpich2 process manager: {mpich2/bin/}smpd.exe -stop
• start smpd in debug mode: {mpich2/bin/}smpd.exe -d O (or just -d if you’re happy to see debugging messages (-d NUM is the level of debugging))

Open another terminal window we can now launch the VS executable on each MPI process with the following command (this is one command and shouldn’t be split over lines but your browser may reformat):

"c:Program Files (x86)MPICH2binmpiexec.exe" -n 2 "c:Program Files (x86)Microsoft Visual Studio 9.0Common7IDEdevenv.exe"  "c:Program Files (x86)MPICH2examplesDebugfpi.exe"

where you should use the path to your mpiexec.exe and to your VS executable (devenv.exe} and to the required executable (or Fortran file or project) This will give you two (or NUM if you use -n NUM) VS applications, one for each thread. You can then debug as normal – you will have to manually start each debugging thread and you may have to “Break All” to bring up the source code for adding break and watch points.