Skip to content

Commit 64687a2

Browse files
committed
Adds get_unpartitioned_parent_device method to SyclDevice
1 parent 472d28b commit 64687a2

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

dpctl/_sycl_device.pyx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,30 @@ cdef class SyclDevice(_SyclDevice):
20422042
else:
20432043
return str(relId)
20442044

2045+
def get_unpartitioned_parent_device(self):
2046+
""" get_unpartitioned_parent_device(self)
2047+
2048+
Returns the unpartitioned parent device of this device, or None for a
2049+
root device.
2050+
2051+
Returns:
2052+
dpctl.SyclDevice:
2053+
A parent, unpartitioned :class:`dpctl.SyclDevice` instance if
2054+
the device is a sub-device, ``None`` otherwise.
2055+
"""
2056+
cdef DPCTLSyclDeviceRef pDRef = NULL
2057+
cdef DPCTLSyclDeviceRef tDRef = NULL
2058+
pDRef = DPCTLDevice_GetParentDevice(self._device_ref)
2059+
if pDRef is NULL:
2060+
return None
2061+
else:
2062+
tDRef = DPCTLDevice_GetParentDevice(pDRef)
2063+
while tDRef is not NULL:
2064+
DPCTLDevice_Delete(pDRef)
2065+
pDRef = tDRef
2066+
tDRef = DPCTLDevice_GetParentDevice(pDRef)
2067+
return SyclDevice._create(pDRef)
2068+
20452069
def get_device_id(self):
20462070
""" get_device_id()
20472071
For an unpartitioned device, returns the canonical index of this device
@@ -2066,10 +2090,11 @@ cdef class SyclDevice(_SyclDevice):
20662090
"""
20672091
cdef int dev_id = -1
20682092

2069-
if self.parent_device:
2070-
raise ValueError("This SyclDevice is not a root device")
2093+
dev = self
2094+
if dev.parent_device:
2095+
dev = dev.get_unpartitioned_parent_device()
20712096

2072-
dev_id = self.get_overall_ordinal()
2097+
dev_id = dev.get_overall_ordinal()
20732098
if dev_id < 0:
20742099
raise ValueError
20752100
return dev_id

0 commit comments

Comments
 (0)