Skip to content

Commit 19e7b49

Browse files
author
Diptorup Deb
authored
Merge pull request #321 from IntelPython/fix/refactor_SyclDevice
Improvements to the SyclDevice class by @oleksandr-pavlyk
2 parents 056b332 + 6f12497 commit 19e7b49

File tree

2 files changed

+146
-142
lines changed

2 files changed

+146
-142
lines changed

dpctl/_sycl_device.pxd

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ cdef class _SyclDevice:
4444
cdef size_t *_max_work_item_sizes
4545
cdef size_t _max_work_group_size
4646
cdef uint32_t _max_num_sub_groups
47+
48+
49+
cdef class SyclDevice(_SyclDevice):
50+
@staticmethod
51+
cdef SyclDevice _create(DPCTLSyclDeviceRef dref)
52+
@staticmethod
53+
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef)
54+
cdef int _init_from__SyclDevice(self, _SyclDevice other)
55+
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef)
4756
cdef DPCTLSyclDeviceRef get_device_ref(self)
4857
cpdef get_backend(self)
4958
cpdef get_device_name(self)
@@ -59,13 +68,3 @@ cdef class _SyclDevice:
5968
cpdef is_cpu(self)
6069
cpdef is_gpu(self)
6170
cpdef is_host(self)
62-
63-
64-
cdef class SyclDevice(_SyclDevice):
65-
@staticmethod
66-
cdef SyclDevice _create(DPCTLSyclDeviceRef dref)
67-
@staticmethod
68-
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef)
69-
cdef void _init_from__SyclDevice(self, _SyclDevice other)
70-
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef)
71-

dpctl/_sycl_device.pyx

Lines changed: 137 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ from ._backend cimport (
2424
_aspect_type,
2525
_backend_type,
2626
_device_type,
27-
DPCTLDefaultSelector_Create,
2827
DPCTLCString_Delete,
28+
DPCTLDefaultSelector_Create,
2929
DPCTLDevice_Copy,
3030
DPCTLDevice_CreateFromSelector,
3131
DPCTLDevice_Delete,
@@ -72,6 +72,139 @@ cdef class _SyclDevice:
7272
DPCTLCString_Delete(self._driver_version)
7373
DPCTLSize_t_Array_Delete(self._max_work_item_sizes)
7474

75+
76+
cdef class SyclDevice(_SyclDevice):
77+
""" Python equivalent for cl::sycl::device class.
78+
79+
There are two ways of creating a SyclDevice instance:
80+
81+
- by directly passing in a filter string to the class constructor. The
82+
filter string needs to conform to the the `DPC++ filter selector SYCL
83+
extension <https://bit.ly/37kqANT>`_.
84+
85+
:Example:
86+
.. code-block:: python
87+
88+
import dpctl
89+
90+
# Create a SyclDevice with an explicit filter string, in
91+
# this case the first level_zero gpu device.
92+
level_zero_gpu = dpctl.SyclDevice("level_zero:gpu:0"):
93+
level_zero_gpu.dump_device_info()
94+
95+
- by calling one of the device selector helper functions:
96+
97+
:func:`dpctl.select_accelerator_device()`,
98+
:func:`dpctl.select_cpu_device()`,
99+
:func:`dpctl.select_default_device()`,
100+
:func:`dpctl.select_gpu_device()`,
101+
:func:`dpctl.select_host_device()`.
102+
103+
104+
:Example:
105+
.. code-block:: python
106+
107+
import dpctl
108+
109+
# Create a SyclDevice of type GPU based on whatever is returned
110+
# by the SYCL `gpu_selector` device selector class.
111+
gpu = dpctl.select_gpu_device():
112+
gpu.dump_device_info()
113+
114+
"""
115+
@staticmethod
116+
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef):
117+
device._device_ref = DRef
118+
device._device_name = DPCTLDevice_GetName(DRef)
119+
device._driver_version = DPCTLDevice_GetDriverInfo(DRef)
120+
device._vendor_name = DPCTLDevice_GetVendorName(DRef)
121+
device._accelerator_device = DPCTLDevice_IsAccelerator(DRef)
122+
device._cpu_device = DPCTLDevice_IsCPU(DRef)
123+
device._gpu_device = DPCTLDevice_IsGPU(DRef)
124+
device._host_device = DPCTLDevice_IsHost(DRef)
125+
device._max_compute_units = DPCTLDevice_GetMaxComputeUnits(DRef)
126+
if (device._host_device):
127+
device._max_num_sub_groups = 0
128+
else:
129+
device._max_num_sub_groups = DPCTLDevice_GetMaxNumSubGroups(DRef)
130+
device._max_work_group_size = DPCTLDevice_GetMaxWorkGroupSize(DRef)
131+
device._max_work_item_dims = DPCTLDevice_GetMaxWorkItemDims(DRef)
132+
device._max_work_item_sizes = DPCTLDevice_GetMaxWorkItemSizes(DRef)
133+
134+
@staticmethod
135+
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
136+
cdef SyclDevice ret = <SyclDevice>_SyclDevice.__new__(_SyclDevice)
137+
# Initialize the attributes of the SyclDevice object
138+
SyclDevice._init_helper(ret, dref)
139+
return SyclDevice(ret)
140+
141+
cdef int _init_from__SyclDevice(self, _SyclDevice other):
142+
self._device_ref = DPCTLDevice_Copy(other._device_ref)
143+
if (self._device_ref is NULL):
144+
return -1
145+
self._device_name = DPCTLDevice_GetName(self._device_ref)
146+
self._driver_version = DPCTLDevice_GetDriverInfo(self._device_ref)
147+
self._max_compute_units = other._max_compute_units
148+
self._max_num_sub_groups = other._max_num_sub_groups
149+
self._max_work_group_size = other._max_work_group_size
150+
self._max_work_item_dims = other._max_work_item_dims
151+
self._max_work_item_sizes = (
152+
DPCTLDevice_GetMaxWorkItemSizes(self._device_ref)
153+
)
154+
self._vendor_name = DPCTLDevice_GetVendorName(self._device_ref)
155+
self._accelerator_device = other._accelerator_device
156+
self._cpu_device = other._cpu_device
157+
self._gpu_device = other._gpu_device
158+
self._host_device = other._host_device
159+
160+
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef):
161+
# Initialize the attributes of the SyclDevice object
162+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
163+
if DRef is NULL:
164+
return -1
165+
else:
166+
SyclDevice._init_helper(self, DRef)
167+
return 0
168+
169+
def __cinit__(self, arg=None):
170+
cdef DPCTLSyclDeviceSelectorRef DSRef = NULL
171+
cdef DPCTLSyclDeviceRef DRef = NULL
172+
cdef const char *filter_c_str = NULL
173+
cdef int ret = 0
174+
175+
if type(arg) is unicode:
176+
string = bytes(<unicode>arg, "utf-8")
177+
filter_c_str = string
178+
DSRef = DPCTLFilterSelector_Create(filter_c_str)
179+
ret = self._init_from_selector(DSRef)
180+
if ret == -1:
181+
raise ValueError("Could not create a Device with the selector")
182+
# Free up the device selector
183+
DPCTLDeviceSelector_Delete(DSRef)
184+
elif isinstance(arg, unicode):
185+
string = bytes(<unicode>unicode(arg), "utf-8")
186+
filter_c_str = string
187+
DSRef = DPCTLFilterSelector_Create(filter_c_str)
188+
ret = self._init_from_selector(DSRef)
189+
if ret == -1:
190+
raise ValueError("Could not create a Device with the selector")
191+
# Free up the device selector
192+
DPCTLDeviceSelector_Delete(DSRef)
193+
elif isinstance(arg, _SyclDevice):
194+
ret = self._init_from__SyclDevice(arg)
195+
if ret == -1:
196+
raise ValueError("Could not create a Device from _SyclDevice instance")
197+
elif arg is None:
198+
DSRef = DPCTLDefaultSelector_Create()
199+
ret = self._init_from_selector(DSRef)
200+
if ret == -1:
201+
raise ValueError("Could not create a Device from default selector")
202+
else:
203+
raise ValueError(
204+
"Invalid argument. Argument should be a str object specifying "
205+
"a SYCL filter selector string."
206+
)
207+
75208
def dump_device_info(self):
76209
""" Print information about the SYCL device.
77210
"""
@@ -240,136 +373,6 @@ cdef class _SyclDevice:
240373
"""
241374
return int(<size_t>self._device_ref)
242375

243-
@property
244-
def __name__(self):
245-
return "SyclDevice"
246-
247-
def __repr__(self):
248-
return "<dpctl." + self.__name__ + " at {}>".format(hex(id(self)))
249-
250-
251-
cdef class SyclDevice(_SyclDevice):
252-
""" Python equivalent for cl::sycl::device class.
253-
254-
There are two ways of creating a SyclDevice instance:
255-
256-
- by directly passing in a filter string to the class constructor. The
257-
filter string needs to conform to the the `DPC++ filter selector SYCL
258-
extension <https://bit.ly/37kqANT>`_.
259-
260-
:Example:
261-
.. code-block:: python
262-
263-
import dpctl
264-
265-
# Create a SyclDevice with an explicit filter string, in
266-
# this case the first level_zero gpu device.
267-
level_zero_gpu = dpctl.SyclDevice("level_zero:gpu:0"):
268-
level_zero_gpu.dump_device_info()
269-
270-
- by calling one of the device selector helper functions:
271-
272-
:func:`dpctl.select_accelerator_device()`,
273-
:func:`dpctl.select_cpu_device()`,
274-
:func:`dpctl.select_default_device()`,
275-
:func:`dpctl.select_gpu_device()`,
276-
:func:`dpctl.select_host_device()`.
277-
278-
279-
:Example:
280-
.. code-block:: python
281-
282-
import dpctl
283-
284-
# Create a SyclDevice of type GPU based on whatever is returned
285-
# by the SYCL `gpu_selector` device selector class.
286-
gpu = dpctl.select_gpu_device():
287-
gpu.dump_device_info()
288-
289-
"""
290-
@staticmethod
291-
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef):
292-
device._device_ref = DRef
293-
device._device_name = DPCTLDevice_GetName(DRef)
294-
device._driver_version = DPCTLDevice_GetDriverInfo(DRef)
295-
device._max_compute_units = DPCTLDevice_GetMaxComputeUnits(DRef)
296-
device._max_num_sub_groups = DPCTLDevice_GetMaxNumSubGroups(DRef)
297-
device._max_work_group_size = DPCTLDevice_GetMaxWorkGroupSize(DRef)
298-
device._max_work_item_dims = DPCTLDevice_GetMaxWorkItemDims(DRef)
299-
device._max_work_item_sizes = DPCTLDevice_GetMaxWorkItemSizes(DRef)
300-
device._vendor_name = DPCTLDevice_GetVendorName(DRef)
301-
device._accelerator_device = DPCTLDevice_IsAccelerator(DRef)
302-
device._cpu_device = DPCTLDevice_IsCPU(DRef)
303-
device._gpu_device = DPCTLDevice_IsGPU(DRef)
304-
device._host_device = DPCTLDevice_IsHost(DRef)
305-
306-
@staticmethod
307-
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
308-
cdef SyclDevice ret = <SyclDevice>_SyclDevice.__new__(_SyclDevice)
309-
# Initialize the attributes of the SyclDevice object
310-
SyclDevice._init_helper(ret, dref)
311-
return SyclDevice(ret)
312-
313-
cdef void _init_from__SyclDevice(self, _SyclDevice other):
314-
self._device_ref = DPCTLDevice_Copy(other._device_ref)
315-
self._device_name = DPCTLDevice_GetName(self._device_ref)
316-
self._driver_version = DPCTLDevice_GetDriverInfo(self._device_ref)
317-
self._max_compute_units = other._max_compute_units
318-
self._max_num_sub_groups = other._max_num_sub_groups
319-
self._max_work_group_size = other._max_work_group_size
320-
self._max_work_item_dims = other._max_work_item_dims
321-
self._max_work_item_sizes = (
322-
DPCTLDevice_GetMaxWorkItemSizes(self._device_ref)
323-
)
324-
self._vendor_name = DPCTLDevice_GetVendorName(self._device_ref)
325-
self._accelerator_device = other._accelerator_device
326-
self._cpu_device = other._cpu_device
327-
self._gpu_device = other._gpu_device
328-
self._host_device = other._host_device
329-
330-
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef):
331-
# Initialize the attributes of the SyclDevice object
332-
DRef = DPCTLDevice_CreateFromSelector(DSRef)
333-
if DRef is NULL:
334-
return -1
335-
else:
336-
SyclDevice._init_helper(self, DRef)
337-
return 0
338-
339-
def __cinit__(self, arg=None):
340-
cdef DPCTLSyclDeviceSelectorRef DSRef = NULL
341-
cdef DPCTLSyclDeviceRef DRef = NULL
342-
cdef const char *filter_c_str = NULL
343-
cdef int ret = 0
344-
345-
if type(arg) is unicode:
346-
string = bytes(<unicode>arg, "utf-8")
347-
filter_c_str = string
348-
DSRef = DPCTLFilterSelector_Create(filter_c_str)
349-
ret = self._init_from_selector(DSRef)
350-
if ret == -1:
351-
raise ValueError("Could not create a Device with the selector")
352-
# Free up the device selector
353-
DPCTLDeviceSelector_Delete(DSRef)
354-
elif isinstance(arg, unicode):
355-
string = bytes(unicode(arg), "utf-8")
356-
filter_c_str = <unicode>string
357-
DSRef = DPCTLFilterSelector_Create(filter_c_str)
358-
if ret == -1:
359-
raise ValueError("Could not create a Device with the selector")
360-
# Free up the device selector
361-
DPCTLDeviceSelector_Delete(DSRef)
362-
elif isinstance(arg, _SyclDevice):
363-
self._init_from__SyclDevice(arg)
364-
elif arg is None:
365-
DSRef = DPCTLDefaultSelector_Create()
366-
self._init_from_selector(DSRef)
367-
else:
368-
raise ValueError(
369-
"Invalid argument. Argument should be a str object specifying "
370-
"a SYCL filter selector string."
371-
)
372-
373376
@property
374377
def has_aspect_host(self):
375378
cdef _aspect_type AT = _aspect_type._host
@@ -465,4 +468,6 @@ cdef class SyclDevice(_SyclDevice):
465468
return "SyclDevice"
466469

467470
def __repr__(self):
468-
return "<dpctl." + self.__name__ + " at {}>".format(hex(id(self)))
471+
return ("<dpctl." + self.__name__ + " [" +
472+
str(self.get_backend()) + ", " + str(self.get_device_type()) +", " +
473+
" " + self.get_device_name() + "] at {}>".format(hex(id(self))) )

0 commit comments

Comments
 (0)