-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Add native Array slices/views with shared data #8809
Conversation
My only concern is that |
I suppose alignment is an issue for any implementation of array views. |
How much better is this than For run-time error checking of user inputs, it's better to use something other than |
Parameters: n = 600
tn = 10000
x = randn(n,3)
v = 1.00001
function f1(x,v)
for j=1:length(x)
x[j] += v
end
end Typical output: julia> @time xview = view(x,n+1:n*2)
elapsed time: 1.2021e-5 seconds (216 bytes allocated)
julia> @time for i=1:tn
f1(xview,v)
end
elapsed time: 0.02136735 seconds (623688 bytes allocated)
julia> @time for i=1:tn
scale!(v,xview)
end
elapsed time: 0.024176824 seconds (623688 bytes allocated)
julia> @time xslice = arrayslice(x,n+1,n)
elapsed time: 1.0543e-5 seconds (200 bytes allocated)
julia> @time for i=1:tn
f1(xslice,v)
end
elapsed time: 0.017143545 seconds (623688 bytes allocated)
julia> @time for i=1:tn
scale!(v,xslice)
end
elapsed time: 0.007889941 seconds (623688 bytes allocated) |
Native method for Array slices using shared memory
I wouldn't take the construction timing too seriously unless you do it in a (function) loop. But the rest is probably OK (it might be best to do the looping inside a function, too). Also, boundschecking should come to views in the not too distant future, see #7941. But these are details; overall, I suspect there is real merit in this---the functionality-to-size ratio for this patch is quite favorable. |
Where is the allocation coming from in |
I suspect if you put the loop inside a function, you won't see it---it's probably just for the return value. But definitely worth testing. |
Indeed. I get
|
The big difference for Anyway, a lightweight construction for array slices could be a useful addition to Base, even it would just be to interface certain libraries (e.g. it would help with #7907 ) |
Yes, unfortunately that is the difference. Calling
The
|
OK thanks for that. The contruction with |
At least on my machine, there's an even bigger difference if you use Of course, for an ideal compiler there really shouldn't be any difference between these. So one option is to take this as evidence that other areas need to be improved. Another is to say, in the short term we should do this and strive to eventually eliminate the gap. |
With #8827, a tweak to annotate loads from immutables as constant (not sure if this is legal in all cases, but it should be legal in this case), and
|
Dratted slog-through-accumulated-email delay... Very nice! |
I am not completely sure whether this is necessary. As @simonster shows, when the compiler does the right thing for immutables, ArrayViews is just as fast, but it is more general. |
Right. I guess this is equivalent to ArrayViews + compiler magic. |
Add method
arrayslice(A, pos, dim)
which returns a slice of typeArray
and is memory safe. The slices use contiguous parts of memory from A and can be any dimensional. The advantage for this special case compared to ArrayViews/SubArrays is performance and compatibility with any code for Arrays.The C function
jl_slice_owned_array
is actually a general case ofjl_reshape_array
, so there is a question whether the code duplication is ok, or whether to make the reshape function call the slice function.Memory check (same as with
reshape
):Code example:
See also #8795