Skip to content

Commit 61b5de2

Browse files
committed
feat: Make legacy_default and per_thread_default public - Fixes NVIDIA#1445
Signed-off-by: Monishver Chandrasekaran <[email protected]>
1 parent 39e3293 commit 61b5de2

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

‎cuda_core/cuda/core/_stream.pyx‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,57 @@ cdef class Stream:
107107
return s
108108

109109
@classmethod
110-
def _legacy_default(cls):
111-
"""Return the legacy default stream (supports subclassing)."""
110+
def legacy_default(cls):
111+
"""Return the legacy default stream.
112+
113+
The legacy default stream is an implicit stream which synchronizes
114+
with all other streams in the same CUDA context except for non-blocking
115+
streams. When any operation is launched on the legacy default stream,
116+
it waits for all previously launched operations in blocking streams to
117+
complete, and all subsequent operations in blocking streams wait for
118+
the legacy default stream operation to complete.
119+
120+
Returns
121+
-------
122+
Stream
123+
The legacy default stream instance for the current context.
124+
125+
See Also
126+
--------
127+
per_thread_default : Per-thread default stream alternative.
128+
129+
Examples
130+
--------
131+
>>> stream = Stream.legacy_default()
132+
>>> stream.sync() # Synchronize all operations
133+
"""
112134
return Stream._from_handle(cls, get_legacy_stream())
113135

114136
@classmethod
115-
def _per_thread_default(cls):
116-
"""Return the per-thread default stream (supports subclassing)."""
137+
def per_thread_default(cls):
138+
"""Return the per-thread default stream.
139+
140+
The per-thread default stream is local to both the calling thread and
141+
the CUDA context. Unlike the legacy default stream, it does not
142+
synchronize with other streams and behaves like an explicitly created
143+
non-blocking stream. This allows for better concurrency in multi-threaded
144+
applications.
145+
146+
Returns
147+
-------
148+
Stream
149+
The per-thread default stream instance for the current thread
150+
and context.
151+
152+
See Also
153+
--------
154+
legacy_default : Legacy default stream alternative.
155+
156+
Examples
157+
--------
158+
>>> stream = Stream.per_thread_default()
159+
>>> stream.sync() # Synchronize only this thread's operations
160+
"""
117161
return Stream._from_handle(cls, get_per_thread_stream())
118162

119163
@classmethod
@@ -378,8 +422,8 @@ cdef class Stream:
378422

379423

380424
# c-only python objects, not public
381-
cdef Stream C_LEGACY_DEFAULT_STREAM = Stream._legacy_default()
382-
cdef Stream C_PER_THREAD_DEFAULT_STREAM = Stream._per_thread_default()
425+
cdef Stream C_LEGACY_DEFAULT_STREAM = Stream.legacy_default()
426+
cdef Stream C_PER_THREAD_DEFAULT_STREAM = Stream.per_thread_default()
383427

384428
# standard python objects, public
385429
LEGACY_DEFAULT_STREAM = C_LEGACY_DEFAULT_STREAM

‎cuda_core/tests/test_stream.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,28 @@ class MyStream(Stream):
116116
def test_stream_legacy_default_subclassing():
117117
class MyStream(Stream):
118118
pass
119-
120-
stream = MyStream._legacy_default()
119+
stream = MyStream.legacy_default()
121120
assert isinstance(stream, MyStream)
122121

123-
124122
def test_stream_per_thread_default_subclassing():
125123
class MyStream(Stream):
126124
pass
127-
128-
stream = MyStream._per_thread_default()
125+
stream = MyStream.per_thread_default()
129126
assert isinstance(stream, MyStream)
130127

128+
def test_stream_legacy_default_public_api():
129+
"""Test public legacy_default() method."""
130+
stream = Stream.legacy_default()
131+
assert isinstance(stream, Stream)
132+
# Verify it's the same as LEGACY_DEFAULT_STREAM
133+
assert stream == LEGACY_DEFAULT_STREAM
134+
135+
def test_stream_per_thread_default_public_api():
136+
"""Test public per_thread_default() method."""
137+
stream = Stream.per_thread_default()
138+
assert isinstance(stream, Stream)
139+
# Verify it's the same as PER_THREAD_DEFAULT_STREAM
140+
assert stream == PER_THREAD_DEFAULT_STREAM
131141

132142
# ============================================================================
133143
# Stream Equality Tests

0 commit comments

Comments
 (0)