JuliaSIMD / StrideArrays.jl

Library supporting the ArrayInterface.jl strided array interface.
MIT License
54 stars 9 forks source link

Slicing doesn't work with `PtrArray` #88

Open efaulhaber opened 7 months ago

efaulhaber commented 7 months ago
julia> x = [1.0, 2.0, 3.0, 4.0];

julia> y = PtrArray(pointer(x), (StaticInt(2), StaticInt(2)))
2×2 StrideArraysCore.PtrArray{Float64, 2, (1, 2), Tuple{Static.StaticInt{2}, Static.StaticInt{2}}, Tuple{Nothing, Nothing}, Tuple{Static.StaticInt{1}, Static.StaticInt{1}}} with indices static(1):static(2)×static(1):static(2):
 1.0  3.0
 2.0  4.0

julia> y[1, :]
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Static.StaticInt{2}}, Tuple{StrideArraysCore.StrideReset{Static.StaticInt{2}}}, Tuple{Static.StaticInt{1}}} with indices static(1):static(2):
 1.0
 2.0

julia> y[2, :]
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Static.StaticInt{2}}, Tuple{StrideArraysCore.StrideReset{Static.StaticInt{2}}}, Tuple{Static.StaticInt{1}}} with indices static(1):static(2):
 2.0
 3.0

Same with dynamic axes sizes:

julia> y = PtrArray(pointer(x), (2, 2))
2×2 StrideArraysCore.PtrArray{Float64, 2, (1, 2), Tuple{Int64, Int64}, Tuple{Nothing, Nothing}, Tuple{Static.StaticInt{1}, Static.StaticInt{1}}}:
 1.0  3.0
 2.0  4.0

julia> y[1, :]
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Int64}, Tuple{StrideArraysCore.StrideReset{Int64}}, Tuple{Static.StaticInt{1}}}:
 1.0
 2.0

julia> y[2, :]
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Int64}, Tuple{StrideArraysCore.StrideReset{Int64}}, Tuple{Static.StaticInt{1}}}:
 2.0
 3.0

Same with views:

julia> view(y, 1, :)
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Int64}, Tuple{StrideArraysCore.StrideReset{Int64}}, Tuple{Static.StaticInt{1}}}:
 1.0
 2.0

Interestingly, this works:

julia> y[end, :] .= 0
2-element StrideArraysCore.PtrArray{Float64, 1, (1,), Tuple{Int64}, Tuple{StrideArraysCore.StrideReset{Int64}}, Tuple{Static.StaticInt{1}}}:
 0.0
 3.0

julia> y
2×2 StrideArraysCore.PtrArray{Float64, 2, (1, 2), Tuple{Int64, Int64}, Tuple{Nothing, Nothing}, Tuple{Static.StaticInt{1}, Static.StaticInt{1}}}:
 1.0  3.0
 0.0  0.0

CC @ranocha

chriselrod commented 7 months ago

Seems that indexing with extra arguments is broken:

julia> using StrideArraysCore
 │ Package StrideArraysCore not found, but a package named StrideArraysCore is available from a registry. 
 │ Install package?
 │   (@lvdev) pkg> add StrideArraysCore 
 └ (y/n/o) [y]: 
   Resolving package versions...
    Updating `~/.julia/environments/lvdev/Project.toml`
  [7792a7ef] + StrideArraysCore v0.5.2
    Updating `~/.julia/environments/lvdev/Manifest.toml`
  [7792a7ef] + StrideArraysCore v0.5.2

julia> x = [1.0, 2.0, 3.0, 4.0];

julia> y = PtrArray(pointer(x), (StaticInt(2), StaticInt(2)))
2×2 PtrArray{Float64, 2, (1, 2), Tuple{StaticInt{2}, StaticInt{2}}, Tuple{Nothing, Nothing}, Tuple{StaticInt{1}, StaticInt{1}}} with indices static(1):static(2)×static(1):static(2):
 1.0  3.0
 2.0  4.0

julia> y[1, :]
2-element PtrArray{Float64, 1, (1,), Tuple{StaticInt{2}}, Tuple{StrideArraysCore.StrideReset{StaticInt{2}}}, Tuple{StaticInt{1}}} with indices static(1):static(2):
 1.0
 2.0

julia> ys = y[1, :];

julia> ys[1]
1.0

julia> ys[2]
3.0

julia> Array(ys)
2-element Vector{Float64}:
 1.0
 3.0

julia> copy(ys)
2-element StrideArraysCore.StaticStrideArray{Float64, 1, (1,), Tuple{StaticInt{2}}, Tuple{Nothing}, Tuple{StaticInt{1}}, 2} with indices static(1):static(2):
 1.0
 3.0

julia> print(ys)
[1.0, 3.0]
julia> show(ys)
[1.0, 3.0]

julia> ys[CartesianIndex(2,1)]
3.0

julia> ys[2]
3.0

julia> ys[2, 1]
2.0
efaulhaber commented 7 months ago

That's interesting.

chriselrod commented 7 months ago

Probably just take the lazy approach and define extra methods, so that extra indices are ignored when indexing into an array.