Building OpenMPI for Fortran compilers

While GNU Gfortran and Intel Fortran have easy-to-use OpenMPI support pre-built for many platforms, it can be desirable to compile OpenMPI to get the latest version or to support other compilers such as Flang or PGI. Compiling OpenMPI is quite easy and takes only several minutes to compile.

  1. Download latest OpenMPI source
  2. This example command configures OpenMPI for Flang Fortran compiler:
    ./configure --prefix=$HOME/.local/openmpi4.0.0-flang CC=clang CXX=clang++ F77=flang FC=flang
    

    Don’t just use $HOME/.local as that spills OpenMPI files into common directories, making it hard to use multipe OpenMPI versions or multiple compilers.

  3. Build and install OpenMPI:
    make -s -j -l 2
       
    make install   # no sudo
    
  4. add to ~/.bashrc:
    export LD_LIBRARY_PATH=$HOME/.local/lib/openmpi:$LD_LIBRARY_PATH
    

CMake for OpenMPI

CMake ≥ 3.10 is recommended for better OpenMPI support.

The FindMPI CMake module works well, and it’s easy to switch between multiple OpenMPI versions installed with MPI_ROOT variable:

cmake -DMPI_ROOT=$HOME/.local/lib/openmpi-4.0.0 ..

As on general for CMake packages, the MPI imported target is highly recommended over the legacy discrete variables, as in this minimal CMakeLists.txt:

cmake_minimum_required (VERSION 3.7)
project(mpidemo Fortran C)

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12)
  cmake_policy(SET CMP0074 NEW)
endif()

find_package(MPI REQUIRED COMPONENTS Fortran)

add_executable(mpivers mpivers.f90)
target_link_libraries(mpivers MPI::MPI_Fortran)

Troubleshooting

If it doesn’t work with CMake, try $HOME/.local/openmpi4.0.0-flang/bin/mpif90 myprog.f90. If that works, try something like:

FC=flang CC=clang cmake -DMPI_Fortran_COMPILER=$HOME/.local/openmpi4.0.0-flang/bin/mpif90 ..

Examples

See the modern Fortran 2008 OpenMPI examples

Tags: , , ,

Categories:

Comments

Written by Michael Hirsch, Ph.D. //