-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps.py
142 lines (101 loc) · 3.03 KB
/
ps.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from mpi4py import MPI
import numpy as np
#WINDOWS
#https://www.microsoft.com/en-us/download/details.aspx?id=105289
#mpiexec
#You may also check:
#https://www.mpich.org/downloads/
#Ubuntu (I didn't check)
#sudo apt install openmpi-bin openmpi-dev openmpi-common openmpi-doc libopenmpi-dev
#mpicc --showme:version
#MacOS (I didn't check)
#brew install open-mpi
#mpicc --showme:version
#mpi4py documentation
#https://mpi4py.readthedocs.io/en/stable/
#MPI Tutorial
#https://mpitutorial.com/tutorials/
#Initilazing MPI environment
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
data = comm.recv(source = 1, tag = 1)
print(rank, data)
i=0
while i<3:
data = "root to leaf {i}"
comm.send(data, dest = 1, tag = 0)
print(rank, data)
i+=1
else:
if rank==1:
data = comm.recv(source = 0, tag = 1)
print(rank, data)
data = "leaf to parent"
comm.send(data, dest = 2, tag = 0)
print(rank, data)
elif rank==2:
data = comm.recv(source = 1, tag = 0)
print(rank, data)
if rank == 0:
print(f"I am the master, I have rank {rank}")
print(f"There are {size} processes running")
for i in range(1, size):
comm.send(42, dest = i, tag = 1)
else:
print(f"I am a slave, I have rank {rank}")
received_int = None
received_int = comm.recv(source = 0, tag = MPI.ANY_TAG)
#comm.Irecv(received_int, source = 0, tag = MPI.ANY_TAG)
print(f"My master blessed me with integer {received_int}")
if rank == 0:
data = np.arange(20, dtype = int)
comm.Send([data, MPI.INT], dest = 1, tag = 2)
print(rank, data)
else:
data = np.empty(20, dtype = int)
comm.Recv([data, MPI.INT], source = 0, tag = 2)
print(rank, data)
if rank == 0:
data = "first"
comm.send(data, dest = 1, tag = 0)
print(rank, data)
data = "second"
comm.send(data, dest = 1, tag = 1)
print(rank, data)
else:
data = comm.recv(source = 0, tag = 1)
print(rank, data)
data = comm.recv(source = 0, tag = 0)
print(rank, data)
if rank == 0:
data = np.empty(10, dtype = int)
comm.Irecv([data, MPI.INT], source = 1, tag = 1)
print(rank, data)
data = np.arange(10, dtype = int)
comm.Isend([data, MPI.INT], dest = 1, tag = 0)
print(rank, data)
else:
data = np.empty(10, dtype = int)
comm.Irecv([data, MPI.INT], source = 0, tag = 1)
print(rank, data)
data = np.arange(10, dtype = int)
comm.Isend([data, MPI.INT], dest = 0, tag = 0)
print(rank, data)
if rank == 0:
for i in range(10):
comm.send(i, dest = 1, tag = 0)
print("Master has done sending")
else:
while True:
data_probe = False
#data_probe = comm.probe(source = 0, tag = 0)
data_probe = comm.iprobe(source = 0, tag = 0)
if data_probe:
data = comm.recv(source = 0, tag = 0)
print(f"Slave received {data}")
if data == 9:
break
print("Waiting")
print("Slave has received all")