JuliaArrays / ArrayViews.jl

A Julia package to explore a new system of array views
MIT License
19 stars 18 forks source link

some differences between aview and 0.6 Base.view #52

Closed goretkin closed 7 years ago

goretkin commented 7 years ago
julia> P = eye(5);

julia> isa(unsafe_aview(P, 1, :), DenseArray)
true

julia> isa(@view(P[1,:]), DenseArray)
false

julia> size(@view P[1,:])
(5,)

julia> size(unsafe_view(P, 1, :))
(1, 5)
andreasnoack commented 7 years ago

The first is intended. The latter is how slicing used to work in base and ArrayViews needs catch up.

goretkin commented 7 years ago

Thanks for the information.

I came across this because I was calling axpy! on SubArrays and UnsafeStridedViews, 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
andreasnoack commented 7 years ago

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 AbstractArrays. SubArrays 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.

goretkin commented 7 years ago

The shape difference would have gotten caught by these commented out tests: https://github.com/JuliaArrays/ArrayViews.jl/blame/25512ae757810b8e44ef9baf998787228f57ad53/test/subviews.jl#L10