Skip to content

Commit 1942968

Browse files
Synchronize via MTLSharedEvents (#633)
1 parent 422c43f commit 1942968

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/mtl/events.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function MTLSharedEvent(dev::MTLDevice)
2929
return obj
3030
end
3131

32+
function waitUntilSignaledValue(ev::MTLSharedEvent, value, timeoutMS=typemax(UInt64))
33+
@objc [ev::id{MTLSharedEvent} waitUntilSignaledValue:value::UInt64
34+
timeoutMS:timeoutMS::UInt64]::Bool
35+
end
3236

3337
## shared event handle
3438

src/state.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ function global_queue(dev::MTLDevice)
5555
end::MTLCommandQueue
5656
end
5757

58+
"""
59+
queue_event(queue::MTLCommandQueue)::MTLSharedEvent
60+
61+
Return the `MTLSharedEvent` used to synchronize a queue
62+
"""
63+
function queue_event(queue::MTLCommandQueue)
64+
get!(task_local_storage(), (:MTLSharedEvent, queue)) do
65+
MTLSharedEvent(queue.device)
66+
end::MTLSharedEvent
67+
end
68+
5869
# TODO: Increase performance (currently ~15us)
5970
"""
6071
synchronize(queue)
@@ -66,9 +77,13 @@ and simply wait for it to be completed. Since command buffers *should* execute i
6677
First-In-First-Out manner, this synchronizes the GPU.
6778
"""
6879
@autoreleasepool function synchronize(queue::MTLCommandQueue=global_queue(device()))
80+
ev = queue_event(queue)
81+
val = ev.signaledValue + 1
6982
cmdbuf = MTLCommandBuffer(queue)
83+
MTL.encode_signal!(cmdbuf, ev, val)
7084
commit!(cmdbuf)
71-
wait_completed(cmdbuf)
85+
MTL.waitUntilSignaledValue(ev,val)
86+
return
7287
end
7388

7489
"""

0 commit comments

Comments
 (0)