Skip to content

Launch trylock managed - #3335

Closed
sheevy wants to merge 2 commits into
JuliaGPU:mainfrom
wave-photonics:launch-trylock-managed
Closed

sheevy wants to merge 2 commits into
JuliaGPU:mainfrom
wave-photonics:launch-trylock-managed

Conversation

@sheevy

@sheevy sheevy commented Oct 4, 2026

Copy link
Copy Markdown

8151595 introduced locking for the duration of the launch, which costs microseconds. Contention is rare, so we try to minimise lock as much as possible. In my testing a launch with 32 array arguments drops from 9.5 microseconds (39 allocations) to 4.1 microseconds (7 allocations).

sheevy added 2 commits October 5, 2026 00:08
lock_managed sorted every launch's memories by objectid to lock them in
a
global order. objectid and the sort cost ~2 µs per launch with 16 array
arguments, more with more. Take the locks with trylock in argument order
instead (it never blocks, so it cannot deadlock), and fall back to the
sorted, blocking order only when a lock is held elsewhere. Repeated
memories are dropped by identity first, so each is still locked and
owned
once.

    using CUDA, BenchmarkTools
    kernel(xs...) = return
    xs = ntuple(_ -> CUDA.zeros(1), 16)
    @Btime @cuda kernel($xs...)

    # before: 5.5 µs, after: 3.5 µs (32 arrays: 9.5 → 4.9 µs)
The managed memories are held as an abstract Vector{Managed}, so each
take_ownership! call was dispatched dynamically and boxed its keyword
arguments: one allocation per array argument. Branch on the common
Managed{DeviceMemory} case to call it statically.

    using CUDA, BenchmarkTools
    kernel(xs...) = return
    xs = ntuple(_ -> CUDA.zeros(1), 32)
    @Btime @cuda kernel($xs...)

    # before: 4.9 µs (39 allocations: 3.02 KiB)
    # after:  4.1 µs (7 allocations: 528 bytes)
@sheevy

sheevy commented Oct 6, 2026

Copy link
Copy Markdown
Author

@maleadt just a ping, would appreciate your feedback, if it's good to be merged.

@maleadt

maleadt commented Oct 8, 2026

Copy link
Copy Markdown
Member

Yeah sorry I wanted the huge stack of launch-related PRs to merge first. I'll take a look.

@maleadt maleadt self-assigned this Oct 8, 2026
@maleadt

maleadt commented Oct 8, 2026

Copy link
Copy Markdown
Member

Should be included in #3362.

@maleadt maleadt closed this Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants