forked from tokyo/tokyo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_speed.pyx
469 lines (357 loc) · 12.5 KB
/
single_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
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 single 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 "Single precision: Vector size = " + str(size)
print
sswap_speed(size)
sscal_speed(size)
scopy_speed(size)
saxpy_speed(size)
sdot_speed(size)
snrm2_speed(size)
sasum_speed(size)
isamax_speed(size)
print
print
print "SPEED TEST BLAS 2"
print
for size in test_sizes:
print "Single Precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
sgemv_speed(size); print
sger_speed(size); print
print
print "SPEED TEST BLAS 3"
print
for size in test_sizes:
print "Single precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
sgemm_speed(size); print
print
print "SPEED TEST EXTRAS"
print
for size in test_sizes:
print "Single precision: Vector size = " + str(size) + \
" Matrix size = " + str(size) + "x" + str(size)
print
smsetzero_speed(size)
svsetzero_speed(size)
smaxpy_speed(size)
print
##################################################################################
#####################################
#
# BLAS LEVEL 1 (vector operations)
#
#####################################
# vector swap: x <-> y
cdef sswap_speed(int size):
cdef int i, loops
loops = speed_base*1000/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
print "sswap: ",
start = time.clock()
for i in range(loops):
tokyo.sswap( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s" % (rate/1000)
# scalar vector multiply: x *= alpha
cdef sscal_speed(int size):
cdef int i, loops
loops = speed_base*2500/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
print "sscal: ",
start = time.clock()
for i in range(loops):
tokyo.sscal( 1.2, x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector copy: y <- x
cdef scopy_speed(int size):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
print "scopy: ",
start = time.clock()
for i in range(loops):
tokyo.scopy( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector addition: y += alpha * x
cdef saxpy_speed( int size ):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
print "saxpy: ",
start = time.clock()
for i in range(loops):
tokyo.saxpy( 1.2, x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# vector dot product: x.T y
cdef sdot_speed(int size):
cdef int i, loops
loops = speed_base*1500/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
print "sdot: ",
start = time.clock()
for i in range(loops):
tokyo.sdot( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# Euclidean norm: ||x||_2
cdef snrm2_speed(int size):
cdef int i, loops
loops = speed_base*700/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
print "snrm2: ",
start = time.clock()
for i in range(loops):
tokyo.snrm2( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# sum of absolute values: ||x||_1
cdef sasum_speed(int size):
cdef int i, loops
loops = speed_base*2000/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
print "sasum: ",
start = time.clock()
for i in range(loops):
tokyo.sasum( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# index of maximum absolute value element
cdef isamax_speed(int size):
cdef int i, loops
loops = speed_base*2000/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
print "isamax: ",
start = time.clock()
for i in range(loops):
tokyo.isamax( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
###########################################
#
# BLAS LEVEL 2 (matrix-vector operations)
#
###########################################
# single precision matrix times vector: y = alpha * A x + beta * y
# or y = alpha * A.T x + beta * y
cdef sgemv_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.float32 )
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
cdef np.ndarray[float, ndim=2, mode='c'] A_
cdef np.ndarray[float, 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 "sgemv: ",
start = time.clock()
for i in range(loops):
y = tokyo.sgemv( A, x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
loops *= 5
print "sgemv3: ",
start = time.clock()
for i in range(loops):
tokyo.sgemv3( A, x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "sgemv5: ",
start = time.clock()
for i in range(loops):
tokyo.sgemv5( 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 "sgemv6: ",
start = time.clock()
for i in range(loops):
tokyo.sgemv6( 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 "sgemv_: ",
start = time.clock()
for i in range(loops):
tokyo.sgemv_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, A_.shape[0], A_.shape[1],
1.2, <float*>A_.data, A_.shape[1], <float*>x_.data, 1,
2.1, <float*>y_.data, 1 )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
# single precision vector outer-product: A = alpha * outer_product( x, y.T )
cdef sger_speed( int size ):
cdef int i, loops
loops = speed_base*10/(<int>(size**1.2))
x = np.array( np.random.random( (size) ), dtype=np.float32 )
y = np.array( np.random.random( (size) ), dtype=np.float32 )
Z = np.array( np.random.random( (size,size) ), dtype=np.float32 )
cdef np.ndarray[float, ndim=1, mode='c'] x_, y_
cdef np.ndarray[float, 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 "sger: ",
start = time.clock()
for i in range(loops):
tokyo.sger( x, y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
loops *= 2
print "sger3: ",
start = time.clock()
for i in range(loops):
tokyo.sger3( x, y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "sger4: ",
start = time.clock()
for i in range(loops):
tokyo.sger4( 1.0, x, y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "sger_: ",
start = time.clock()
for i in range(loops):
tokyo.sger_( tokyo.CblasRowMajor, x_.shape[0], y_.shape[0],
1.0, <float*>x_.data, 1, <float*>y_.data, 1, <float*>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
#
# single precision
cdef sgemm_speed( int size ):
cdef int i, loops
loops = speed_base*150/(size*size)
X = np.array( np.random.random( (size,size) ), dtype=np.float32 )
Y = np.array( np.random.random( (size,size) ), dtype=np.float32 )
Z = np.array( np.random.random( (size,size) ), dtype=np.float32 )
cdef np.ndarray[float, 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 "sgemm: ",
start = time.clock()
for i in range(loops):
tokyo.sgemm( X, Y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "sgemm3: ",
start = time.clock()
for i in range(loops):
tokyo.sgemm3( X, Y, Z )
rate = loops/(time.clock()-start)
print "%9.0f kc/s %5.1fx" % (rate/1000,rate/np_rate)
print "sgemm5: ",
start = time.clock()
for i in range(loops):
tokyo.sgemm5( 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 "sgemm7: ",
start = time.clock()
for i in range(loops):
tokyo.sgemm7( 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 "sgemm_: ",
start = time.clock()
for i in range(loops):
tokyo.sgemm_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, tokyo.CblasNoTrans,
size, size, size, 1.0, <float*>X_.data, size, <float*>Y_.data, size,
0.0, <float*>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 floats to all zeros
cdef smsetzero_speed(int size):
cdef int i, loops
loops = speed_base*5000/(size*size)
A = np.array( np.random.random( (size,size) ), dtype=np.float32 )
print "smsetzero: ",
start = time.clock()
for i in range(loops):
tokyo.smsetzero( A )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# set a vector of floats to all zeros
cdef svsetzero_speed(int size):
cdef int i, loops
loops = speed_base*5000/size
x = np.array( np.random.random( (size) ), dtype=np.float32 )
print "svsetzero: ",
start = time.clock()
for i in range(loops):
tokyo.svsetzero( x )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)
# single precision matrix += scalar * matrix
cdef smaxpy_speed( int size ):
cdef int i, loops
loops = speed_base*10000/(size*size)
X = np.array( np.random.random( (size,size) ), dtype=np.float32 )
Y = np.array( np.random.random( (size,size) ), dtype=np.float32 )
print "smaxpy: ",
start = time.clock()
for i in range(loops):
tokyo.smaxpy( 1.2, X, Y )
rate = loops/(time.clock()-start)
print "%9.0f kc/s " % (rate/1000)