@@ -26,6 +26,10 @@ from libcpp cimport bool
2626from ._backend cimport ( # noqa: E211
2727 DPCTLCString_Delete,
2828 DPCTLDeviceSelector_Delete,
29+ DPCTLDeviceVector_Delete,
30+ DPCTLDeviceVector_GetAt,
31+ DPCTLDeviceVector_Size,
32+ DPCTLDeviceVectorRef,
2933 DPCTLFilterSelector_Create,
3034 DPCTLPlatform_AreEq,
3135 DPCTLPlatform_Copy,
@@ -34,6 +38,7 @@ from ._backend cimport ( # noqa: E211
3438 DPCTLPlatform_Delete,
3539 DPCTLPlatform_GetBackend,
3640 DPCTLPlatform_GetDefaultContext,
41+ DPCTLPlatform_GetDevices,
3742 DPCTLPlatform_GetName,
3843 DPCTLPlatform_GetPlatforms,
3944 DPCTLPlatform_GetVendor,
@@ -46,17 +51,21 @@ from ._backend cimport ( # noqa: E211
4651 DPCTLPlatformVector_Size,
4752 DPCTLPlatformVectorRef,
4853 DPCTLSyclContextRef,
54+ DPCTLSyclDeviceRef,
4955 DPCTLSyclDeviceSelectorRef,
5056 DPCTLSyclPlatformRef,
5157 _backend_type,
58+ _device_type,
5259)
5360
5461import warnings
5562
5663from ._sycl_context import SyclContextCreationError
5764from .enum_types import backend_type
65+ from .enum_types import device_type as device_type_t
5866
5967from ._sycl_context cimport SyclContext
68+ from ._sycl_device cimport SyclDevice
6069
6170__all__ = [
6271 " get_platforms" ,
@@ -366,6 +375,92 @@ cdef class SyclPlatform(_SyclPlatform):
366375 """
367376 return DPCTLPlatform_Hash(self ._platform_ref)
368377
378+ def get_devices (self , device_type = device_type_t.all):
379+ """
380+ Returns the list of :class:`dpctl.SyclDevice` objects associated with
381+ :class:`dpctl.SyclPlatform` instance selected based on
382+ the given :class:`dpctl.device_type`.
383+
384+ Args:
385+ device_type (optional):
386+ A :class:`dpctl.device_type` enum value or a string that
387+ specifies a SYCL device type. Currently, accepted values are:
388+ "gpu", "cpu", "accelerator", "host", or "all".
389+ Default: ``dpctl.device_type.all``.
390+
391+ Returns:
392+ list:
393+ A :obj:`list` of :class:`dpctl.SyclDevice` objects
394+ that belong to this platform.
395+
396+ Raises:
397+ TypeError:
398+ If `device_type` is not a str or :class:`dpctl.device_type`
399+ enum.
400+ ValueError:
401+ If the value of `device_type` is not supported.
402+
403+ If the ``DPCTLPlatform_GetDevices`` call returned
404+ ``NULL`` instead of a ``DPCTLDeviceVectorRef`` object.
405+ """
406+ cdef _device_type DTy = _device_type._ALL_DEVICES
407+ cdef DPCTLDeviceVectorRef DVRef = NULL
408+ cdef size_t num_devs
409+ cdef size_t i
410+ cdef DPCTLSyclDeviceRef DRef
411+
412+ if isinstance (device_type, str ):
413+ dty_str = device_type.strip().lower()
414+ if dty_str == " accelerator" :
415+ DTy = _device_type._ACCELERATOR
416+ elif dty_str == " all" :
417+ DTy = _device_type._ALL_DEVICES
418+ elif dty_str == " automatic" :
419+ DTy = _device_type._AUTOMATIC
420+ elif dty_str == " cpu" :
421+ DTy = _device_type._CPU
422+ elif dty_str == " custom" :
423+ DTy = _device_type._CUSTOM
424+ elif dty_str == " gpu" :
425+ DTy = _device_type._GPU
426+ else :
427+ raise ValueError (
428+ " Unexpected value of `device_type`."
429+ )
430+ elif isinstance (device_type, device_type_t):
431+ if device_type == device_type_t.all:
432+ DTy = _device_type._ALL_DEVICES
433+ elif device_type == device_type_t.accelerator:
434+ DTy = _device_type._ACCELERATOR
435+ elif device_type == device_type_t.automatic:
436+ DTy = _device_type._AUTOMATIC
437+ elif device_type == device_type_t.cpu:
438+ DTy = _device_type._CPU
439+ elif device_type == device_type_t.custom:
440+ DTy = _device_type._CUSTOM
441+ elif device_type == device_type_t.gpu:
442+ DTy = _device_type._GPU
443+ else :
444+ raise ValueError (
445+ " Unexpected value of `device_type`."
446+ )
447+ else :
448+ raise TypeError (
449+ " device type should be specified as a str or an "
450+ " ``enum_types.device_type``."
451+ )
452+ DVRef = DPCTLPlatform_GetDevices(self .get_platform_ref(), DTy)
453+ if (DVRef is NULL ):
454+ raise ValueError (" Internal error: NULL device vector encountered" )
455+ num_devs = DPCTLDeviceVector_Size(DVRef)
456+ devices = []
457+ for i in range (num_devs):
458+ DRef = DPCTLDeviceVector_GetAt(DVRef, i)
459+ devices.append(SyclDevice._create(DRef))
460+ DPCTLDeviceVector_Delete(DVRef)
461+
462+ return devices
463+
369464
370465def lsplatform (verbosity = 0 ):
371466 """
0 commit comments