1

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 Files\Microsoft MPI\Benchmarks\
MSMPI_BIN=C:\Program Files\Microsoft MPI\Bin\
MSMPI_INC=C:\Program Files (x86)\Microsoft SDKs\MPI\Include\
MSMPI_LIB32=C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x86\
MSMPI_LIB64=C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\

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.

6
  • Please tag fortran for all Fortran questions. See the tag descriptions of tags that you select. The code sample is at least Fortran 90 anyway. If you do use MPI, do not also use include 'mpif.h'. One use one of these, not both. Commented Mar 13, 2023 at 23:42
  • ... and what @VladimirFГероямслава might have added ... use use MPI rather than include 'mpif.h'. Using the module rather than the include file traps more mistakes at compile time. Commented Mar 14, 2023 at 6:00
  • 1
    Oh, and you might find you can use mpi_f08, though I have my doubts that MS are that up to date. Commented Mar 14, 2023 at 15:35
  • @HighPerformanceMark: Thank you. I have tried only using 'use MPI' and I have tried only using 'use mpi_f08.' Neither of these make a difference. I continue to get the same error message: "Fatal Error: Can't open module file 'mpi.mod' for reading at (1): No such file or directory." And I am not sure that it makes much of a difference - since the issue is more or less with VS Code - but the format is Fortran-77 (not Fortran-90), i.e., the file is a .f file, not a .f90 file. Commented Mar 15, 2023 at 1:53
  • Visual Studio Code is an editor. Which Fortran compiler are you using (gfortran/ifort etc)? Commented Mar 15, 2023 at 20:02

2 Answers 2

1

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.

set OPTC=-I"C:\Program Files (x86)\Microsoft SDKs\MPI\Include\gfortran" -ff2c -fallow-argument-mismatch
set OPTL="C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpi.lib" "C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpifec.lib"
call gfortran %OPTC% -o %1.exe %1.f90 %OPTL%

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):

m.bat test

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:

mpiexec -n 2 test.exe

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.

Sign up to request clarification or add additional context in comments.

16 Comments

@AD203, you won’t see a .exe file, nor should you expect to: you are merely trying to compile the module file mpi.f90 to mpi.mod. Can you try doing this in the standard windows cmd shell, rather than powershell. If it “took it” then it should create that mpi.mod file in the subdirectory where you were trying to compile it. If you simply run gfortran -c mpi.f90 in a windows cmd window what happens?
Also, I found it necessary to run cmd.exe with Administrator privileges to write anything into that folder. (Right-click any cmd.exe icon and choose "Run as Administrator", at least in Windows 10.) Using cmd.exe navigate to the relevant folder and try gfortran -c mpi.f90. Please report exactly what warnings and error messages it gives.
You are unlikely to use an MPI type of MPI_FLOAT_INT or similar parameters, so no worries here. The need for -fno-range-check is because usage in a parameter statement is not standard Fortran, although it is widely used as an extension. (Your compiler is also old: I'd guess gfortran 9.4; you will need a different - as my link - if you upgrade to gfortran 12.2). Go ahead and compile your main code. IMPORTANT: ensure that your INCLUDE path contains wherever you decide to put or leave MPI.mod. You may need further compiler options then (depending on compiler version). Remove include mpif.h
When you say "successfully compile the code" have you actually managed to LINK it as well? That is, actually create a .exe file. It sounds like you haven't. You need to link against the MPI libraries, which for MS-MPI are (with full paths if you need them) "C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpi.lib" "C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpifec.lib". Do NOT add anything to the headers: you simply haven't linked against the relevant libraries.
@AD203, I have edited my post in your thread to include, at the end, a batch file that will compile and link your program. I have indicated where you may need to make amendments, depending on where you choose to put mpi.mod. You run this batch file ... from the command line ... in whichever directory your MPI-calling fortran program happens to be (anywhere you want it on your computer). The batch file is minimalist in terms of compiler options (you can add checking or optimising options later) and contains full paths (again, can be simplified later).
|
1

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 and use 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

{
    "fortran.linter.includePaths": [
        // Location of your mpi_f08.mod file
        "/usr/lib/x86_64-linux-gnu/fortran/gfortran-mod-15/openmpi"
    ]
}
Without correct includePaths With includePaths
before after

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

mpifort main.f90 -o main.out
mpiexec -n 2 main.out

Output

 Hello from process            0
 Hello from process            1

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.

2 Comments

Sorry about the lack of information. I'm still fairly new to coding, so I sometimes don't notice how many factors could matter. What I'm doing in VS code: coding and running Monte-Carlo simulations at 500 million iterations per simulation. Version of VS Code (1.77). Compiler: gfortran (6.3.0). MPI library (10.1.12498.18). Extensions used in VS Code: Modern Fortran (3.2.0), Code Runner (0.12.0), C/C++ (1.14.5), fortran-ekon (0.01), Fortran Intellisense (0.6.2). And many apologies, but I'm not familiar with the concept of a linter. What exactly are you referring to?
Install the pre-release of Modern Fortran and uninstall Fortran Intellisense. Not sure if you need fortran-ekon either. A linter is a tool used to analyse your code and detect errors without you having to compile every time. The red squiggles that you see are from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.