Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnsafeArrays doesn't correctly handle dimensions of size zero #21

Closed
MasonProtter opened this issue Jul 23, 2024 · 1 comment · Fixed by #22
Closed

UnsafeArrays doesn't correctly handle dimensions of size zero #21

MasonProtter opened this issue Jul 23, 2024 · 1 comment · Fixed by #22

Comments

@MasonProtter
Copy link

Discovered in MasonProtter/Bumper.jl#43, here's a MWE:

Compare

julia> let A = Array{Float64}(undef, 0, 10)
           A[:, 1] .= 1
       end
0-element view(::Matrix{Float64}, :, 1) with eltype Float64

against

julia> let A = Array{Float64}(undef, 0, 10)
           UA = UnsafeArray(pointer(A), (0, 10))
           UA[:, 1] .= 1
       end
ERROR: BoundsError: attempt to access 0×10 LinearIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}} at index [1, 1]
Stacktrace:
  [1] throw_boundserror(A::LinearIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, I::Tuple{Int64, Int64})
    @ Base ./abstractarray.jl:737
  [2] checkbounds
    @ ./abstractarray.jl:702 [inlined]
  [3] _getindex
    @ ./abstractarray.jl:1323 [inlined]
  [4] getindex
    @ ./abstractarray.jl:1291 [inlined]
  [5] _unsafe_view_impl
    @ ~/Dropbox/Julia/UnsafeArrays/src/unsafe_array.jl:98 [inlined]
  [6] unsafe_view
    @ ~/Dropbox/Julia/UnsafeArrays/src/unsafe_array.jl:71 [inlined]
  [7] view
    @ ./subarray.jl:186 [inlined]
  [8] maybeview
    @ ./views.jl:148 [inlined]
  [9] dotview(::UnsafeArray{Float64, 2}, ::Function, ::Int64)
    @ Base.Broadcast ./broadcast.jl:1244
 [10] top-level scope
    @ REPL[13]:3

Offending code is https://github.com/JuliaArrays/UnsafeArrays.jl/blob/main/src/unsafe_array.jl#L89-L97

I guess the easiest would be to do something like

@inline function _unsafe_view_impl(IFwd::NTuple{M,Base.Slice}, A::UnsafeArray{T,N}, i::DenseIdx, I::Integer...) where {T,M,N}
    @assert IFwd == ntuple(i -> axes(A)[i], Val{M}())
    I_all = (IFwd..., i, I...)
    @boundscheck checkbounds(A, I_all...)
    if length(A) == 0
        p = pointer(A)
    else
        startidxs = map(first, (IFwd..., i, I...))
        sub_s = _sub_size(I_all...)
        p = pointer(A, LinearIndices(size(A))[startidxs...])
    end
    UnsafeArray(p, sub_s)
end

but I wonder if that branch would have negative performance impacts in code that's taking a lot of views (e.g. blocking SIMD), so I wonder if there's a non-branching way to do this.

@oschulz
Copy link
Collaborator

oschulz commented Jul 24, 2024

Thanks for reporting this! I hope #22 fixes this in a branch-free fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants