visual-studio-codefortranincludempiinclude-path

Using MS Visual Studio Code, how do I access an MPI library in a Fortran code?


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.


Solution

  • 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.