Created
January 9, 2019 19:37
-
-
Save yzchen/2986fa5dc39c3b44cda7ff19695dcfa7 to your computer and use it in GitHub Desktop.
mpi4py usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from mpi4py import MPI | |
| comm = MPI.COMM_WORLD | |
| # each thread will have different rank, we treat 0 as master, others as slave | |
| rank = comm.Get_rank() | |
| # total number of threads | |
| size = comm.Get_size() | |
| if rank == 0: | |
| # for master node | |
| # list to store data from slaves | |
| alist = [0 for i in range(0, size-1)] | |
| asum = 0 | |
| print(rank, ": master is waiting for data from all slaves...") | |
| for i in range(1, size): | |
| req = comm.irecv(source=i, tag=0) | |
| datai = req.wait() | |
| alist[i-1] = datai | |
| print(rank, ": master has received all data, summation all the data...") | |
| print(rank, ": data, ", alist) | |
| asum = sum(alist) | |
| print(rank, ": master will send the sum results to all slaves...") | |
| for i in range(1, size): | |
| req = comm.isend(asum, dest=i, tag=1) | |
| req.wait() | |
| print(rank, ": master has sent all data to slaves!") | |
| else: | |
| # for all slave nodes | |
| print(rank, ": slave will generate a random value and send it to master node...") | |
| mydata = random.randint(0, 100) | |
| req = comm.isend(mydata, dest=0, tag=0) | |
| req.wait() | |
| print(rank, ": slave has sent the private data, waiting for final results...") | |
| req = comm.irecv(source=0, tag=1) | |
| resdata = req.wait() | |
| print(rank, ": slave has received the sum results,", resdata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment