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

Documentation: An example for allocating Unified Memory arrays #33

Open
fghorow opened this issue Jul 26, 2019 · 0 comments
Open

Documentation: An example for allocating Unified Memory arrays #33

fghorow opened this issue Jul 26, 2019 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@fghorow
Copy link

fghorow commented Jul 26, 2019

"""This is a hack from the JuliaCon 2019 Hackathon;
Credit to Tim Bessard and Rohan McLure for helping me with the
intricacies of the memory slinging.

This allocates "unified" memory on both the host cpu and the gpu.
"Unified" memory is communicated between the host and the gpu at the discretion of the CUDA driver.
The intention is that the communication overlaps with the computation, without the programmer needing to
know about the underlying details.

IMPORTANT!
This assumes that prealloc_array is already built elsewhere. (That fits my particular use case.)
The user is responsible for copying the contents of prealloc_array into the cpu array.
The user is responsible for calling Mem.free(buf) after the arrays go out of scope.

julia> using CUDAdrv, CuArrays
julia> foo = Array{Float32,1}([1., 2., 3., 4.]); nothing
julia> ca,ga,b = build_cu_unified_array_similar(foo); nothing
julia> ca .= foo; nothing
julia> ga ≈ ca
True
julia> ca ≈ foo
True

"""
function build_cu_unified_array_similar(prealloc_array::Array{T,N}) where {T,N}
buf = Mem.alloc(Mem.UnifiedBuffer, sizeof(prealloc_array))
cpu_array = unsafe_wrap(Array, convert(Ptr{T}, buf), size(prealloc_array))
gpu_array = unsafe_wrap(CuArray, convert(CuPtr{T}, buf), size(prealloc_array))
return cpu_array, gpu_array, buf
end

@maleadt maleadt transferred this issue from JuliaGPU/CUDAdrv.jl May 27, 2020
@maleadt maleadt added the documentation Improvements or additions to documentation label May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants