forked from wesm/tokyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_speed.pyx
477 lines (357 loc) · 12.6 KB
/
double_speed.pyx
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
cimport tokyo
import tokyo
import numpy as np
cimport numpy as np
import time
import sys
tokyo.verbose = True
speed_base = 200000 # increase to get slower but more precise speed test results
test_sizes = [4, 15, 30]
print
print "Tokyo BLAS wrapper double precision speed test"
print "----------------------------------------------"
print
print "Make sure your CPU isn't doing frequency scaling, otherwise"
print "the speed results here might be all messed up. A few percent"
print "variation in speed results from run to run is normal."
print
print "Speed is given in thousands of calls per second (kc/s), and in"
print "some cases how many times faster than scipy/numpy the call is."
print "Naturally the advantage is greatest on small vectors/matrices"
print "because that's when the numpy/scipy overhead is high relative"
print "to the total computation cost."
print
print "SPEED TEST BLAS 1"
print
for size in test_sizes:
print "Double precision: Vector size = " + str(size)
print
dswap_speed(size)
dscal_speed(size)
dcopy_speed(size)
daxpy_speed(size)
ddot_speed(size)
dnrm2_speed(size)
dasum_speed(size)
idamax_speed(size)
print
print
print "SPEED TEST BLAS 2"
print
for size in test_sizes:
print "Double precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
dgemv_speed(size); print
dger_speed(size); print
print
print "SPEED TEST BLAS 3"
print
for size in test_sizes:
print "Double precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
dgemm_speed(size); print
print
print "SPEED TEST EXTRAS"
print
for size in test_sizes:
print "Double precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
dmsetzero_speed(size)
dvsetzero_speed(size)
dmaxpy_speed(size)
print
##################################################################################
#####################################
#
# BLAS LEVEL 1 (vector operations)
#
#####################################
# vector swap: x <-> y
cdef dswap_speed(int size):
cdef int i, loops
loops = speed_base*1000/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dswap: ",
start = time.clock()
for i in range(loops):
tokyo.dswap( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s" % (rate/1000)
# scalar vector multiply: x *= alpha
cdef dscal_speed(int size):
cdef int i, loops
loops = speed_base*2500/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dscal: ",
start = time.clock()
for i in range(loops):
tokyo.dscal( 1.2, x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector copy: y <- x
cdef dcopy_speed(int size):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dcopy: ",
start = time.clock()
for i in range(loops):
tokyo.dcopy( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector addition: y += alpha * x
cdef daxpy_speed( int size ):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
print "daxpy: ",
start = time.clock()
for i in range(loops):
tokyo.daxpy( 1.2, x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector dot product: x.T y
cdef ddot_speed(int size):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
print "ddot: ",
start = time.clock()
for i in range(loops):
tokyo.ddot( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# Euclidean norm: ||x||_2
cdef dnrm2_speed(int size):
cdef int i, loops
loops = speed_base*700/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dnrm2: ",
start = time.clock()
for i in range(loops):
tokyo.dnrm2( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# sum of absolute values: ||x||_1
cdef dasum_speed(int size):
cdef int i, loops
loops = speed_base*2000/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dasum: ",
start = time.clock()
for i in range(loops):
tokyo.dasum( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# index of maximum absolute value element
cdef idamax_speed(int size):
cdef int i, loops
loops = speed_base*2000/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
print "idamax: ",
start = time.clock()
for i in range(loops):
tokyo.idamax( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
###########################################
#
# BLAS LEVEL 2 (matrix-vector operations)
#
###########################################
# double precision matrix times vector: y = alpha * A x + beta * y
# or y = alpha * A.T x + beta * y
cdef dgemv_speed( int size ):
cdef int i, loops
loops = speed_base*10/(<int>(size**1.2))
A = np.array( np.random.random( (size,size) ), dtype=np.float64 )
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
cdef np.ndarray[double, ndim=2, mode='c'] A_
cdef np.ndarray[double, ndim=1, mode='c'] x_, y_
A_ = A; x_ = x; y_ = y
print "numpy.dot +: ",
start = time.clock()
for i in range(loops):
y += np.dot(A,x)
np_rate = loops/(time.clock()-start)
print "%9.0f kc/s" % (np_rate/1000)
loops *= 3
print "dgemv: ",
start = time.clock()
for i in range(loops):
y = tokyo.dgemv( A, x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
loops *= 5
print "dgemv3: ",
start = time.clock()
for i in range(loops):
tokyo.dgemv3( A, x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemv5: ",
start = time.clock()
for i in range(loops):
tokyo.dgemv5( 1.2, A, x, 2.1, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemv6: ",
start = time.clock()
for i in range(loops):
tokyo.dgemv6( tokyo.CblasNoTrans, 1.2, A, x, 2.1, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemv_: ",
start = time.clock()
for i in range(loops):
tokyo.dgemv_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, A_.shape[0], A_.shape[1],
1.2, <double*>A_.data, A_.shape[1], <double*>x_.data, 1,
2.1, <double*>y_.data, 1 )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
# double precision vector outer-product: A = alpha * outer_product( x, y.T )
cdef dger_speed( int size ):
cdef int i, loops
loops = speed_base*10/(<int>(size**1.2))
x = np.array( np.random.random( (size) ), dtype=np.float64 )
y = np.array( np.random.random( (size) ), dtype=np.float64 )
Z = np.array( np.random.random( (size,size) ), dtype=np.float64 )
cdef np.ndarray[double, ndim=1, mode='c'] x_, y_
cdef np.ndarray[double, ndim=2, mode='c'] Z_
x_ = x; y_ = y; Z_ = Z
print "numpy.outer: ",
start = time.clock()
for i in range(loops):
np.outer( x, y )
np_rate = loops/(time.clock()-start)
print "%9.0f kc/s" % (np_rate/1000)
loops *= 15
print "dger: ",
start = time.clock()
for i in range(loops):
tokyo.dger( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
loops *= 2
print "dger3: ",
start = time.clock()
for i in range(loops):
tokyo.dger3( x, y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dger4: ",
start = time.clock()
for i in range(loops):
tokyo.dger4( 1.0, x, y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dger_: ",
start = time.clock()
for i in range(loops):
tokyo.dger_( tokyo.CblasRowMajor, x_.shape[0], y_.shape[0],
1.0, <double*>x_.data, 1, <double*>y_.data, 1, <double*>Z_.data, Z_.shape[1])
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
###########################################
#
# BLAS LEVEL 3 (matrix-matrix operations)
#
###########################################
# matrix times matrix: C = alpha * A B + beta * C
# or C = alpha * A.T B + beta * C
# or C = alpha * A B.T + beta * C
# or C = alpha * A.T B.T + beta * C
#
# double precision
cdef dgemm_speed( int size ):
cdef int i, loops
loops = speed_base*150/(size*size)
X = np.array( np.random.random( (size,size) ), dtype=np.float64 )
Y = np.array( np.random.random( (size,size) ), dtype=np.float64 )
Z = np.array( np.random.random( (size,size) ), dtype=np.float64 )
cdef np.ndarray[double, ndim=2, mode='c'] X_, Y_, Z_
X_ = X; Y_ = Y; Z_ = Z
print "numpy.dot: ",
start = time.clock()
for i in range(loops): np.dot( X, Y )
np_rate = loops/(time.clock()-start)
print "%9.0f kc/s" % (np_rate/1000)
print "dgemm: ",
start = time.clock()
for i in range(loops):
tokyo.dgemm( X, Y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemm3: ",
start = time.clock()
for i in range(loops):
tokyo.dgemm3( X, Y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemm5: ",
start = time.clock()
for i in range(loops):
tokyo.dgemm5( 1.0, X, Y, 0.0, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemm7: ",
start = time.clock()
for i in range(loops):
tokyo.dgemm7( tokyo.CblasNoTrans, tokyo.CblasNoTrans, 1.0, X, Y, 0.0, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "dgemm_: ",
start = time.clock()
for i in range(loops):
tokyo.dgemm_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, tokyo.CblasNoTrans,
size, size, size, 1.0, <double*>X_.data, size, <double*>Y_.data, size,
0.0, <double*>Z_.data, size )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
####################################################################
#
# Utility function I have put together that aren't in BLAS or LAPACK
#
####################################################################
# set a matrix of double to all zeros
cdef dmsetzero_speed(int size):
cdef int i, loops
loops = speed_base*5000/(size*size)
A = np.array( np.random.random( (size,size) ), dtype=np.float64 )
print "dmsetzero: ",
start = time.clock()
for i in range(loops):
tokyo.dmsetzero( A )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# set a vector of doubles to all zeros
cdef dvsetzero_speed(int size):
cdef int i, loops
loops = speed_base*5000/size
x = np.array( np.random.random( (size) ), dtype=np.float64 )
print "dvsetzero: ",
start = time.clock()
for i in range(loops):
tokyo.dvsetzero( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# double precision matrix += scalar * matrix
cdef dmaxpy_speed( int size ):
cdef int i, loops
loops = speed_base*10000/(size*size)
X = np.array( np.random.random( (size,size) ), dtype=np.float64 )
Y = np.array( np.random.random( (size,size) ), dtype=np.float64 )
print "dmaxpy: ",
start = time.clock()
for i in range(loops):
tokyo.dmaxpy( 1.2, X, Y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)