-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.py
65 lines (45 loc) · 957 Bytes
/
basics.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from mpi4py import MPI
from numpy import *
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
# simple send and recv
if rank == 0:
print "I'm master process"
data = {'a':97,'b':98,'c':99}
comm.send(data, dest=2, tag=42)
elif rank == 2:
print "I'm chosen receiver"
data = comm.recv(source=0,tag=42)
print data
else:
print "I'm a normal receiver"
#broadcast
if rank == 0:
data = [2,4,6]
else:
data = []
data = comm.bcast(data,root=0)
print "I'm process "+str(rank)+" and my data :"+str(data)
#scatter data
if rank == 0:
data = array([0,1,4,9,16,25,36,49,64])
else:
data = None
data_local = array([0,0])
data = comm.Scatter(data,data_local,root=0)
print rank,data_local
#gather data
data = rank**2
data = comm.gather(data,root=0)
if rank == 0:
print rank, data
else:
print rank, data
#reduce data
data = rank**2
data = comm.reduce(data,op=MPI.MAX,root=0)
if rank == 0:
print rank, data
else:
print rank, data