Skip to content

Commit 23b8884

Browse files
Synchronization function release GIL
DPCTLQueue_Wait and friends are declared nogil, and their use is moved into `with nogil` context. This allows us to use host_task to decrement reference count on a Python object without introducing a deadlock.
1 parent b4b9722 commit 23b8884

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

dpctl/_backend.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ cdef extern from "syclinterface/dpctl_sycl_device_selector_interface.h":
227227
cdef extern from "syclinterface/dpctl_sycl_event_interface.h":
228228
cdef DPCTLSyclEventRef DPCTLEvent_Create()
229229
cdef DPCTLSyclEventRef DPCTLEvent_Copy(const DPCTLSyclEventRef ERef)
230-
cdef void DPCTLEvent_Wait(DPCTLSyclEventRef ERef)
231-
cdef void DPCTLEvent_WaitAndThrow(DPCTLSyclEventRef ERef)
230+
cdef void DPCTLEvent_Wait(DPCTLSyclEventRef ERef) nogil
231+
cdef void DPCTLEvent_WaitAndThrow(DPCTLSyclEventRef ERef) nogil
232232
cdef void DPCTLEvent_Delete(DPCTLSyclEventRef ERef)
233233
cdef _event_status_type DPCTLEvent_GetCommandExecutionStatus(DPCTLSyclEventRef ERef)
234234
cdef _backend_type DPCTLEvent_GetBackend(DPCTLSyclEventRef ERef)
@@ -356,7 +356,7 @@ cdef extern from "syclinterface/dpctl_sycl_queue_interface.h":
356356
size_t NDims,
357357
const DPCTLSyclEventRef *DepEvents,
358358
size_t NDepEvents)
359-
cdef void DPCTLQueue_Wait(const DPCTLSyclQueueRef QRef)
359+
cdef void DPCTLQueue_Wait(const DPCTLSyclQueueRef QRef) nogil
360360
cdef DPCTLSyclEventRef DPCTLQueue_Memcpy(
361361
const DPCTLSyclQueueRef Q,
362362
void *Dest,

dpctl/_sycl_event.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ cdef class _SyclEvent:
9898

9999
def __dealloc__(self):
100100
if (self._event_ref):
101-
DPCTLEvent_Wait(self._event_ref)
101+
with nogil:
102+
DPCTLEvent_Wait(self._event_ref)
102103
DPCTLEvent_Delete(self._event_ref)
103104
self._event_ref = NULL
104105
self.args = None
@@ -224,7 +225,8 @@ cdef class SyclEvent(_SyclEvent):
224225

225226
@staticmethod
226227
cdef void _wait(SyclEvent event):
227-
DPCTLEvent_WaitAndThrow(event._event_ref)
228+
with nogil:
229+
DPCTLEvent_WaitAndThrow(event._event_ref)
228230

229231
@staticmethod
230232
def wait_for(event):
@@ -370,4 +372,5 @@ cdef class SyclEvent(_SyclEvent):
370372

371373
cpdef void wait(self):
372374
"Synchronously wait for completion of this event."
373-
DPCTLEvent_Wait(self._event_ref)
375+
with nogil:
376+
DPCTLEvent_Wait(self._event_ref)

dpctl/_sycl_queue.pyx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ cdef class SyclQueue(_SyclQueue):
824824
return SyclEvent._create(Eref, args)
825825

826826
cpdef void wait(self):
827-
DPCTLQueue_Wait(self._queue_ref)
827+
with nogil:
828+
DPCTLQueue_Wait(self._queue_ref)
828829

829830
cpdef memcpy(self, dest, src, size_t count):
830831
cdef void *c_dest
@@ -846,7 +847,8 @@ cdef class SyclQueue(_SyclQueue):
846847
raise RuntimeError(
847848
"SyclQueue.memcpy operation encountered an error"
848849
)
849-
DPCTLEvent_Wait(ERef)
850+
with nogil:
851+
DPCTLEvent_Wait(ERef)
850852
DPCTLEvent_Delete(ERef)
851853

852854
cpdef prefetch(self, mem, size_t count=0):
@@ -866,7 +868,8 @@ cdef class SyclQueue(_SyclQueue):
866868
raise RuntimeError(
867869
"SyclQueue.prefetch encountered an error"
868870
)
869-
DPCTLEvent_Wait(ERef)
871+
with nogil:
872+
DPCTLEvent_Wait(ERef)
870873
DPCTLEvent_Delete(ERef)
871874

872875
cpdef mem_advise(self, mem, size_t count, int advice):
@@ -886,7 +889,8 @@ cdef class SyclQueue(_SyclQueue):
886889
raise RuntimeError(
887890
"SyclQueue.mem_advise operation encountered an error"
888891
)
889-
DPCTLEvent_Wait(ERef)
892+
with nogil:
893+
DPCTLEvent_Wait(ERef)
890894
DPCTLEvent_Delete(ERef)
891895

892896
@property

dpctl/memory/_memory.pyx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,17 @@ cdef void copy_via_host(void *dest_ptr, SyclQueue dest_queue,
9494
src_ptr,
9595
nbytes
9696
)
97-
DPCTLEvent_Wait(E1Ref)
97+
with nogil:
98+
DPCTLEvent_Wait(E1Ref)
9899

99100
E2Ref = DPCTLQueue_Memcpy(
100101
dest_queue.get_queue_ref(),
101102
dest_ptr,
102103
<void *>&host_buf[0],
103104
nbytes
104105
)
105-
DPCTLEvent_Wait(E2Ref)
106+
with nogil:
107+
DPCTLEvent_Wait(E2Ref)
106108
DPCTLEvent_Delete(E1Ref)
107109
DPCTLEvent_Delete(E2Ref)
108110

@@ -398,7 +400,8 @@ cdef class _Memory:
398400
<void *>self.memory_ptr, # source
399401
<size_t>self.nbytes
400402
)
401-
DPCTLEvent_Wait(ERef)
403+
with nogil:
404+
DPCTLEvent_Wait(ERef)
402405
DPCTLEvent_Delete(ERef)
403406

404407
return obj
@@ -423,7 +426,8 @@ cdef class _Memory:
423426
<void *>&host_buf[0], # source
424427
<size_t>buf_len
425428
)
426-
DPCTLEvent_Wait(ERef)
429+
with nogil:
430+
DPCTLEvent_Wait(ERef)
427431
DPCTLEvent_Delete(ERef)
428432

429433
cpdef copy_from_device(self, object sycl_usm_ary):
@@ -465,7 +469,8 @@ cdef class _Memory:
465469
<void *>src_buf.p,
466470
<size_t>src_buf.nbytes
467471
)
468-
DPCTLEvent_Wait(ERef)
472+
with nogil:
473+
DPCTLEvent_Wait(ERef)
469474
DPCTLEvent_Delete(ERef)
470475
else:
471476
copy_via_host(

0 commit comments

Comments
 (0)