This is a Python example based on one from this MPI with Python presentation. This example creates a number of processes that communicate over MPI and pass a simple message around the processes in a ring. See the presentation for more details.
To run it you'll first need to install Python and the mpi4py library.
For most Python users we recommend installing Conda and using that to manage your environment. To install Conda:
wget https://github.com/conda-forge/miniforge/releases/download/24.7.1-0/Miniforge-pypy3-24.7.1-0-Linux-x86_64.sh
bash Miniforge-pypy3-24.7.1-0-Linux-x86_64.sh -b -p $HOME/miniforge
eval "$(${HOME}/miniforge/bin/conda shell.bash hook)"
conda initIn order to make Conda available automatically when you log into the cluster
you will also need to add the following to your ~/.bash_profile
if [ -e ${HOME}/.bashrc ]
then
source ${HOME}/.bashrc
fiHere is some information on the difference between bashrc and bash_profile
After making these changes log out and log back in.
You can now use the conda command to install additional packages you'll need.
To install mpi4py:
conda install mpi4pyIt's worth reading through the Conda users guide. Some useful commands are
conda listlists all installed packagesconda searchfinds available packages that match the provided name, for exampleconda search torchwill find all avaialable versions oftorch,pytorchetcconda updateupdates packages
After installing you can submit the program to the cluster with
sbatch ring.shYou can then check the status of your job with
squeue -u netid(replace "netid" with your SU netID). The job should move from the PD
(pending) state to the R (run) state and then complete, although this may
happen too fast to notice. If squeue reports that you have no jobs then it
has completed. Check the output with
cat output/ring.out