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

view() returns CuArray instead of view for 1-D CuArrays #2566

Closed
omlins opened this issue Nov 27, 2024 · 6 comments
Closed

view() returns CuArray instead of view for 1-D CuArrays #2566

omlins opened this issue Nov 27, 2024 · 6 comments
Labels
needs information Further information is requested

Comments

@omlins
Copy link
Contributor

omlins commented Nov 27, 2024

MWE:

using CUDA

nx = 3
B = CUDA.zeros(nx)
view(B, 1:nx-1)
typeof(view(B, 1:nx-1))
@omlins omlins added the bug Something isn't working label Nov 27, 2024
@maleadt
Copy link
Member

maleadt commented Nov 27, 2024

view doesn't have to return a SubArray. This object shares memory with the parent, which is all that matters.

@maleadt maleadt added needs information Further information is requested and removed bug Something isn't working labels Nov 27, 2024
@omlins
Copy link
Contributor Author

omlins commented Nov 27, 2024

Thanks for looking into this, @maleadt . For multi-dimensional arrays, it creates a SubArray; for Base.Array, it it does in any case:

julia> using CUDA

julia> nx = ny = nz = 3;

julia> B = CUDA.zeros(nx);

julia> C = CUDA.zeros(nx,ny);

julia> D = CUDA.zeros(nx,ny,nz);

julia> E = zeros(nx);

julia> F = zeros(nx,ny);

julia> G = zeros(nx,ny,nz);

julia> typeof(view(B, 1:nx))
CuArray{Float32, 1, CUDA.DeviceMemory}

julia> typeof(view(C, 1:nx, 1:ny))
SubArray{Float32, 2, CuArray{Float32, 2, CUDA.DeviceMemory}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}

julia> typeof(view(D, 1:nx, 1:ny, 1:nz))
SubArray{Float32, 3, CuArray{Float32, 3, CUDA.DeviceMemory}, Tuple{UnitRange{Int64}, UnitRange{Int64}, UnitRange{Int64}}, false}

julia> typeof(view(E, 1:nx))
SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}

julia> typeof(view(F, 1:nx, 1:ny))
SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}

julia> typeof(view(G, 1:nx, 1:ny, 1:nz))
SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, UnitRange{Int64}}, false}

@maleadt
Copy link
Member

maleadt commented Nov 27, 2024

That's correct. It tries to figure out if the view is contiguous. If it may be, a CuArray is returned.

Why is this a problem?

@omlins
Copy link
Contributor Author

omlins commented Nov 29, 2024

view doesn't have to return a SubArray. This object shares memory with the parent, which is all that matters.

Wouldn't it be more helpful for the programmer if it returns always a SubArray for views on CuArrays? Also, that's the behavior for Base.Arrays and therefore in order to have maximum consistency, I would have expected the same behavior. Furthermore, if the view returns simply a CuArray that shares the memory, I cannot conveniently access the parent, indices,... - i.e. the fields provided by a SubArray - if i need to do so. In my use case, I would like to access the parent etc...

@maleadt
Copy link
Member

maleadt commented Dec 3, 2024

I get what you're asking and why, but this won't change. There is no contract for these calls to return a SubArray, and the very real problems with wrapped arrays are much more problematic than the inconveniences resulting from this design. From the top of my head: much more complex subtype queries, notably in linalg wrappers, leading to significant package load time regressions; many more ambiguities, again especially around the linalg stuff; triggering generic fallbacks where we already dispatch on wrapped arrays (since we currently do not support multiply-wrapped arrays); etc. If anything, this first needs some fixes in Base, e.g., JuliaLang/julia#51910, before we can revisit this.

@maleadt maleadt closed this as completed Dec 3, 2024
@omlins
Copy link
Contributor Author

omlins commented Dec 3, 2024

@maleadt Okay, I understand. Thanks for clarifying the situation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs information Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants