Skip to content

Commit c41c5c1

Browse files
author
Diptorup Deb
committed
Expose SYCL's device selector classes to Python API.
1 parent 7baf73b commit c41c5c1

File tree

1 file changed

+156
-16
lines changed

1 file changed

+156
-16
lines changed

dpctl/_sycl_device.pyx

Lines changed: 156 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,41 @@
2020
""" Implements SyclDevice Cython extension type.
2121
"""
2222

23-
from ._backend cimport *
23+
from ._backend cimport (
24+
DPCTLAcceleratorSelector_Create,
25+
DPCTLCPUSelector_Create,
26+
DPCTLDefaultSelector_Create,
27+
DPCTLGPUSelector_Create,
28+
DPCTLHostSelector_Create,
29+
DPCTLCString_Delete,
30+
DPCTLDevice_CreateFromSelector,
31+
DPCTLDevice_Delete,
32+
DPCTLDevice_DumpInfo,
33+
DPCTLDevice_GetVendorName,
34+
DPCTLDevice_GetName,
35+
DPCTLDevice_GetDriverInfo,
36+
DPCTLDevice_GetMaxComputeUnits,
37+
DPCTLDevice_GetMaxWorkItemDims,
38+
DPCTLDevice_GetMaxWorkItemSizes,
39+
DPCTLDevice_GetMaxWorkGroupSize,
40+
DPCTLDevice_GetMaxNumSubGroups,
41+
DPCTLDevice_HasInt64BaseAtomics,
42+
DPCTLDevice_HasInt64ExtendedAtomics,
43+
DPCTLDevice_IsGPU,
44+
DPCTLDevice_IsCPU,
45+
DPCTLDeviceSelector_Delete,
46+
DPCTLSize_t_Array_Delete,
47+
DPCTLSyclDeviceRef,
48+
)
2449
from . import device_type
2550

2651
__all__ = [
2752
"SyclDevice",
53+
"select_accelerator_device",
54+
"select_cpu_device",
55+
"select_default_device",
56+
"select_gpu_device",
57+
"select_host_device",
2858
]
2959

3060

@@ -33,7 +63,7 @@ cdef class SyclDevice:
3363
"""
3464

3565
@staticmethod
36-
cdef SyclDevice _create (DPCTLSyclDeviceRef dref):
66+
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
3767
cdef SyclDevice ret = SyclDevice.__new__(SyclDevice)
3868
ret._device_ref = dref
3969
ret._vendor_name = DPCTLDevice_GetVendorName(dref)
@@ -48,24 +78,28 @@ cdef class SyclDevice:
4878
ret._int64_extended_atomics = DPCTLDevice_HasInt64ExtendedAtomics(dref)
4979
return ret
5080

51-
def __dealloc__ (self):
81+
82+
def __dealloc__(self):
5283
DPCTLDevice_Delete(self._device_ref)
5384
DPCTLCString_Delete(self._device_name)
5485
DPCTLCString_Delete(self._vendor_name)
5586
DPCTLCString_Delete(self._driver_version)
5687
DPCTLSize_t_Array_Delete(self._max_work_item_sizes)
5788

58-
def dump_device_info (self):
89+
90+
def dump_device_info(self):
5991
""" Print information about the SYCL device.
6092
"""
6193
DPCTLDevice_DumpInfo(self._device_ref)
6294

63-
cpdef get_device_name (self):
95+
96+
cpdef get_device_name(self):
6497
""" Returns the name of the device as a string
6598
"""
6699
return self._device_name.decode()
67100

68-
cpdef get_device_type (self):
101+
102+
cpdef get_device_type(self):
69103
""" Returns the type of the device as a `device_type` enum
70104
"""
71105
if DPCTLDevice_IsGPU(self._device_ref):
@@ -75,36 +109,42 @@ cdef class SyclDevice:
75109
else:
76110
raise ValueError("Unknown device type.")
77111

78-
cpdef get_vendor_name (self):
112+
113+
cpdef get_vendor_name(self):
79114
""" Returns the device vendor name as a string
80115
"""
81116
return self._vendor_name.decode()
82117

83-
cpdef get_driver_version (self):
118+
119+
cpdef get_driver_version(self):
84120
""" Returns the OpenCL software driver version as a string
85121
in the form: major number.minor number, if this SYCL
86122
device is an OpenCL device. Returns a string class
87123
with the value "1.2" if this SYCL device is a host device.
88124
"""
89125
return self._driver_version.decode()
90126

91-
cpdef has_int64_base_atomics (self):
127+
128+
cpdef has_int64_base_atomics(self):
92129
""" Returns true if device has int64_base_atomics else returns false.
93130
"""
94131
return self._int64_base_atomics
95132

96-
cpdef has_int64_extended_atomics (self):
133+
134+
cpdef has_int64_extended_atomics(self):
97135
""" Returns true if device has int64_extended_atomics else returns false.
98136
"""
99137
return self._int64_extended_atomics
100138

101-
cpdef get_max_compute_units (self):
139+
140+
cpdef get_max_compute_units(self):
102141
""" Returns the number of parallel compute units
103142
available to the device. The minimum value is 1.
104143
"""
105144
return self._max_compute_units
106145

107-
cpdef get_max_work_item_dims (self):
146+
147+
cpdef get_max_work_item_dims(self):
108148
""" Returns the maximum dimensions that specify
109149
the global and local work-item IDs used by the
110150
data parallel execution model. The minimum
@@ -113,7 +153,8 @@ cdef class SyclDevice:
113153
"""
114154
return self._max_work_item_dims
115155

116-
cpdef get_max_work_item_sizes (self):
156+
157+
cpdef get_max_work_item_sizes(self):
117158
""" Returns the maximum number of work-items
118159
that are permitted in each dimension of the
119160
work-group of the nd_range. The minimum
@@ -125,27 +166,31 @@ cdef class SyclDevice:
125166
max_work_item_sizes.append(self._max_work_item_sizes[n])
126167
return tuple(max_work_item_sizes)
127168

128-
cpdef get_max_work_group_size (self):
169+
170+
cpdef get_max_work_group_size(self):
129171
""" Returns the maximum number of work-items
130172
that are permitted in a work-group executing a
131173
kernel on a single compute unit. The minimum
132174
value is 1.
133175
"""
134176
return self._max_work_group_size
135177

136-
cpdef get_max_num_sub_groups (self):
178+
179+
cpdef get_max_num_sub_groups(self):
137180
""" Returns the maximum number of sub-groups
138181
in a work-group for any kernel executed on the
139182
device. The minimum value is 1.
140183
"""
141184
return self._max_num_sub_groups
142185

186+
143187
cdef DPCTLSyclDeviceRef get_device_ref (self):
144188
""" Returns the DPCTLSyclDeviceRef pointer for this class.
145189
"""
146190
return self._device_ref
147191

148-
def addressof_ref (self):
192+
193+
def addressof_ref(self):
149194
"""
150195
Returns the address of the DPCTLSyclDeviceRef pointer as a size_t.
151196
@@ -154,3 +199,98 @@ cdef class SyclDevice:
154199
SyclDevice cast to a size_t.
155200
"""
156201
return int(<size_t>self._device_ref)
202+
203+
204+
cpdef select_accelerator_device():
205+
""" A wrapper for SYCL's `accelerator_selector` device_selector class.
206+
207+
Returns:
208+
A new SyclDevice object containing the SYCL device returned by the
209+
`accelerator_selector`.
210+
Raises:
211+
A ValueError is raised if the SYCL `accelerator_selector` is unable to
212+
select a device.
213+
"""
214+
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLAcceleratorSelector_Create()
215+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
216+
DPCTLDeviceSelector_Delete(DSRef)
217+
if DRef is NULL:
218+
raise ValueError("Device unavailable.")
219+
Device = SyclDevice._create(DRef)
220+
return Device
221+
222+
223+
cpdef select_cpu_device():
224+
""" A wrapper for SYCL's `cpu_selector` device_selector class.
225+
226+
Returns:
227+
A new SyclDevice object containing the SYCL device returned by the
228+
`cpu_selector`.
229+
Raises:
230+
A ValueError is raised if the SYCL `cpu_seector` is unable to select a
231+
device.
232+
"""
233+
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLCPUSelector_Create()
234+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
235+
DPCTLDeviceSelector_Delete(DSRef)
236+
if DRef is NULL:
237+
raise ValueError("Device unavailable.")
238+
Device = SyclDevice._create(DRef)
239+
return Device
240+
241+
242+
cpdef select_default_device():
243+
""" A wrapper for SYCL's `default_selector` device_selector class.
244+
245+
Returns:
246+
A new SyclDevice object containing the SYCL device returned by the
247+
`default_selector`.
248+
Raises:
249+
A ValueError is raised if the SYCL `default_seector` is unable to
250+
select a device.
251+
"""
252+
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLDefaultSelector_Create()
253+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
254+
DPCTLDeviceSelector_Delete(DSRef)
255+
if DRef is NULL:
256+
raise ValueError("Device unavailable.")
257+
Device = SyclDevice._create(DRef)
258+
return Device
259+
260+
261+
cpdef select_gpu_device():
262+
""" A wrapper for SYCL's `gpu_selector` device_selector class.
263+
264+
Returns:
265+
A new SyclDevice object containing the SYCL device returned by the
266+
`gpu_selector`.
267+
Raises:
268+
A ValueError is raised if the SYCL `gpu_seector` is unable to select a
269+
device.
270+
"""
271+
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLGPUSelector_Create()
272+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
273+
DPCTLDeviceSelector_Delete(DSRef)
274+
if DRef is NULL:
275+
raise ValueError("Device unavailable.")
276+
Device = SyclDevice._create(DRef)
277+
return Device
278+
279+
280+
cpdef select_host_device():
281+
""" A wrapper for SYCL's `host_selector` device_selector class.
282+
283+
Returns:
284+
A new SyclDevice object containing the SYCL device returned by the
285+
`host_selector`.
286+
Raises:
287+
A ValueError is raised if the SYCL `host_seector` is unable to select a
288+
device.
289+
"""
290+
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLHostSelector_Create()
291+
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
292+
DPCTLDeviceSelector_Delete(DSRef)
293+
if DRef is NULL:
294+
raise ValueError("Device unavailable.")
295+
Device = SyclDevice._create(DRef)
296+
return Device

0 commit comments

Comments
 (0)