Skip to content

Commit bbc1c65

Browse files
committed
reduce the applicability of __cuda_stream__
1 parent c0db6b3 commit bbc1c65

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

cuda_core/cuda/core/experimental/_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class StreamOptions:
4747
class IsStreamT(Protocol):
4848
def __cuda_stream__(self) -> Tuple[int, int]:
4949
"""
50-
For any Python objects that are meant to be interpreted as a CUDA stream, they
51-
can do so by implementing this protocol that returns a 2-tuple: The protocol
50+
For any Python object that is meant to be interpreted as a CUDA stream, the intent
51+
can be communicated by implementing this protocol that returns a 2-tuple: The protocol
5252
version number (currently ``0``) and the address of ``cudaStream_t``. Both values
5353
should be Python `int`.
5454
"""

cuda_core/docs/source/interoperability.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ in Python. While we encourage new Python projects to start using streams (and ot
3434
CUDA types) from ``cuda.core``, we understand that there are already several projects
3535
exposing their own stream types.
3636

37-
To address this issue, we propose the :attr:`~_stream.IsStreamT.__cuda_stream__` protocol (currently version
38-
0) as follows: For any Python objects that are meant to be interpreted as a stream, they
39-
should add a ``__cuda_stream__`` method that returns a 2-tuple: The version number
40-
(``0``) and the address of ``cudaStream_t`` (both as Python `int`):
37+
To address this issue, we propose the :attr:`~_stream.IsStreamT.__cuda_stream__` protocol
38+
(currently version 0) as follows: For any Python objects that are meant to be interpreted
39+
as a stream, they should add a ``__cuda_stream__`` *method* that returns a 2-tuple: The
40+
version number (``0``) and the address of ``cudaStream_t`` (both as Python `int`):
4141

4242
.. code-block:: python
4343
@@ -48,12 +48,11 @@ should add a ``__cuda_stream__`` method that returns a 2-tuple: The version numb
4848
4949
...
5050
51-
Then such objects can be understood by ``cuda.core`` anywhere a stream-like object
52-
is needed.
51+
Then such objects can be understood and wrapped by :meth:`Device.create_stream`.
5352

54-
We suggest all existing Python projects that expose a stream class to also support this
55-
protocol wherever a function takes a stream. For new Python projects that need to access
56-
CUDA streams, we encourage you to use :class:`~_stream.Stream` directly.
53+
We suggest all existing Python projects that already expose a stream class to also support
54+
this protocol. For new Python projects that need to access CUDA streams, we encourage you
55+
to use :class:`~_stream.Stream` from ``cuda.core`` directly.
5756

5857

5958
Memory view utilities for CPU/GPU buffers

cuda_core/examples/pytorch_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __cuda_stream__(self):
4040
return (0, stream_id) # Return format required by CUDA Python
4141

4242

43-
s = PyTorchStreamWrapper(pt_stream)
43+
s = dev.create_stream(PyTorchStreamWrapper(pt_stream))
4444

4545
# prepare program
4646
arch = "".join(f"{i}" for i in dev.compute_capability)

cuda_core/examples/simple_multi_gpu_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __cuda_stream__(self):
8787
a = cp.random.random(size, dtype=dtype)
8888
b = cp.random.random(size, dtype=dtype)
8989
c = cp.empty_like(a)
90-
cp_stream0 = StreamAdaptor(cp.cuda.get_current_stream())
90+
cp_stream0 = dev0.create_stream(StreamAdaptor(cp.cuda.get_current_stream()))
9191

9292
# Establish a stream order to ensure that memory has been initialized before
9393
# accessed by the kernel.
@@ -102,7 +102,7 @@ def __cuda_stream__(self):
102102
x = cp.random.random(size, dtype=dtype)
103103
y = cp.random.random(size, dtype=dtype)
104104
z = cp.empty_like(a)
105-
cp_stream1 = StreamAdaptor(cp.cuda.get_current_stream())
105+
cp_stream1 = dev1.create_stream(StreamAdaptor(cp.cuda.get_current_stream()))
106106

107107
# Establish a stream order
108108
stream1.wait(cp_stream1)

0 commit comments

Comments
 (0)