Skip to content

Commit 3177221

Browse files
committed
fix(cuda.core): pass non-NULL phGraph to cuStreamEndCapture
Use a stack-local CUgraph output for end_building() and GB_end_capture_if_needed(), and release the GIL inside the helper so driver calls are nogil regardless of caller (__dealloc__ or close()).
1 parent 4fccf6e commit 3177221

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

cuda_core/cuda/core/graph/_graph_builder.pyx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ cdef class GraphBuilder:
266266

267267
def close(self):
268268
"""Destroy the graph builder."""
269-
with nogil:
270-
GB_end_capture_if_needed(self, True)
269+
GB_end_capture_if_needed(self, True)
271270
self._h_graph.reset()
272271
self._h_stream.reset()
273272
self._state = CLOSED
@@ -363,8 +362,9 @@ cdef class GraphBuilder:
363362
if not self.is_building:
364363
raise RuntimeError("Graph builder is not building.")
365364
cdef cydriver.CUstream c_stream = as_cu(self._h_stream)
365+
cdef cydriver.CUgraph c_graph
366366
with nogil:
367-
HANDLE_RETURN(cydriver.cuStreamEndCapture(c_stream, NULL))
367+
HANDLE_RETURN(cydriver.cuStreamEndCapture(c_stream, &c_graph))
368368

369369
# TODO: Resolving https://github.com/NVIDIA/cuda-python/issues/617 would allow us to
370370
# resume the build process after the first call to end_building()
@@ -791,17 +791,18 @@ cdef inline int GB_end_capture_if_needed(GraphBuilder gb, bint check_status) exc
791791
capture. A FORKED builder must not call cuStreamEndCapture: the driver
792792
requires forked streams to be joined first.
793793

794-
A NULL phGraph ends the capture and discards the graph; the driver
795-
guards every write to phGraph (cuapiStreamEndCaptureCommon).
796-
797794
check_status=True checks the driver return (close()); False ignores it
798795
(__dealloc__).
799796
"""
797+
cdef cydriver.CUgraph c_graph
798+
cdef cydriver.CUresult err
799+
cdef cydriver.CUstream c_stream
800800
if gb._h_stream and gb._state == CAPTURING and gb._kind != FORKED:
801-
if check_status:
802-
HANDLE_RETURN(cydriver.cuStreamEndCapture(as_cu(gb._h_stream), NULL))
803-
else:
804-
cydriver.cuStreamEndCapture(as_cu(gb._h_stream), NULL)
801+
c_stream = as_cu(gb._h_stream)
802+
with nogil:
803+
err = cydriver.cuStreamEndCapture(c_stream, &c_graph)
804+
if check_status:
805+
HANDLE_RETURN(err)
805806
return 0
806807

807808

0 commit comments

Comments
 (0)