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

Improve context switching #761

Merged
merged 6 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Perform low-level clean-up in the correct context.
  • Loading branch information
maleadt committed Mar 11, 2021
commit 2723f6585214ecae117c829eb7d51a4c1183ec71
18 changes: 18 additions & 0 deletions lib/cudadrv/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ function Base.pop!(::Type{CuContext})
CuContext(handle_ref[])
end

# perform some finalizer actions in a context
macro finalize_in_ctx(ctx, body)
# XXX: should this not integrate with the high-level context management from state.jl?
# it might be good that the driver API wrappers don't need that runtime-esque
# state management, but it might be confusing that `context()` doesn't work here.
quote
ctx = $(esc(ctx))
if isvalid(ctx)
push!(CuContext, ctx)
try
$(esc(body))
finally
pop!(CuContext)
end
end
end
end

"""
activate(ctx::CuContext)

Expand Down
4 changes: 1 addition & 3 deletions lib/cudadrv/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ mutable struct CuEvent
end

function unsafe_destroy!(e::CuEvent)
if isvalid(e.ctx)
cuEventDestroy_v2(e)
end
@finalize_in_ctx e.ctx cuEventDestroy_v2(e)
end

Base.unsafe_convert(::Type{CUevent}, e::CuEvent) = e.handle
Expand Down
6 changes: 3 additions & 3 deletions lib/cudadrv/memory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export Mem, attribute, attribute!, memory_type, is_managed
module Mem

using ..CUDA
using ..CUDA: @enum_without_prefix, CUstream, CUdevice, CuDim3, CUarray, CUarray_format
using ..CUDA: @enum_without_prefix, CUstream, CUdevice, CuDim3, CUarray, CUarray_format, @finalize_in_ctx
using ..CUDA.APIUtils

using Base: @deprecate_binding
Expand Down Expand Up @@ -656,9 +656,9 @@ function __unpin(ptr::Ptr{Nothing})

@spinlock __pin_lock begin
pin_count = @inbounds __pin_count[key] -= 1
if pin_count == 0 && CUDA.isvalid(ctx)
if pin_count == 0
buf = @inbounds __pins[key]
Mem.unregister(buf)
@finalize_in_ctx ctx Mem.unregister(buf)
delete!(__pins, key)
delete!(__pin_count, key)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/cudadrv/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ mutable struct CuModule
end

function unsafe_unload!(mod::CuModule)
if isvalid(mod.ctx)
cuModuleUnload(mod)
end
@finalize_in_ctx mod.ctx cuModuleUnload(mod)
end

Base.unsafe_convert(::Type{CUmodule}, mod::CuModule) = mod.handle
Expand Down
4 changes: 1 addition & 3 deletions lib/cudadrv/module/linker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ mutable struct CuLink
end

function unsafe_destroy!(link::CuLink)
if isvalid(link.ctx)
cuLinkDestroy(link)
end
@finalize_in_ctx link.ctx cuLinkDestroy(link)
end

Base.unsafe_convert(::Type{CUlinkState}, link::CuLink) = link.handle
Expand Down
4 changes: 1 addition & 3 deletions lib/cudadrv/pool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function CuMemoryPool(dev::CuDevice)
end

function unsafe_destroy!(pool::CuMemoryPool)
if isvalid(pool.ctx)
cuMemPoolDestroy(pool)
end
@finalize_in_ctx pool.ctx cuMemPoolDestroy(pool)
end

Base.unsafe_convert(::Type{CUmemoryPool}, pool::CuMemoryPool) = pool.handle
Expand Down
4 changes: 1 addition & 3 deletions lib/cudadrv/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ function CuStream(; flags::CUstream_flags=STREAM_DEFAULT,
end

function unsafe_destroy!(s::CuStream)
if isvalid(s.ctx)
cuStreamDestroy_v2(s)
end
@finalize_in_ctx s.ctx cuStreamDestroy_v2(s)
end

function Base.show(io::IO, stream::CuStream)
Expand Down