Skip to content

Commit 88e1117

Browse files
committed
Progress? towards synchronization
1 parent c41cb37 commit 88e1117

File tree

7 files changed

+121
-24
lines changed

7 files changed

+121
-24
lines changed

lib/mtl/command_alloc4.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2+
export MTL4CommandAllocatorDescriptor
3+
4+
function MTL4CommandAllocatorDescriptor()
5+
handle = @objc [MTL4CommandAllocatorDescriptor new]::id{MTL4CommandAllocatorDescriptor}
6+
obj = MTL4CommandAllocatorDescriptor(handle)
7+
finalizer(release, obj)
8+
return obj
9+
end
10+
function MTL4CommandAllocatorDescriptor(label)
11+
desc = MTL4CommandAllocatorDescriptor()
12+
desc.label = label
13+
return desc
14+
end
15+
16+
17+
118
#
219
# command allocator
320
#
@@ -13,6 +30,20 @@ function MTL4CommandAllocator(device::MTLDevice)
1330
return obj
1431
end
1532

33+
function MTL4CommandAllocator(dev::MTLDevice, descriptor::MTL4CommandAllocatorDescriptor)
34+
err = Ref{id{NSError}}(nil)
35+
handle = @objc [dev::id{MTLDevice} newCommandAllocatorWithDescriptor:descriptor::id{MTL4CommandAllocatorDescriptor}
36+
error:err::Ptr{id{NSError}}]::id{MTL4CommandAllocator}
37+
obj = MTL4CommandAllocator(handle)
38+
finalizer(release, obj)
39+
return obj
40+
end
41+
42+
function MTL4CommandAllocator(dev::MTLDevice, label)
43+
desc = MTL4CommandAllocatorDescriptor(label)
44+
return MTL4CommandAllocator(dev, desc)
45+
end
46+
1647
function allocatedSize(alloc::MTL4CommandAllocator)::UInt64
1748
@objc [alloc::id{MTL4CommandAllocator} allocatedSize]::UInt64
1849
end

lib/mtl/command_buf4.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ export MTL4CommandBuffer, commit!, beginCommandBufferWithAllocator!, endCommandB
66

77
# @objcwrapper immutable=false MTL4CommandBuffer <: NSObject
88

9-
function MTL4CommandBuffer(device::MTLDevice)
9+
function MTL4CommandBuffer(device::MTLDevice, label=nothing)
1010
handle = @objc [device::id{MTLDevice} newCommandBuffer]::id{MTL4CommandBuffer}
11-
return MTL4CommandBuffer(handle)
11+
buf = MTL4CommandBuffer(handle)
12+
if !isnothing(label)
13+
buf.label = label
14+
end
15+
return buf
1216
end
1317

14-
function MTL4CommandBuffer(f::Base.Callable, device::MTLDevice; queue::MTL4CommandQueue=MTL4CommandQueue(device), allocator::MTL4CommandAllocator=MTL4CommandAllocator(device))
15-
cmdbuf = MTL4CommandBuffer(device)
16-
17-
beginCommandBufferWithAllocator!(cmdbuf, allocator)
18+
function MTL4CommandBuffer(f::Base.Callable, device::MTLDevice, label=nothing; queue::MTL4CommandQueue=MTL4CommandQueue(device), allocator::MTL4CommandAllocator=MTL4CommandAllocator(device))
19+
cmdbuf = MTL4CommandBuffer(device, label)
1820

19-
try
20-
ret = f(cmdbuf)
21-
return ret
22-
finally
23-
endCommandBuffer!(cmdbuf)
24-
commit!(queue, cmdbuf)
25-
end
21+
commit!(f, cmdbuf, queue, allocator)
2622
end
2723

2824
function beginCommandBufferWithAllocator!(cmdbuf::MTL4CommandBuffer, allocator::MTL4CommandAllocator, options::Union{Nothing, MTL4CommandBufferOptions} = nothing)
@@ -49,3 +45,15 @@ function commit!(cmdqueue::MTL4CommandQueue, cmdbuf::MTL4CommandBuffer, options:
4945
count:1::NSUInteger
5046
options:options::id{MTL4CommitOptions}]::Nothing
5147
end
48+
49+
function commit!(f::Base.Callable, cmdbuf::MTL4CommandBuffer, queue::MTL4CommandQueue, allocator::MTL4CommandAllocator)
50+
beginCommandBufferWithAllocator!(cmdbuf, allocator)
51+
52+
try
53+
ret = f(cmdbuf)
54+
return ret
55+
finally
56+
endCommandBuffer!(cmdbuf)
57+
commit!(queue, cmdbuf)
58+
end
59+
end

lib/mtl/command_queue4.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ end
3636

3737

3838

39+
export MTL4CommandQueueDescriptor
40+
41+
function MTL4CommandQueueDescriptor()
42+
handle = @objc [MTL4CommandQueueDescriptor new]::id{MTL4CommandQueueDescriptor}
43+
obj = MTL4CommandQueueDescriptor(handle)
44+
finalizer(release, obj)
45+
return obj
46+
end
47+
function MTL4CommandQueueDescriptor(label)
48+
desc = MTL4CommandQueueDescriptor()
49+
desc.label = label
50+
return desc
51+
end
52+
53+
3954
export MTL4CommandQueue
4055

4156
# @objcwrapper immutable=false MTL4CommandQueue <: NSObject
@@ -46,3 +61,12 @@ function MTL4CommandQueue(dev::MTLDevice)
4661
finalizer(release, obj)
4762
return obj
4863
end
64+
65+
function MTL4CommandQueue(dev::MTLDevice, descriptor::MTL4CommandQueueDescriptor)
66+
err = Ref{id{NSError}}(nil)
67+
handle = @objc [dev::id{MTLDevice} newMTL4CommandQueueWithDescriptor:descriptor::id{MTL4CommandQueueDescriptor}
68+
error:err::Ptr{id{NSError}}]::id{MTL4CommandQueue}
69+
obj = MTL4CommandQueue(handle)
70+
finalizer(release, obj)
71+
return obj
72+
end

lib/mtl/libmtl.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@ end
28822882

28832883
@objcwrapper immutable = false availability = macos(v"26.0.0") MTL4CommitOptions <: NSObject
28842884

2885-
@objcwrapper immutable = true availability = macos(v"26.0.0") MTL4CommandQueueDescriptor <: NSObject
2885+
@objcwrapper immutable = false availability = macos(v"26.0.0") MTL4CommandQueueDescriptor <: NSObject
28862886

28872887
@objcproperties MTL4CommandQueueDescriptor begin
28882888
@autoproperty label::id{NSString} setter = setLabel
@@ -3327,7 +3327,7 @@ end
33273327
@autoproperty functionType::MTLFunctionType
33283328
end
33293329

3330-
@objcwrapper immutable = true availability = macos(v"26.0.0") MTL4CommandAllocatorDescriptor <: NSObject
3330+
@objcwrapper immutable = false availability = macos(v"26.0.0") MTL4CommandAllocatorDescriptor <: NSObject
33313331

33323332
@objcproperties MTL4CommandAllocatorDescriptor begin
33333333
@autoproperty label::id{NSString} setter = setLabel

res/wrap/libmtl.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ immutable=false
5151
[api.MTLCommandQueue]
5252
immutable=false
5353

54+
[api.MTL4CommandQueueDescriptor]
55+
immutable=false
56+
5457
[api.MTL4CommandQueue]
5558
immutable=false
5659

@@ -68,6 +71,9 @@ immutable=false
6871
[api.MTL4ComputeCommandEncoder]
6972
immutable=false
7073

74+
[api.MTL4CommandAllocatorDescriptor]
75+
immutable=false
76+
7177
[api.MTL4CommandAllocator]
7278
immutable=false
7379

src/compiler/execution.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ end
281281
if use_mtl4
282282
allocator = MTL4CommandAllocator(device())
283283
cmdbuf = MTL4CommandBuffer(device())
284-
285-
# TODO: Initialize with descriptor to set label
286-
# cmdbuf.label = "MTL4CommandBuffer($(nameof(kernel.f)))"
284+
cmdbuf.label = "MTL4CommandBuffer($(nameof(kernel.f)))"
287285

288286
beginCommandBufferWithAllocator!(cmdbuf, allocator)
289287
cce = MTL4ComputeCommandEncoder(cmdbuf)

src/state.jl

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Sets the Metal GPU device associated with the current Julia task.
3636
device!(dev::MTLDevice) = task_local_storage(:MTLDevice, dev)
3737

3838
const global_queues = WeakKeyDict{MTLCommandQueue,Nothing}()
39-
const global_queues4 = WeakKeyDict{MTL4CommandQueue,Nothing}()
39+
const global_queues4 = WeakKeyDict{MTL4CommandQueue,Tuple{MTL4CommandBuffer, MTL4CommandAllocator}}()
4040

4141
"""
4242
global_queue(dev::MTLDevice)::MTLCommandQueue
@@ -64,13 +64,14 @@ Return the Metal 4 command queue associated with the current Julia thread.
6464
function global_queue4(dev::MTLDevice)
6565
get!(task_local_storage(), (:MTL4CommandQueue, dev)) do
6666
@autoreleasepool begin
67+
desc = MTL4CommandQueueDescriptor("global_queue4($(current_task()))")
68+
6769
# NOTE: MTL4CommandQueue itself is manually reference-counted,
6870
# the release pool is for resources used during its construction.
69-
queue = MTL4CommandQueue(dev)
71+
queue = MTL4CommandQueue(dev, desc)
72+
7073

71-
# # TODO: Initialize with descriptor to set label
72-
# queue.label = "global_queue4($(current_task()))"
73-
global_queues4[queue] = nothing
74+
global_queues4[queue] = (MTL4CommandBuffer(dev, "sync_buffer($(current_task()))"), MTL4CommandAllocator(dev, "sync_allocater($(current_task()))"))
7475
queue
7576
end
7677
end::MTL4CommandQueue
@@ -86,10 +87,36 @@ Create a new MTLCommandBuffer from the global command queue, commit it to the qu
8687
and simply wait for it to be completed. Since command buffers *should* execute in a
8788
First-In-First-Out manner, this synchronizes the GPU.
8889
"""
89-
@autoreleasepool function synchronize(queue::MTLCommandQueue=global_queue(device()))
90+
@autoreleasepool function synchronize(queue::MTLCommandQueue)
9091
cmdbuf = MTLCommandBuffer(queue)
9192
commit!(cmdbuf)
9293
wait_completed(cmdbuf)
94+
return
95+
end
96+
@autoreleasepool function synchronize(queue::MTL4CommandQueue)
97+
cmdbuf, allocator = get(global_queues4, queue) do
98+
@info "hello"
99+
dev = queue.device
100+
MTL4CommandBuffer(dev), MTL4CommandAllocator(dev)
101+
end
102+
103+
cmdbuf = commit!(cmdbuf, queue, allocator) do cmdbuf
104+
MTL4ComputeCommandEncoder(identity, cmdbuf, #=sync=#true)
105+
end
106+
return
107+
end
108+
function synchronize()
109+
dev = device()
110+
tlskeys = keys(task_local_storage())
111+
# hasmtl3key = (:MTLCommandQueue, dev) in tlskeys
112+
# hasmtl4key = use_metal4() && (:MTL4CommandQueue, dev) in tlskeys
113+
if (:MTLCommandQueue, dev) in tlskeys
114+
synchronize(global_queue(dev))
115+
end
116+
if use_metal4() && (:MTL4CommandQueue, dev) in tlskeys
117+
synchronize(global_queue4(dev))
118+
end
119+
return
93120
end
94121

95122
"""
@@ -101,4 +128,7 @@ function device_synchronize()
101128
for queue in keys(global_queues)
102129
synchronize(queue)
103130
end
131+
for queue in keys(global_queues4)
132+
synchronize(queue)
133+
end
104134
end

0 commit comments

Comments
 (0)