MPI for Python
MPI for Python
MPI for Python (mpi4py) is an implementation of language bindings for Python of the MPI parallel computing framework. The package allows access to MPI function calls and allows the user to leverage multiple processors to run their code natively in Python without having to use other bindings for MPI such as C or Fortran.
Using MPI for Python on RCC Resources
MPI for Python is a native Python library which allows access to the MPI framework for parallel programming. Before using this library, the GNU OpenMPI module must be loaded. Then you can run your Python program using the srun
command.
Below is an example of running a program with 4 processors. Replace TEST
with the name of your Python program.
$ module load gnu openmpi
$ srun -n 4 python TEST.py
Running MPI for Python in Parallel
MPI for Python programs can also be run on HPC by submitting a script to the Slurm scheduler. Your script file must use the .sh extension. Below is an example script that again uses 4 processors. Replace TEST
with the name of your Python program.
#!/bin/bash
#SBATCH -J MPI4PY_Test
#SBATCH -p genacc_q
#SBATCH -n 4
#SBATCH -t 00:10:00
#SBATCH --mail-type=ALL
module load gnu openmpi
srun python TEST.py
Then submit your script using the following command, replacing YOURSCRIPT
with the name of your script file:
$ sbatch YOURSCRIPT.sh
For more example programs, please refer to the MPI for Python users' manual, as it has both documentation and a good set of tutorials. You can also refer to the official website.