-
Notifications
You must be signed in to change notification settings - Fork 63
/
all_tests.py
308 lines (254 loc) · 9.09 KB
/
all_tests.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
MVP : Demonstrating a MPI parallel Matrix-Vector Multiplication.
This code will run *iter* iterations of
v(t+1) = M * v(t)
where v is a vector of length *size* and M a dense size*size
matrix. *size* must be an integer multiple of comm.size.
v is initialized to be zero except of v[0] = 1.0
M is a "off-by-one" diagonal matrix M[i, i+1] = 1.0
In effect, after *iter* iterations, the vector v should look like
v[iter] = 1. (all others zero).
In this example every MPI process is responsible for calculating a
different portion of v. Every process only knows the stripe of M, that
is relevant for it's calculation. At the end of every iteration,
Allgather is used to distribute the partial vectors v to all other
processes.
"""
from __future__ import division
import numpy as np
# from numpy.fft import fft2, ifft2
from math import ceil, fabs
from mpi4py import MPI
#=============================================================================
# I/O Utilities
def pprint(str="", end="\n", comm=MPI.COMM_WORLD):
"""Print for MPI parallel programs: Only rank 0 prints *str*."""
if comm.rank == 0:
print str+end,
#=============================================================================
# Main
def mvp_main(BENCHMARH="MPI Matrix action on a vector",
size=10000,
iter=200):
# size = 10000 # length of vector v
# iter = 200 # number of iterations to run
counter = 0
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
pprint("============================================================================")
pprint(" Running %d parallel MPI processes" % comm.size)
my_size = size // comm.size # Every process computes a vector of lenth *my_size*
size = comm.size*my_size # size must be integer multiple of comm.size
my_offset = comm.rank*my_size
bs = 20 # batch size
if myid == 0:
print ('# %s, %d iterations of size %d' % (BENCHMARH, bs, size))
print ('# %-8s%20s' % ("Duration [s]", "Throughput [#/s]"))
# pprint(" %d iterations of size %d " % (bs, size))
# This is the complete vector
vec = np.zeros(size) # Every element zero...
vec[0] = 1.0 # ... besides vec[0]
# Create my (local) slice of the matrix
my_M = np.zeros((my_size, size))
for i in xrange(my_size):
j = (my_offset+i-1) % size
my_M[i,j] = 1.0
while counter < iter:
comm.Barrier() ### Start stopwatch ###
t_start = MPI.Wtime()
for t in xrange(20):
my_new_vec = np.inner(my_M, vec)
comm.Allgather(
[my_new_vec, MPI.DOUBLE],
[vec, MPI.DOUBLE]
)
comm.Barrier()
t_diff = MPI.Wtime() - t_start ### Stop stopwatch ###
# if fabs(vec[iter]-1.0) > 0.01:
# pprint("!! Error: Wrong result!")
# pprint(" %d iterations of size %d in %5.2fs: %5.2f iterations per second" %
# (bs, size, t_diff, bs/t_diff)
# )
if myid == 0:
print ('%-10.3f%20.2f' % (t_diff, bs/t_diff))
counter += bs
def osu_latency(
BENCHMARH = "MPI Latency Test",
skip = 1000,
loop = 10000,
skip_large = 10,
loop_large = 100,
large_message_size = 8192,
MAX_MSG_SIZE = 1<<22,
):
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
numprocs = comm.Get_size()
s_buf = allocate(MAX_MSG_SIZE)
r_buf = allocate(MAX_MSG_SIZE)
if myid == 0:
print ('# %s' % (BENCHMARH,))
print ('# %-8s%20s' % ("Size [B]", "Latency [us]"))
message_sizes = [0] + [2**i for i in range(30)]
for size in message_sizes:
if size > MAX_MSG_SIZE:
break
if size > large_message_size:
skip = skip_large
loop = loop_large
iterations = list(range(loop+skip))
s_msg = [s_buf, size, MPI.BYTE]
r_msg = [r_buf, size, MPI.BYTE]
comm.Barrier()
if myid == 0:
for i in iterations:
if i == skip:
t_start = MPI.Wtime()
comm.Send(s_msg, 1, 1)
comm.Recv(r_msg, 1, 1)
t_end = MPI.Wtime()
elif myid == 1:
for i in iterations:
comm.Recv(r_msg, 0, 1)
comm.Send(s_msg, 0, 1)
if myid == 0:
latency = (t_end - t_start) * 1e6 / (2 * loop)
print ('%-10d%20.2f' % (size, latency))
def osu_bibw(
BENCHMARH = "MPI Bi-Directional Bandwidth Test",
skip = 10,
loop = 100,
window_size = 64,
skip_large = 2,
loop_large = 20,
window_size_large = 64,
large_message_size = 8192,
MAX_MSG_SIZE = 1<<22,
):
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
numprocs = comm.Get_size()
s_buf = allocate(MAX_MSG_SIZE)
r_buf = allocate(MAX_MSG_SIZE)
if myid == 0:
print ('# %s' % (BENCHMARH,))
print ('# %-8s%20s' % ("Size [B]", "Bandwidth [MB/s]"))
message_sizes = [2**i for i in range(30)]
for size in message_sizes:
if size > MAX_MSG_SIZE:
break
if size > large_message_size:
skip = skip_large
loop = loop_large
window_size = window_size_large
iterations = list(range(loop+skip))
window_sizes = list(range(window_size))
s_msg = [s_buf, size, MPI.BYTE]
r_msg = [r_buf, size, MPI.BYTE]
send_request = [MPI.REQUEST_NULL] * window_size
recv_request = [MPI.REQUEST_NULL] * window_size
#
comm.Barrier()
if myid == 0:
for i in iterations:
if i == skip:
t_start = MPI.Wtime()
for j in window_sizes:
recv_request[j] = comm.Irecv(r_msg, 1, 10)
for j in window_sizes:
send_request[j] = comm.Isend(s_msg, 1, 100)
MPI.Request.Waitall(send_request)
MPI.Request.Waitall(recv_request)
t_end = MPI.Wtime()
elif myid == 1:
for i in iterations:
for j in window_sizes:
recv_request[j] = comm.Irecv(r_msg, 0, 100)
for j in window_sizes:
send_request[j] = comm.Isend(s_msg, 0, 10)
MPI.Request.Waitall(send_request)
MPI.Request.Waitall(recv_request)
#
if myid == 0:
MB = size / 1e6 * loop * window_size
s = t_end - t_start
print ('%-10d%20.2f' % (size, MB/s))
def osu_bw(
BENCHMARH = "MPI Bandwidth Test",
skip = 10,
loop = 100,
window_size = 64,
skip_large = 2,
loop_large = 20,
window_size_large = 64,
large_message_size = 8192,
MAX_MSG_SIZE = 1<<22,
):
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
numprocs = comm.Get_size()
s_buf = allocate(MAX_MSG_SIZE)
r_buf = allocate(MAX_MSG_SIZE)
if myid == 0:
print ('# %s' % (BENCHMARH,))
print ('# %-8s%20s' % ("Size [B]", "Bandwidth [MB/s]"))
message_sizes = [2**i for i in range(30)]
for size in message_sizes:
if size > MAX_MSG_SIZE:
break
if size > large_message_size:
skip = skip_large
loop = loop_large
window_size = window_size_large
iterations = list(range(loop+skip))
window_sizes = list(range(window_size))
requests = [MPI.REQUEST_NULL] * window_size
#
comm.Barrier()
if myid == 0:
s_msg = [s_buf, size, MPI.BYTE]
r_msg = [r_buf, 4, MPI.BYTE]
for i in iterations:
if i == skip:
t_start = MPI.Wtime()
for j in window_sizes:
requests[j] = comm.Isend(s_msg, 1, 100)
MPI.Request.Waitall(requests)
comm.Recv(r_msg, 1, 101)
t_end = MPI.Wtime()
elif myid == 1:
s_msg = [s_buf, 4, MPI.BYTE]
r_msg = [r_buf, size, MPI.BYTE]
for i in iterations:
for j in window_sizes:
requests[j] = comm.Irecv(r_msg, 0, 100)
MPI.Request.Waitall(requests)
comm.Send(s_msg, 0, 101)
#
if myid == 0:
MB = size / 1e6 * loop * window_size
s = t_end - t_start
print ('%-10d%20.2f' % (size, MB/s))
def allocate(n):
try:
import mmap
return mmap.mmap(-1, n)
except (ImportError, EnvironmentError):
try:
from numpy import zeros
return zeros(n, 'B')
except ImportError:
from array import array
return array('B', [0]) * n
if __name__ == '__main__':
mvp_main()
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
numprocs = comm.Get_size()
if numprocs==2 :
osu_latency()
osu_bw()
osu_bibw()
else:
if myid==0:
print ("# Warning ! OSU examples require MPI rank size == 2. Not running since rank size = %d" % (numprocs))