-
Notifications
You must be signed in to change notification settings - Fork 150
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
Relation to FixedSizeArrays.jl #11
Comments
Thanks for your question, Ed. One subtle difference is in how dimensions greater than one are treated: in FixedSizeArrays an m x n matrix is stored as m tuples of n elements each and in StaticArrays it is a flat tuple of size m x n . In Julia 0.4 the nesting was necessary to get decent performance when you have more than 8 elements, while on 0.5 we seem to get better codegen out of the flat storage scheme. It seems it's more effort for the compiler to unload the nested tuples, but for small matrices it does eventually give exactly the same codegen (e.g. 3x3 matrix multiplication). (of course, in StaticArrays, you could implement a matrix type that uses nested tuples. You only need to implement Another difference which affects codegen is that StaticArrays inlines almost everything. This can make code faster slightly faster and I think it means that it gets around some issues where Julia's inference engine will start "approximating" return types that are "too complex" (see the wizardry in StaticArrays also has built-in mutable types ( Let me know if you have any more questions :) Regards, EDIT - Sorry I think I hit "post" prematurely! |
Also, do note that some functionality available in FixedSizeArrays is missing in StaticArrays, and vice-versa. Hopefully the list of "missing" features in StaticArrays will decrease over time. |
From the user's point of view, I think there's two big differences:
|
Oh yes, dispatch on size is a difference. Hopefully (in time) we can make that easier by changing the type tree or else implementing (and exporting) a trait. |
By the way, you can of course match against |
Thanks for the detailed responses! I have a much better picture of the statically-sized array landscape now. I'll leave this open in case anyone else who comes across this package has similar questions, but you can close it if you'd like. |
The right place for this is probably the README - leaving it open until then makes sense. |
By the way, we just had a long discussion about this over at FixedSizeArrays.jl, which led to more-or-less a decision to move forward with StaticArrays as the fixed size implementation for julia-0.5, and to move to the new home at JuliaArrays/StaticArrays.jl |
Right... when the dust settles we'll update the README. Something along the lines of "we recommend new packages to use StaticArrays and for packages depending on FixedSizeArrays to switch over sometime during the Julia 0.5 cycle (as we won't guarantee that FixedSizeArrays will be Julia 0.6 compatible)."? |
Where are we on this? @SimonDanisch do you agree with the above comment? |
Yes, seems reasonable! using StaticArrays
immutable Point{N, T} <: StaticVector{T}
data::NTuple{N, T}
end
# all these work with SVector:
Point{3}([1,2,3]) --> ERROR: The size of type `Point{3,Float32}` is not known
rand(Point{3, Float32}) --> ERROR: The size of type `Point{3,Float32}` is not known
zeros(Point{3, Float32}) --> ERROR: The size of type `Point{3,Float32}` is not known |
Hi Simon, Your point about missing constructors is interesting. In this case the problem is that you haven't defined One unfortunate thing is that you need to define both: @pure Base.size{N}(::Type{Point{N}}) = (N,)
@pure Base.size{N,T}(::Type{Point{N,T}}) = (N,) However I see there are further constructor issues! Sigh. @SimonDanisch this is something we should definitely improve! Please note that, in my experience, We need to figure out if we want the size to become a type parameter, like FixedSizeArrays does. Having |
This is a hard one; personally I'd be all for it if the compiler was a bit more flexible with type constructors. Without that, there's three options I know about:
I think some of the things we were able to achieve with the FixedSizeArrays approach were quite powerful (eg, https://github.com/SimonDanisch/FixedSizeArrays.jl/blob/0f54fa25d033c492ff7e1028b6c4b97340e5e30b/src/core.jl#L102), but I was a bit worried that it was overly complex and asking a lot from the compiler. |
@andyferris I was using StaticArrays.FixedSizeArrays, where you defined these methods (since they behaved the same, I shortened the example): julia> using StaticArrays # newest master
INFO: Recompiling stale cache file /home/s/.julia/lib/v0.5/StaticArrays.ji for module StaticArrays.
u
julia> using StaticArrays.FixedSizeArrays
julia> a = Point(1,2,3)
ERROR: No precise constructor found. Length of input was 3 while length of StaticArrays.FixedSizeArrays.Point{3,Int64} is 3.
in StaticArrays.FixedSizeArrays.Point{S,T}(::Int64, ::Int64, ::Int64) at /home/s/.julia/v0.5/StaticArrays/src/core.jl:51
julia> rand(Point{3, Float32})
ERROR: No precise constructor found. Length of input was 3 while length of StaticArrays.FixedSizeArrays.Point{3,Float32} is 3.
in macro expansion at /home/s/.julia/v0.5/StaticArrays/src/arraymath.jl:78 [inlined]
in rand(::MersenneTwister, ::Type{StaticArrays.FixedSizeArrays.Point{3,Float32}}) at /home/s/.julia/v0.5/StaticArrays/src/arraymath.jl:70
in rand(::Type{T}) at ./random.jl:228 |
@SimonDanisch this is due to some changes I made to make it more obvious to the user when a type is constructed but the size isn't inferrable. I just pushed a change which reduces that fallback error precedence to This package clearly needs more tests... sorry about that. |
To add to @c42f's (very good) list, I have also considered a few more approaches:
I really can't think of the best approach. I feel traits will play a bigger role in Julia in the future and am happy to move in that direction. However dispatch on traits is a little nasty currently in Julia when there are multiple traits and multiple method definitions. Maybe we should just get some feedback on the Tuple type hack and see whether that is a "maintainable" approach. |
Sure! I actually started porting the FSA tests to StaticArrays.... That seems like a good start and in the end, we can just migrate the tests :) |
Wasn't sure where to put this, but I advertised on julia-users https://groups.google.com/forum/#!topic/julia-users/wxtAnqk3FWk |
I think this is well resolved by now. |
Hi Andy,
Could you help me understand the differences in approach (pros/cons) between the implementations of
SVector
andSMatrix
in this package andVec
andMat
in FixedSizeArrays.jl? The type definitions are identical (e.g.,SVector
andVec
both wrapNTuple
) and the benchmarks are close, although it looks like StaticArrays might scale a bit better (do you have an idea of why)? At a very cursory level I understand that both implementations pack tightly in memory and are amenable to specialized/unrolled linear algebra code, and I'm a fan of howStaticArray
is a subtype ofAbstractArray
, but are there any hidden design subtleties that potential users should be aware of?Thanks,
Ed
The text was updated successfully, but these errors were encountered: