Difference between revisions of "Building an MPI library"
(Created page with "There are a few freely available MPI libraries around; among the most widely spread are the OpenMPI https://www.open-mpi.org/ and MPICH https://www.mpich.org/ libraries == Ch...") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 47: | Line 47: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | == A simple example to test your MPI installation == | ||
| + | If you want to double check that all is well, you can for instance test with this ultra minimal program: | ||
| + | <syntaxhighlight lang="fortran"> | ||
| + | program simple_mpi | ||
| + | use mpi | ||
| + | |||
| + | implicit none | ||
| + | |||
| + | integer :: myMPIRank, nbMPI | ||
| + | integer :: code, level_mpi_provided | ||
| + | |||
| + | ! Initialisation for MPI | ||
| + | call MPI_INIT_THREAD(MPI_THREAD_FUNNELED,level_mpi_provided,code) | ||
| + | call MPI_COMM_SIZE(MPI_COMM_WORLD,nbMPI,code) | ||
| + | call MPI_COMM_RANK(MPI_COMM_WORLD,myMPIRank,code) | ||
| + | |||
| + | write(*,*) "Message from MPI process:",myMPIRank," of ", nbMPI | ||
| + | |||
| + | ! finalize MPI region | ||
| + | call MPI_FINALIZE(code) | ||
| + | |||
| + | end program simple_mpi | ||
| + | </syntaxhighlight> | ||
| + | which you can compile, assuming the source code above is in file '''simple_mpi.f90''', with: | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | mpif90 simple_mpi.f90 -l mpi -o simple_mpi | ||
| + | </syntaxhighlight> | ||
| + | And then run on a few processors, e.g. 4, with: | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | mpirun -np 4 simple_mpi | ||
| + | </syntaxhighlight> | ||
| + | which will return something like (order not guaranteed as all processes are simultaneously sending their message to the screen): | ||
| + | <pre> | ||
| + | Message from MPI process: 1 of 4 | ||
| + | Message from MPI process: 2 of 4 | ||
| + | Message from MPI process: 3 of 4 | ||
| + | Message from MPI process: 0 of 4 | ||
| + | </pre> | ||
== A few things worth knowing about == | == A few things worth knowing about == | ||
| Line 55: | Line 93: | ||
* in some cases (e.g. with ifort) the Fortran wrapper is called '''mpifort''' or '''mpiifort''' (e.g. with ifort, sometimes), not '''mpif90''' | * in some cases (e.g. with ifort) the Fortran wrapper is called '''mpifort''' or '''mpiifort''' (e.g. with ifort, sometimes), not '''mpif90''' | ||
* It is vital to use the '''mpirun''' command from the same package than the '''mpif90''' compilation wrapper used to compile the code. Otherwise one can get some really weird results... | * It is vital to use the '''mpirun''' command from the same package than the '''mpif90''' compilation wrapper used to compile the code. Otherwise one can get some really weird results... | ||
| + | * Likewise, if your MPI library is installed in a non-standard location you'll need to specify the path to it in your arch.fcm file (see [[The Target Architecture ("arch") Files]]) for the PCM,; e.g. if your MPI distribution is installed in ''/path/to/your/mpi/library'': | ||
| + | <pre> | ||
| + | %MPI_FFLAGS -I/path/to/your/mpi/library/include | ||
| + | ... | ||
| + | %MPI_LD -L/path/to/your/mpi/library/lib -lmpi | ||
| + | </pre> | ||
| − | 24/ | + | 24/09/2026 |
[[Category:FAQ]] | [[Category:FAQ]] | ||
Latest revision as of 13:00, 30 September 2026
There are a few freely available MPI libraries around; among the most widely spread are the OpenMPI https://www.open-mpi.org/ and MPICH https://www.mpich.org/ libraries
Contents
Checking if you have an Fortran enabled MPI library already installed and at hand
This can easily be done by checking if the MPI wrapper mpif90 is available, i.e. that
which mpif90
returns something meaningful
using a package manager
If you have sudo privileges and are on an Ubuntu or Linux Mint machine you should be able to install the OpenMPI library with
sudo apt install libopenmpi-dev openmpi-bin
Example of personal installation of the the OpenMPI library
To adapt to fit your own needs and system specificities, obviously. In the example below the installation is done in the $HOME/myopenmpi directory (for convenience this path is stored in the PATH_OPENMPI variable.
PATH_OPENMPI=$HOME/myopenmpi/
mkdir -p $PATH_OPENMPI
cd $PATH_OPENMPI
wget https://www.open-mpi.org/software/ompi/v1.10/downloads/openmpi-1.10.3.tar.gz
gunzip openmpi-1.10.3.tar.gz
tar xvf openmpi-1.10.3.tar
cd openmpi-1.10.3
./configure --prefix $PATH_OPENMPI
make all install
To check that the install went well check that
$PATH_OPENMPI/bin/mpif90 -V
returns something meaningful like
GNU Fortran (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Once you have a distribution correctly installed you will need to update your environment variables PATH and LD_LIBRARY_PATH to point to the relevant locations (the bin and lib subdirectories of the distribution). In practice this is typically done in your $HOME/.bash_profile or $HOME/.bashrc file, where for instance, if your installed the library in $HOME/myopenmpi you would add:
export PATH=$PATH:$HOME/myopenmpi/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/myopenmpi/lib
A simple example to test your MPI installation
If you want to double check that all is well, you can for instance test with this ultra minimal program:
program simple_mpi
use mpi
implicit none
integer :: myMPIRank, nbMPI
integer :: code, level_mpi_provided
! Initialisation for MPI
call MPI_INIT_THREAD(MPI_THREAD_FUNNELED,level_mpi_provided,code)
call MPI_COMM_SIZE(MPI_COMM_WORLD,nbMPI,code)
call MPI_COMM_RANK(MPI_COMM_WORLD,myMPIRank,code)
write(*,*) "Message from MPI process:",myMPIRank," of ", nbMPI
! finalize MPI region
call MPI_FINALIZE(code)
end program simple_mpi
which you can compile, assuming the source code above is in file simple_mpi.f90, with:
mpif90 simple_mpi.f90 -l mpi -o simple_mpi
And then run on a few processors, e.g. 4, with:
mpirun -np 4 simple_mpi
which will return something like (order not guaranteed as all processes are simultaneously sending their message to the screen):
Message from MPI process: 1 of 4 Message from MPI process: 2 of 4 Message from MPI process: 3 of 4 Message from MPI process: 0 of 4
A few things worth knowing about
- mpif90 is just a wrapper around a given compiler which could be gfortran, ifort, pgfortran, etc. to know more about that run
mpif90 --version
- in some cases (e.g. with ifort) the Fortran wrapper is called mpifort or mpiifort (e.g. with ifort, sometimes), not mpif90
- It is vital to use the mpirun command from the same package than the mpif90 compilation wrapper used to compile the code. Otherwise one can get some really weird results...
- Likewise, if your MPI library is installed in a non-standard location you'll need to specify the path to it in your arch.fcm file (see The Target Architecture ("arch") Files) for the PCM,; e.g. if your MPI distribution is installed in /path/to/your/mpi/library:
%MPI_FFLAGS -I/path/to/your/mpi/library/include ... %MPI_LD -L/path/to/your/mpi/library/lib -lmpi
24/09/2026