I am coding and running a physics simulation in Fortran using MS Visual Studio Code on Windows 11 and I want to use MPI. I have installed Microsoft MPI (MSMPI) and know that it is correctly installed. When I enter ‘set msmpi,’ into the Windows powershell, I see all directories and locations that I want to be seeing:
MSMPI_BENCHMARKS=C:Program FilesMicrosoft MPIBenchmarks
MSMPI_BIN=C:Program FilesMicrosoft MPIBin
MSMPI_INC=C:Program Files (x86)Microsoft SDKsMPIInclude
MSMPI_LIB32=C:Program Files (x86)Microsoft SDKsMPILibx86
MSMPI_LIB64=C:Program Files (x86)Microsoft SDKsMPILibx64
The program itself has the following form (I omit the parts that are irrelevant to the MPI issue):
PROGRAM xyz
USE MPI
...irrelevant...
INCLUDE 'mpif.h'
...irrelevant...
CALL MPI_INIT(ierr)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
CALL MPI_REDUCE(var1, var2, 1, MPI_DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
...irrelevant...
CALL MPI_FINALIZE(ierr)
END PROGRAM xyz
When I attempt to run the code, I receive an error message:
Error: Can’t open include file ‘mpif.h’`
I have watched several Youtube videos on how to get VS Code to access the MPI library, but I have only been able to gather information piece-meal, because some of these information sources are older, i.e., VS Code no longer contains the settings and options as they’re shown in the videos. So far, I have only been able to gather that I must make entries into the settings.JSON file. At the moment, this is what I have:
{
"workbench.colorTheme": "Visual Studio Dark",
"explorer.confirmDelete": false,
"code-runner.runInTerminal": true,
"fortran.preferredCase": "uppercase",
"fortran.linter.compiler": "gfortran",
"editor.renderWhitespace": "none",
"editor.accessibilitySupport": "off",
"editor.tabSize": 6,
"C_Cpp.default.includePath": [
"C :\Program Files\Microsoft MPI\MPI\Include",
"${workspaceFolder}/**",
],
"fortran.linter.includePaths": [
"C:\Program Files (x86)\Microsoft SDKs\MPI\Include",
"${workspaceFolder}/**",
],
"fortran.fortls.preprocessor.directories": [
],
"jake.autoDetect": "on",
"grunt.autoDetect": "on",
"gulp.autoDetect": "on",
"json.schemas": [
]
}
I am not sure what I must do from here out. Is this what my settings.JSON file should look like?
I continue to receive an error message that the header file cannot be found when I run the code. I apologize in advance if I am overlooking something that is obvious to a more experienced programmer. I am younger student who is still new to programming and this is the first time that I’ve ever had to code while manually accessing libraries. Any help at all would be immensely appreciated.
2
Answers
As Ian, Mark and Vladimir have mentioned in the comments there are some inconsistencies in your description of your issue as well as the code you provided, e.g. making use of
include mpif.h
anduse mpi
.In addition, you need to be specific about what you are doing in VS Code, the extensions you are using, the version of the editor, your compiler version and the MPI library version. There are too many undetermined parameters for us to be able to provide specific help.
Nonetheless, I will try and give you some general guidelines to get started in VS Code.
From your settings I can tell that you are using Modern Fortran for VS Code.
Modern Fortran for VS Code will not be able to compile and run your MPI, for a plethora of reasons. What you can do is set it up so that the linter is able to locate you MPI modules/files e.g. for my Linux workstation
includePaths
includePaths
This will get rid of any Errors that show up in VS Code but you still won’t be able to compile and run your program. For that you will still have to compile and run your program from a terminal
Output
As a side note,
I am not sure if MS-MPI will work for you, there are a few notably better alternatives like Intel’s OneApi and MSYS2 which are widely used throughout the Fortran community.
You should be able to use MS-MPI.
However, mpi.mod is not provided itself: you have to create it by compiling the file mpi.f90 in the MS-MPI include directory. You do this with whichever compiler you are using with VS Code (which appears to be gfortran). Note that you will also need to copy the file mpifptr.h from the .x64 subdirectory before trying to compile mpi.f90.
I concur that you are probably going to have to run mpiexec from the command line to actually run your executable.
I did summarise my experience of installing the MS-MPI system for ifort (and, as an aside, gfortran) at Fortran MPI using Cygwin64 (ignore the thread title, I was NOT running on Cygwin). Note that: (a) I was compiling and running from the command line, not a code editor like VS Code; (b) I created separate mpi.mod files for the different compilers: ifort and gfortran; (c) when actually compiling a code using MPI with gfortran some more compiler options need to be set, as the stringent type checking of the modern Fortran standard doesn’t sit entirely happily with the void* pointers implicit in MPI calls.
EDIT: Create the following batch file with, e.g., notepad, and call it m.bat, putting it in the same directory as your program file. NOTE: please read to the end of the whole post before running this batch file. The first line should contain the location of the folder where mpi.mod is; if it is elsewhere, then adapt this line. The second line indicates where your MPI libraries are.
Suppose that your program file is called test.f90. Then you can compile and link it by issuing (from the command line, in the same folder as your test.f90 file):
It should create an executable test.exe, which you can run by issuing the following command (again, from the command line, in the same folder as test.exe), e.g. for 2 processors:
Obviously, you can substitute "test" by whatever filename (minus any extension) you have given your program file.
NOTE: your compiler appears to be somewhat older than mine, and the compiler option -fallow-argument-mismatch may be neither accepted nor relevant. Try with and without it if so. Newer compilers, which adhere more strictly to the type-checking requirements of the latest Fortran standard, will require it for MPI calls. Fortran has no real equivalent of the void* pointer implicit in the MPI definitions.