@@ -36,7 +36,7 @@ Sets the Metal GPU device associated with the current Julia task.
3636device! (dev:: MTLDevice ) =  task_local_storage (:MTLDevice , dev)
3737
3838const  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.
6464function  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
8687and simply wait for it to be completed. Since command buffers *should* execute in a 
8788First-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 
93120end 
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 
104134end 
0 commit comments