1

I installed MS-MPI. I can run Python MPI codes in my Windows-CMD. I like to code in sublime text, especially C/C++. I run my code in the CMD using gcc/g++.

How can I compile and run MPI-codes from the command line?

I don't like using Visual Studio.

So, is there any way I can compile and run my C/C++-MPI codes with MS-MPI in the command line?

I know there are two questions in the title :

Compile C++ MPI Code on Windows.

But there's no clear answer or comment. That's why I am re-asking now.

I couldn't find a single tutorial that was for the Windows command line, mostly it was with Visual Studio.

1 Answers1

0

Assuming that you have g++ and the MS-MPI routines in your path then you simply need to set the location of include files and MPI library files when you run g++ to compile your program.

The easiest way to do this is with a batch file. Copy the following to the directory where you are going to compile your code (or anywhere else in your path) and call it mp.bat

set OPT1="C:\Program Files (x86)\Microsoft SDKs\MPI\Include"
set OPT2="C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpi.lib"
g++ -I%OPT1% -o %1.exe %1.cpp %OPT2%

If your program is called sendAndRecv.cpp then you can use your batch file to compile and link it to create sendAndRecv.exe by just

mp sendAndRecv

Then you run it (on, say, 2 processors) with, e.g.

mpiexec -n 2 sendAndRecv.exe

Use batch files to make life easy when you aren't using an IDE.

lastchance
  • 1,436
  • 1
  • 3
  • 12
  • error 'mp' is not recognized as an internal or external command, operable program or batch file. – reza taghavi May 27 '23 at 21:03
  • @reza taghavi, please read my post carefully. YOU have to create file mp.bat (e.g. with an editor like notepad or whatever your favourite editor is). Cut and paste those three lines that I have given you into it and save it. – lastchance May 28 '23 at 05:19
  • thanks a lot Do you think the same works on C codes? – reza taghavi May 28 '23 at 12:10
  • Probably. Just change g++ to gcc and the file type from .cpp to .c (It would be worth calling the batch file something different, so that you can use either file type.) – lastchance May 28 '23 at 13:48
  • I just changed cpp to c and it was ok – reza taghavi May 29 '23 at 12:33
  • I suggest to change the last line to `if /I "%~x1" == ".cpp" (g++.exe -I%OPT1% -o "%~dpn1.exe" %1 %OPT2%) else (gcc.exe -I%OPT1% -o "%~dpn1.exe" %1 %OPT2%)` and call the batch file with source file name with the file extension `.cpp` or `.c` without or with path. It would be good to read also [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It is usually better to use `set "variable=value"` instead of `set variable="value"` although in this case the latter can be used too. – Mofi May 29 '23 at 15:12
  • Thanks @Mofi. Sounds good. I also might note that the latest installers for Microsoft MPI set up some environment variables (MSMPI_INC and MSMPI_LIB64) which contain paths to the directories with mpi.h and the 64-bit MPI libraries. However, I sometimes "tidy up" system environment variables and end up losing them, so I prefer to put the relevant paths in my compilation batch file. – lastchance May 29 '23 at 19:39
  • In this case I would use in the batch file `if not defined MSMPI_INC set "MSMPI_INC=C:\Program Files (x86)\Microsoft SDKs\MPI\Include"` and `if not defined MSMPI_LIB64 set "MSMPI_LIB64=C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64"` and `if not defined MSMPI_BIN set "MSMPI_BIN=C:\Program Files (x86)\Microsoft SDKs\MPI\bin"` (hope that is right) and finally `if /I "%~x1" == ".cpp" ("%MSMPI_BIN%\g++.exe" "-I%MSMPI_INC%" -o "%~dpn1.exe" %1 "%MSMPI_LIB64%\msmpi.lib") else ("%MSMPI_BIN%\gcc.exe" "-I%MSMPI_INC%" -o "%~dpn1.exe" %1 "%MSMPI_LIB64%\msmpi.lib")`. – Mofi May 30 '23 at 05:40
  • I want to do the same thing on Mac but I can't use more than 2 processors can you help – reza taghavi May 30 '23 at 20:06
  • I’m afraid that I don’t use a Mac, @reza taghavi, so I don't know what MPI systems are available for it. I'm pretty sure that MS-MPI wouldn't be. You could try MPICH (https://www.mpich.org/downloads/). As far as number of processors is concerned, the compilation process is oblivious to the number of processors. This number is set by the mpiexec command at runtime. E.g ```mpiexec -n 2``` would use 2 processors, but there is nothing stopping you using more (except your computer and, possibly, operating system). – lastchance May 31 '23 at 06:56