File tree 4 files changed +59
-21
lines changed
4 files changed +59
-21
lines changed Original file line number Diff line number Diff line change 1
1
from mpi4py import MPI
2
2
import numpy as np
3
- import sys
3
+ from sys import stdout
4
4
5
5
comm = MPI .COMM_WORLD
6
6
rank = comm .Get_rank ()
10
10
11
11
data = np .arange (8 ) / 10. + rank
12
12
recv_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 })
13
17
14
18
if rank == 0 :
15
19
print ("Original data" )
18
22
for r in range (size ):
19
23
if rank == r :
20
24
print ("rank " , rank , data )
21
- sys . stdout . flush ( )
25
+ print ( "rank " , rank , py_data )
22
26
comm .Barrier ()
23
27
28
+ stdout .flush ()
29
+
24
30
comm .Alltoall (data , recv_buf )
31
+ new_data = comm .alltoall (py_data )
25
32
26
33
comm .Barrier ()
27
34
if rank == 0 :
28
35
print ()
29
36
print ("Final data" )
30
- sys .stdout .flush ()
37
+
38
+ stdout .flush ()
31
39
comm .Barrier ()
32
40
33
41
for r in range (size ):
34
42
if rank == r :
35
43
print ("rank " , rank , recv_buf )
36
- sys . stdout . flush ( )
44
+ print ( "rank " , rank , new_data )
37
45
comm .Barrier ()
38
46
47
+ stdout .flush ()
48
+
39
49
Original file line number Diff line number Diff line change 1
1
from mpi4py import MPI
2
2
import numpy as np
3
- import sys
3
+ from sys import stdout
4
4
5
5
comm = MPI .COMM_WORLD
6
6
rank = comm .Get_rank ()
9
9
assert size == 4
10
10
11
11
if 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
13
14
else :
14
15
data = np .zeros (8 )
16
+ py_data = None
15
17
16
18
if rank == 0 :
17
19
print ("Original data" )
18
- sys . stdout .flush ()
20
+ stdout .flush ()
19
21
comm .Barrier ()
20
22
21
23
print ("rank " , rank , data )
22
- sys .stdout .flush ()
24
+ print ("rank " , rank , py_data )
25
+ stdout .flush ()
23
26
24
27
comm .Bcast (data , root = 0 )
28
+ new_data = comm .bcast (py_data , root = 0 )
25
29
26
30
comm .Barrier ()
27
31
if rank == 0 :
28
32
print ()
29
33
print ("Final data" )
30
- sys . stdout .flush ()
34
+ stdout .flush ()
31
35
comm .Barrier ()
32
36
33
37
print ("rank " , rank , data )
38
+ print ("rank " , rank , new_data )
34
39
35
40
Original file line number Diff line number Diff line change 1
1
from mpi4py import MPI
2
2
import numpy as np
3
- import sys
3
+ from sys import stdout
4
4
5
5
comm = MPI .COMM_WORLD
6
6
rank = comm .Get_rank ()
7
7
size = comm .Get_size ()
8
8
9
9
assert size == 4
10
10
11
- data = np .arange (2 ) / 10. + rank
11
+ data = np .arange (2 ) / 10. + rank # NumPy array
12
12
13
+ # Let's create different Python objects for different MPI tasks
13
14
if 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 :
14
25
recv_buf = np .zeros (8 )
15
26
else :
16
27
recv_buf = None
17
28
18
29
if rank == 0 :
19
30
print ("Original data" )
20
- sys .stdout .flush ()
31
+
32
+ stdout .flush ()
21
33
comm .Barrier ()
22
34
23
35
print ("rank " , rank , data )
24
- sys .stdout .flush ()
36
+ print ("rank " , rank , py_data )
37
+ stdout .flush ()
25
38
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 )
27
41
28
42
comm .Barrier ()
29
43
if rank == 0 :
30
44
print ()
31
45
print ("Final data" )
32
- sys .stdout .flush ()
46
+
47
+ stdout .flush ()
33
48
comm .Barrier ()
34
49
35
50
print ("rank " , rank , recv_buf )
36
-
51
+ print ( "rank " , rank , new_data )
37
52
Original file line number Diff line number Diff line change 1
1
from mpi4py import MPI
2
2
import numpy as np
3
- import sys
3
+ from sys import stdout
4
4
5
5
comm = MPI .COMM_WORLD
6
6
rank = comm .Get_rank ()
9
9
assert size == 4
10
10
11
11
if 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 }]
13
15
else :
14
16
data = None
17
+ py_data = None
15
18
16
19
recv_buf = np .zeros (2 )
17
20
18
21
if rank == 0 :
19
22
print ("Original data" )
20
- sys .stdout .flush ()
23
+
24
+ stdout .flush ()
21
25
comm .Barrier ()
22
26
23
27
print ("rank " , rank , data )
24
- sys .stdout .flush ()
28
+ print ("rank " , rank , py_data )
29
+ stdout .flush ()
25
30
26
31
comm .Scatter (data , recv_buf , root = 0 )
32
+ new_data = comm .scatter (py_data , root = 0 )
27
33
28
34
comm .Barrier ()
29
35
if rank == 0 :
30
36
print ()
31
37
print ("Final data" )
32
- sys .stdout .flush ()
38
+
39
+ stdout .flush ()
33
40
comm .Barrier ()
34
41
35
42
print ("rank " , rank , recv_buf )
43
+ print ("rank " , rank , new_data )
36
44
37
45
You can’t perform that action at this time.
0 commit comments