-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathcublas.jl
2145 lines (2016 loc) · 72.8 KB
/
cublas.jl
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
using CUDA.CUBLAS
using CUDA.CUBLAS: band, bandex
using LinearAlgebra
using BFloat16s
using StaticArrays
@test CUBLAS.version() isa VersionNumber
@test CUBLAS.version().major == CUBLAS.cublasGetProperty(CUDA.MAJOR_VERSION)
@test CUBLAS.version().minor == CUBLAS.cublasGetProperty(CUDA.MINOR_VERSION)
@test CUBLAS.version().patch == CUBLAS.cublasGetProperty(CUDA.PATCH_LEVEL)
m = 20
n = 35
k = 13
############################################################################################
@testset "level 1" begin
@testset for T in [Float32, Float64, ComplexF32, ComplexF64]
A = CUDA.rand(T, m)
B = CuArray{T}(undef, m)
CUBLAS.copy!(m,A,B)
@test Array(A) == Array(B)
@test testf(rmul!, rand(T, 6, 9, 3), Ref(rand()))
@test testf(dot, rand(T, m), rand(T, m))
@test testf(*, transpose(rand(T, m)), rand(T, m))
@test testf(*, rand(T, m)', rand(T, m))
@test testf(norm, rand(T, m))
@test testf(BLAS.asum, rand(T, m))
@test testf(axpy!, Ref(rand()), rand(T, m), rand(T, m))
@test testf(axpby!, Ref(rand()), rand(T, m), Ref(rand()), rand(T, m))
if T <: Complex
@test testf(dot, rand(T, m), rand(T, m))
x = rand(T, m)
y = rand(T, m)
dx = CuArray(x)
dy = CuArray(y)
dz = dot(dx, dy)
z = dot(x, y)
@test dz ≈ z
end
@test testf(rotate!, rand(T, m), rand(T, m), rand(real(T)), rand(real(T)))
@test testf(rotate!, rand(T, m), rand(T, m), rand(real(T)), rand(T))
@test testf(reflect!, rand(T, m), rand(T, m), rand(real(T)), rand(real(T)))
@test testf(reflect!, rand(T, m), rand(T, m), rand(real(T)), rand(T))
# swap is an extension
x = rand(T, m)
y = rand(T, m)
dx = CuArray(x)
dy = CuArray(y)
CUBLAS.swap!(m, dx, dy)
h_x = collect(dx)
h_y = collect(dy)
@test h_x ≈ y
@test h_y ≈ x
a = convert.(T, [1.0, 2.0, -0.8, 5.0, 3.0])
ca = CuArray(a)
@test BLAS.iamax(a) == CUBLAS.iamax(ca)
@test CUBLAS.iamin(ca) == 3
end # level 1 testset
@testset for T in [Float16, ComplexF16]
A = CuVector(rand(T, m)) # CUDA.rand doesn't work with 16 bit types yet
B = CuArray{T}(undef, m)
CUBLAS.copy!(m,A,B)
@test Array(A) == Array(B)
@test testf(dot, rand(T, m), rand(T, m))
@test testf(*, transpose(rand(T, m)), rand(T, m))
@test testf(*, rand(T, m)', rand(T, m))
@test testf(norm, rand(T, m))
@test testf(axpy!, Ref(rand()), rand(T, m), rand(T, m))
@test testf(axpby!, Ref(rand()), rand(T, m), Ref(rand()), rand(T, m))
if T <: Complex
@test testf(dot, rand(T, m), rand(T, m))
x = rand(T, m)
y = rand(T, m)
dx = CuArray(x)
dy = CuArray(y)
dz = dot(dx, dy)
z = dot(x, y)
@test dz ≈ z
end
end # level 1 testset
end
############################################################################################
@testset "level 2" begin
@testset for elty in [Float32, Float64, ComplexF32, ComplexF64]
alpha = rand(elty)
beta = rand(elty)
@testset "gemv" begin
@test testf(*, rand(elty, m, n), rand(elty, n))
@test testf(*, transpose(rand(elty, m, n)), rand(elty, m))
@test testf(*, rand(elty, m, n)', rand(elty, m))
x = rand(elty, m)
A = rand(elty, m, m + 1 )
y = rand(elty, m)
dx = CuArray(x)
dA = CuArray(A)
dy = CuArray(y)
@test_throws DimensionMismatch mul!(dy, dA, dx)
A = rand(elty, m + 1, m )
dA = CuArray(A)
@test_throws DimensionMismatch mul!(dy, dA, dx)
x = rand(elty, m)
A = rand(elty, n, m)
dx = CuArray(x)
dA = CuArray(A)
alpha = rand(elty)
dy = CUBLAS.gemv('N', alpha, dA, dx)
hy = collect(dy)
@test hy ≈ alpha * A * x
dy = CUBLAS.gemv('N', dA, dx)
hy = collect(dy)
@test hy ≈ A * x
end
if CUBLAS.version() >= v"11.9"
@testset "gemv_batched" begin
x = [rand(elty, m) for i=1:10]
A = [rand(elty, n, m) for i=1:10]
y = [rand(elty, n) for i=1:10]
dx = CuArray{elty, 1}[]
dA = CuArray{elty, 2}[]
dy = CuArray{elty, 1}[]
dbad = CuArray{elty, 1}[]
for i=1:length(A)
push!(dA, CuArray(A[i]))
push!(dx, CuArray(x[i]))
push!(dy, CuArray(y[i]))
if i < length(A) - 2
push!(dbad,CuArray(dx[i]))
end
end
@test_throws DimensionMismatch CUBLAS.gemv_batched!('N', alpha, dA, dx, beta, dbad)
CUBLAS.gemv_batched!('N', alpha, dA, dx, beta, dy)
for i=1:length(A)
hy = collect(dy[i])
y[i] = alpha * A[i] * x[i] + beta * y[i]
@test y[i] ≈ hy
end
end
end
if CUBLAS.version() >= v"11.9"
@testset "gemv_strided_batched" begin
x = rand(elty, m, 10)
A = rand(elty, n, m, 10)
y = rand(elty, n, 10)
bad = rand(elty, m, 10)
dx = CuArray(x)
dA = CuArray(A)
dy = CuArray(y)
dbad = CuArray(bad)
@test_throws DimensionMismatch CUBLAS.gemv_strided_batched!('N', alpha, dA, dx, beta, dbad)
bad = rand(elty, n, 2)
dbad = CuArray(bad)
@test_throws DimensionMismatch CUBLAS.gemv_strided_batched!('N', alpha, dA, dx, beta, dbad)
CUBLAS.gemv_strided_batched!('N', alpha, dA, dx, beta, dy)
for i=1:size(A, 3)
hy = collect(dy[:, i])
y[:, i] = alpha * A[:, :, i] * x[:, i] + beta * y[:, i]
@test y[:, i] ≈ hy
end
end
end
@testset "mul! y = $f(A) * x * $Ts(a) + y * $Ts(b)" for f in (identity, transpose, adjoint), Ts in (Int, elty)
y, A, x = rand(elty, 5), rand(elty, 5, 5), rand(elty, 5)
dy, dA, dx = CuArray(y), CuArray(A), CuArray(x)
mul!(dy, f(dA), dx, Ts(1), Ts(2))
mul!(y, f(A), x, Ts(1), Ts(2))
@test Array(dy) ≈ y
end
@testset "hermitian" begin
y, A, x = rand(elty, 5), Hermitian(rand(elty, 5, 5)), rand(elty, 5)
dy, dA, dx = CuArray(y), Hermitian(CuArray(A)), CuArray(x)
mul!(dy, dA, dx)
mul!(y, A, x)
@test Array(dy) ≈ y
end
@testset "banded methods" begin
# bands
ku = 2
kl = 3
# generate banded matrix
A = rand(elty,m,n)
A = bandex(A,kl,ku)
# get packed format
Ab = band(A,kl,ku)
d_Ab = CuArray(Ab)
x = rand(elty,n)
d_x = CuArray(x)
@testset "gbmv!" begin
# test y = alpha*A*x + beta*y
y = rand(elty,m)
d_y = CuArray(y)
CUBLAS.gbmv!('N',m,kl,ku,alpha,d_Ab,d_x,beta,d_y)
BLAS.gbmv!('N',m,kl,ku,alpha,Ab,x,beta,y)
h_y = Array(d_y)
@test y ≈ h_y
# test y = alpha*transpose(A)*x + beta*y
x = rand(elty,n)
d_x = CuArray(x)
y = rand(elty,m)
d_y = CuArray(y)
CUBLAS.gbmv!('T',m,kl,ku,alpha,d_Ab,d_y,beta,d_x)
BLAS.gbmv!('T',m,kl,ku,alpha,Ab,y,beta,x)
h_x = Array(d_x)
@test x ≈ h_x
# test y = alpha*A'*x + beta*y
x = rand(elty,n)
d_x = CuArray(x)
y = rand(elty,m)
d_y = CuArray(y)
CUBLAS.gbmv!('C',m,kl,ku,alpha,d_Ab,d_y,beta,d_x)
BLAS.gbmv!('C',m,kl,ku,alpha,Ab,y,beta,x)
h_x = Array(d_x)
@test x ≈ h_x
# test alpha=1 version without y
d_y = CUBLAS.gbmv('N',m,kl,ku,d_Ab,d_x)
y = BLAS.gbmv('N',m,kl,ku,Ab,x)
h_y = Array(d_y)
@test y ≈ h_y
end
x = rand(elty,n)
d_x = CuArray(x)
@testset "gbmv" begin
# test y = alpha*A*x
d_y = CUBLAS.gbmv('N',m,kl,ku,alpha,d_Ab,d_x)
y = zeros(elty,m)
y = BLAS.gbmv('N',m,kl,ku,alpha,Ab,x)
h_y = Array(d_y)
@test y ≈ h_y
end
A = rand(elty,m,m)
A = A + A'
nbands = 3
@test m >= 1+nbands
A = bandex(A,nbands,nbands)
# convert to 'upper' banded storage format
AB = band(A,0,nbands)
# construct x
x = rand(elty,m)
d_AB = CuArray(AB)
d_x = CuArray(x)
if elty <: Real
@testset "sbmv!" begin
y = rand(elty,m)
d_y = CuArray(y)
# sbmv!
CUBLAS.sbmv!('U',nbands,alpha,d_AB,d_x,beta,d_y)
y = alpha*(A*x) + beta*y
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "sbmv" begin
d_y = CUBLAS.sbmv('U',nbands,d_AB,d_x)
y = A*x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
else
@testset "hbmv!" begin
y = rand(elty,m)
d_y = CuArray(y)
# hbmv!
CUBLAS.hbmv!('U',nbands,alpha,d_AB,d_x,beta,d_y)
y = alpha*(A*x) + beta*y
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "hbmv" begin
d_y = CUBLAS.hbmv('U',nbands,d_AB,d_x)
y = A*x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
end
# generate triangular matrix
A = rand(elty,m,m)
# restrict to 3 bands
nbands = 3
@test m >= 1+nbands
A = bandex(A,0,nbands)
# convert to 'upper' banded storage format
AB = band(A,0,nbands)
d_AB = CuArray(AB)
@testset "tbmv!" begin
y = rand(elty, m)
# move to host
d_y = CuArray(y)
# tbmv!
CUBLAS.tbmv!('U','N','N',nbands,d_AB,d_y)
y = A*y
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "tbmv" begin
# tbmv
d_y = CUBLAS.tbmv('U','N','N',nbands,d_AB,d_x)
y = A*x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "tbsv!" begin
d_y = copy(d_x)
#tbsv!
CUBLAS.tbsv!('U','N','N',nbands,d_AB,d_y)
y = A\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "tbsv" begin
d_y = CUBLAS.tbsv('U','N','N',nbands,d_AB,d_x)
y = A\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
end
A = rand(elty,m,n)
dA = CuArray(A)
sA = rand(elty,m,m)
sA = sA + transpose(sA)
dsA = CuArray(sA)
hA = rand(elty,m,m)
hA = hA + hA'
dhA = CuArray(hA)
x = rand(elty,m)
dx = CuArray(x)
function pack(A, uplo)
AP = Vector{elty}(undef, (n*(n+1))>>1)
k = 1
for j in 1:n
for i in (uplo==:L ? (j:n) : (1:j))
AP[k] = A[i,j]
k += 1
end
end
return AP
end
if elty in ["Float32", "Float64"]
# pack matrices
sAPU = pack(sA, :U)
dsAPU = CuVector(sAPU)
sAPL = pack(sA, :L)
dsAPL = CuVector(sAPL)
@testset "spmv!" begin
# generate vectors
y = rand(elty,m)
# copy to device
dy = CuArray(y)
# execute on host
BLAS.spmv!('U',alpha,sAPU,x,beta,y)
# execute on device
CUBLAS.spmv!('U',alpha,dsAPU,dx,beta,dy)
# compare results
hy = Array(dy)
@test y ≈ hy
# execute on host
BLAS.spmv!('U',alpha,sAPL,x,beta,y)
# execute on device
CUBLAS.spmv!('U',alpha,dsAPL,dx,beta,dy)
# compare results
hy = Array(dy)
@test y ≈ hy
end
@testset "spr!" begin
# execute on host
BLAS.spr!('U',alpha,x,sAPU)
# execute on device
CUBLAS.spr!('U',alpha,dx,dsAPU)
# compare results
hsAPU = Array(dsAPU)
@test sAPU ≈ hsAPU
# execute on host
BLAS.spr!('U',alpha,x,sAPL)
# execute on device
CUBLAS.spr!('U',alpha,dx,dsAPL)
# compare results
hAPL = Array(dAPL)
@test sAPL ≈ hAPL
end
end
@testset "symv!" begin
# generate vectors
y = rand(elty,m)
# copy to device
dy = CuArray(y)
# execute on host
BLAS.symv!('U',alpha,sA,x,beta,y)
# execute on device
CUBLAS.symv!('U',alpha,dsA,dx,beta,dy)
# compare results
hy = Array(dy)
@test y ≈ hy
end
@testset "symv" begin
y = BLAS.symv('U',sA,x)
# execute on device
dy = CUBLAS.symv('U',dsA,dx)
# compare results
hy = Array(dy)
@test y ≈ hy
end
if elty <: Complex
@testset "hemv!" begin
y = rand(elty,m)
dy = CuArray(y)
# execute on host
BLAS.hemv!('U',alpha,hA,x,beta,y)
# execute on device
CUBLAS.hemv!('U',alpha,dhA,dx,beta,dy)
# compare results
hy = Array(dy)
@test y ≈ hy
end
@testset "hemv" begin
y = BLAS.hemv('U',hA,x)
# execute on device
dy = CUBLAS.hemv('U',dhA,dx)
# compare results
hy = Array(dy)
@test y ≈ hy
end
end
A = triu(sA)
dA = CuArray(A)
@testset "trmv!" begin
d_y = copy(dx)
# execute trmv!
CUBLAS.trmv!('U','N','N',dA,d_y)
y = A*x
# compare
h_y = Array(d_y)
@test y ≈ h_y
@test_throws DimensionMismatch CUBLAS.trmv!('U','N','N',dA,CUDA.rand(elty,m+1))
end
@testset "trmv" begin
d_y = CUBLAS.trmv('U','N','N',dA,dx)
y = A*x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "lmul!(::UpperTriangular)" begin
dy = copy(dx)
lmul!(UpperTriangular(dA), dy)
y = UpperTriangular(A) * x
@test y ≈ Array(dy)
end
@testset "lmul!(::UpperTriangular{Adjoint})" begin
dy = copy(dx)
lmul!(adjoint(UpperTriangular(dA)), dy)
y = adjoint(UpperTriangular(A)) * x
@test y ≈ Array(dy)
end
@testset "lmul!(::UpperTriangular{Transpose})" begin
dy = copy(dx)
lmul!(transpose(UpperTriangular(dA)), dy)
y = transpose(UpperTriangular(A)) * x
@test y ≈ Array(dy)
end
@testset "lmul!(::LowerTriangular)" begin
dy = copy(dx)
lmul!(LowerTriangular(dA), dy)
y = LowerTriangular(A) * x
@test y ≈ Array(dy)
end
@testset "lmul!(::LowerTriangular{Adjoint})" begin
dy = copy(dx)
lmul!(adjoint(LowerTriangular(dA)), dy)
y = adjoint(LowerTriangular(A)) * x
@test y ≈ Array(dy)
end
@testset "lmul!(::LowerTriangular{Transpose})" begin
dy = copy(dx)
lmul!(transpose(LowerTriangular(dA)), dy)
y = transpose(LowerTriangular(A)) * x
@test y ≈ Array(dy)
end
@testset "trsv!" begin
d_y = copy(dx)
# execute trsv!
CUBLAS.trsv!('U','N','N',dA,d_y)
y = A\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
@test_throws DimensionMismatch CUBLAS.trsv!('U','N','N',dA,CUDA.rand(elty,m+1))
end
@testset "trsv" begin
d_y = CUBLAS.trsv('U','N','N',dA,dx)
y = A\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "trsv (adjoint)" begin
d_y = CUBLAS.trsv('U','C','N',dA,dx)
y = adjoint(A)\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "trsv (transpose)" begin
d_y = CUBLAS.trsv('U','T','N',dA,dx)
y = transpose(A)\x
# compare
h_y = Array(d_y)
@test y ≈ h_y
end
@testset "ldiv!(::UpperTriangular)" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(UpperTriangular(dA), dy)
y = UpperTriangular(A) \ x
@test y ≈ Array(dy)
end
@testset "ldiv!(::UpperTriangular{Adjoint})" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(adjoint(UpperTriangular(dA)), dy)
y = adjoint(UpperTriangular(A)) \ x
@test y ≈ Array(dy)
end
@testset "ldiv!(::UpperTriangular{Transpose})" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(transpose(UpperTriangular(dA)), dy)
y = transpose(UpperTriangular(A)) \ x
@test y ≈ Array(dy)
end
@testset "ldiv!(::LowerTriangular)" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(LowerTriangular(dA), dy)
y = LowerTriangular(A) \ x
@test y ≈ Array(dy)
end
@testset "ldiv!(::LowerTriangular{Adjoint})" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(adjoint(LowerTriangular(dA)), dy)
y = adjoint(LowerTriangular(A)) \ x
@test y ≈ Array(dy)
end
@testset "ldiv!(::LowerTriangular{Transpose})" begin
A = copy(sA)
dA = CuArray(A)
dy = copy(dx)
ldiv!(transpose(LowerTriangular(dA)), dy)
y = transpose(LowerTriangular(A)) \ x
@test y ≈ Array(dy)
end
@testset "inv($TR)" for TR in (UpperTriangular, LowerTriangular, UnitUpperTriangular, UnitLowerTriangular)
@test testf(x -> inv(TR(x)), rand(elty, m, m))
end
A = rand(elty,m,m)
x = rand(elty,m)
y = rand(elty,m)
dA = CuArray(A)
dx = CuArray(x)
dy = CuArray(y)
@testset "ger!" begin
# perform rank one update
dB = copy(dA)
CUBLAS.ger!(alpha,dx,dy,dB)
B = (alpha*x)*y' + A
# move to host and compare
hB = Array(dB)
@test B ≈ hB
end
@testset "syr!" begin
dB = copy(dsA)
CUBLAS.syr!('U',alpha,dx,dB)
B = (alpha*x)*transpose(x) + sA
# move to host and compare upper triangles
hB = Array(dB)
B = triu(B)
hB = triu(hB)
@test B ≈ hB
end
if elty <: Complex
@testset "her!" begin
dB = copy(dhA)
# perform rank one update
CUBLAS.her!('U',real(alpha),dx,dB)
B = (real(alpha)*x)*x' + hA
# move to host and compare upper triangles
hB = Array(dB)
B = triu(B)
hB = triu(hB)
@test B ≈ hB
end
@testset "her2!" begin
dB = copy(dhA)
CUBLAS.her2!('U',real(alpha),dx,dy,dB)
B = (real(alpha)*x)*y' + y*(real(alpha)*x)' + hA
# move to host and compare upper triangles
hB = Array(dB)
B = triu(B)
hB = triu(hB)
@test B ≈ hB
end
end
end
@testset "gemv! with strided inputs" begin # JuliaGPU/CUDA.jl#445
testf(rand(16), rand(4)) do p, b
W = @view p[reshape(1:(16),4,4)]
W*b
end
end
@testset "StaticArray eltype" begin
A = CuArray(rand(SVector{2, Float64}, 3, 3))
B = CuArray(rand(Float64, 3, 1))
C = A * B
hC = Array(A) * Array(B)
@test Array(C) ≈ hC
end
end
############################################################################################
@testset "level 3" begin
@testset for elty in [Float32, Float64, ComplexF32, ComplexF64]
alpha = rand(elty)
beta = rand(elty)
@testset "mul! C = $f(A) * $g(B) * $Ts(a) + C * $Ts(b)" for f in (identity, transpose, adjoint), g in (identity, transpose, adjoint), Ts in (Int, elty)
C, A, B = rand(elty, 5, 5), rand(elty, 5, 5), rand(elty, 5, 5)
dC, dA, dB = CuArray(C), CuArray(A), CuArray(B)
mul!(dC, f(dA), g(dB), Ts(1), Ts(2))
mul!(C, f(A), g(B), Ts(1), Ts(2))
@test Array(dC) ≈ C
end
@testset "hermitian" begin
C, A, B = rand(elty, 5, 5), Hermitian(rand(elty, 5, 5)), rand(elty, 5, 5)
dC, dA, dB = CuArray(C), Hermitian(CuArray(A)), CuArray(B)
mul!(dC, dA, dB)
mul!(C, A, B)
@test Array(dC) ≈ C
end
A = rand(elty,m,k)
B = rand(elty,k,n)
Bbad = rand(elty,k+1,n+1)
C1 = rand(elty,m,n)
C2 = copy(C1)
d_A = CuArray(A)
d_B = CuArray(B)
d_Bbad = CuArray(Bbad)
d_C1 = CuArray(C1)
d_C2 = CuArray(C2)
hA = rand(elty,m,m)
hA = hA + hA'
dhA = CuArray(hA)
sA = rand(elty,m,m)
sA = sA + transpose(sA)
dsA = CuArray(sA)
@testset "gemm!" begin
CUBLAS.gemm!('N','N',alpha,d_A,d_B,beta,d_C1)
mul!(d_C2, d_A, d_B)
h_C1 = Array(d_C1)
h_C2 = Array(d_C2)
C1 = (alpha*A)*B + beta*C1
C2 = A*B
# compare
@test C1 ≈ h_C1
@test C2 ≈ h_C2
@test_throws ArgumentError mul!(dhA, dhA, dsA)
@test_throws DimensionMismatch mul!(d_C1, d_A, dsA)
end
@testset "strided gemm!" begin
denseA = CUDA.rand(elty, 4,4)
denseB = CUDA.rand(elty, 4,4)
denseC = CUDA.zeros(elty, 4,4)
stridedA = view(denseA, 1:2, 1:2)::SubArray
stridedB = view(denseB, 1:2, 1:2)::SubArray
stridedC = view(denseC, 1:2, 1:2)::SubArray
CUBLAS.gemm!('N', 'N', true, stridedA, stridedB, false, stridedC)
@test Array(stridedC) ≈ Array(stridedA) * Array(stridedB)
stridedC .= 0
mul!(stridedC, stridedA, stridedB, true, false)
@test Array(stridedC) ≈ Array(stridedA) * Array(stridedB)
end
if capability(device()) > v"5.0"
@testset "gemmEx!" begin
α = rand(elty)
β = rand(elty)
CUBLAS.gemmEx!('N','N',α,d_A,d_B,β,d_C1)
h_C1 = Array(d_C1)
C1 = (α*A)*B + β*C1
# compare
@test C1 ≈ h_C1
end
end
@testset "gemm" begin
d_C = CUBLAS.gemm('N','N',d_A,d_B)
C = A*B
C2 = d_A * d_B
# compare
h_C = Array(d_C)
h_C2 = Array(C2)
@test C ≈ h_C
@test C ≈ h_C2
end
@testset "xt_gemm! gpu" begin
synchronize()
CUBLAS.xt_gemm!('N','N',alpha,d_A,d_B,beta,d_C1)
mul!(d_C2, d_A, d_B)
h_C1 = Array(d_C1)
h_C2 = Array(d_C2)
C1 = (alpha*A)*B + beta*C1
C2 = A*B
# compare
@test C1 ≈ h_C1
@test C2 ≈ h_C2
@test_throws DimensionMismatch CUBLAS.xt_gemm!('N','N',alpha,d_A,d_Bbad,beta,d_C1)
end
@testset "xt_gemm! cpu" begin
h_C1 = Array(d_C1)
CUBLAS.xt_gemm!('N','N',alpha,Array(d_A),Array(d_B),beta,h_C1)
mul!(d_C2, d_A, d_B)
h_C2 = Array(d_C2)
C1 = (alpha*A)*B + beta*C1
C2 = A*B
# compare
@test C1 ≈ h_C1
@test C2 ≈ h_C2
end
@testset "xt_gemm gpu" begin
synchronize()
d_C = CUBLAS.xt_gemm('N','N',d_A,d_B)
C = A*B
C2 = d_A * d_B
# compare
@test d_C isa CuArray
h_C = Array(d_C)
h_C2 = Array(C2)
@test C ≈ h_C
@test C ≈ h_C2
end
@testset "xt_gemm cpu" begin
h_C = CUBLAS.xt_gemm('N','N',Array(d_A),Array(d_B))
C = A*B
C2 = d_A * d_B
# compare
@test h_C isa Array
h_C2 = Array(C2)
@test C ≈ h_C
@test C ≈ h_C2
end
B = rand(elty,m,n)
C = rand(elty,m,n)
Bbad = rand(elty,m+1,n+1)
d_B = CuArray(B)
d_C = CuArray(C)
d_Bbad = CuArray(Bbad)
@testset "symm!" begin
CUBLAS.symm!('L','U',alpha,dsA,d_B,beta,d_C)
C = (alpha*sA)*B + beta*C
# compare
h_C = Array(d_C)
@test C ≈ h_C
@test_throws DimensionMismatch CUBLAS.symm!('L','U',alpha,dsA,d_Bbad,beta,d_C)
end
@testset "symm" begin
d_C = CUBLAS.symm('L','U',dsA,d_B)
C = sA*B
# compare
h_C = Array(d_C)
@test C ≈ h_C
@test_throws DimensionMismatch CUBLAS.symm('L','U',dsA,d_Bbad)
end
@testset "xt_symm! gpu" begin
synchronize()
CUBLAS.xt_symm!('L','U',alpha,dsA,d_B,beta,d_C)
C = (alpha*sA)*B + beta*C
# compare
h_C = Array(d_C)
@test C ≈ h_C
end
@testset "xt_symm! cpu" begin
h_C = Array(d_C)
CUBLAS.xt_symm!('L','U',alpha,Array(dsA),Array(d_B),beta,h_C)
C = (alpha*sA)*B + beta*C
# compare
@test C ≈ h_C
end
@testset "xt_symm gpu" begin
synchronize()
d_C = CUBLAS.xt_symm('L','U',dsA,d_B)
C = sA*B
# compare
@test d_C isa CuArray
h_C = Array(d_C)
@test C ≈ h_C
end
@testset "xt_symm cpu" begin
h_C = CUBLAS.xt_symm('L','U',Array(dsA),Array(d_B))
C = sA*B
# compare
@test h_C isa Array
@test C ≈ h_C
end
A = triu(rand(elty, m, m))
B = rand(elty,m,n)
C = zeros(elty,m,n)
dA = CuArray(A)
dB = CuArray(B)
dC = CuArray(C)
@testset "trmm!" begin
C = alpha*A*B
CUBLAS.trmm!('L','U','N','N',alpha,dA,dB,dC)
# move to host and compare
h_C = Array(dC)
@test C ≈ h_C
end
@testset "trmm" begin
C = alpha*A*B
d_C = CUBLAS.trmm('L','U','N','N',alpha,dA,dB)
# move to host and compare
h_C = Array(d_C)
@test C ≈ h_C
end
@testset "xt_trmm! gpu" begin
C = alpha*A*B
synchronize()
CUBLAS.xt_trmm!('L','U','N','N',alpha,dA,dB,dC)
# move to host and compare
h_C = Array(dC)
@test C ≈ h_C
end
@testset "xt_trmm! cpu" begin
C = alpha*A*B
h_C = Array(dC)
CUBLAS.xt_trmm!('L','U','N','N',alpha,Array(dA),Array(dB),h_C)
@test C ≈ h_C
end
@testset "xt_trmm gpu" begin
C = alpha*A*B
synchronize()
d_C = CUBLAS.xt_trmm('L','U','N','N',alpha,dA,dB)
# move to host and compare
@test d_C isa CuArray
h_C = Array(d_C)
@test C ≈ h_C
end
@testset "xt_trmm cpu" begin
C = alpha*A*B
h_C = CUBLAS.xt_trmm('L','U','N','N',alpha,Array(dA),Array(dB))
@test h_C isa Array
@test C ≈ h_C
end
@testset "xt_trsm! gpu" begin
C = alpha*(A\B)
dC = copy(dB)
synchronize()
CUBLAS.xt_trsm!('L','U','N','N',alpha,dA,dC)
# move to host and compare
h_C = Array(dC)
@test C ≈ h_C
end
@testset "xt_trsm! cpu" begin
C = alpha*(A\B)
dC = copy(dB)
h_C = Array(dC)
CUBLAS.xt_trsm!('L','U','N','N',alpha,Array(dA),h_C)
@test C ≈ h_C
end
@testset "xt_trsm gpu" begin
C = alpha*(A\B)
synchronize()
dC = CUBLAS.xt_trsm('L','U','N','N',alpha,dA,dB)
# move to host and compare
@test dC isa CuArray
h_C = Array(dC)
@test C ≈ h_C
end
@testset "xt_trsm cpu" begin
C = alpha*(A\B)
h_C = CUBLAS.xt_trsm('L','U','N','N',alpha,Array(dA),Array(dB))
@test h_C isa Array
@test C ≈ h_C
end
@testset "trsm" begin
Br = rand(elty,m,n)
Bl = rand(elty,n,m)
d_Br = CuArray(Br)
d_Bl = CuArray(Bl)
# compute
@testset "adjtype=$adjtype, uplotype=$uplotype" for
adjtype in (identity, adjoint, transpose),
uplotype in (UpperTriangular, UnitUpperTriangular, LowerTriangular, UnitLowerTriangular)
@test adjtype(uplotype(A))\Br ≈ Array(adjtype(uplotype(dA))\d_Br)
@test Bl/adjtype(uplotype(A)) ≈ Array(d_Bl/adjtype(uplotype(dA)))
end
# Check also that scaling parameter works
@test BLAS.trsm('L','U','N','N',alpha,A,Br) ≈ Array(CUBLAS.trsm('L','U','N','N',alpha,dA,d_Br))
end
@testset "trsm_batched!" begin
bA = [rand(elty,m,m) for i in 1:10]
map!((x) -> triu(x), bA, bA)
bB = [rand(elty,m,n) for i in 1:10]
bBbad = [rand(elty,m,n) for i in 1:9]
# move to device
bd_A = CuArray{elty, 2}[]
bd_B = CuArray{elty, 2}[]
bd_Bbad = CuArray{elty, 2}[]
for i in 1:length(bA)
push!(bd_A,CuArray(bA[i]))
push!(bd_B,CuArray(bB[i]))
end
for i in 1:length(bBbad)
push!(bd_Bbad,CuArray(bBbad[i]))
end
# compute
CUBLAS.trsm_batched!('L','U','N','N',alpha,bd_A,bd_B)
@test_throws DimensionMismatch CUBLAS.trsm_batched!('L','U','N','N',alpha,bd_A,bd_Bbad)
# move to host and compare
for i in 1:length(bd_B)
bC = alpha*(bA[i]\bB[i])
h_C = Array(bd_B[i])
#compare
@test bC ≈ h_C
end
end
@testset "trsm_batched" begin
# generate parameter alpha = rand(elty)
# generate matrices
bA = [rand(elty,m,m) for i in 1:10]
map!((x) -> triu(x), bA, bA)
bB = [rand(elty,m,n) for i in 1:10]
# move to device
bd_A = CuArray{elty, 2}[]
bd_B = CuArray{elty, 2}[]
for i in 1:length(bA)
push!(bd_A,CuArray(bA[i]))
push!(bd_B,CuArray(bB[i]))
end
# compute
bd_C = CUBLAS.trsm_batched('L','U','N','N',alpha,bd_A,bd_B)
# move to host and compare
for i in 1:length(bd_C)