Skip to content

Commit ef40014

Browse files
Merge pull request #1127 from IntelPython/cache-filter-string
Used functools.cache to cache filter_string property
2 parents e4236a2 + 586a2ab commit ef40014

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

dpctl/_sycl_device.pyx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ from libc.stdlib cimport free, malloc
110110
from ._sycl_platform cimport SyclPlatform
111111

112112
import collections
113+
import functools
113114
import warnings
114115

115116
__all__ = [
@@ -198,6 +199,37 @@ cdef void _init_helper(_SyclDevice device, DPCTLSyclDeviceRef DRef):
198199
device._max_work_item_sizes = DPCTLDevice_GetMaxWorkItemSizes3d(DRef)
199200

200201

202+
@functools.lru_cache(maxsize=None)
203+
def _cached_filter_string(d : SyclDevice):
204+
"""
205+
Internal utility to compute filter_string of input SyclDevice
206+
and cached with `functools.cache`.
207+
208+
Args:
209+
d (dpctl.SyclDevice):
210+
A device for which to compute the filter string.
211+
Returns:
212+
out(str):
213+
Filter string that can be used to create input device,
214+
if the device is a root (unpartitioned) device.
215+
216+
Raises:
217+
ValueError: if the input device is a sub-device.
218+
"""
219+
cdef _backend_type BTy
220+
cdef _device_type DTy
221+
cdef int64_t relId = -1
222+
cdef SyclDevice cd = <SyclDevice> d
223+
relId = DPCTLDeviceMgr_GetRelativeId(cd._device_ref)
224+
if (relId == -1):
225+
raise ValueError("This SyclDevice is not a root device")
226+
BTy = DPCTLDevice_GetBackend(cd._device_ref)
227+
br_str = _backend_type_to_filter_string_part(BTy)
228+
DTy = DPCTLDevice_GetDeviceType(cd._device_ref)
229+
dt_str = _device_type_to_filter_string_part(DTy)
230+
return ":".join((br_str, dt_str, str(relId)))
231+
232+
201233
cdef class SyclDevice(_SyclDevice):
202234
""" SyclDevice(arg=None)
203235
A Python wrapper for the :sycl_device:`sycl::device <>` C++ class.
@@ -1360,14 +1392,14 @@ cdef class SyclDevice(_SyclDevice):
13601392

13611393
@property
13621394
def filter_string(self):
1363-
""" For a parent device, returns a fully specified filter selector
1364-
string``backend:device_type:relative_id`` selecting the device.
1395+
""" For a root device, returns a fully specified filter selector
1396+
string ``"backend:device_type:relative_id"`` selecting the device.
13651397
13661398
Returns:
13671399
str: A Python string representing a filter selector string.
13681400
13691401
Raises:
1370-
TypeError: If the device is a sub-devices.
1402+
TypeError: If the device is a sub-device.
13711403
13721404
:Example:
13731405
.. code-block:: python
@@ -1387,14 +1419,7 @@ cdef class SyclDevice(_SyclDevice):
13871419
cdef int64_t relId = -1
13881420
pDRef = DPCTLDevice_GetParentDevice(self._device_ref)
13891421
if (pDRef is NULL):
1390-
BTy = DPCTLDevice_GetBackend(self._device_ref)
1391-
DTy = DPCTLDevice_GetDeviceType(self._device_ref)
1392-
relId = DPCTLDeviceMgr_GetRelativeId(self._device_ref)
1393-
if (relId == -1):
1394-
raise TypeError("This SyclDevice is not a root device")
1395-
br_str = _backend_type_to_filter_string_part(BTy)
1396-
dt_str = _device_type_to_filter_string_part(DTy)
1397-
return ":".join((br_str, dt_str, str(relId)))
1422+
return _cached_filter_string(self)
13981423
else:
13991424
# this a sub-device, free it, and raise an exception
14001425
DPCTLDevice_Delete(pDRef)

0 commit comments

Comments
 (0)