-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallgather.py
33 lines (24 loc) · 866 Bytes
/
allgather.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
"""
Similar to the gather example, but this uses allgather and parses the results
to reflect the original list structure and discards the old data.
"""
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
all_data = comm.allgather(data)
# Create a single updated piece of data based on the updates that each core performed
data = [all_data[i][positions_per_core[i]] for i in range(size)]
print("Ending data for rank {}: {}".format(rank, data))