How to compile and run C/C++ MPI codes in cmd on windows without Visual Studio

13,668

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.I recommend that i 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

Share:
13,668
SHAH MD IMRAN HOSSAIN
Author by

SHAH MD IMRAN HOSSAIN

Mobile Application Developer iOS Application Developer(Swift) Android Application Developer(Java, Kotlin) I love to code I have professional experience in iOS Application Development with Swift I also have personal experience in Android Application Development with Java and Kotlin I know the languages C, C++, C#, Java, Kotlin, Swift Also I know oracle SQL I have some minor experience in Game Development with C# Unity3D

Updated on June 04, 2022

Comments

  • SHAH MD IMRAN HOSSAIN
    SHAH MD IMRAN HOSSAIN almost 2 years

    I install MS-MPI

    I like to code in sublime text specially C/C++

    I run code in cmd using gcc/g++

    How can I compile and run MPI codes from cmd

    I don't like visual studio

    So, is there any way i can compile and run my C/C++ MPI codes with MS-MPI in cmd

    I know there is a question in title : Compile C++ MPI Code on Windows

    but there's no clear answer or comment. that's why i am re-asking now