Skip to content

Commit f2c900c

Browse files
Added device_filter_string
``` In [1]: import dpctl In [2]: dpctl.SyclDevice("cpu").filter_string Out[2]: 'opencl:cpu:0' In [3]: dpctl.SyclDevice("gpu").filter_string Out[3]: 'level_zero:gpu:0' In [4]: dpctl.SyclDevice("opencl:gpu").filter_string Out[4]: 'opencl:gpu:0' In [5]: dpctl.SyclDevice().filter_string Out[5]: 'level_zero:gpu:0' In [6]: dpctl.SyclDevice("0").filter_string Out[6]: 'opencl:accelerator:0' In [7]: dpctl.SyclDevice("1").filter_string Out[7]: 'opencl:cpu:0' In [8]: dpctl.SyclDevice("2").filter_string Out[8]: 'opencl:cpu:0' In [9]: dpctl.SyclDevice("3").filter_string Out[9]: 'opencl:gpu:0' In [10]: dpctl.SyclDevice("4").filter_string Out[10]: 'level_zero:gpu:0' ```
1 parent 7bddc4b commit f2c900c

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

dpctl/_sycl_device.pyx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ from ._backend cimport (
5050
DPCTLDevice_IsGPU,
5151
DPCTLDevice_IsHost,
5252
DPCTLDeviceMgr_PrintDeviceInfo,
53+
DPCTLDeviceMgr_GetRelativeId,
5354
DPCTLFilterSelector_Create,
5455
DPCTLDeviceSelector_Delete,
5556
DPCTLDeviceSelector_Score,
@@ -78,7 +79,7 @@ from ._backend cimport (
7879
DPCTLDevice_GetParentDevice,
7980
)
8081
from . import backend_type, device_type
81-
from libc.stdint cimport uint32_t
82+
from libc.stdint cimport uint32_t, int64_t
8283
from libc.stdlib cimport malloc, free
8384
import warnings
8485
import collections
@@ -126,6 +127,34 @@ cdef list _get_devices(DPCTLDeviceVectorRef DVRef):
126127
return devices
127128

128129

130+
cdef str _backend_type_to_filter_string_part(DPCTLSyclBackendType BTy):
131+
if BTy == _backend_type._CUDA:
132+
return "cuda"
133+
elif BTy == _backend_type._HOST:
134+
return "host"
135+
elif BTy == _backend_type._LEVEL_ZERO:
136+
return "level_zero"
137+
elif BTy == _backend_type._OPENCL:
138+
return "opencl"
139+
else:
140+
return "unknown"
141+
142+
143+
cdef str _device_type_to_filter_string_part(DPCTLSyclDeviceType DTy):
144+
if DTy == _device_type._ACCELERATOR:
145+
return "accelerator"
146+
elif DTy == _device_type._AUTOMATIC:
147+
return "automatic"
148+
elif DTy == _device_type._CPU:
149+
return "cpu"
150+
elif DTy == _device_type._GPU:
151+
return "gpu"
152+
elif DTy == _device_type._HOST_DEVICE:
153+
return "host"
154+
else:
155+
return "unknown"
156+
157+
129158
cdef class SyclDevice(_SyclDevice):
130159
""" Python equivalent for cl::sycl::device class.
131160
@@ -719,6 +748,8 @@ cdef class SyclDevice(_SyclDevice):
719748

720749
@property
721750
def parent_device(self):
751+
""" Parent device for a sub-device, or None for a root device.
752+
"""
722753
cdef DPCTLSyclDeviceRef pDRef = NULL
723754
pDRef = DPCTLDevice_GetParentDevice(self._device_ref)
724755
if (pDRef is NULL):
@@ -736,3 +767,27 @@ cdef class SyclDevice(_SyclDevice):
736767
return self.equals(<SyclDevice> other)
737768
else:
738769
return False
770+
771+
@property
772+
def filter_string(self):
773+
""" For a parent device returns a tuple (backend, device_kind, relative_id).
774+
Raises an exception for sub-devices.
775+
"""
776+
cdef DPCTLSyclDeviceRef pDRef = NULL
777+
cdef DPCTLSyclBackendType BTy
778+
cdef DPCTLSyclDeviceType DTy
779+
cdef int64_t relId = -1
780+
pDRef = DPCTLDevice_GetParentDevice(self._device_ref)
781+
if (pDRef is NULL):
782+
BTy = DPCTLDevice_GetBackend(self._device_ref)
783+
DTy = DPCTLDevice_GetDeviceType(self._device_ref)
784+
relId = DPCTLDeviceMgr_GetRelativeId(self._device_ref)
785+
if (relId == -1):
786+
raise TypeError("This SyclDevice is not a root device")
787+
br_str = _backend_type_to_filter_string_part(BTy)
788+
dt_str = _device_type_to_filter_string_part(DTy)
789+
return ":".join((br_str, dt_str, str(relId)))
790+
else:
791+
# this a sub-device, free it, and raise an exception
792+
DPCTLDevice_Delete(pDRef)
793+
raise TypeError("This SyclDevice is not a root device")

0 commit comments

Comments
 (0)