How to compile and run a simple MS-MPI program

  1. Download MS-MPI SDK and Redist installers and install them. The download link can be found at our homepage https://msdn.microsoft.com/en-us/library/bb524831.aspx

  2. After installation you can verify that the MS-MPI environment variables have been set (you will want to use these env vars in Visual Studio)

  3. Open up Visual Studio and create a new Visual C++ Win32 Console Application project. Let’s name it MPIHelloWorld and use default settings.

  4. Setup the include directories so that the compiler can find the MS-MPI header files. Note that we will be building for 64 bits so we will point the include directory to $(MSMPI_INC);$(MSMPI_INC)\x64. If you will be building for 32 bits please use $(MSMPI_INC);$(MSMPI_INC)\x86


  5. Setup the linker lib (notice I add msmpi.lib to the additional dependencies and also add $(MSMPI_LIB64) to the Additional Library Directories). Note that we will be building for 64 bits so we will point the Additional Library Directories to $(MSMPI_LIB64). If you will be building for 32 bits please use $(MSMPI_LIB32)


    If you see these error messages below it is most likely you're building for 32 bits yet specifying 64 bits linking libraries.

    LNK1120: 5 unresolved externals
    LNK2019: unresolved external symbol _MPI_Comm_rank@8 referenced in function _main
    LNK2019: unresolved external symbol _MPI_Finalize@0 referenced in function _main
    LNK2019: unresolved external symbol _MPI_Init@8 referenced in function _main
    LNK2019: unresolved external symbol _MPI_Recv@28 referenced in function _main
    LNK2019: unresolved external symbol _MPI_Send@24 referenced in function _main

  6. Code and build a simple Hello World program

  7. Test run the program on the command line

  8. We recommend that our users use HPC Pack to run MPI across machines. However, you can still run jobs across different machines without HPC Pack, wherein you would need to install MS-MPI on all the machines and start SMPD daemon on each machine using the command smpd –d. Make sure you add the necessary firewall rules for your application. To launch the MPIHelloWorld.exe application with 2 processes, 1 on hostA and 1 on hostB, you can use the following command
    mpiexec -hosts 2 hostA 1 hostB 1 -wdir \\hostA\c$\SomeDirectory MPIHelloWorld.exe

Alternatively, you can use the command line to compile and link your program (replacing steps 1-6 above). Note that I have added “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64” to my path environment variable so that cl.exe and link.exe are available.

To compile your program into .obj files:
cl /I"C:\Program Files (x86)\Microsoft SDKs\MPI\Include" /I"C:\Program Files (x86)\Microsoft SDKs\MPI\Include\x64" /I. /I"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include" /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" /c MPIHelloWorld.cpp 
 
Linking the .obj files:
link /machine:x64 /out:MpiHelloWorld.exe /dynamicbase "msmpi.lib" /libpath:"C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\amd64" /LIBPATH:"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64" MPIHelloWorld.obj


Please feel free to contact the MS-MPI team should you have any questions, or have suggestions for things you would like to see on this blog.  You can reach us at askmpi@microsoft.com