@@ -239,6 +239,7 @@ cdef tuple _to_enum_tuple(
239239
240240
241241cdef str _backend_type_to_filter_string_part(_backend_type BTy):
242+ """ Returns the filter string part for the backend, or ``None``."""
242243 if BTy == _backend_type._CUDA:
243244 return " cuda"
244245 elif BTy == _backend_type._HIP:
@@ -248,20 +249,55 @@ cdef str _backend_type_to_filter_string_part(_backend_type BTy):
248249 elif BTy == _backend_type._OPENCL:
249250 return " opencl"
250251 else :
251- return " unknown "
252+ return None
252253
253254
254255cdef str _device_type_to_filter_string_part(_device_type DTy):
256+ """ Returns the filter string part for the device type, or ``None``."""
255257 if DTy == _device_type._ACCELERATOR:
256258 return " accelerator"
257- elif DTy == _device_type._AUTOMATIC:
258- return " automatic"
259259 elif DTy == _device_type._CPU:
260260 return " cpu"
261261 elif DTy == _device_type._GPU:
262262 return " gpu"
263263 else :
264- return " unknown"
264+ return None
265+
266+
267+ cdef str _make_filter_string(
268+ DPCTLSyclDeviceRef DRef,
269+ bint include_backend,
270+ bint include_device_type,
271+ int64_t relId,
272+ ):
273+ """ Joins the requested parts of the filter string of a root device.
274+
275+ Raises ``ValueError`` if the backend or device type has no filter
276+ string representation.
277+ """
278+ cdef list parts = []
279+ if include_backend:
280+ be_str = _backend_type_to_filter_string_part(
281+ DPCTLDevice_GetBackend(DRef)
282+ )
283+ if be_str is None :
284+ raise ValueError (
285+ " The backend of this SyclDevice is not supported in filter "
286+ " strings"
287+ )
288+ parts.append(be_str)
289+ if include_device_type:
290+ dt_str = _device_type_to_filter_string_part(
291+ DPCTLDevice_GetDeviceType(DRef)
292+ )
293+ if dt_str is None :
294+ raise ValueError (
295+ " The device type of this SyclDevice is not supported in "
296+ " filter strings"
297+ )
298+ parts.append(dt_str)
299+ parts.append(str (relId))
300+ return " :" .join(parts)
265301
266302
267303cdef void _init_helper(_SyclDevice device, DPCTLSyclDeviceRef DRef) except * :
@@ -317,22 +353,17 @@ cdef inline void _raise_invalid_peer_access(
317353 ]
318354 cdef _backend_type BTy1 = DPCTLDevice_GetBackend(dev._device_ref)
319355 cdef _backend_type BTy2 = DPCTLDevice_GetBackend(peer.get_device_ref())
356+ be1 = _backend_type_to_filter_string_part(BTy1) or " unknown"
357+ be2 = _backend_type_to_filter_string_part(BTy2) or " unknown"
320358 if (BTy1 != BTy2):
321359 raise ValueError (
322- f" Device with backend {_backend_type_to_filter_string_part(BTy1)} "
323- " cannot peer access device with backend "
324- f" {_backend_type_to_filter_string_part(BTy2)}"
360+ f" Device with backend {be1} "
361+ f" cannot peer access device with backend {be2}"
325362 )
326363 if (BTy1 not in _peer_access_backends):
327- raise ValueError (
328- " Peer access not supported for backend "
329- f" {_backend_type_to_filter_string_part(BTy1)}"
330- )
364+ raise ValueError (f" Peer access not supported for backend {be1}" )
331365 if (BTy2 not in _peer_access_backends):
332- raise ValueError (
333- " Peer access not supported for backend "
334- f" {_backend_type_to_filter_string_part(BTy2)}"
335- )
366+ raise ValueError (f" Peer access not supported for backend {be2}" )
336367 if (dev == peer):
337368 raise ValueError (
338369 " Peer access cannot be enabled between a device and itself"
@@ -355,20 +386,15 @@ def _cached_filter_string(d : SyclDevice):
355386 if the device is a root (unpartitioned) device.
356387
357388 Raises:
358- ValueError: if the input device is a sub-device.
389+ ValueError: if the input device is a sub-device, or its backend or
390+ device type is not supported in filter strings.
359391 """
360- cdef _backend_type BTy
361- cdef _device_type DTy
362392 cdef int64_t relId = - 1
363393 cdef SyclDevice cd = < SyclDevice> d
364394 relId = DPCTLDeviceMgr_GetRelativeId(cd._device_ref)
365395 if (relId == - 1 ):
366396 raise ValueError (" This SyclDevice is not a root device" )
367- BTy = DPCTLDevice_GetBackend(cd._device_ref)
368- br_str = _backend_type_to_filter_string_part(BTy)
369- DTy = DPCTLDevice_GetDeviceType(cd._device_ref)
370- dt_str = _device_type_to_filter_string_part(DTy)
371- return " :" .join((br_str, dt_str, str (relId)))
397+ return _make_filter_string(cd._device_ref, True , True , relId)
372398
373399
374400cdef class SyclDevice(_SyclDevice):
@@ -2640,6 +2666,9 @@ cdef class SyclDevice(_SyclDevice):
26402666 ValueError:
26412667 If the device is a sub-device.
26422668
2669+ If the backend or device type of the device is not
2670+ supported in filter strings.
2671+
26432672 :Example:
26442673 .. code-block:: python
26452674
@@ -2746,6 +2775,9 @@ cdef class SyclDevice(_SyclDevice):
27462775 If no match for the device was found in the vector
27472776 returned by ``sycl::device::get_devices()``
27482777
2778+ If the requested backend or device type of the device is
2779+ not supported in filter strings.
2780+
27492781 :Example:
27502782 .. code-block:: python
27512783
@@ -2760,8 +2792,6 @@ cdef class SyclDevice(_SyclDevice):
27602792 """
27612793 cdef int relId = - 1
27622794 cdef DPCTLSyclDeviceRef pDRef = NULL
2763- cdef _device_type DTy
2764- cdef _backend_type BTy
27652795
27662796 if include_backend:
27672797 if include_device_type:
@@ -2783,22 +2813,9 @@ cdef class SyclDevice(_SyclDevice):
27832813 DPCTLDevice_Delete(pDRef)
27842814 raise ValueError (" This SyclDevice is not a root device" )
27852815 else :
2786- if include_backend:
2787- BTy = DPCTLDevice_GetBackend(self ._device_ref)
2788- be_str = _backend_type_to_filter_string_part(BTy)
2789- if include_device_type:
2790- DTy = DPCTLDevice_GetDeviceType(self ._device_ref)
2791- dt_str = _device_type_to_filter_string_part(DTy)
2792- return " :" .join((be_str, dt_str, str (relId)))
2793- else :
2794- return " :" .join((be_str, str (relId)))
2795- else :
2796- if include_device_type:
2797- DTy = DPCTLDevice_GetDeviceType(self ._device_ref)
2798- dt_str = _device_type_to_filter_string_part(DTy)
2799- return " :" .join((dt_str, str (relId)))
2800- else :
2801- return str (relId)
2816+ return _make_filter_string(
2817+ self ._device_ref, include_backend, include_device_type, relId
2818+ )
28022819
28032820 def get_unpartitioned_parent_device (self ):
28042821 """ get_unpartitioned_parent_device()
0 commit comments