Skip to content

Commit

Permalink
Ignore 1-sized dimension during vector layout check.
Browse files Browse the repository at this point in the history
Close #44497
  • Loading branch information
N5N3 committed Mar 9, 2022
1 parent 811e534 commit 777910d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 16 additions & 3 deletions stdlib/LinearAlgebra/src/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,22 @@ end
# A help function to pick the pointer and inc for 1d like inputs.
@inline function vec_pointer_stride(x::AbstractArray, stride0check = nothing)
isdense(x) && return pointer(x), 1 # simpify runtime check when possibe
ndims(x) == 1 || strides(x) == Base.size_to_strides(stride(x, 1), size(x)...) ||
throw(ArgumentError("only support vector like inputs"))
st = stride(x, 1)
if ndims(x) <= 1
st = stride(x, 1)
else
sts, szs = strides(x), size(x)
i = 0
while i < ndims(x) && szs[i+=1] == 1 end # skip 1-sized dimension.
st = sts[i]
stᵢ = st
while i < ndims(x)
i += 1
stᵢ *= szs[i-1]
if szs[i] != 1
stᵢ == sts[i] || throw(ArgumentError("only support vector like inputs"))
end
end
end
isnothing(stride0check) || (st == 0 && throw(stride0check))
ptr = st > 0 ? pointer(x) : pointer(x, lastindex(x))
ptr, st
Expand Down
9 changes: 7 additions & 2 deletions stdlib/LinearAlgebra/test/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ function pack(A, uplo)
end

@testset "vec_pointer_stride" begin
a = zeros(4,4,4)
@test BLAS.asum(view(a,1:2:4,:,:)) == 0 # vector like
a = float(rand(1:20,4,4,4))
@test BLAS.asum(a) == sum(a) # dense case
@test BLAS.asum(view(a,1:2:4,:,:)) == sum(view(a,1:2:4,:,:)) # vector like
@test BLAS.asum(view(a,1:3,2:2,3:3)) == sum(view(a,1:3,2:2,3:3))
@test BLAS.asum(view(a,1:1,1:3,1:1)) == sum(view(a,1:1,1:3,1:1))
@test BLAS.asum(view(a,1:1,1:1,1:3)) == sum(view(a,1:1,1:1,1:3))
@test_throws ArgumentError BLAS.asum(view(a,1:3:4,:,:)) # non-vector like
@test_throws ArgumentError BLAS.asum(view(a,1:2,1:1,1:3))
end
Random.seed!(100)
## BLAS tests - testing the interface code to BLAS routines
Expand Down

0 comments on commit 777910d

Please sign in to comment.