Skip to content

Raises dedicate exception type of failure to create SyclDevice #826

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

Merged
merged 4 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions dpctl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
"""
__author__ = "Intel Corp."

from dpctl._sycl_context import SyclContext
from dpctl._sycl_device import SyclDevice
from dpctl._sycl_context import SyclContext, SyclContextCreationError
from dpctl._sycl_device import (
SyclDevice,
SyclDeviceCreationError,
SyclSubDeviceCreationError,
)
from dpctl._sycl_device_factory import (
get_devices,
get_num_devices,
Expand Down Expand Up @@ -73,9 +77,12 @@

__all__ = [
"SyclContext",
"SyclContextCreationError",
]
__all__ += [
"SyclDevice",
"SyclDeviceCreationError",
"SyclSubDeviceCreationError",
]
__all__ += [
"get_devices",
Expand Down
5 changes: 3 additions & 2 deletions dpctl/_device_selection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import collections.abc
from itertools import chain

from . import SyclDevice, get_devices
from ._sycl_device import SyclDevice, SyclDeviceCreationError
from ._sycl_device_factory import get_devices


def select_device_with_aspects(required_aspects, excluded_aspects=[]):
Expand Down Expand Up @@ -66,7 +67,7 @@ def select_device_with_aspects(required_aspects, excluded_aspects=[]):
selected_dev = dev

if selected_dev is None:
raise ValueError(
raise SyclDeviceCreationError(
f"Requested device is unavailable: "
f"required_aspects={required_aspects}, "
f"excluded_aspects={excluded_aspects}"
Expand Down
25 changes: 22 additions & 3 deletions dpctl/_sycl_context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ from ._backend cimport ( # noqa: E211
)
from ._sycl_device cimport SyclDevice
from ._sycl_queue cimport default_async_error_handler
from ._sycl_device import SyclDeviceCreationError

__all__ = [
"SyclContext",
"SyclContextCreationError",
]

_logger = logging.getLogger(__name__)


cdef class SyclContextCreationError(Exception):
"""
A SyclContextCreationError exception is raised when
SyclContext could not created.

"""
pass


cdef void _context_capsule_deleter(object o):
cdef DPCTLSyclContextRef CRef = NULL
if pycapsule.PyCapsule_IsValid(o, "SyclContextRef"):
Expand Down Expand Up @@ -162,7 +174,8 @@ cdef class SyclContext(_SyclContext):
Raises:
MemoryError: If the constructor could not allocate necessary
temporary memory.
ValueError: If the :class:`dpctl.SyclContext` object creation failed.
SyclContextCreationError: If the :class:`dpctl.SyclContext` object
creation failed.
TypeError: If the list of :class:`dpctl.SyclDevice` objects was empty,
or the input capsule contained a null pointer or could not
be renamed.
Expand Down Expand Up @@ -283,11 +296,17 @@ cdef class SyclContext(_SyclContext):
):
ret = self._init_context_from_devices(arg, 0)
else:
dev = SyclDevice(arg)
try:
dev = SyclDevice(arg)
except SyclDeviceCreationError as e:
raise SyclContextCreationError(
"SyclContext failed to be created because "
f"SyclDevice could not be created from the argument {arg}"
) from e
ret = self._init_context_from_one_device(<SyclDevice> dev, 0)
if (ret < 0):
if (ret == -1):
raise ValueError("Context failed to be created.")
raise SyclContextCreationError("Context failed to be created.")
elif (ret == -2):
raise TypeError(
"List of devices to create context from must be non-empty."
Expand Down
42 changes: 33 additions & 9 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ import collections
import warnings

__all__ = [
"SyclDevice",
"SyclDevice", "SyclDeviceCreationError", "SyclSubDeviceCreationError",
]


cdef class SubDeviceCreationError(Exception):
cdef class SyclDeviceCreationError(Exception):
"""
A SubDeviceCreationError exception is raised when
A SyclDeviceCreationError exception is raised when
SyclDevice instance could not created.

"""
pass


cdef class SyclSubDeviceCreationError(Exception):
"""
A SyclSubDeviceCreationError exception is raised when
sub-devices were not created.

"""
Expand Down Expand Up @@ -165,6 +174,7 @@ cdef str _device_type_to_filter_string_part(_device_type DTy):
else:
return "unknown"


cdef void _init_helper(_SyclDevice device, DPCTLSyclDeviceRef DRef):
"Populate attributes of device from opaque device reference DRef"
device._device_ref = DRef
Expand Down Expand Up @@ -213,6 +223,20 @@ cdef class SyclDevice(_SyclDevice):
gpu = dpctl.select_gpu_device():
gpu.print_device_info()

Args:
arg (optional): The argument can be a selector string or None.
Defaults to ``None``.

Raises:
MemoryError: If the constructor could not allocate necessary
temporary memory.
SyclDeviceCreationError: If the :class:`dpctl.SyclDevice` object
creation failed.
TypeError: If the list of :class:`dpctl.SyclDevice` objects was empty,
or the input capsule contained a null pointer or could not
be renamed.


"""
@staticmethod
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
Expand Down Expand Up @@ -262,20 +286,20 @@ cdef class SyclDevice(_SyclDevice):
DSRef = DPCTLFilterSelector_Create(filter_c_str)
ret = self._init_from_selector(DSRef)
if ret == -1:
raise ValueError(
raise SyclDeviceCreationError(
"Could not create a SyclDevice with the selector string"
)
elif isinstance(arg, _SyclDevice):
ret = self._init_from__SyclDevice(arg)
if ret == -1:
raise ValueError(
raise SyclDeviceCreationError(
"Could not create a SyclDevice from _SyclDevice instance"
)
elif arg is None:
DSRef = DPCTLDefaultSelector_Create()
ret = self._init_from_selector(DSRef)
if ret == -1:
raise ValueError(
raise SyclDeviceCreationError(
"Could not create a SyclDevice from default selector"
)
else:
Expand Down Expand Up @@ -746,7 +770,7 @@ cdef class SyclDevice(_SyclDevice):
if count > 0:
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
if DVRef is NULL:
raise SubDeviceCreationError(
raise SyclSubDeviceCreationError(
"Sub-devices were not created." if (count > 0) else
"Sub-devices were not created, "
"requested compute units count was zero."
Expand Down Expand Up @@ -785,7 +809,7 @@ cdef class SyclDevice(_SyclDevice):
)
free(counts_buff)
if DVRef is NULL:
raise SubDeviceCreationError(
raise SyclSubDeviceCreationError(
"Sub-devices were not created." if (min_count > 0) else
"Sub-devices were not created, "
"sub-device execution units counts must be positive."
Expand All @@ -801,7 +825,7 @@ cdef class SyclDevice(_SyclDevice):
cdef DPCTLDeviceVectorRef DVRef = NULL
DVRef = DPCTLDevice_CreateSubDevicesByAffinity(self._device_ref, domain)
if DVRef is NULL:
raise SubDeviceCreationError("Sub-devices were not created.")
raise SyclSubDeviceCreationError("Sub-devices were not created.")
return _get_devices(DVRef)

def create_sub_devices(self, **kwargs):
Expand Down
31 changes: 16 additions & 15 deletions dpctl/_sycl_device_factory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ from ._backend cimport ( # noqa: E211
_device_type,
)

from ._sycl_device import SyclDeviceCreationError
from .enum_types import backend_type
from .enum_types import device_type as device_type_t

Expand Down Expand Up @@ -307,15 +308,15 @@ cpdef SyclDevice select_accelerator_device():
dpctl.SyclDevice: A Python object wrapping the SYCL ``device``
returned by the SYCL ``accelerator_selector``.
Raises:
ValueError: If the SYCL ``accelerator_selector`` is unable to select a
``device``.
dpctl.SyclDeviceCreatioError: If the SYCL ``accelerator_selector`` is
unable to select a ``device``.
"""
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLAcceleratorSelector_Create()
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
# Free up the device selector
DPCTLDeviceSelector_Delete(DSRef)
if DRef is NULL:
raise ValueError("Device unavailable.")
raise SyclDeviceCreationError("Accelerator device is unavailable.")
Device = SyclDevice._create(DRef)
return Device

Expand All @@ -327,15 +328,15 @@ cpdef SyclDevice select_cpu_device():
dpctl.SyclDevice: A Python object wrapping the SYCL ``device``
returned by the SYCL ``cpu_selector``.
Raises:
ValueError: If the SYCL ``cpu_selector`` is unable to select a
``device``.
dpctl.SyclDeviceCreationError: If the SYCL ``cpu_selector`` is
unable to select a ``device``.
"""
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLCPUSelector_Create()
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
# Free up the device selector
DPCTLDeviceSelector_Delete(DSRef)
if DRef is NULL:
raise ValueError("Device unavailable.")
raise SyclDeviceCreationError("CPU device is unavailable.")
Device = SyclDevice._create(DRef)
return Device

Expand All @@ -347,15 +348,15 @@ cpdef SyclDevice select_default_device():
dpctl.SyclDevice: A Python object wrapping the SYCL ``device``
returned by the SYCL ``default_selector``.
Raises:
ValueError: If the SYCL ``default_selector`` is unable to select a
``device``.
dpctl.SyclDeviceCreationError: If the SYCL ``default_selector`` is
unable to select a ``device``.
"""
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLDefaultSelector_Create()
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
# Free up the device selector
DPCTLDeviceSelector_Delete(DSRef)
if DRef is NULL:
raise ValueError("Device unavailable.")
raise SyclDeviceCreationError("Default device is unavailable.")
Device = SyclDevice._create(DRef)
return Device

Expand All @@ -367,15 +368,15 @@ cpdef SyclDevice select_gpu_device():
dpctl.SyclDevice: A Python object wrapping the SYCL ``device``
returned by the SYCL ``gpu_selector``.
Raises:
ValueError: If the SYCL ``gpu_selector`` is unable to select a
``device``.
dpctl.SyclDeviceCreationError: If the SYCL ``gpu_selector`` is
unable to select a ``device``.
"""
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLGPUSelector_Create()
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
# Free up the device selector
DPCTLDeviceSelector_Delete(DSRef)
if DRef is NULL:
raise ValueError("Device unavailable.")
raise SyclDeviceCreationError("Device unavailable.")
Device = SyclDevice._create(DRef)
return Device

Expand All @@ -387,14 +388,14 @@ cpdef SyclDevice select_host_device():
dpctl.SyclDevice: A Python object wrapping the SYCL ``device``
returned by the SYCL ``host_selector``.
Raises:
ValueError: If the SYCL ``host_selector`` is unable to select a
``device``.
dpctl.SyclDeviceCreationError: If the SYCL ``host_selector`` is
unable to select a ``device``.
"""
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLHostSelector_Create()
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
# Free up the device selector
DPCTLDeviceSelector_Delete(DSRef)
if DRef is NULL:
raise ValueError("Device unavailable.")
raise SyclDeviceCreationError("Host device is unavailable.")
Device = SyclDevice._create(DRef)
return Device
16 changes: 8 additions & 8 deletions dpctl/tests/test_sycl_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_ctxt_creation_from_filter(valid_filter):
"""
try:
dpctl.SyclContext(valid_filter)
except ValueError:
except dpctl.SyclContextCreationError:
pytest.skip("Failed to create context with supported filter")


Expand All @@ -70,11 +70,11 @@ def test_context_not_equals():
"""
try:
ctx_gpu = dpctl.SyclContext("gpu")
except ValueError:
except dpctl.SyclContextCreationError:
pytest.skip()
try:
ctx_cpu = dpctl.SyclContext("cpu")
except ValueError:
except dpctl.SyclContextCreationError:
pytest.skip()
assert ctx_cpu != ctx_gpu
assert hash(ctx_cpu) != hash(ctx_gpu)
Expand All @@ -93,7 +93,7 @@ def test_context_equals():
try:
ctx1 = dpctl.SyclContext("gpu")
ctx0 = dpctl.SyclContext("gpu")
except ValueError:
except dpctl.SyclContextCreationError:
pytest.skip()
assert ctx0 == ctx1
assert hash(ctx0) == hash(ctx1)
Expand All @@ -118,7 +118,7 @@ def test_repr():
def test_context_can_be_used_in_queue(valid_filter):
try:
ctx = dpctl.SyclContext(valid_filter)
except ValueError:
except dpctl.SyclContextCreationError:
pytest.skip()
devs = ctx.get_devices()
assert len(devs) == ctx.device_count
Expand All @@ -129,7 +129,7 @@ def test_context_can_be_used_in_queue(valid_filter):
def test_context_can_be_used_in_queue2(valid_filter):
try:
d = dpctl.SyclDevice(valid_filter)
except ValueError:
except dpctl.SyclDeviceCreationError:
pytest.skip()
if d.default_selector_score < 0:
# skip test for devices rejected by default selector
Expand All @@ -141,7 +141,7 @@ def test_context_can_be_used_in_queue2(valid_filter):
def test_context_multi_device():
try:
d = dpctl.SyclDevice("cpu")
except ValueError:
except dpctl.SyclDeviceCreationError:
pytest.skip()
if d.default_selector_score < 0:
pytest.skip()
Expand Down Expand Up @@ -244,5 +244,5 @@ def test_invalid_capsule():
def test_multi_device_different_platforms():
devs = dpctl.get_devices() # all devices
if len(devs) > 1:
with pytest.raises(ValueError):
with pytest.raises(dpctl.SyclContextCreationError):
dpctl.SyclContext(devs)
Loading