Mpi4py hello world
From ScientificComputing
< Examples |
This example shows how to use MPI library in a Python script by using the package mpi4py.
Load modules
On the login node, load the new software stack
$ env2lmod
Or, set the new software stack as the default
$ set_software_stack.sh new
Load necessary modules
$ module load gcc/6.3.0 openmpi/4.0.2 python/3.8.5 eth_proxy
If not provided, install the package mpi4py locally
$ pip install --user mpi4py
mpi4py Hello World
Go to $SCRATCH and create a work directory
$ cd $SCRATCH $ mkdir mpi4py $ cd mpi4py $ pwd /cluster/jarunanp/scratch/mpi4py
Create a file called hello_mpi4py.py and add these lines
from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() procname = MPI.Get_processor_name() print("Hello world from processor {}, rank {:d} out of {:d} processors".format(procname, rank, size))
Get an interactive session on compute nodes
Request 4 processors on 4 nodes, one processor on each node
$ bsub -n 4 -R "span[ptile=1]" -Is bash Generic job. Job <187694829> is submitted to queue <normal.4h>. <<Waiting for dispatch ...>> <<Starting on eu-ms-002-19>> FILE: /sys/fs/cgroup/cpuset/lsf/euler/job.187694829.29543.1633967743/tasks [jarunanp@eu-ms-002-19 mpi4py]$
Run the Python script with mpirun
[jarunanp@eu-ms-002-19 mpi4py]$ mpirun -np 4 python hello_mpi4py.py Hello world from processor eu-ms-002-19, rank 0 out of 4 processors Hello world from processor eu-ms-004-25, rank 1 out of 4 processors Hello world from processor eu-ms-005-34, rank 2 out of 4 processors Hello world from processor eu-ms-005-42, rank 3 out of 4 processors [jarunanp@eu-ms-002-19 mpi4py]$
Submit a job from the command line
$ bsub -n 4 -R "span[ptile=1]" "mpirun -n 4 python hello_mpi4py.py"
You should see similar screen output in the LSF output file.
< Examples |