Skip to content

MPI for Python (mpi4py)

MPI for Python is an implementation of language bindings for Python of the MPI parallel computing framework.


MPI for Python (mpi4py) requires an environment module

In order to use MPI for Python (mpi4py), you must first load the appropriate environment module:

module load gnu openmpi

This 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.

Running MPI for Python on the HPC#

MPI for Python is a native Python library that allows access to the MPI framework for parallel processing. Once you have loaded the requisite environment modules (module load gnu openmpi), you can then run your Python script using the srun command:

$ module load gnu openmpi
$ srun -n 4 python MY_SCRIPT.py

Submitting a mpi4py Job to Slurm#

Below is an example script that uses four processes:

1
2
3
4
5
6
7
8
9
#!/bin/bash

#SBATCH --job-name=MY_MPI4PY_JOB
#SBATCH -n 4
#SBATCH -t 01:00:00
# ..additional Slurm Params here..

module load gnu openmpi
srun python MY_SCRIPT.py

For example Python scripts that you can use for testing, refer to the official tutorial.