Fortran MPI with MSYS2 MinGW on Windows

OpenMPI is not currently available for native Windows. While Cygwin and WSL do have working OpenMPI, it is also possible to use MPICH via Microsoft MS-MPI as described in this article.

setup MPI on Windows MSYS2

This procedure gives MPI via MSYS2 GCC / Gfortran compilers on Windows.

  1. Download and install Microsoft MS-MPI

  2. To make mpiexec.exe available, add to user PATH: C:\Program Files\Microsoft MPI\Bin – this is needed even when using MSYS2.

  3. Install MSYS2 MS-MPI library, from the MSYS2 Terminal

    pacman -S mingw-w64-x86_64-msmpi
    
  4. compile, creating “mpi.mod” for Fortran use. Do NOT link with ar or MPI may only run with a single image.

    cd C:/msys64/mingw64/
    
    gfortran include/mpi.f90 -c -fno-range-check -fallow-invalid-boz
    

Notes

If you don’t compile the mpi.f90 as above, errors may result like:

use mpi

Fatal Error: Cannot open module file 'mpi.mod' for reading at (1): No such file or directory

MS-MPI without MSYS2

If not using MSYS2, it’s still possible use MS-MPI. We will put the resulting artifacts under c:/lib/mpi

mkdir c:/lib/mpi/

Copy-Item -Path "C:/Program Files (x86)/Microsoft SDKs/MPI/Include" -Destination "c:/lib/mpi/" -Recurse

cd c:/lib/mpi

you may need to edit “include/mpi.f90” to have INCLUDE 'mpifptr.h'

gfortran include/mpi.f90 -c -fno-range-check -fallow-invalid-boz

Do NOT link with ar or MPI may only run with a single image.

IntelMPI clash with MinGW MS-MPI

Upon installing or updating the Intel Parallel Studio on Windows, you may experience CMake failing to find MPI for MinGW. This happens because Intel compilers put Intel MPI on the system PATH. Generally we remove Intel MPI from the system PATH and use the Intel Compiler shell instead, which provides all the needed directories. This is because we don’t typically use Intel MPI with Visual Studio, and if we did, we’d make a shell script for that.