Skip to content

Commit c593ac9

Browse files
committed
fully cythonize Stream
1 parent 4c4bb17 commit c593ac9

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

cuda_core/cuda/core/experimental/_stream.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ cdef class Stream:
1313
cdef:
1414
cydriver.CUstream _handle
1515
object _owner
16-
object _builtin
17-
object _nonblocking
18-
object _priority
16+
bint _builtin
17+
int _nonblocking
18+
int _priority
1919
cydriver.CUdevice _device_id
2020
cydriver.CUcontext _ctx_handle
2121

cuda_core/cuda/core/experimental/_stream.pyx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from libc.stdint cimport uintptr_t
7+
from libc.stdint cimport uintptr_t, INT32_MIN
88

99
from cuda.bindings cimport cydriver
1010

@@ -111,6 +111,10 @@ cdef class Stream:
111111
"""
112112
def __cinit__(self):
113113
self._handle = <cydriver.CUstream>(NULL)
114+
self._owner = None
115+
self._builtin = False
116+
self._nonblocking = -1 # delayed
117+
self._priority = INT32_MIN # delayed
114118
self._device_id = cydriver.CU_DEVICE_INVALID # delayed
115119
self._ctx_handle = CU_CONTEXT_INVALID # delayed
116120

@@ -124,36 +128,26 @@ cdef class Stream:
124128
def _legacy_default(cls):
125129
cdef Stream self = Stream.__new__(cls)
126130
self._handle = <cydriver.CUstream>(cydriver.CU_STREAM_LEGACY)
127-
self._owner = None
128131
self._builtin = True
129-
self._nonblocking = None # delayed
130-
self._priority = None # delayed
131132
return self
132133

133134
@classmethod
134135
def _per_thread_default(cls):
135136
cdef Stream self = Stream.__new__(cls)
136137
self._handle = <cydriver.CUstream>(cydriver.CU_STREAM_PER_THREAD)
137-
self._owner = None
138138
self._builtin = True
139-
self._nonblocking = None # delayed
140-
self._priority = None # delayed
141139
return self
142140

143141
@classmethod
144142
def _init(cls, obj: Optional[IsStreamT] = None, options=None, device_id: int = None):
145143
cdef Stream self = Stream.__new__(cls)
146-
self._owner = None
147-
self._builtin = False
148144

149145
if obj is not None and options is not None:
150146
raise ValueError("obj and options cannot be both specified")
151147
if obj is not None:
152148
self._handle = _try_to_get_stream_ptr(obj)
153149
# TODO: check if obj is created under the current context/device
154150
self._owner = obj
155-
self._nonblocking = None # delayed
156-
self._priority = None # delayed
157151
return self
158152

159153
cdef StreamOptions opts = check_or_create_options(StreamOptions, options, "Stream options")
@@ -177,9 +171,8 @@ cdef class Stream:
177171
with nogil:
178172
HANDLE_RETURN(cydriver.cuStreamCreateWithPriority(&s, flags, prio))
179173
self._handle = s
180-
self._owner = None
181-
self._nonblocking = nonblocking
182-
self._priority = priority
174+
self._nonblocking = int(nonblocking)
175+
self._priority = prio
183176
self._device_id = device_id if device_id is not None else self._device_id
184177
return self
185178

@@ -220,20 +213,20 @@ cdef class Stream:
220213
def is_nonblocking(self) -> bool:
221214
"""Return True if this is a nonblocking stream, otherwise False."""
222215
cdef unsigned int flags
223-
if self._nonblocking is None:
216+
if self._nonblocking == -1:
224217
with nogil:
225218
HANDLE_RETURN(cydriver.cuStreamGetFlags(self._handle, &flags))
226219
if flags & cydriver.CUstream_flags.CU_STREAM_NON_BLOCKING:
227220
self._nonblocking = True
228221
else:
229222
self._nonblocking = False
230-
return self._nonblocking
223+
return bool(self._nonblocking)
231224

232225
@property
233226
def priority(self) -> int:
234227
"""Return the stream priority."""
235228
cdef int prio
236-
if self._priority is None:
229+
if self._priority == INT32_MIN:
237230
with nogil:
238231
HANDLE_RETURN(cydriver.cuStreamGetPriority(self._handle, &prio))
239232
self._priority = prio

0 commit comments

Comments
 (0)