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

Slicing doesn't work with PtrArray #88

Open
efaulhaber opened this issue Mar 10, 2024 · 3 comments
Open

Slicing doesn't work with PtrArray #88

efaulhaber opened this issue Mar 10, 2024 · 3 comments

Comments

@efaulhaber
Copy link

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
Copy link
Member

chriselrod commented Mar 10, 2024

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
Copy link
Author

That's interesting.

@chriselrod
Copy link
Member

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

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

No branches or pull requests

2 participants