Skip to content

Commit 82f2893

Browse files
committed
Add get_device_id method to SyclDevice and permit construction of SyclDevice from result id
This enables users to convert from a dldevice ID to a SYCL device, i.e., ``` import dpctl import dpctl.tensor as dpt x = dpt.arange(10) dldev = x.__dlpack_device__() dev = dpctl.SyclDevice(dldev[1]) print(dev.get_device_id() == dldev[1]) ```
1 parent f7cb1b1 commit 82f2893

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

dpctl/_sycl_device.pyx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ cdef class SyclDevice(_SyclDevice):
355355
raise SyclDeviceCreationError(
356356
"Could not create a SyclDevice from _SyclDevice instance"
357357
)
358+
elif type(arg) is int:
359+
string = bytes(str(arg), "utf-8")
360+
filter_c_str = string
361+
DSRef = DPCTLFilterSelector_Create(filter_c_str)
362+
ret = self._init_from_selector(DSRef)
363+
if ret == -1:
364+
raise SyclDeviceCreationError(
365+
"Could not create a SyclDevice from device id "
366+
"'{dev_id}'".format(dev_id=arg)
367+
)
358368
elif arg is None:
359369
DSRef = DPCTLDefaultSelector_Create()
360370
ret = self._init_from_selector(DSRef)
@@ -1950,9 +1960,7 @@ cdef class SyclDevice(_SyclDevice):
19501960

19511961
cdef int get_overall_ordinal(self):
19521962
""" If this device is a root ``sycl::device``, returns the ordinal
1953-
position of this device in the vector ``sycl::device::get_devices()``
1954-
filtered to contain only devices with the same backend as this
1955-
device.
1963+
position of this device in the vector ``sycl::device::get_devices()``.
19561964
19571965
Returns -1 if the device is a sub-device, or the device could not
19581966
be found in the vector.
@@ -2045,6 +2053,18 @@ cdef class SyclDevice(_SyclDevice):
20452053
else:
20462054
return str(relId)
20472055

2056+
def get_device_id(self):
2057+
cdef int dev_id = -1
2058+
2059+
if self.parent_device:
2060+
raise TypeError("This SyclDevice is not a root device")
2061+
2062+
dev_id = self.get_overall_ordinal()
2063+
if dev_id < 0:
2064+
raise ValueError
2065+
return dev_id
2066+
2067+
20482068
cdef api DPCTLSyclDeviceRef SyclDevice_GetDeviceRef(SyclDevice dev):
20492069
"""
20502070
C-API function to get opaque device reference from

0 commit comments

Comments
 (0)