MPI Gather Function in Python

MPI Gather Function in Python

The gather function is used to gather data from multiple processes into a single process. We’ll go through the provided code, line by line, and understand how the gather function works. The detailed tutorial of MPI with a python can be visited here.

Code

from mpi4py import MPI

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
data = (rank + 1)
data = comm.gather(data, root=0)
print(f"Process {rank}: Calculated data = {data}")

if rank == 0:
    for i in range(size):
        assert data[i] == (i + 1)
        print(f"Process 0: Checked data received from process {i + 1}")
else:
    assert data is None

Explanation

from mpi4py import MPI

This line imports the MPI functionality from the mpi4py library.

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

These lines initialize the MPI communicator (comm) and obtain the total number of processes (size) and the rank of the current process (rank).

data = (rank + 1)
data = comm.gather(data, root=0)

Each process calculates its own data value based on its rank (rank + 1). Then, the gather function is called on the communicator comm. This function gathers data from all processes and returns it to the root process (rank 0) as a list. In this case, the data from each process is gathered at process 0 (root=0).

print(f"Process {rank}: Calculated data = {data}")

Each process prints its own calculated data value. Since data is different for each process, the output will vary accordingly.

if rank == 0:
    for i in range(size):
        assert data[i] == (i + 1)
        print(f"Process 0: Checked data received from process {i + 1}")

In the root process (rank 0), a loop iterates over all processes (size). It checks whether the received data from each process matches the expected value ((i + 1)). If the data matches, it prints a confirmation message.

else:
    assert data is None

In non-root processes (ranks other than 0), it’s ensured that the data variable is None since they do not receive any data through the gather operation.

In this tutorial, we’ve learned how to use the gather function in MPI to collect data from multiple processes into a single process. This is a fundamental operation in parallel programming, often used to gather results from worker processes to a master process for further processing or analysis. Understanding MPI collective operations gather is essential for developing efficient parallel programs.

Material

Download the programs (code), covering the MPI4Py.

38 thoughts on “MPI Gather Function in Python

  1. Do you have a spam problem on this website; I also am a blogger, and I was wanting to know your situation; we have developed some nice practices and we are looking to exchange solutions with others, be sure to shoot me an email if interested.

  2. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

  3. canada drug pharmacy [url=https://canadapharmast.com/#]canadian pharmacy[/url] canadian online pharmacy

  4. mexico drug stores pharmacies [url=https://foruspharma.com/#]mexican border pharmacies shipping to usa[/url] mexican pharmacy

  5. canadian pharmacy antibiotics [url=https://canadapharmast.com/#]canadian pharmacy prices[/url] onlinecanadianpharmacy

  6. Somebody essentially help to make seriously articles I would state. This is the first time I frequented your web page and thus far? I amazed with the research you made to make this particular publish extraordinary. Great job!

  7. Thank you for every other informative web site. Where else could I am getting that kind of information written in such an ideal manner? I have a mission that I’m just now working on, and I’ve been at the look out for such information.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this:
Verified by MonsterInsights