-
Notifications
You must be signed in to change notification settings - Fork 5
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
Some work on supporting non-standard indexing #44
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good to me 👍
I just noticed that slicing
HybridArraywith statically known size produces an
SArray`, e.g.
julia> u_array = rand(2, 3, 4); u_hybrid = HybridArray{Tuple{2, 3, HybridArrays.Dynamic()}}(copy(u_array));
julia> u_hybrid[:, :, 1]
2×3 StaticArrays.SArray{Tuple{2,3},Float64,2,6} with indices SOneTo(2)×SOneTo(3):
0.491955 0.926182 0.880438
0.591433 0.132179 0.574867
Is this intended behavior? Without further thinking about it, I would have expected to get a HybridArray{Tuple{2,3}}
from this operation, which would be mutable. It somehow feels a bit strange to get something immutable from slicing (copying a part of) a mutable array.
src/indexing.jl
Outdated
@@ -1,5 +1,5 @@ | |||
@inline function getindex(sa::HybridArray{S}, ::Colon) where S | |||
return HybridArray{S}(getindex(sa.data, :)) | |||
return getindex(sa.data, :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will lose any static size information by doing this. Is there a way to prevent this? My first impression is that it might be cleaner to always return some HybridArray
from this operation, either with Dynamic
size (if necessary) or the statically known size (if possible). Maybe something along the lines of hasdynamic(S)
and tuple_nodynamic_prod(S)
could help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HybridArray
without any statically known sizes doesn't provide any benefits actually, and if all dimensions are statically known then SizedArray
from StaticArrays
should be used (because it can take advantage of all existing StaticArray
methods). In this case, getindex
with :
can result only in an array without statically known sizes (where HybridArray
isn't helpful) or all axes of HybridArrays
are statically sized, in which case SizedArray
should have been used instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. So what about doing what you described, i.e. return sa.data[:]
if not all sizes are known statically and a SizedArray
otherwise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say that the place where HybridArray
is created with all statically known sizes should be modified in the first place but we could also have a special case for that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, right, that's true. I was just triggered by the failing test
Compatibility with EllipsisNotation: Test Failed at /home/runner/work/HybridArrays.jl/HybridArrays.jl/test/nonstandard_indices.jl:19
Expression: typeof(u_hybrid[..]) == typeof(u_hybrid[:])
Evaluated: HybridArray{Tuple{2,StaticArrays.Dynamic()},Float64,2,2,Array{Float64,2}} == Array{Float64,1}
src/indexing.jl
Outdated
@@ -10,39 +10,53 @@ Base.@propagate_inbounds function getindex(sa::HybridArray{S}, inds::Union{Int, | |||
_getindex(all_dynamic_fixed_val(S, inds...), sa, inds...) | |||
end | |||
|
|||
Base.@propagate_inbounds function _getindex(::Val{:dynamic_fixed_true}, sa::HybridArray, inds::Union{Int, StaticArray{<:Tuple, Int}, Colon}...) | |||
@inline function Base._getindex(l::IndexLinear, sa::HybridArray{S}, s::Base.Slice) where S |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be good to include some basic information about the indexing process in Base
here to explain why this is done (and that it relies on undocumented behavior that can change from release to release)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's a good idea.
Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
That's a great question. Anyway, what I believe should happen here is that it returns |
Well, I didn't dive that deep into this behavior so far 😉
That sounds good to me (as a new user of HybridArrays) - if it gives the same performance as using an |
After thinking a bit more about it, I think it's totally fine to return an |
I'd be fine with |
I've updated the |
That failure on 1.3 looks like a bug in julia> a = randn(2, 3)
2×3 Array{Float64,2}:
-0.155822 -0.365016 0.390315
-1.3536 0.160612 -0.0588314
julia> a[:]
6-element Array{Float64,1}:
-0.15582181305933568
-1.353604684762468
-0.3650163553825738
0.16061201860891566
0.39031531138352515
-0.05883139149425665
julia> a[..]
2×3 Array{Float64,2}:
-0.155822 -0.365016 0.390315
-1.3536 0.160612 -0.0588314 On 1.5: julia> a = randn(2, 3)
2×3 Array{Float64,2}:
-1.99061 0.333919 0.649871
-1.0537 -1.43223 1.69521
julia> a[:]
6-element Array{Float64,1}:
-1.9906141581780128
-1.0537017264077428
0.3339193921096322
-1.4322293088370968
0.6498713869479286
1.6952138277847535
julia> a[..]
6-element Array{Float64,1}:
-1.9906141581780128
-1.0537017264077428
0.3339193921096322
-1.4322293088370968
0.6498713869479286
1.6952138277847535 |
Ah, ok, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍 Thanks!
You're welcome 🙂 . Do you also need |
We don't use |
@ranocha I've done some work towards supporting non-standard indexing. Could you take a look?