File tree Expand file tree Collapse file tree 4 files changed +59
-21
lines changed Expand file tree Collapse file tree 4 files changed +59
-21
lines changed Original file line number Diff line number Diff line change 11from mpi4py import MPI
22import numpy as np
3- import sys
3+ from sys import stdout
44
55comm = MPI .COMM_WORLD
66rank = comm .Get_rank ()
1010
1111data = np .arange (8 ) / 10. + rank
1212recv_buf = np .zeros (8 )
13+ # Python sequence, lenght has to be equal number to MPI tasks
14+ py_data = []
15+ for r in range (4 ):
16+ py_data .append ({'key{0:02d}' .format (10 * rank + r ) : 10 * rank + r })
1317
1418if rank == 0 :
1519 print ("Original data" )
1822for r in range (size ):
1923 if rank == r :
2024 print ("rank " , rank , data )
21- sys . stdout . flush ( )
25+ print ( "rank " , rank , py_data )
2226 comm .Barrier ()
2327
28+ stdout .flush ()
29+
2430comm .Alltoall (data , recv_buf )
31+ new_data = comm .alltoall (py_data )
2532
2633comm .Barrier ()
2734if rank == 0 :
2835 print ()
2936 print ("Final data" )
30- sys .stdout .flush ()
37+
38+ stdout .flush ()
3139comm .Barrier ()
3240
3341for r in range (size ):
3442 if rank == r :
3543 print ("rank " , rank , recv_buf )
36- sys . stdout . flush ( )
44+ print ( "rank " , rank , new_data )
3745 comm .Barrier ()
3846
47+ stdout .flush ()
48+
3949
Original file line number Diff line number Diff line change 11from mpi4py import MPI
22import numpy as np
3- import sys
3+ from sys import stdout
44
55comm = MPI .COMM_WORLD
66rank = comm .Get_rank ()
99assert size == 4
1010
1111if rank == 0 :
12- data = np .arange (8 ) / 10.
12+ data = np .arange (8 ) / 10. # NumPy array
13+ py_data = {'key1' : 0.0 , 'key2' : 11 } # Python object
1314else :
1415 data = np .zeros (8 )
16+ py_data = None
1517
1618if rank == 0 :
1719 print ("Original data" )
18- sys . stdout .flush ()
20+ stdout .flush ()
1921comm .Barrier ()
2022
2123print ("rank " , rank , data )
22- sys .stdout .flush ()
24+ print ("rank " , rank , py_data )
25+ stdout .flush ()
2326
2427comm .Bcast (data , root = 0 )
28+ new_data = comm .bcast (py_data , root = 0 )
2529
2630comm .Barrier ()
2731if rank == 0 :
2832 print ()
2933 print ("Final data" )
30- sys . stdout .flush ()
34+ stdout .flush ()
3135comm .Barrier ()
3236
3337print ("rank " , rank , data )
38+ print ("rank " , rank , new_data )
3439
3540
Original file line number Diff line number Diff line change 11from mpi4py import MPI
22import numpy as np
3- import sys
3+ from sys import stdout
44
55comm = MPI .COMM_WORLD
66rank = comm .Get_rank ()
77size = comm .Get_size ()
88
99assert size == 4
1010
11- data = np .arange (2 ) / 10. + rank
11+ data = np .arange (2 ) / 10. + rank # NumPy array
1212
13+ # Let's create different Python objects for different MPI tasks
1314if rank == 0 :
15+ py_data = 'foo.bar'
16+ elif rank == 1 :
17+ py_data = 12.34
18+ elif rank == 2 :
19+ py_data = {'key1' : 99.0 , 'key2' : [- 1 , 2.3 ]}
20+ else :
21+ py_data = [6.5 , 4.3 ]
22+
23+
24+ if rank == 1 :
1425 recv_buf = np .zeros (8 )
1526else :
1627 recv_buf = None
1728
1829if rank == 0 :
1930 print ("Original data" )
20- sys .stdout .flush ()
31+
32+ stdout .flush ()
2133comm .Barrier ()
2234
2335print ("rank " , rank , data )
24- sys .stdout .flush ()
36+ print ("rank " , rank , py_data )
37+ stdout .flush ()
2538
26- comm .Gather (data , recv_buf , root = 0 )
39+ comm .Gather (data , recv_buf , root = 1 )
40+ new_data = comm .gather (py_data , root = 1 )
2741
2842comm .Barrier ()
2943if rank == 0 :
3044 print ()
3145 print ("Final data" )
32- sys .stdout .flush ()
46+
47+ stdout .flush ()
3348comm .Barrier ()
3449
3550print ("rank " , rank , recv_buf )
36-
51+ print ( "rank " , rank , new_data )
3752
Original file line number Diff line number Diff line change 11from mpi4py import MPI
22import numpy as np
3- import sys
3+ from sys import stdout
44
55comm = MPI .COMM_WORLD
66rank = comm .Get_rank ()
99assert size == 4
1010
1111if rank == 0 :
12- data = np .arange (8 ) / 10.
12+ data = np .arange (8 ) / 10. # NumPy array
13+ # Python sequence, lenght has to be equal number to MPI tasks
14+ py_data = ['foo' , 'bar' , 11.2 , {'key' : 22 }]
1315else :
1416 data = None
17+ py_data = None
1518
1619recv_buf = np .zeros (2 )
1720
1821if rank == 0 :
1922 print ("Original data" )
20- sys .stdout .flush ()
23+
24+ stdout .flush ()
2125comm .Barrier ()
2226
2327print ("rank " , rank , data )
24- sys .stdout .flush ()
28+ print ("rank " , rank , py_data )
29+ stdout .flush ()
2530
2631comm .Scatter (data , recv_buf , root = 0 )
32+ new_data = comm .scatter (py_data , root = 0 )
2733
2834comm .Barrier ()
2935if rank == 0 :
3036 print ()
3137 print ("Final data" )
32- sys .stdout .flush ()
38+
39+ stdout .flush ()
3340comm .Barrier ()
3441
3542print ("rank " , rank , recv_buf )
43+ print ("rank " , rank , new_data )
3644
3745
You can’t perform that action at this time.
0 commit comments