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

Implement convert between Array and CuArray like unsafe_wrap #2434

Open
Jutho opened this issue Jul 3, 2024 · 2 comments
Open

Implement convert between Array and CuArray like unsafe_wrap #2434

Jutho opened this issue Jul 3, 2024 · 2 comments
Labels
cuda array Stuff about CuArray. enhancement New feature or request speculative Not sure about this one yet.

Comments

@Jutho
Copy link
Contributor

Jutho commented Jul 3, 2024

I am not sure if this qualifies as a bug or a feature request, but I was wondering whether the constructor CuArray{T,N,CUDA.HostMemory}(A::Array{T,N}) should unsafe_wrap(CuArray, A)?

The current general implementation at https://github.com/JuliaGPU/CUDA.jl/blob/master/src/array.jl#L404 will allocate new host memory and then copy the contents of A into this. Because of this, also convert(CuArray{T,N,CUDA.HostMemory}, A::Array{T,N}) (over at GPUArrays.jl) and ultimately adapt(CuArray{T,N,CUDA.HostMemory}, A::Array{T,N}) will result in new host memory being allocated. Hence, the only way to actually reuse A is via unsafe_wrap(CuArray, A), but that does only work for A::Array and not for any of the wrapper types that one could feed into adapt.

@Jutho Jutho added the bug Something isn't working label Jul 3, 2024
@Jutho
Copy link
Contributor Author

Jutho commented Jul 3, 2024

On second thought, I assume Julia's array constructors are typically assumed to make a copy of the incoming data. But probably convert or adapt(CuArray{T,N,CUDA.HostMemory}, A::Array{T,N}) could be special cased to return unsafe_wrap(CuArray, A).

In the same way, adapt(Array, A::CuArray{T,N,<:Union{CUDA.HostMemory,CUDA.UnifiedMemory}) could be special cased to return unsafe_wrap(Array, A)?

@maleadt
Copy link
Member

maleadt commented Jul 4, 2024

I would agree that convert could be made to work like this, while constructors probably should remain copying. However, there's a reason this operation is currently marked unsafe: We can't track the owning memory object, so when you e.g. unsafe_wrap an Array as a CuArray, and the Array is GC'd, the CuArray becomes invalid too. Without that, I don't think we can reasonably make convert call / behave like unsafe_wrap.

EDIT: Thinking about this some more, I'm not entirely convinced if convert or adapt should result in an aliased object. Are there precedents in Base or packages?

@maleadt maleadt added enhancement New feature or request cuda array Stuff about CuArray. and removed bug Something isn't working labels Jul 4, 2024
@maleadt maleadt changed the title Should CuArray{T,N,CUDA.HostMemory}(A::Array{T,N}) equal unsafe_wrap(CuArray, A)? Implement convert between Array and CuArray like unsafe_wrap Jul 4, 2024
@maleadt maleadt added the speculative Not sure about this one yet. label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuda array Stuff about CuArray. enhancement New feature or request speculative Not sure about this one yet.
Projects
None yet
Development

No branches or pull requests

2 participants