Site icon Afzal Badshah, PhD

Running MPI4py on Jupyter Notebook – Step-by-Step Guide

Running MPI4py on Jupyter Notebook enables parallel computing within an interactive and user-friendly environment. This guide provides a step-by-step approach to setting up and executing MPI (Message Passing Interface) Python programs using MPI4py library in a Jupyter Notebook. Whether you’re operating on Windows or Ubuntu, this tutorial caters to users on both platforms. From installing the necessary components to executing MPI programs with multiple processors, each step is meticulously outlined to facilitate seamless integration and efficient utilization of MPI4py within Jupyter Notebook. Access the detailed tutorial here.

Installing MPI on Windows

Video tutorial to run mpi4py on windows

1. Installing Microsoft MPI

2. Installing Jupyter

  pip install jupyter

3. Configuring MPI with Jupyter

4. Installing MPI4py

  pip install mpi4py

5. Importing MPI4py

  from mpi4py import MPI

6. Running MPI Program in Jupyter Notebook

  
 from mpi4py import MPI

  comm = MPI.COMM_WORLD
  rank = comm.Get_rank()

  if rank == 0:
      data = {'a': 7, 'b': 3.14}
      comm.send(data, dest=1)
  elif rank == 1:
      data = comm.recv(source=0)
      print("Received data on rank 1:", data)

7. Running MPI Program from Command Line

  mpiexec -n 2 python your_mpi_program.py
Or
mpirun -n 2 python_complete_path your_mpi_program.py

Replace your_mpi_program.py with the name of your MPI Python program file.

Installing MPI on Windows

1. Installing MPI Implementation

  sudo apt-get update
  sudo apt-get install openmpi-bin libopenmpi-dev

2. Installing Jupyter

  sudo apt-get install python3 python3-pip

Then, install Jupyter using pip:

  sudo pip3 install jupyter

3. Installing MPI4py

  sudo pip3 install mpi4py

4. Importing MPI4py

  from mpi4py import MPI

5. Running MPI Program in Jupyter Notebook

  %%px
  from mpi4py import MPI

  comm = MPI.COMM_WORLD
  rank = comm.Get_rank()

  if rank == 0:
      data = {'a': 7, 'b': 3.14}
      comm.send(data, dest=1)
  elif rank == 1:
      data = comm.recv(source=0)
      print("Received data on rank 1:", data)

6. Running MPI Program from Command Line

  mpiexec -n 2 python3 your_mpi_program.py

Replace your_mpi_program.py with the name of your MPI Python program file.

Material

Download the programs (code), covering the MPI4Py.

Exit mobile version