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 the MPI module with Gfortran from PowerShell or Windows Terminal. This also creates “mpi.mod” for Fortran use.

    cd C:/msys64/mingw64/
    
    gfortran include/mpi.f90 -c -fno-range-check
    
    ar cr lib/libmpi.a mpi.o
    

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/mkl/include

Copy-Item -Path "C:/Program Files (x86)/Microsoft SDKs/MPI/Include" -Destination "c:/lib/mpi/include" -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

ar cr lib/libmpi.a mpi.o