Site icon Afzal Badshah, PhD

Blocking and Non-blocking Communication in MPI

In parallel computing with MPI (Message Passing Interface), communication between processes plays a crucial role in achieving efficient parallelization of algorithms. Two common approaches to communication are blocking and non-blocking communication. You can visit the detailed tutorial on MPI with Python here.

Blocking Communication

Blocking communication involves processes halting their execution until the communication operation is complete. In MPI, blocking communication functions like comm.send() and comm.recv() ensure that the sender waits until the receiver receives the message, and vice versa. Blocking communication is often used when processes need to synchronize their execution or when the sender and receiver must coordinate closely. While blocking communication simplifies program logic and synchronization, it can lead to potential performance bottlenecks if processes spend significant time waiting for communication to complete. Let’s see the below code;

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, tag=11)
elif rank == 1:
    data = comm.recv(source=0, tag=11)

Explanation

Non-blocking Communication

Non-blocking communication, on the other hand, allows processes to continue their execution immediately after initiating communication operations, without waiting for the operations to complete. In MPI, non-blocking communication functions like comm.isend() and comm.irecv() return a request object immediately, enabling processes to overlap computation with communication. Non-blocking communication is particularly useful in scenarios where processes can perform useful work while waiting for communication to progress. By overlapping computation with communication, non-blocking communication can improve overall performance and scalability in parallel applications. Let’s see the below code;

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
    data = {'a': 7, 'b': 3.14}
    req = comm.isend(data, dest=1, tag=11)
    req.wait()
elif rank == 1:
    req = comm.irecv(source=0, tag=11)
    data = req.wait()

Explanation

Material

Download the programs (code), covering the MPI4Py.

Exit mobile version