Closed goretkin closed 7 years ago
The first is intended. The latter is how slicing used to work in base and ArrayViews
needs catch up.
Thanks for the information.
I came across this because I was calling axpy!
on SubArray
s and UnsafeStridedView
s, and in some cases it dispatched to blas.jl
, and in some cases to generic.jl
.
I'm not sure I understand why unsafe_aview
shouldn't be SubArray
like view
. Or why they aren't both DenseArray
.
julia> n=5; z = zeros(n); P = 1.0 * copy(reshape(1:(n^2), n, n))
5×5 Array{Float64,2}:
1.0 6.0 11.0 16.0 21.0
2.0 7.0 12.0 17.0 22.0
3.0 8.0 13.0 18.0 23.0
4.0 9.0 14.0 19.0 24.0
5.0 10.0 15.0 20.0 25.0
julia> z .= 0; LinAlg.axpy!(1.0, copy(unsafe_aview(P, 1, :)), z)
5-element Array{Float64,1}:
1.0
6.0
11.0
16.0
21.0
julia> z .= 0; LinAlg.axpy!(1.0, P[1,:], z)
5-element Array{Float64,1}:
1.0
6.0
11.0
16.0
21.0
julia> z .= 0; LinAlg.axpy!(1.0, @view(P[1,:]), z)
5-element Array{Float64,1}:
1.0
6.0
11.0
16.0
21.0
# old julia equivalent of P[1,:], what ArrayViews still does.
julia> z .= 0; LinAlg.axpy!(1.0, @view(P[1:1,:]), z)
5-element Array{Float64,1}:
1.0
6.0
11.0
16.0
21.0
julia> z .= 0; LinAlg.axpy!(1.0, unsafe_aview(P, 1, :), z)
5-element Array{Float64,1}:
1.0
2.0
3.0
4.0
5.0
Even though I'm getting the wrong answer, it's good that its trying to call BLAS. I would think it would also be possible to call BLAS for this:
julia> @which LinAlg.axpy!(1.0, @view(P[1:1,:]), z)
axpy!(α, x::AbstractArray, y::AbstractArray) in Base.LinAlg at linalg/generic.jl:1112
The idea is that ArrayViews
are optimized views for Array
whereas SubArrays
are more general. As an out-of-base-thing, being a subtype of DenseArray
allow them to dispatch to the BLAS routines which wouldn't be necessary if they were just subtypes of AbstractArray
s. SubArray
s sometimes also dispatch to BLAS but they are included specifically in the StridedArray
union which cannot be extended so they benefit from being defined in Base.
The shape difference would have gotten caught by these commented out tests: https://github.com/JuliaArrays/ArrayViews.jl/blame/25512ae757810b8e44ef9baf998787228f57ad53/test/subviews.jl#L10