Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 40
Add get_devices method to dpctl.SyclPlatform class#1992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3a77c9819437f9bc1e4aa034f7cd34658ae068b7fc8fcfc67226aed7c99d791d166366File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,6 +26,10 @@ from libcpp cimport bool | ||
| from ._backend cimport ( # noqa: E211 | ||
| DPCTLCString_Delete, | ||
| DPCTLDeviceSelector_Delete, | ||
| DPCTLDeviceVector_Delete, | ||
| DPCTLDeviceVector_GetAt, | ||
| DPCTLDeviceVector_Size, | ||
| DPCTLDeviceVectorRef, | ||
| DPCTLFilterSelector_Create, | ||
| DPCTLPlatform_AreEq, | ||
| DPCTLPlatform_Copy, | ||
| @@ -34,6 +38,7 @@ from ._backend cimport ( # noqa: E211 | ||
| DPCTLPlatform_Delete, | ||
| DPCTLPlatform_GetBackend, | ||
| DPCTLPlatform_GetDefaultContext, | ||
| DPCTLPlatform_GetDevices, | ||
| DPCTLPlatform_GetName, | ||
| DPCTLPlatform_GetPlatforms, | ||
| DPCTLPlatform_GetVendor, | ||
| @@ -46,17 +51,21 @@ from ._backend cimport ( # noqa: E211 | ||
| DPCTLPlatformVector_Size, | ||
| DPCTLPlatformVectorRef, | ||
| DPCTLSyclContextRef, | ||
| DPCTLSyclDeviceRef, | ||
| DPCTLSyclDeviceSelectorRef, | ||
| DPCTLSyclPlatformRef, | ||
| _backend_type, | ||
| _device_type, | ||
| ) | ||
| import warnings | ||
| from ._sycl_context import SyclContextCreationError | ||
| from .enum_types import backend_type | ||
| from .enum_types import device_type as device_type_t | ||
| from ._sycl_context cimport SyclContext | ||
| from ._sycl_device cimport SyclDevice | ||
| __all__ = [ | ||
| "get_platforms", | ||
| @@ -366,6 +375,79 @@ cdef class SyclPlatform(_SyclPlatform): | ||
| """ | ||
| return DPCTLPlatform_Hash(self._platform_ref) | ||
| def get_devices(self, device_type=device_type_t.all): | ||
| """ | ||
| Returns the list of :class:`dpctl.SyclDevice` objects associated with | ||
| :class:`dpctl.SyclPlatform` instance selected based on | ||
| the given :class:`dpctl.device_type`. | ||
| Args: | ||
| device_type (str, :class:`dpctl.device_type`, optional): | ||
| A :class:`dpctl.device_type` enum value or a string that | ||
| specifies a SYCL device type. Currently, accepted values are: | ||
| "gpu", "cpu", "accelerator", or "all", and their equivalent | ||
| ``dpctl.device_type`` enumerators. | ||
| Default: ``dpctl.device_type.all``. | ||
| Returns: | ||
| list: | ||
| A :obj:`list` of :class:`dpctl.SyclDevice` objects | ||
| that belong to this platform. | ||
| Raises: | ||
| TypeError: | ||
| If `device_type` is not a string or :class:`dpctl.device_type` | ||
| enum. | ||
| ValueError: | ||
| If the ``DPCTLPlatform_GetDevices`` call returned | ||
| ``NULL`` instead of a ``DPCTLDeviceVectorRef`` object. | ||
| """ | ||
| cdef _device_type DTy = _device_type._ALL_DEVICES | ||
| cdef DPCTLDeviceVectorRef DVRef = NULL | ||
| cdef size_t num_devs | ||
| cdef size_t i | ||
| cdef DPCTLSyclDeviceRef DRef | ||
| if isinstance(device_type, str): | ||
| dty_str = device_type.strip().lower() | ||
| if dty_str == "accelerator": | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| DTy = _device_type._ACCELERATOR | ||
| elif dty_str == "all": | ||
| DTy = _device_type._ALL_DEVICES | ||
| elif dty_str == "cpu": | ||
| DTy = _device_type._CPU | ||
| elif dty_str == "gpu": | ||
| DTy = _device_type._GPU | ||
| else: | ||
| DTy = _device_type._UNKNOWN_DEVICE | ||
| elif isinstance(device_type, device_type_t): | ||
| if device_type == device_type_t.all: | ||
| DTy = _device_type._ALL_DEVICES | ||
| elif device_type == device_type_t.accelerator: | ||
| DTy = _device_type._ACCELERATOR | ||
| elif device_type == device_type_t.cpu: | ||
| DTy = _device_type._CPU | ||
| elif device_type == device_type_t.gpu: | ||
| DTy = _device_type._GPU | ||
| else: | ||
| DTy = _device_type._UNKNOWN_DEVICE | ||
| else: | ||
| raise TypeError( | ||
| "device type should be specified as a str or an " | ||
| "``enum_types.device_type``." | ||
| ) | ||
| DVRef = DPCTLPlatform_GetDevices(self.get_platform_ref(), DTy) | ||
| if (DVRef is NULL): | ||
| raise ValueError("Internal error: NULL device vector encountered") | ||
| num_devs = DPCTLDeviceVector_Size(DVRef) | ||
| devices = [] | ||
| for i in range(num_devs): | ||
| DRef = DPCTLDeviceVector_GetAt(DVRef, i) | ||
| devices.append(SyclDevice._create(DRef)) | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| DPCTLDeviceVector_Delete(DVRef) | ||
| return devices | ||
| def lsplatform(verbosity=0): | ||
| """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -51,8 +51,6 @@ def string_to_device_type(dty_str): | ||
| return dty.accelerator | ||
| elif dty_str == "cpu": | ||
| return dty.cpu | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| elif dty_str == "host": | ||
| return dty.host | ||
| elif dty_str == "gpu": | ||
| return dty.gpu | ||
| @@ -62,8 +60,6 @@ def string_to_backend_type(bty_str): | ||
| return bty.cuda | ||
| elif bty_str == "hip": | ||
| return bty.hip | ||
| elif bty_str == "host": | ||
| return bty.host | ||
| elif bty_str == "level_zero": | ||
| return bty.level_zero | ||
| elif bty_str == "opencl": | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -22,6 +22,7 @@ | ||
| import pytest | ||
| import dpctl | ||
| from dpctl import device_type | ||
| from .helper import has_sycl_platforms | ||
| @@ -212,3 +213,49 @@ def test_get_platforms(): | ||
| assert has_sycl_platforms() | ||
| except Exception: | ||
| pytest.fail("Encountered an exception inside get_platforms().") | ||
| def test_platform_get_devices(): | ||
| platforms = dpctl.get_platforms() | ||
| if platforms: | ||
| for p in platforms: | ||
| assert len(p.get_devices()) | ||
| else: | ||
| pytest.skip("No platforms available") | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def _str_device_type_to_enum(dty): | ||
| if dty == "accelerator": | ||
| return device_type.accelerator | ||
| elif dty == "cpu": | ||
| return device_type.cpu | ||
| elif dty == "gpu": | ||
| return device_type.gpu | ||
| def test_platform_get_devices_str_device_type(): | ||
| platforms = dpctl.get_platforms() | ||
| dtys = ["accelerator", "all", "cpu", "gpu"] | ||
| if platforms: | ||
| for p in platforms: | ||
| for dty in dtys: | ||
| devices = p.get_devices(device_type=dty) | ||
| if len(devices): | ||
| dty_enum = _str_device_type_to_enum(dty) | ||
| assert (d.device_type == dty_enum for d in devices) | ||
| def test_platform_get_devices_enum_device_type(): | ||
| platforms = dpctl.get_platforms() | ||
| dtys = [ | ||
| device_type.accelerator, | ||
| device_type.all, | ||
| device_type.cpu, | ||
| device_type.gpu, | ||
| ] | ||
| if platforms: | ||
| for p in platforms: | ||
| for dty in dtys: | ||
| devices = p.get_devices(device_type=dty) | ||
| if len(devices): | ||
| assert (d.device_type == dty for d in devices) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -29,6 +29,7 @@ | ||
| #include "dpctl_device_selection.hpp" | ||
| #include "dpctl_error_handlers.h" | ||
| #include "dpctl_string_utils.hpp" | ||
| #include "dpctl_sycl_enum_types.h" | ||
| #include "dpctl_sycl_type_casters.hpp" | ||
| #include "dpctl_utils_helper.h" | ||
| #include <iomanip> | ||
| @@ -269,3 +270,49 @@ size_t DPCTLPlatform_Hash(__dpctl_keep const DPCTLSyclPlatformRef PRef) | ||
| return 0; | ||
| } | ||
| } | ||
| __dpctl_give DPCTLDeviceVectorRef | ||
| DPCTLPlatform_GetDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef, | ||
| DPCTLSyclDeviceType DTy) | ||
| { | ||
| auto P = unwrap<platform>(PRef); | ||
| if (!P) { | ||
| error_handler("Cannot retrieve devices from DPCTLSyclPlatformRef as " | ||
| "input is a nullptr.", | ||
| __FILE__, __func__, __LINE__); | ||
| return nullptr; | ||
| } | ||
| using vecTy = std::vector<DPCTLSyclDeviceRef>; | ||
| vecTy *DevicesVectorPtr = nullptr; | ||
| try { | ||
| DevicesVectorPtr = new vecTy(); | ||
| } catch (std::exception const &e) { | ||
| delete DevicesVectorPtr; | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| error_handler(e, __FILE__, __func__, __LINE__); | ||
| return nullptr; | ||
| } | ||
| // handle unknown device | ||
ndgrigorian marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // custom and automatic are also treated as unknown | ||
| // as DPC++ would normally treat as `all` | ||
| // see CMPLRLLVM-65826 | ||
| if (DTy == DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE) { | ||
| return wrap<vecTy>(DevicesVectorPtr); | ||
| } | ||
| try { | ||
| auto SyclDTy = DPCTL_DPCTLDeviceTypeToSyclDeviceType(DTy); | ||
| auto Devices = P->get_devices(SyclDTy); | ||
| DevicesVectorPtr->reserve(Devices.size()); | ||
| for (const auto &Dev : Devices) { | ||
| DevicesVectorPtr->emplace_back( | ||
| wrap<device>(new device(std::move(Dev)))); | ||
| } | ||
| return wrap<vecTy>(DevicesVectorPtr); | ||
| } catch (std::exception const &e) { | ||
| delete DevicesVectorPtr; | ||
| error_handler(e, __FILE__, __func__, __LINE__); | ||
| return nullptr; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.