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.

205 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.

  8. I’ve been surfing on-line more than three hours lately, but I by no means found any attention-grabbing article like yours. It’s beautiful value enough for me. Personally, if all webmasters and bloggers made excellent content material as you probably did, the net shall be a lot more useful than ever before. “Revolution is not a onetime event.” by Audre Lorde.

  9. hi!,I love your writing very much! percentage we communicate more about your article on AOL? I need an expert on this house to resolve my problem. May be that’s you! Looking forward to look you.

  10. mexico drug stores pharmacies [url=https://mexicandeliverypharma.online/#]mexican mail order pharmacies[/url] buying prescription drugs in mexico online

  11. buying prescription drugs in mexico [url=http://mexicandeliverypharma.com/#]reputable mexican pharmacies online[/url] п»їbest mexican online pharmacies

  12. mexican drugstore online [url=https://mexicandeliverypharma.online/#]mexican mail order pharmacies[/url] mexico drug stores pharmacies

  13. mexican mail order pharmacies [url=http://mexicandeliverypharma.com/#]pharmacies in mexico that ship to usa[/url] medication from mexico pharmacy

  14. I do trust all the ideas you have introduced on your post. They’re really convincing and will certainly work. Nonetheless, the posts are very quick for novices. May just you please prolong them a little from next time? Thank you for the post.

  15. mexico drug stores pharmacies [url=http://mexicandeliverypharma.com/#]mexico drug stores pharmacies[/url] п»їbest mexican online pharmacies

  16. mexican online pharmacies prescription drugs [url=https://mexicandeliverypharma.online/#]buying from online mexican pharmacy[/url] mexican pharmaceuticals online

  17. mexico pharmacies prescription drugs [url=http://mexicandeliverypharma.com/#]mexico pharmacies prescription drugs[/url] mexican pharmaceuticals online

  18. mexican border pharmacies shipping to usa [url=https://mexicandeliverypharma.online/#]mexico drug stores pharmacies[/url] п»їbest mexican online pharmacies

  19. mexican drugstore online [url=https://mexicandeliverypharma.com/#]mexican online pharmacies prescription drugs[/url] mexican border pharmacies shipping to usa

  20. mexican pharmacy [url=https://mexicandeliverypharma.com/#]mexico drug stores pharmacies[/url] mexico pharmacy

  21. I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic info I was looking for this info for my mission.

  22. Almost all of what you point out is astonishingly legitimate and it makes me ponder the reason why I hadn’t looked at this in this light previously. This piece really did turn the light on for me personally as far as this topic goes. Nevertheless there is just one position I am not too cozy with and whilst I make an effort to reconcile that with the main theme of the position, allow me observe what the rest of the subscribers have to point out.Nicely done.

  23. I have been exploring for a little for any high quality articles or weblog posts on this kind of space . Exploring in Yahoo I ultimately stumbled upon this site. Studying this information So i’m satisfied to convey that I have a very excellent uncanny feeling I came upon just what I needed. I most undoubtedly will make sure to don’t omit this site and give it a look regularly.

  24. I’ve been surfing on-line more than three hours today, but I by no means discovered any attention-grabbing article like yours. It is beautiful value sufficient for me. In my opinion, if all site owners and bloggers made good content as you did, the web will be much more helpful than ever before.

  25. tamoxifen alternatives [url=https://tamoxifen.bid/#]buy tamoxifen online[/url] tamoxifen dosage

  26. alternatives to tamoxifen [url=https://tamoxifen.bid/#]buy tamoxifen citrate[/url] tamoxifen hair loss

  27. lipitor generic price canada [url=http://lipitor.guru/#]generic lipitor 10 mg[/url] generic for lipitor

  28. generic cost of lipitor [url=https://lipitor.guru/#]Lipitor 10 mg price[/url] prescription medication lipitor

  29. казино вавада [url=http://vavada.auction/#]вавада зеркало[/url] вавада

Leave a Reply

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

%d bloggers like this:
Verified by MonsterInsights