forked from tokyo/tokyo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.pyx
1196 lines (904 loc) · 33.1 KB
/
test.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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"
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 "Correctness is verified against scipy/numpy equivalent calls."
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 "VERIFY CORRECTNESS BLAS 1"
print
sswap_verify()
sscal_verify()
scopy_verify()
saxpy_verify()
sdot_verify()
snrm2_verify()
sasum_verify()
isamax_verify()
print
dswap_verify()
dscal_verify()
dcopy_verify()
daxpy_verify()
ddot_verify()
dnrm2_verify()
dasum_verify()
idamax_verify()
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
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 "VERIFY CORRECTNESS BLAS 2"
print
sgemv_verify(); print
sger_verify(); print
dgemv_verify(); print
dger_verify(); 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
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 "VERIFY CORRECTNESS BLAS 3"
print
sgemm_verify(); print
dgemm_verify(); 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
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 "VERIFY CORRECTNESS EXTRAS"
print
smsetzero_verify();
svsetzero_verify();
smaxpy_verify();
print
dmsetzero_verify();
dvsetzero_verify();
dmaxpy_verify();
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
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
##################################################################################
# A function for checking that different matrices from different
# computations are some sense "equal" in the verification tests.
def approx_eq( X, Y ): return abs(np.sum(X - Y)) < 1e-5
#####################################
#
# BLAS LEVEL 1 (vector operations)
#
#####################################
# vector swap: x <-> y
def sswap_verify():
x = np.array( np.random.random( (4) ), dtype=np.float32 )
y = np.array( np.random.random( (4) ), dtype=np.float32 )
temp1 = x.copy()
temp2 = y.copy()
tokyo.sswap(x,y)
print "sswap: ", (approx_eq( temp1, y ) and approx_eq( temp2, x ))
def 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)
def dswap_verify():
x = np.array( np.random.random( (4) ), dtype=np.float64 )
y = np.array( np.random.random( (4) ), dtype=np.float64 )
temp1 = x.copy()
temp2 = y.copy()
tokyo.dswap(x,y)
print "dswap: ", (approx_eq( temp1, y ) and approx_eq( temp2, x ))
def 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
def sscal_verify():
x = np.array( np.random.random( (4) ), dtype=np.float32 )
temp = 1.2 * x
tokyo.sscal( 1.2, x)
print "sscal: ", approx_eq( temp, x )
def 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)
def dscal_verify():
x = np.array( np.random.random( (4) ), dtype=np.float64 )
temp = 1.2 * x
tokyo.dscal( 1.2, x)
print "dscal: ", approx_eq( temp, x )
def 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
def scopy_verify():
x = np.array( np.random.random( (4) ), dtype=np.float32 )
y = np.array( np.random.random( (4) ), dtype=np.float32 )
tokyo.scopy(x,y)
print "scopy: ", approx_eq( x, y )
def 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)
def dcopy_verify():
x = np.array( np.random.random( (4) ), dtype=np.float64 )
y = np.array( np.random.random( (4) ), dtype=np.float64 )
tokyo.dcopy(x,y)
print "dcopy: ", approx_eq( x, y )
def 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
def saxpy_verify():
x = np.array( np.random.random( (5) ), dtype=np.float32 )
y = np.array( np.random.random( (5) ), dtype=np.float32 )
temp = 1.2 * x + y
tokyo.saxpy( 1.2, x, y )
print "saxpy: ", approx_eq( temp, y )
def 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)
def daxpy_verify():
x = np.array( np.random.random( (5) ), dtype=np.float64 )
y = np.array( np.random.random( (5) ), dtype=np.float64 )
temp = 1.2 * x + y
tokyo.daxpy( 1.2, x, y )
print "daxpy: ", approx_eq( temp, y )
def 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
def sdot_verify():
x = np.array( np.random.random( (5) ), dtype=np.float32 )
y = np.array( np.random.random( (5) ), dtype=np.float32 )
print "sdot: ", approx_eq( np.dot(x,y), tokyo.sdot(x,y) )
def 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)
def ddot_verify():
x = np.array( np.random.random( (5) ), dtype=np.float64 )
y = np.array( np.random.random( (5) ), dtype=np.float64 )
print "ddot: ", approx_eq( np.dot(x,y), tokyo.ddot(x,y) )
def 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
def snrm2_verify():
x = np.array( np.random.random( (5) ), dtype=np.float32 )
print "snrm2: ", approx_eq( np.sqrt(np.sum(np.dot(x,x))), tokyo.snrm2(x) )
def 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)
def dnrm2_verify():
x = np.array( np.random.random( (5) ), dtype=np.float64 )
print "dnrm2: ", approx_eq( np.sqrt(np.sum(np.dot(x,x))), tokyo.dnrm2(x) )
def 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
def sasum_verify():
x = np.array( np.random.random( (5) ), dtype=np.float32 )
print "sasum: ", approx_eq( np.sum(np.abs(x)), tokyo.sasum(x) )
def 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)
def dasum_verify():
x = np.array( np.random.random( (5) ), dtype=np.float64 )
print "dasum: ", approx_eq( np.sum(np.abs(x)), tokyo.dasum(x) )
def 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
def isamax_verify():
x = np.array( [0.06, -0.1, -0.05, -0.001, 0.07], dtype=np.float32 )
print "isamax: ", ( 1 == tokyo.isamax(x) )
def 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)
def idamax_verify():
x = np.array( [0.06, -0.1, -0.05, -0.001, 0.07], dtype=np.float64 )
print "idamax: ", ( 1 == tokyo.idamax(x) )
def 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)
#
###########################################
# single precision matrix times vector: y = alpha * A x + beta * y
# or y = alpha * A.T x + beta * y
def sgemv_verify():
A = np.array( np.random.random( (4,5) ), dtype=np.float32 )
x = np.array( np.random.random( (5) ), dtype=np.float32 )
y = np.array( np.random.random( (4) ), 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
temp = np.dot(A,x)
temp2 = tokyo.sgemv( A, x )
print "sgemv: ", approx_eq( temp, temp2 )
temp = np.dot(A,x)
tokyo.sgemv3( A, x, y )
print "sgemv3: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.sgemv5( 1.2, A, x, 2.1, y )
print "sgemv5: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.sgemv6( tokyo.CblasNoTrans, 1.2, A, x, 2.1, y )
print "sgemv6: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.sgemv_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, 4, 5,
1.2, <float*>A_.data, 5, <float*>x_.data, 1,
2.1, <float*>y_.data, 1 )
print "sgemv_: ", approx_eq( temp, y )
def 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)
# double precision matrix times vector: y = alpha * A x + beta * y
# or y = alpha * A.T x + beta * y
def dgemv_verify():
A = np.array( np.random.random( (4,5) ), dtype=np.float64 )
x = np.array( np.random.random( (5) ), dtype=np.float64 )
y = np.array( np.random.random( (4) ), 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
temp = np.dot(A,x)
temp2 = tokyo.dgemv( A, x )
print "dgemv: ", approx_eq( temp, temp2 )
temp = np.dot(A,x)
tokyo.dgemv3( A, x, y )
print "dgemv3: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.dgemv5( 1.2, A, x, 2.1, y )
print "dgemv5: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.dgemv6( tokyo.CblasNoTrans, 1.2, A, x, 2.1, y )
print "dgemv6: ", approx_eq( temp, y )
temp = 1.2*np.dot(A,x) + 2.1*y
tokyo.dgemv_( tokyo.CblasRowMajor, tokyo.CblasNoTrans, 4, 5,
1.2, <double*>A_.data, 5, <double*>x_.data, 1,
2.1, <double*>y_.data, 1 )
print "dgemv_: ", approx_eq( temp, y )
def 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)
# single precision vector outer-product: A = alpha * outer_product( x, y.T )
def sger_verify():
x = np.array( np.random.random( (4) ), dtype=np.float32 )
y = np.array( np.random.random( (5) ), dtype=np.float32 )
A = np.array( np.random.random( (4,5) ), dtype=np.float32 )
result = np.outer( x, y )
print "sger: ", approx_eq( result, tokyo.sger( x, y ))
result = A + np.outer( x, y )
tokyo.sger3( x, y, A )
print "sger3: ", approx_eq( result, A )
result = A + 1.2*np.outer( x, y )
tokyo.sger4( 1.2, x, y, A )
print "sger4: ", approx_eq( result, A )
def 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)
# double precision vector outer-product: A = alpha * outer_product( x, y.T )
def dger_verify():
x = np.array( np.random.random( (4) ), dtype=np.float64 )
y = np.array( np.random.random( (5) ), dtype=np.float64 )
A = np.array( np.random.random( (4,5) ), dtype=np.float64 )
result = np.outer( x, y )
print "dger: ", approx_eq( result, tokyo.dger( x, y ))
result = A + np.outer( x, y )
tokyo.dger3( x, y, A )
print "dger3: ", approx_eq( result, A )
result = A + 1.2*np.outer( x, y )
tokyo.dger4( 1.2, x, y, A )
print "dger4: ", approx_eq( result, A )
def 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
#
# single precision
def sgemm_verify():
X = np.array( np.random.random( (3,4) ), dtype=np.float32 )
Y = np.array( np.random.random( (4,5) ), dtype=np.float32 )
print "sgemm: ", approx_eq(np.dot( X, Y ), tokyo.sgemm( X, Y ))
Z = np.array( np.random.random( (3,5) ), dtype=np.float32 )
tokyo.sgemm3( X, Y, Z )
print "sgemm3: ", approx_eq( np.dot( X, Y ), Z )
Z = np.array( np.random.random( (3,5) ), dtype=np.float32 )
result = 2.3*np.dot( X, Y ) + 1.2*Z
tokyo.sgemm5( 2.3, X, Y, 1.2, Z )
print "sgemm5: ", approx_eq( result, Z )
Z = np.array( np.random.random( (3,5) ), dtype=np.float32 )
result = 2.3*np.dot( X, Y ) + 1.2*Z
tokyo.sgemm7( tokyo.CblasNoTrans, tokyo.CblasNoTrans, 2.3, X, Y, 1.2, Z )
print "sgemm7: ", approx_eq( result, Z )
def 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)
# 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
def dgemm_verify():
X = np.array( np.random.random( (3,4) ), dtype=np.float64 )
Y = np.array( np.random.random( (4,5) ), dtype=np.float64 )
print "dgemm: ", approx_eq(np.dot( X, Y ), tokyo.dgemm( X, Y ))
Z = np.array( np.random.random( (3,5) ), dtype=np.float64 )
tokyo.dgemm3( X, Y, Z )
print "dgemm3: ", approx_eq( np.dot( X, Y ), Z )
Z = np.array( np.random.random( (3,5) ), dtype=np.float64 )
result = 2.3*np.dot( X, Y ) + 1.2*Z
tokyo.dgemm5( 2.3, X, Y, 1.2, Z )
print "dgemm5: ", approx_eq( result, Z )
Z = np.array( np.random.random( (3,5) ), dtype=np.float64 )
result = 2.3*np.dot( X, Y ) + 1.2*Z
tokyo.dgemm7( tokyo.CblasNoTrans, tokyo.CblasNoTrans, 2.3, X, Y, 1.2, Z )
print "dgemm7: ", approx_eq( result, Z )