Skip to content

Commit 45ee558

Browse files
Changed error type raised on failure to create SyclDevice
It used to be ValueError, now SyclDeviceCreationError is raised. removed incorrect comment Adjusted example sycl_queue to handle error creating sub-devices lower the value of partition keyword from 4 to 2
1 parent ecae1c6 commit 45ee558

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

dpctl/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
__author__ = "Intel Corp."
3434

3535
from dpctl._sycl_context import SyclContext
36-
from dpctl._sycl_device import SyclDevice
36+
from dpctl._sycl_device import (
37+
SyclDevice,
38+
SyclDeviceCreationError,
39+
SyclSubDeviceCreationError,
40+
)
3741
from dpctl._sycl_device_factory import (
3842
get_devices,
3943
get_num_devices,
@@ -76,6 +80,8 @@
7680
]
7781
__all__ += [
7882
"SyclDevice",
83+
"SyclDeviceCreationError",
84+
"SyclSubDeviceCreationError",
7985
]
8086
__all__ += [
8187
"get_devices",

dpctl/_sycl_device.pyx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,20 @@ import collections
9595
import warnings
9696

9797
__all__ = [
98-
"SyclDevice",
98+
"SyclDevice", "SyclDeviceCreationError", "SyclSubDeviceCreationError",
9999
]
100100

101101

102-
cdef class SubDeviceCreationError(Exception):
102+
cdef class SyclDeviceCreationError(Exception):
103+
"""
104+
A DeviceCreationError exception is raised when
105+
device could not created.
106+
107+
"""
108+
pass
109+
110+
111+
cdef class SyclSubDeviceCreationError(Exception):
103112
"""
104113
A SubDeviceCreationError exception is raised when
105114
sub-devices were not created.
@@ -165,6 +174,7 @@ cdef str _device_type_to_filter_string_part(_device_type DTy):
165174
else:
166175
return "unknown"
167176

177+
168178
cdef void _init_helper(_SyclDevice device, DPCTLSyclDeviceRef DRef):
169179
"Populate attributes of device from opaque device reference DRef"
170180
device._device_ref = DRef
@@ -262,20 +272,20 @@ cdef class SyclDevice(_SyclDevice):
262272
DSRef = DPCTLFilterSelector_Create(filter_c_str)
263273
ret = self._init_from_selector(DSRef)
264274
if ret == -1:
265-
raise ValueError(
275+
raise SyclDeviceCreationError(
266276
"Could not create a SyclDevice with the selector string"
267277
)
268278
elif isinstance(arg, _SyclDevice):
269279
ret = self._init_from__SyclDevice(arg)
270280
if ret == -1:
271-
raise ValueError(
281+
raise SyclDeviceCreationError(
272282
"Could not create a SyclDevice from _SyclDevice instance"
273283
)
274284
elif arg is None:
275285
DSRef = DPCTLDefaultSelector_Create()
276286
ret = self._init_from_selector(DSRef)
277287
if ret == -1:
278-
raise ValueError(
288+
raise SyclDeviceCreationError(
279289
"Could not create a SyclDevice from default selector"
280290
)
281291
else:
@@ -746,7 +756,7 @@ cdef class SyclDevice(_SyclDevice):
746756
if count > 0:
747757
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
748758
if DVRef is NULL:
749-
raise SubDeviceCreationError(
759+
raise SyclSubDeviceCreationError(
750760
"Sub-devices were not created." if (count > 0) else
751761
"Sub-devices were not created, "
752762
"requested compute units count was zero."
@@ -785,7 +795,7 @@ cdef class SyclDevice(_SyclDevice):
785795
)
786796
free(counts_buff)
787797
if DVRef is NULL:
788-
raise SubDeviceCreationError(
798+
raise SyclSubDeviceCreationError(
789799
"Sub-devices were not created." if (min_count > 0) else
790800
"Sub-devices were not created, "
791801
"sub-device execution units counts must be positive."
@@ -801,7 +811,7 @@ cdef class SyclDevice(_SyclDevice):
801811
cdef DPCTLDeviceVectorRef DVRef = NULL
802812
DVRef = DPCTLDevice_CreateSubDevicesByAffinity(self._device_ref, domain)
803813
if DVRef is NULL:
804-
raise SubDeviceCreationError("Sub-devices were not created.")
814+
raise SyclSubDeviceCreationError("Sub-devices were not created.")
805815
return _get_devices(DVRef)
806816

807817
def create_sub_devices(self, **kwargs):

dpctl/tests/test_sycl_device.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pytest
2121

2222
import dpctl
23-
from dpctl._sycl_device import SubDeviceCreationError
23+
from dpctl import SyclDeviceCreationError, SyclSubDeviceCreationError
2424

2525
list_of_standard_selectors = [
2626
dpctl.select_accelerator_device,
@@ -363,7 +363,7 @@ def check_create_sub_devices_equally(device):
363363
try:
364364
n = int(device.max_compute_units / 2)
365365
device.create_sub_devices(partition=n)
366-
except SubDeviceCreationError:
366+
except SyclSubDeviceCreationError:
367367
pytest.skip(
368368
"create_sub_devices can't create sub-devices on this device"
369369
)
@@ -382,7 +382,7 @@ def check_create_sub_devices_by_counts(device):
382382
try:
383383
n = device.max_compute_units / 2
384384
device.create_sub_devices(partition=(n, n))
385-
except SubDeviceCreationError:
385+
except SyclSubDeviceCreationError:
386386
pytest.skip(
387387
"create_sub_devices can't create sub-devices on this device"
388388
)
@@ -400,7 +400,7 @@ def check_create_sub_devices_by_counts_zeros(device):
400400
def check_create_sub_devices_by_affinity_not_applicable(device):
401401
try:
402402
device.create_sub_devices(partition="not_applicable")
403-
except SubDeviceCreationError:
403+
except SyclSubDeviceCreationError:
404404
pytest.skip(
405405
"create_sub_devices can't create sub-devices on this device"
406406
)
@@ -411,7 +411,7 @@ def check_create_sub_devices_by_affinity_not_applicable(device):
411411
def check_create_sub_devices_by_affinity_numa(device):
412412
try:
413413
device.create_sub_devices(partition="numa")
414-
except SubDeviceCreationError:
414+
except SyclSubDeviceCreationError:
415415
pytest.skip(
416416
"create_sub_devices can't create sub-devices on this device"
417417
)
@@ -422,7 +422,7 @@ def check_create_sub_devices_by_affinity_numa(device):
422422
def check_create_sub_devices_by_affinity_L4_cache(device):
423423
try:
424424
device.create_sub_devices(partition="L4_cache")
425-
except SubDeviceCreationError:
425+
except SyclSubDeviceCreationError:
426426
pytest.skip(
427427
"create_sub_devices can't create sub-devices on this device"
428428
)
@@ -433,7 +433,7 @@ def check_create_sub_devices_by_affinity_L4_cache(device):
433433
def check_create_sub_devices_by_affinity_L3_cache(device):
434434
try:
435435
device.create_sub_devices(partition="L3_cache")
436-
except SubDeviceCreationError:
436+
except SyclSubDeviceCreationError:
437437
pytest.skip(
438438
"create_sub_devices can't create sub-devices on this device"
439439
)
@@ -444,7 +444,7 @@ def check_create_sub_devices_by_affinity_L3_cache(device):
444444
def check_create_sub_devices_by_affinity_L2_cache(device):
445445
try:
446446
device.create_sub_devices(partition="L2_cache")
447-
except SubDeviceCreationError:
447+
except SyclSubDeviceCreationError:
448448
pytest.skip(
449449
"create_sub_devices can't create sub-devices on this device"
450450
)
@@ -455,7 +455,7 @@ def check_create_sub_devices_by_affinity_L2_cache(device):
455455
def check_create_sub_devices_by_affinity_L1_cache(device):
456456
try:
457457
device.create_sub_devices(partition="L1_cache")
458-
except SubDeviceCreationError:
458+
except SyclSubDeviceCreationError:
459459
pytest.skip(
460460
"create_sub_devices can't create sub-devices on this device"
461461
)
@@ -466,7 +466,7 @@ def check_create_sub_devices_by_affinity_L1_cache(device):
466466
def check_create_sub_devices_by_affinity_next_partitionable(device):
467467
try:
468468
device.create_sub_devices(partition="next_partitionable")
469-
except SubDeviceCreationError:
469+
except SyclSubDeviceCreationError:
470470
pytest.skip(
471471
"create_sub_devices can't create sub-devices on this device"
472472
)
@@ -613,7 +613,12 @@ def test_invalid_filter_selectors(invalid_filter):
613613
"""
614614
An invalid filter string should always be caught and a ValueError raised.
615615
"""
616-
with pytest.raises(ValueError):
616+
exc = (
617+
SyclDeviceCreationError
618+
if isinstance(invalid_filter, str)
619+
else ValueError
620+
)
621+
with pytest.raises(exc):
617622
dpctl.SyclDevice(invalid_filter)
618623

619624

examples/python/sycl_queue.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ def create_queue_from_subdevice():
5555
Create a queue from a sub-device.
5656
"""
5757
cpu_d = dpctl.SyclDevice("opencl:cpu:0")
58-
sub_devs = cpu_d.create_sub_devices(partition=4)
58+
try:
59+
sub_devs = cpu_d.create_sub_devices(partition=2)
60+
except dpctl.SyclSubDeviceCreationError:
61+
print("Could not create sub device.")
62+
print(f"{cpu_d} has {cpu_d.max_compute_units} compute units")
63+
return
5964
q = dpctl.SyclQueue(sub_devs[0])
6065
# a single-device context is created automatically
6166
print(
@@ -69,10 +74,14 @@ def create_queue_from_subdevice_multidevice_context():
6974
Create a queue from a sub-device.
7075
"""
7176
cpu_d = dpctl.SyclDevice("opencl:cpu:0")
72-
sub_devs = cpu_d.create_sub_devices(partition=4)
77+
try:
78+
sub_devs = cpu_d.create_sub_devices(partition=2)
79+
except dpctl.SyclSubDeviceCreationError:
80+
print("Could not create sub device.")
81+
print(f"{cpu_d} has {cpu_d.max_compute_units} compute units")
82+
return
7383
ctx = dpctl.SyclContext(sub_devs)
74-
q = dpctl.SyclQueue(ctx, sub_devs[0])
75-
# a single-device context is created automatically
84+
q = dpctl.SyclQueue(ctx, sub_devs[0], partition="enable_profiling")
7685
print(
7786
"Number of devices in SyclContext " "associated with the queue: ",
7887
q.sycl_context.device_count,

0 commit comments

Comments
 (0)