-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathblas.jl
2140 lines (1901 loc) · 78.4 KB
/
blas.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
Interface to BLAS subroutines.
"""
module BLAS
import Base: copyto!
using Base: require_one_based_indexing, USE_BLAS64
export
# Note: `xFUNC_NAME` is a placeholder for not exported BLAS functions
# ref: http://www.netlib.org/blas/blasqr.pdf
# Level 1
# xROTG
# xROTMG
rot!,
# xROTM
# xSWAP
scal!,
scal,
blascopy!,
# xAXPY!,
# xAXPBY!,
# xDOT
dotc,
dotu,
# xxDOT
nrm2,
asum,
iamax,
# Level 2
gemv!,
gemv,
gbmv!,
gbmv,
hemv!,
hemv,
# xHBMV
hpmv!,
symv!,
symv,
sbmv!,
sbmv,
spmv!,
trmv!,
trmv,
# xTBMV
# xTPMV
trsv!,
trsv,
# xTBSV
# xTPSV
ger!,
# xGERU
# xGERC
her!,
# xHPR
# xHER2
# xHPR2
syr!,
spr!,
# xSYR2
# xSPR2
# Level 3
gemm!,
gemm,
symm!,
symm,
hemm!,
hemm,
syrk!,
syrk,
herk!,
herk,
syr2k!,
syr2k,
her2k!,
her2k,
trmm!,
trmm,
trsm!,
trsm
using ..LinearAlgebra: libblastrampoline, BlasReal, BlasComplex, BlasFloat, BlasInt, DimensionMismatch, checksquare, stride1, chkstride1
include("lbt.jl")
# Legacy bindings that some packages (such as NNlib.jl) use.
# We maintain these for backwards-compatibility but new packages
# should not look at these, instead preferring to parse the output
# of BLAS.get_config()
const libblas = libblastrampoline
const liblapack = libblastrampoline
vendor() = :lbt
"""
get_config()
Return an object representing the current `libblastrampoline` configuration.
!!! compat "Julia 1.7"
`get_config()` requires at least Julia 1.7.
"""
get_config() = lbt_get_config()
if USE_BLAS64
macro blasfunc(x)
return Expr(:quote, Symbol(x, "64_"))
end
else
macro blasfunc(x)
return Expr(:quote, x)
end
end
_tryparse_env_int(key) = tryparse(Int, get(ENV, key, ""))
"""
set_num_threads(n::Integer)
set_num_threads(::Nothing)
Set the number of threads the BLAS library should use equal to `n::Integer`.
Also accepts `nothing`, in which case julia tries to guess the default number of threads.
Passing `nothing` is discouraged and mainly exists for historical reasons.
"""
set_num_threads(nt::Integer)::Nothing = lbt_set_num_threads(Int32(nt))
function set_num_threads(::Nothing)
nt = something(
_tryparse_env_int("OPENBLAS_NUM_THREADS"),
_tryparse_env_int("OMP_NUM_THREADS"),
_tryparse_env_int("VECLIB_MAXIMUM_THREADS"),
max(1, Sys.CPU_THREADS ÷ 2),
)
return set_num_threads(nt)
end
"""
get_num_threads()
Get the number of threads the BLAS library is using.
!!! compat "Julia 1.6"
`get_num_threads` requires at least Julia 1.6.
"""
get_num_threads()::Int = lbt_get_num_threads()
function check()
# TODO: once we have bitfields of the BLAS functions that are actually forwarded,
# ensure that we have a complete set here (warning on an incomplete BLAS implementation)
config = get_config()
# Ensure that one of our loaded libraries satisfies our interface requirement
interface = USE_BLAS64 ? :ilp64 : :lp64
if !any(lib.interface == interface for lib in config.loaded_libs)
interfacestr = uppercase(string(interface))
@error("No loaded BLAS libraries were built with $(interfacestr) support")
println("Quitting.")
exit()
end
end
"Check that upper/lower (for special matrices) is correctly specified"
function chkuplo(uplo::AbstractChar)
if !(uplo == 'U' || uplo == 'L')
throw(ArgumentError(lazy"uplo argument must be 'U' (upper) or 'L' (lower), got $uplo"))
end
uplo
end
# Level 1
# A help function to pick the pointer and inc for 1d like inputs.
@inline function vec_pointer_stride(x::AbstractArray, stride0check = nothing)
Base._checkcontiguous(Bool, x) && return pointer(x), 1 # simplify runtime check when possible
st, ptr = checkedstride(x), pointer(x)
isnothing(stride0check) || (st == 0 && throw(stride0check))
ptr += min(st, 0) * sizeof(eltype(x)) * (length(x) - 1)
ptr, st
end
function checkedstride(x::AbstractArray)
szs::Dims = size(x)
sts::Dims = strides(x)
_, st, n = Base.merge_adjacent_dim(szs, sts)
n === ndims(x) && return st
throw(ArgumentError("only support vector like inputs"))
end
## copy
"""
blascopy!(n, X, incx, Y, incy)
Copy `n` elements of array `X` with stride `incx` to array `Y` with stride `incy`. Returns `Y`.
"""
function blascopy! end
for (fname, elty) in ((:dcopy_,:Float64),
(:scopy_,:Float32),
(:zcopy_,:ComplexF64),
(:ccopy_,:ComplexF32))
@eval begin
# SUBROUTINE DCOPY(N,DX,INCX,DY,INCY)
function blascopy!(n::Integer, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer, DY::Union{Ptr{$elty},AbstractArray{$elty}}, incy::Integer)
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}),
n, DX, incx, DY, incy)
DY
end
end
end
## rot
"""
rot!(n, X, incx, Y, incy, c, s)
Overwrite `X` with `c*X + s*Y` and `Y` with `-conj(s)*X + c*Y` for the first `n` elements of array `X` with stride `incx` and
first `n` elements of array `Y` with stride `incy`. Returns `X` and `Y`.
!!! compat "Julia 1.5"
`rot!` requires at least Julia 1.5.
"""
function rot! end
for (fname, elty, cty, sty, lib) in ((:drot_, :Float64, :Float64, :Float64, libblastrampoline),
(:srot_, :Float32, :Float32, :Float32, libblastrampoline),
(:zdrot_, :ComplexF64, :Float64, :Float64, libblastrampoline),
(:csrot_, :ComplexF32, :Float32, :Float32, libblastrampoline),
(:zrot_, :ComplexF64, :Float64, :ComplexF64, libblastrampoline),
(:crot_, :ComplexF32, :Float32, :ComplexF32, libblastrampoline))
@eval begin
# SUBROUTINE DROT(N,DX,INCX,DY,INCY,C,S)
function rot!(n::Integer, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer, DY::Union{Ptr{$elty},AbstractArray{$elty}}, incy::Integer, C::$cty, S::$sty)
ccall((@blasfunc($fname), $lib), Cvoid,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ref{$cty}, Ref{$sty}),
n, DX, incx, DY, incy, C, S)
DX, DY
end
end
end
## scal
"""
scal!(n, a, X, incx)
scal!(a, X)
Overwrite `X` with `a*X` for the first `n` elements of array `X` with stride `incx`. Returns `X`.
If `n` and `incx` are not provided, `length(X)` and `stride(X,1)` are used.
"""
function scal! end
"""
scal(n, a, X, incx)
scal(a, X)
Return `X` scaled by `a` for the first `n` elements of array `X` with stride `incx`.
If `n` and `incx` are not provided, `length(X)` and `stride(X,1)` are used.
"""
function scal end
for (fname, elty) in ((:dscal_,:Float64),
(:sscal_,:Float32),
(:zscal_,:ComplexF64),
(:cscal_,:ComplexF32))
@eval begin
# SUBROUTINE DSCAL(N,DA,DX,INCX)
function scal!(n::Integer, DA::$elty, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer)
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{BlasInt}, Ref{$elty}, Ptr{$elty}, Ref{BlasInt}),
n, DA, DX, incx)
DX
end
function scal!(DA::$elty, DX::AbstractArray{$elty})
p, st = vec_pointer_stride(DX, ArgumentError("dest vector with 0 stride is not allowed"))
GC.@preserve DX scal!(length(DX), DA, p, abs(st))
DX
end
end
end
scal(n, DA, DX, incx) = scal!(n, DA, copy(DX), incx)
scal(DA, DX) = scal!(DA, copy(DX))
## dot
"""
dot(n, X, incx, Y, incy)
Dot product of two vectors consisting of `n` elements of array `X` with stride `incx` and
`n` elements of array `Y` with stride `incy`.
# Examples
```jldoctest
julia> BLAS.dot(10, fill(1.0, 10), 1, fill(1.0, 20), 2)
10.0
```
"""
function dot end
"""
dotc(n, X, incx, U, incy)
Dot function for two complex vectors, consisting of `n` elements of array `X`
with stride `incx` and `n` elements of array `U` with stride `incy`,
conjugating the first vector.
# Examples
```jldoctest
julia> BLAS.dotc(10, fill(1.0im, 10), 1, fill(1.0+im, 20), 2)
10.0 - 10.0im
```
"""
function dotc end
"""
dotu(n, X, incx, Y, incy)
Dot function for two complex vectors consisting of `n` elements of array `X`
with stride `incx` and `n` elements of array `Y` with stride `incy`.
# Examples
```jldoctest
julia> BLAS.dotu(10, fill(1.0im, 10), 1, fill(1.0+im, 20), 2)
-10.0 + 10.0im
```
"""
function dotu end
for (fname, elty) in ((:cblas_ddot,:Float64),
(:cblas_sdot,:Float32))
@eval begin
# DOUBLE PRECISION FUNCTION DDOT(N,DX,INCX,DY,INCY)
# * .. Scalar Arguments ..
# INTEGER INCX,INCY,N
# * ..
# * .. Array Arguments ..
# DOUBLE PRECISION DX(*),DY(*)
function dot(n::Integer, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer, DY::Union{Ptr{$elty},AbstractArray{$elty}}, incy::Integer)
ccall((@blasfunc($fname), libblastrampoline), $elty,
(BlasInt, Ptr{$elty}, BlasInt, Ptr{$elty}, BlasInt),
n, DX, incx, DY, incy)
end
end
end
for (fname, elty) in ((:cblas_zdotc_sub,:ComplexF64),
(:cblas_cdotc_sub,:ComplexF32))
@eval begin
# DOUBLE PRECISION FUNCTION DDOT(N,DX,INCX,DY,INCY)
# * .. Scalar Arguments ..
# INTEGER INCX,INCY,N
# * ..
# * .. Array Arguments ..
# DOUBLE PRECISION DX(*),DY(*)
function dotc(n::Integer, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer, DY::Union{Ptr{$elty},AbstractArray{$elty}}, incy::Integer)
result = Ref{$elty}()
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(BlasInt, Ptr{$elty}, BlasInt, Ptr{$elty}, BlasInt, Ptr{$elty}),
n, DX, incx, DY, incy, result)
result[]
end
end
end
for (fname, elty) in ((:cblas_zdotu_sub,:ComplexF64),
(:cblas_cdotu_sub,:ComplexF32))
@eval begin
# DOUBLE PRECISION FUNCTION DDOT(N,DX,INCX,DY,INCY)
# * .. Scalar Arguments ..
# INTEGER INCX,INCY,N
# * ..
# * .. Array Arguments ..
# DOUBLE PRECISION DX(*),DY(*)
function dotu(n::Integer, DX::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer, DY::Union{Ptr{$elty},AbstractArray{$elty}}, incy::Integer)
result = Ref{$elty}()
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(BlasInt, Ptr{$elty}, BlasInt, Ptr{$elty}, BlasInt, Ptr{$elty}),
n, DX, incx, DY, incy, result)
result[]
end
end
end
for (elty, f) in ((Float32, :dot), (Float64, :dot),
(ComplexF32, :dotc), (ComplexF64, :dotc),
(ComplexF32, :dotu), (ComplexF64, :dotu))
@eval begin
function $f(x::AbstractArray{$elty}, y::AbstractArray{$elty})
n, m = length(x), length(y)
n == m || throw(DimensionMismatch(lazy"dot product arguments have lengths $n and $m"))
GC.@preserve x y $f(n, vec_pointer_stride(x)..., vec_pointer_stride(y)...)
end
end
end
## nrm2
"""
nrm2(n, X, incx)
2-norm of a vector consisting of `n` elements of array `X` with stride `incx`.
# Examples
```jldoctest
julia> BLAS.nrm2(4, fill(1.0, 8), 2)
2.0
julia> BLAS.nrm2(1, fill(1.0, 8), 2)
1.0
```
"""
function nrm2 end
for (fname, elty, ret_type) in ((:dnrm2_,:Float64,:Float64),
(:snrm2_,:Float32,:Float32),
(:dznrm2_,:ComplexF64,:Float64),
(:scnrm2_,:ComplexF32,:Float32))
@eval begin
# SUBROUTINE DNRM2(N,X,INCX)
function nrm2(n::Integer, X::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer)
ccall((@blasfunc($fname), libblastrampoline), $ret_type,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}),
n, X, incx)
end
end
end
# openblas returns 0 for negative stride
function nrm2(x::AbstractArray)
p, st = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
GC.@preserve x nrm2(length(x), p, abs(st))
end
## asum
"""
asum(n, X, incx)
Sum of the magnitudes of the first `n` elements of array `X` with stride `incx`.
For a real array, the magnitude is the absolute value. For a complex array, the
magnitude is the sum of the absolute value of the real part and the absolute value
of the imaginary part.
# Examples
```jldoctest
julia> BLAS.asum(5, fill(1.0im, 10), 2)
5.0
julia> BLAS.asum(2, fill(1.0im, 10), 5)
2.0
```
"""
function asum end
for (fname, elty, ret_type) in ((:dasum_,:Float64,:Float64),
(:sasum_,:Float32,:Float32),
(:dzasum_,:ComplexF64,:Float64),
(:scasum_,:ComplexF32,:Float32))
@eval begin
# SUBROUTINE ASUM(N, X, INCX)
function asum(n::Integer, X::Union{Ptr{$elty},AbstractArray{$elty}}, incx::Integer)
ccall((@blasfunc($fname), libblastrampoline), $ret_type,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}),
n, X, incx)
end
end
end
function asum(x::AbstractArray)
p, st = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
GC.@preserve x asum(length(x), p, abs(st))
end
## axpy
"""
axpy!(a, X, Y)
Overwrite `Y` with `X*a + Y`, where `a` is a scalar. Return `Y`.
# Examples
```jldoctest
julia> x = [1.; 2; 3];
julia> y = [4. ;; 5 ;; 6];
julia> BLAS.axpy!(2, x, y)
1×3 Matrix{Float64}:
6.0 9.0 12.0
```
"""
function axpy! end
for (fname, elty) in ((:daxpy_,:Float64),
(:saxpy_,:Float32),
(:zaxpy_,:ComplexF64),
(:caxpy_,:ComplexF32))
@eval begin
# SUBROUTINE DAXPY(N,DA,DX,INCX,DY,INCY)
# DY <- DA*DX + DY
#* .. Scalar Arguments ..
# DOUBLE PRECISION DA
# INTEGER INCX,INCY,N
#* .. Array Arguments ..
# DOUBLE PRECISION DX(*),DY(*)
function axpy!(n::Integer, alpha::($elty), dx::Union{Ptr{$elty}, AbstractArray{$elty}}, incx::Integer, dy::Union{Ptr{$elty}, AbstractArray{$elty}}, incy::Integer)
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{BlasInt}, Ref{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}),
n, alpha, dx, incx, dy, incy)
dy
end
end
end
function axpy!(alpha::Number, x::AbstractArray{T}, y::AbstractArray{T}) where T<:BlasFloat
if length(x) != length(y)
throw(DimensionMismatch(lazy"x has length $(length(x)), but y has length $(length(y))"))
end
GC.@preserve x y axpy!(length(x), T(alpha), vec_pointer_stride(x)...,
vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))...)
y
end
function axpy!(alpha::Number, x::Array{T}, rx::AbstractRange{Ti},
y::Array{T}, ry::AbstractRange{Ti}) where {T<:BlasFloat,Ti<:Integer}
if length(rx) != length(ry)
throw(DimensionMismatch("ranges of differing lengths"))
end
if minimum(rx) < 1 || maximum(rx) > length(x)
throw(ArgumentError(lazy"range out of bounds for x, of length $(length(x))"))
end
if minimum(ry) < 1 || maximum(ry) > length(y)
throw(ArgumentError(lazy"range out of bounds for y, of length $(length(y))"))
end
GC.@preserve x y axpy!(
length(rx),
T(alpha),
pointer(x, minimum(rx)),
step(rx),
pointer(y, minimum(ry)),
step(ry))
return y
end
"""
axpby!(a, X, b, Y)
Overwrite `Y` with `X*a + Y*b`, where `a` and `b` are scalars. Return `Y`.
# Examples
```jldoctest
julia> x = [1., 2, 3];
julia> y = [4., 5, 6];
julia> BLAS.axpby!(2., x, 3., y)
3-element Vector{Float64}:
14.0
19.0
24.0
```
"""
function axpby! end
for (fname, elty) in ((:daxpby_,:Float64), (:saxpby_,:Float32),
(:zaxpby_,:ComplexF64), (:caxpby_,:ComplexF32))
@eval begin
# SUBROUTINE DAXPBY(N,DA,DX,INCX,DB,DY,INCY)
# DY <- DA*DX + DB*DY
#* .. Scalar Arguments ..
# DOUBLE PRECISION DA,DB
# INTEGER INCX,INCY,N
#* .. Array Arguments ..
# DOUBLE PRECISION DX(*),DY(*)
function axpby!(n::Integer, alpha::($elty), dx::Union{Ptr{$elty},
AbstractArray{$elty}}, incx::Integer, beta::($elty),
dy::Union{Ptr{$elty}, AbstractArray{$elty}}, incy::Integer)
ccall((@blasfunc($fname), libblastrampoline), Cvoid, (Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
Ref{BlasInt}, Ref{$elty}, Ptr{$elty}, Ref{BlasInt}),
n, alpha, dx, incx, beta, dy, incy)
dy
end
end
end
function axpby!(alpha::Number, x::AbstractArray{T}, beta::Number, y::AbstractArray{T}) where T<:BlasFloat
require_one_based_indexing(x, y)
if length(x) != length(y)
throw(DimensionMismatch(lazy"x has length $(length(x)), but y has length $(length(y))"))
end
GC.@preserve x y axpby!(length(x), T(alpha), vec_pointer_stride(x)..., T(beta),
vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))...)
y
end
## iamax
for (fname, elty) in ((:idamax_,:Float64),
(:isamax_,:Float32),
(:izamax_,:ComplexF64),
(:icamax_,:ComplexF32))
@eval begin
function iamax(n::Integer, dx::Union{Ptr{$elty}, AbstractArray{$elty}}, incx::Integer)
ccall((@blasfunc($fname), libblastrampoline),BlasInt,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}),
n, dx, incx)
end
end
end
function iamax(dx::AbstractArray)
p, st = vec_pointer_stride(dx)
st <= 0 && return BlasInt(0)
iamax(length(dx), p, st)
end
"""
iamax(n, dx, incx)
iamax(dx)
Find the index of the element of `dx` with the maximum absolute value. `n` is the length of `dx`, and `incx` is the
stride. If `n` and `incx` are not provided, they assume default values of `n=length(dx)` and `incx=stride1(dx)`.
"""
iamax
# Level 2
## mv
### gemv
for (fname, elty) in ((:dgemv_,:Float64),
(:sgemv_,:Float32),
(:zgemv_,:ComplexF64),
(:cgemv_,:ComplexF32))
@eval begin
#SUBROUTINE DGEMV(TRANS,M,N,ALPHA,A,LDA,X,INCX,BETA,Y,INCY)
#* .. Scalar Arguments ..
# DOUBLE PRECISION ALPHA,BETA
# INTEGER INCX,INCY,LDA,M,N
# CHARACTER TRANS
#* .. Array Arguments ..
# DOUBLE PRECISION A(LDA,*),X(*),Y(*)
function gemv!(trans::AbstractChar, alpha::Union{($elty), Bool},
A::AbstractVecOrMat{$elty}, X::AbstractVector{$elty},
beta::Union{($elty), Bool}, Y::AbstractVector{$elty})
require_one_based_indexing(A, X, Y)
m,n = size(A,1),size(A,2)
if trans == 'N' && (length(X) != n || length(Y) != m)
throw(DimensionMismatch(lazy"A has dimensions $(size(A)), X has length $(length(X)) and Y has length $(length(Y))"))
elseif trans == 'C' && (length(X) != m || length(Y) != n)
throw(DimensionMismatch(lazy"the adjoint of A has dimensions $n, $m, X has length $(length(X)) and Y has length $(length(Y))"))
elseif trans == 'T' && (length(X) != m || length(Y) != n)
throw(DimensionMismatch(lazy"the transpose of A has dimensions $n, $m, X has length $(length(X)) and Y has length $(length(Y))"))
end
chkstride1(A)
lda = stride(A,2)
pX, sX = vec_pointer_stride(X, ArgumentError("input vector with 0 stride is not allowed"))
pY, sY = vec_pointer_stride(Y, ArgumentError("dest vector with 0 stride is not allowed"))
pA = pointer(A)
if lda < 0
pA += (size(A, 2) - 1) * lda * sizeof($elty)
lda = -lda
trans == 'N' ? (sX = -sX) : (sY = -sY)
end
lda >= size(A,1) || size(A,2) <= 1 || error("when `size(A,2) > 1`, `abs(stride(A,2))` must be at least `size(A,1)`")
lda = max(1, size(A,1), lda)
GC.@preserve A X Y ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ref{$elty},
Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ref{$elty}, Ptr{$elty}, Ref{BlasInt}, Clong),
trans, size(A,1), size(A,2), alpha,
pA, lda, pX, sX,
beta, pY, sY, 1)
Y
end
function gemv(trans::AbstractChar, alpha::($elty), A::AbstractMatrix{$elty}, X::AbstractVector{$elty})
gemv!(trans, alpha, A, X, zero($elty), similar(X, $elty, size(A, (trans == 'N' ? 1 : 2))))
end
function gemv(trans::AbstractChar, A::AbstractMatrix{$elty}, X::AbstractVector{$elty})
gemv!(trans, one($elty), A, X, zero($elty), similar(X, $elty, size(A, (trans == 'N' ? 1 : 2))))
end
end
end
"""
gemv!(tA, alpha, A, x, beta, y)
Update the vector `y` as `alpha*A*x + beta*y` or `alpha*A'x + beta*y`
according to [`tA`](@ref stdlib-blas-trans).
`alpha` and `beta` are scalars. Return the updated `y`.
"""
gemv!
"""
gemv(tA, alpha, A, x)
Return `alpha*A*x` or `alpha*A'x` according to [`tA`](@ref stdlib-blas-trans).
`alpha` is a scalar.
"""
gemv(tA, alpha, A, x)
"""
gemv(tA, A, x)
Return `A*x` or `A'x` according to [`tA`](@ref stdlib-blas-trans).
"""
gemv(tA, A, x)
### (GB) general banded matrix-vector multiplication
"""
gbmv!(trans, m, kl, ku, alpha, A, x, beta, y)
Update vector `y` as `alpha*A*x + beta*y` or `alpha*A'*x + beta*y` according to [`trans`](@ref stdlib-blas-trans).
The matrix `A` is a general band matrix of dimension `m` by `size(A,2)` with `kl`
sub-diagonals and `ku` super-diagonals. `alpha` and `beta` are scalars. Return the updated `y`.
"""
function gbmv! end
"""
gbmv(trans, m, kl, ku, alpha, A, x)
Return `alpha*A*x` or `alpha*A'*x` according to [`trans`](@ref stdlib-blas-trans).
The matrix `A` is a general band matrix of dimension `m` by `size(A,2)` with `kl` sub-diagonals and `ku`
super-diagonals, and `alpha` is a scalar.
"""
function gbmv end
for (fname, elty) in ((:dgbmv_,:Float64),
(:sgbmv_,:Float32),
(:zgbmv_,:ComplexF64),
(:cgbmv_,:ComplexF32))
@eval begin
# SUBROUTINE DGBMV(TRANS,M,N,KL,KU,ALPHA,A,LDA,X,INCX,BETA,Y,INCY)
# * .. Scalar Arguments ..
# DOUBLE PRECISION ALPHA,BETA
# INTEGER INCX,INCY,KL,KU,LDA,M,N
# CHARACTER TRANS
# * .. Array Arguments ..
# DOUBLE PRECISION A(LDA,*),X(*),Y(*)
function gbmv!(trans::AbstractChar, m::Integer, kl::Integer, ku::Integer,
alpha::Union{($elty), Bool}, A::AbstractMatrix{$elty},
x::AbstractVector{$elty}, beta::Union{($elty), Bool},
y::AbstractVector{$elty})
require_one_based_indexing(A, x, y)
chkstride1(A)
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
py, sty = vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))
GC.@preserve x y ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt},
Ref{BlasInt}, Ref{$elty}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
Ref{BlasInt}, Clong),
trans, m, size(A,2), kl,
ku, alpha, A, max(1,stride(A,2)),
px, stx, beta, py, sty, 1)
y
end
function gbmv(trans::AbstractChar, m::Integer, kl::Integer, ku::Integer, alpha::($elty), A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
n = size(A,2)
leny = trans == 'N' ? m : n
gbmv!(trans, m, kl, ku, alpha, A, x, zero($elty), similar(x, $elty, leny))
end
function gbmv(trans::AbstractChar, m::Integer, kl::Integer, ku::Integer, A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
gbmv(trans, m, kl, ku, one($elty), A, x)
end
end
end
### symv
"""
symv!(ul, alpha, A, x, beta, y)
Update the vector `y` as `alpha*A*x + beta*y`. `A` is assumed to be symmetric.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
`alpha` and `beta` are scalars. Return the updated `y`.
"""
function symv! end
for (fname, elty, lib) in ((:dsymv_,:Float64,libblastrampoline),
(:ssymv_,:Float32,libblastrampoline),
(:zsymv_,:ComplexF64,libblastrampoline),
(:csymv_,:ComplexF32,libblastrampoline))
# Note that the complex symv are not BLAS but auiliary functions in LAPACK
@eval begin
# SUBROUTINE DSYMV(UPLO,N,ALPHA,A,LDA,X,INCX,BETA,Y,INCY)
# .. Scalar Arguments ..
# DOUBLE PRECISION ALPHA,BETA
# INTEGER INCX,INCY,LDA,N
# CHARACTER UPLO
# .. Array Arguments ..
# DOUBLE PRECISION A(LDA,*),X(*),Y(*)
function symv!(uplo::AbstractChar, alpha::Union{($elty), Bool},
A::AbstractMatrix{$elty}, x::AbstractVector{$elty},
beta::Union{($elty), Bool}, y::AbstractVector{$elty})
chkuplo(uplo)
require_one_based_indexing(A, x, y)
m, n = size(A)
if m != n
throw(DimensionMismatch(lazy"matrix A is $m by $n but must be square"))
end
if n != length(x)
throw(DimensionMismatch(lazy"A has size $(size(A)), and x has length $(length(x))"))
end
if m != length(y)
throw(DimensionMismatch(lazy"A has size $(size(A)), and y has length $(length(y))"))
end
chkstride1(A)
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
py, sty = vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))
GC.@preserve x y ccall((@blasfunc($fname), $lib), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ref{$elty},
Ptr{$elty}, Ref{BlasInt}, Clong),
uplo, n, alpha, A,
max(1,stride(A,2)), px, stx, beta,
py, sty, 1)
y
end
function symv(uplo::AbstractChar, alpha::($elty), A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
symv!(uplo, alpha, A, x, zero($elty), similar(x))
end
function symv(uplo::AbstractChar, A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
symv(uplo, one($elty), A, x)
end
end
end
"""
symv(ul, alpha, A, x)
Return `alpha*A*x`. `A` is assumed to be symmetric.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
`alpha` is a scalar.
"""
symv(ul, alpha, A, x)
"""
symv(ul, A, x)
Return `A*x`. `A` is assumed to be symmetric.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
"""
symv(ul, A, x)
### hemv
"""
hemv!(ul, alpha, A, x, beta, y)
Update the vector `y` as `alpha*A*x + beta*y`. `A` is assumed to be Hermitian.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
`alpha` and `beta` are scalars. Return the updated `y`.
"""
function hemv! end
for (fname, elty) in ((:zhemv_,:ComplexF64),
(:chemv_,:ComplexF32))
@eval begin
function hemv!(uplo::AbstractChar, α::Union{$elty, Bool}, A::AbstractMatrix{$elty}, x::AbstractVector{$elty}, β::Union{$elty, Bool}, y::AbstractVector{$elty})
chkuplo(uplo)
require_one_based_indexing(A, x, y)
m, n = size(A)
if m != n
throw(DimensionMismatch(lazy"matrix A is $m by $n but must be square"))
end
if n != length(x)
throw(DimensionMismatch(lazy"A has size $(size(A)), and x has length $(length(x))"))
end
if m != length(y)
throw(DimensionMismatch(lazy"A has size $(size(A)), and y has length $(length(y))"))
end
chkstride1(A)
lda = max(1, stride(A, 2))
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
py, sty = vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))
GC.@preserve x y ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ref{$elty},
Ptr{$elty}, Ref{BlasInt}, Clong),
uplo, n, α, A,
lda, px, stx, β,
py, sty, 1)
y
end
function hemv(uplo::AbstractChar, α::($elty), A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
hemv!(uplo, α, A, x, zero($elty), similar(x))
end
function hemv(uplo::AbstractChar, A::AbstractMatrix{$elty}, x::AbstractVector{$elty})
hemv(uplo, one($elty), A, x)
end
end
end
"""
hemv(ul, alpha, A, x)
Return `alpha*A*x`. `A` is assumed to be Hermitian.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
`alpha` is a scalar.
"""
hemv(ul, alpha, A, x)
"""
hemv(ul, A, x)
Return `A*x`. `A` is assumed to be Hermitian.
Only the [`ul`](@ref stdlib-blas-uplo) triangle of `A` is used.
"""
hemv(ul, A, x)
### hpmv!, (HP) Hermitian packed matrix-vector operation defined as y := alpha*A*x + beta*y.
for (fname, elty) in ((:zhpmv_, :ComplexF64),
(:chpmv_, :ComplexF32))
@eval begin
# SUBROUTINE ZHPMV(UPLO,N,ALPHA,AP,X,INCX,BETA,Y,INCY)
# Y <- ALPHA*AP*X + BETA*Y
# * .. Scalar Arguments ..
# DOUBLE PRECISION ALPHA,BETA
# INTEGER INCX,INCY,N
# CHARACTER UPLO
# * .. Array Arguments ..
# DOUBLE PRECISION A(N,N),X(N),Y(N)
function hpmv!(uplo::AbstractChar,
n::Integer,
α::$elty,
AP::Union{Ptr{$elty}, AbstractArray{$elty}},
x::Union{Ptr{$elty}, AbstractArray{$elty}},
incx::Integer,
β::$elty,
y::Union{Ptr{$elty}, AbstractArray{$elty}},
incy::Integer)
ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{UInt8}, # uplo,
Ref{BlasInt}, # n,
Ref{$elty}, # α,
Ptr{$elty}, # AP,
Ptr{$elty}, # x,
Ref{BlasInt}, # incx,
Ref{$elty}, # β,
Ptr{$elty}, # y, output
Ref{BlasInt}, # incy
Clong), # length of uplo
uplo,
n,
α,
AP,
x,
incx,
β,
y,
incy,
1)
return y
end
end
end
function hpmv!(uplo::AbstractChar,
α::Number, AP::AbstractArray{T}, x::AbstractArray{T},
β::Number, y::AbstractArray{T}) where {T <: BlasComplex}
require_one_based_indexing(AP, x, y)
N = length(x)
if N != length(y)
throw(DimensionMismatch(lazy"x has length $(N), but y has length $(length(y))"))
end
if 2*length(AP) < N*(N + 1)
throw(DimensionMismatch(lazy"Packed hermitian matrix A has size smaller than length(x) = $(N)."))
end
chkstride1(AP)
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
py, sty = vec_pointer_stride(y, ArgumentError("dest vector with 0 stride is not allowed"))
GC.@preserve x y hpmv!(uplo, N, T(α), AP, px, stx, T(β), py, sty)
y
end
"""
hpmv!(uplo, α, AP, x, β, y)
Update vector `y` as `α*A*x + β*y`, where `A` is a Hermitian matrix provided
in packed format `AP`.
With `uplo = 'U'`, the array AP must contain the upper triangular part of the
Hermitian matrix packed sequentially, column by column, so that `AP[1]`
contains `A[1, 1]`, `AP[2]` and `AP[3]` contain `A[1, 2]` and `A[2, 2]`
respectively, and so on.
With `uplo = 'L'`, the array AP must contain the lower triangular part of the
Hermitian matrix packed sequentially, column by column, so that `AP[1]`
contains `A[1, 1]`, `AP[2]` and `AP[3]` contain `A[2, 1]` and `A[3, 1]`
respectively, and so on.
The scalar inputs `α` and `β` must be complex or real numbers.
The array inputs `x`, `y` and `AP` must all be of `ComplexF32` or `ComplexF64` type.
Return the updated `y`.
!!! compat "Julia 1.5"