Build OpenMPI for Fortran
OpenMPI is often available from package managers across computing platforms. Users might build OpenMPI from source to get the latest version or to support other compilers. OpenMPI takes several minutes to build.
Download latest OpenMPI source. Configure OpenMPI with Fortran compiler:
./configure --prefix=~/.local/openmpi CC=gcc CXX=g++ FC=gfortran
Don’t just use ~/.local
as that spills OpenMPI files into common directories, making it hard to use multiple OpenMPI versions or multiple compilers.
Build and install OpenMPI:
make -j
make install # no sudo
Linux: add to “~/.profile”:
export LD_LIBRARY_PATH=$HOME/.local/openmpi/lib:$LD_LIBRARY_PATH
The
FindMPI
CMake module allows switching between multiple OpenMPI versions installed with MPI_ROOT
variable:
cmake -DMPI_ROOT=~/.local/openmpi ..
As in general for CMake packages, the MPI imported target is highly recommended over the legacy discrete variables, as in this minimal CMakeLists.txt
:
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 ~/.local/openmpi/bin/mpif90 myprog.f90
.
If that works, try something like:
FC=gfortran CC=gcc cmake -DMPI_Fortran_COMPILER=~/.local/openmpi/bin/mpif90 ..