-
Notifications
You must be signed in to change notification settings - Fork 152
add convenience wrappers for ccall(:jl_reshape_array,...) #496
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
base: master
Are you sure you want to change the base?
Conversation
Thanks a lot for this. I agree we need to address this! A couple of thoughts:
Finally - @Keno and @vtjnash, as always I'd appreciate knowing whether you think we're crazy :) |
By violating the aliasing rules, you are encouraging LLVM to miscompile the code. By using unsafe load/store, you are simply encouraging LLVM not to optimize it. Neither matters in trivial tests—they only become problematic if someone wants to integrate the code with their own. |
I know! That's why we should offer multiple versions, this being the unsafe one. The way I use such constructs looks like: Read data as Array. Run some very expensive operations on the data using the SArray-form. Run other operations on the Array-form (maybe using external libraries like BLAS). Maybe write transformed data as Array. Aliasing accesses are typically separated my multiple non-inlined, often type-unstable/non-inferred (size of the SArray), function boundaries, and often even across language boundaries. For such purposes, a ReinterpretArray, or any pure julia construct, cannot not work, even if its performance gets fixed: FFI-related functions wisely should dispatch on Array, not AbstractArray. I don't know whether there is an optimization-boundary that people can use and we could document/export. For example, I could imagine also a
I really liked @Keno's idea of the new On naming and signature: what about |
So for all this tbaa-discussion, a little reality check:
Now the resulting |
Correct, julia master doesn't express the TBAA information, but the reason it had to be done in 1.0 was that changing the behavior of reinterpret is a breaking change. |
Thanks Keno for the explanation! So I added a safe version using the new reinterpret, and switched the name and calling convention around a little. Better? Also, I'd like to note that reshape/reinterpret-chains accumulate and don't appear to saturate. That could be optimized (on the other hand, doing this is just silly).
|
I honestly don't feel that comfortable providing code that might be miscompiled, even if the method begins with On the other hand, given this:
I'm wondering if we should maybe upper-bound Julia so it's in the range [0.7 1.1) in the REQUIRE file, i.e. |
I'm willing to make that guarantee. |
I kinda disagree. Really, we should have traits for array memory layouts which tell us when we can get a raw pointer to (column oriented) dense data - this has been discussed elsewhere. In the status quo, there is |
Call it Provide and document now but refuse to export, and hope that most people use the safe and convenient Regarding the signature, I am quite happy with the change; especially the most common case Matrix - |
The old reintrepret was type-stable because you specified the new element type. I'm wondering whether we can use array = randn(3, 100)
splitdims(array, SVector{3, Float64}) I'd really rather use a keyword argument but they get inferred as "Pack" and "unpack" aren't bad, by the way, it just might be nice to use similar terminology across packages and base. The other package that implements this array-splitting funcitonality is JuliennedArrays, which uses functions named |
Hmm. So something along the line of the following?
My reason for the dims specifier is that SArray-Types are annoying to construct (I, at least, always forget how to specify them). A single type-instability is not problematic when isolated behind function barriers. If the caller already knows the resulting type, he can place a type-assert behind the call? The speedup for knowing the restype in advance should be in the low usec, since we basically do a cheap Re name: |
What's the current thinking on having such convenience functions, with or without unsafe magic? Before finding this thread I found myself writing such a function: I see that was one of the proposals above. No reason one couldn't also accept a number of dimensions and dispatch. But I think keyword Re names, note that JuliennedArrays has moved to Slice / Align, and of course Julia 1.1 has |
Well... I think we all agree that doing this operation should be convenient enough. |
Reinterpreting between
Array{T}
andArray{SVector{T}}
is one of the most common operations when using StaticArrays. This adds a convenient wrapper for the underlying runtime function. This does literally the same as the old reinterpret, but is now maybe unsafe due to type-based aliasing analysis. I am open for changing the name and the usage. This avoids the performance impact of #410.Examples: