forked from jbornschein/mpi4py-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-scatter-gather
executable file
·47 lines (33 loc) · 930 Bytes
/
03-scatter-gather
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
#!/usr/bin/env python
from __future__ import division
from __future__ import print_function
import numpy as np
from mpi4py import MPI
from parutils import pprint
comm = MPI.COMM_WORLD
pprint("-"*78)
pprint(" Running on %d cores" % comm.size)
pprint("-"*78)
my_N = 4
N = my_N * comm.size
if comm.rank == 0:
A = np.arange(N, dtype=np.float64)
else:
A = np.empty(N, dtype=np.float64)
my_A = np.empty(my_N, dtype=np.float64)
# Scatter data into my_A arrays
comm.Scatter( [A, MPI.DOUBLE], [my_A, MPI.DOUBLE] )
pprint("After Scatter:")
for r in range(comm.size):
if comm.rank == r:
print("[%d] %s" % (comm.rank, my_A))
comm.Barrier()
# Everybody is multiplying by 2
my_A *= 2
# Allgather data into A again
comm.Allgather( [my_A, MPI.DOUBLE], [A, MPI.DOUBLE] )
pprint("After Allgather:")
for r in range(comm.size):
if comm.rank == r:
print("[%d] %s" % (comm.rank, A))
comm.Barrier()