forked from csc-training/hpc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi-gather.py
52 lines (38 loc) · 890 Bytes
/
mpi-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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from mpi4py import MPI
import numpy as np
from sys import stdout
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
assert size == 4
data = np.arange(2) / 10. + rank # NumPy array
# Let's create different Python objects for different MPI tasks
if rank == 0:
py_data = 'foo.bar'
elif rank == 1:
py_data = 12.34
elif rank == 2:
py_data = {'key1' : 99.0, 'key2' : [-1, 2.3]}
else:
py_data = [6.5, 4.3]
if rank == 1:
recv_buf = np.zeros(8)
else:
recv_buf = None
if rank == 0:
print("Original data")
stdout.flush()
comm.Barrier()
print("rank ", rank, data)
print("rank ", rank, py_data)
stdout.flush()
comm.Gather(data, recv_buf, root=1)
new_data = comm.gather(py_data, root=1)
comm.Barrier()
if rank == 0:
print()
print("Final data")
stdout.flush()
comm.Barrier()
print("rank ", rank, recv_buf)
print("rank ", rank, new_data)