-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgather.py
36 lines (27 loc) · 1020 Bytes
/
gather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
This example creates data contained in a list.
The length of the list is equal to the number of cores mpi4py is using.
Each core gets assigned one piece of data in that list and modifies it.
The updated data is passed to the root via gather, where it is then
broadcast to all the other cores.
"""
import sys
from mpi4py import MPI
from random import shuffle
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
root = 0
data = [i*10 for i in range(size)]
shuffle(data)
data = comm.bcast(data)
print("Starting data for rank {}: {}".format(rank, data))
# Assign a piece of data to each core
positions_per_core = {i: i for i in range(len(data))}
# Update the data assigned to this core
data[positions_per_core[rank]] += 1
# Allgather all the data
data = comm.gather(data[positions_per_core[rank]])
print("Ending data for rank {}: {} (this is only correct on the root)".format(rank, data))
data = comm.bcast(data)
print("After broadcasting, rank {} has: {}".format(rank, data))