@@ -34,6 +34,7 @@ from ._backend cimport (
3434 DPCTLDeviceVector_Delete,
3535 DPCTLDeviceVector_GetAt,
3636 DPCTLDeviceVector_Size,
37+ DPCTLDevice_AreEq,
3738 DPCTLDevice_GetBackend,
3839 DPCTLDevice_GetDeviceType,
3940 DPCTLDevice_GetDriverInfo,
@@ -49,6 +50,7 @@ from ._backend cimport (
4950 DPCTLDevice_IsGPU,
5051 DPCTLDevice_IsHost,
5152 DPCTLDeviceMgr_PrintDeviceInfo,
53+ DPCTLDeviceMgr_GetRelativeId,
5254 DPCTLFilterSelector_Create,
5355 DPCTLDeviceSelector_Delete,
5456 DPCTLDeviceSelector_Score,
@@ -74,9 +76,10 @@ from ._backend cimport (
7476 DPCTLDevice_CreateSubDevicesEqually,
7577 DPCTLDevice_CreateSubDevicesByCounts,
7678 DPCTLDevice_CreateSubDevicesByAffinity,
79+ DPCTLDevice_GetParentDevice,
7780)
7881from . import backend_type, device_type
79- from libc.stdint cimport uint32_t
82+ from libc.stdint cimport uint32_t, int64_t
8083from libc.stdlib cimport malloc, free
8184import warnings
8285import collections
@@ -96,7 +99,7 @@ cdef class SubDeviceCreationError(Exception):
9699
97100
98101cdef class _SyclDevice:
99- """ A helper metaclass to abstract a cl::sycl::device instance.
102+ """ A helper data-owner class to abstract a cl::sycl::device instance.
100103 """
101104
102105 def __dealloc__ (self ):
@@ -124,6 +127,34 @@ cdef list _get_devices(DPCTLDeviceVectorRef DVRef):
124127 return devices
125128
126129
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+
127158cdef class SyclDevice(_SyclDevice):
128159 """ Python equivalent for cl::sycl::device class.
129160
@@ -714,3 +745,49 @@ cdef class SyclDevice(_SyclDevice):
714745 return self .create_sub_devices_equally(partition)
715746 except Exception as e:
716747 raise TypeError (" Unsupported type of sub-device argument" )
748+
749+ @property
750+ def parent_device (self ):
751+ """ Parent device for a sub-device, or None for a root device.
752+ """
753+ cdef DPCTLSyclDeviceRef pDRef = NULL
754+ pDRef = DPCTLDevice_GetParentDevice(self ._device_ref)
755+ if (pDRef is NULL ):
756+ return None
757+ return SyclDevice._create(pDRef)
758+
759+ cpdef cpp_bool equals(self , SyclDevice other):
760+ """ Returns true if the SyclDevice argument has the same _device_ref
761+ as this SyclDevice.
762+ """
763+ return DPCTLDevice_AreEq(self ._device_ref, other.get_device_ref())
764+
765+ def __eq__ (self , other ):
766+ if isinstance (other, SyclDevice):
767+ return self .equals(< SyclDevice> other)
768+ else :
769+ 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