-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path_sycl_device_factory.pyx
More file actions
428 lines (362 loc) · 13.8 KB
/
_sycl_device_factory.pyx
File metadata and controls
428 lines (362 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Data Parallel Control (dpctl)
#
# Copyright 2020-2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# distutils: language = c++
# cython: language_level=3
# cython: linetrace=True
""" This module implements several device creation helper functions:
- wrapper functions to create a SyclDevice from the standard SYCL
device selector classes.
- functions to return a list of devices based on a specified device_type or
backend_type combination.
"""
from ._backend cimport ( # noqa: E211
DPCTLAcceleratorSelector_Create,
DPCTLCPUSelector_Create,
DPCTLDefaultSelector_Create,
DPCTLDevice_CreateFromSelector,
DPCTLDeviceMgr_GetDevices,
DPCTLDeviceMgr_GetNumDevices,
DPCTLDeviceSelector_Delete,
DPCTLDeviceVector_Delete,
DPCTLDeviceVector_GetAt,
DPCTLDeviceVector_Size,
DPCTLDeviceVectorRef,
DPCTLGPUSelector_Create,
DPCTLSyclDeviceRef,
DPCTLSyclDeviceSelectorRef,
_backend_type,
_device_type,
)
from contextvars import ContextVar
from ._sycl_device import SyclDeviceCreationError
from .enum_types import backend_type
from .enum_types import device_type as device_type_t
__all__ = [
"get_devices",
"select_accelerator_device",
"select_cpu_device",
"select_default_device",
"select_gpu_device",
"get_num_devices",
"has_cpu_devices",
"has_gpu_devices",
"has_accelerator_devices",
"_cached_default_device",
]
cdef _backend_type _string_to_dpctl_sycl_backend_ty(str backend_str):
backend_str = backend_str.strip().lower()
if backend_str == "all":
return _backend_type._ALL_BACKENDS
elif backend_str == "cuda":
return _backend_type._CUDA
elif backend_str == "hip":
return _backend_type._HIP
elif backend_str == "level_zero":
return _backend_type._LEVEL_ZERO
elif backend_str == "opencl":
return _backend_type._OPENCL
else:
return _backend_type._UNKNOWN_BACKEND
cdef _device_type _string_to_dpctl_sycl_device_ty(str dty_str):
dty_str = dty_str.strip().lower()
if dty_str == "accelerator":
return _device_type._ACCELERATOR
elif dty_str == "all":
return _device_type._ALL_DEVICES
elif dty_str == "automatic":
return _device_type._AUTOMATIC
elif dty_str == "cpu":
return _device_type._CPU
elif dty_str == "custom":
return _device_type._CUSTOM
elif dty_str == "gpu":
return _device_type._GPU
else:
return _device_type._UNKNOWN_DEVICE
cdef _backend_type _enum_to_dpctl_sycl_backend_ty(BTy):
if BTy == backend_type.all:
return _backend_type._ALL_BACKENDS
elif BTy == backend_type.hip:
return _backend_type._HIP
elif BTy == backend_type.cuda:
return _backend_type._CUDA
elif BTy == backend_type.level_zero:
return _backend_type._LEVEL_ZERO
elif BTy == backend_type.opencl:
return _backend_type._OPENCL
else:
return _backend_type._UNKNOWN_BACKEND
cdef _device_type _enum_to_dpctl_sycl_device_ty(DTy):
if DTy == device_type_t.all:
return _device_type._ALL_DEVICES
elif DTy == device_type_t.accelerator:
return _device_type._ACCELERATOR
elif DTy == device_type_t.automatic:
return _device_type._AUTOMATIC
elif DTy == device_type_t.cpu:
return _device_type._CPU
elif DTy == device_type_t.custom:
return _device_type._CUSTOM
elif DTy == device_type_t.gpu:
return _device_type._GPU
else:
return _device_type._UNKNOWN_DEVICE
cdef list _get_devices(DPCTLDeviceVectorRef DVRef):
cdef list devices = []
cdef size_t nelems = 0
if DVRef:
nelems = DPCTLDeviceVector_Size(DVRef)
for i in range(0, nelems):
DRef = DPCTLDeviceVector_GetAt(DVRef, i)
D = SyclDevice._create(DRef)
devices.append(D)
return devices
cpdef list get_devices(backend=backend_type.all, device_type=device_type_t.all):
"""
Returns a list of :class:`dpctl.SyclDevice` instances selected based on
the given :class:`dpctl.device_type` and :class:`dpctl.backend_type` values.
The function is analogous to ``sycl::devices::get_devices()``, but with an
additional functionality that allows filtering SYCL devices based on
``backend`` in addition to only ``device_type``.
Args:
backend (optional):
A :class:`dpctl.backend_type` enum value or a string that
specifies a SYCL backend. Currently, accepted values are: "cuda",
"hip", "opencl", "level_zero", or "all".
Default: ``dpctl.backend_type.all``.
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".
Default: ``dpctl.device_type.all``.
Returns:
list:
A list of available :class:`dpctl.SyclDevice` instances that
satisfy the provided :class:`dpctl.backend_type` and
:class:`dpctl.device_type` values.
"""
cdef _backend_type BTy = _backend_type._ALL_BACKENDS
cdef _device_type DTy = _device_type._ALL_DEVICES
cdef DPCTLDeviceVectorRef DVRef = NULL
cdef list devices
if isinstance(backend, str):
BTy = _string_to_dpctl_sycl_backend_ty(backend)
elif isinstance(backend, backend_type):
BTy = _enum_to_dpctl_sycl_backend_ty(backend)
else:
raise TypeError(
"backend should be specified as a str or an "
"``enum_types.backend_type``."
)
if isinstance(device_type, str):
DTy = _string_to_dpctl_sycl_device_ty(device_type)
elif isinstance(device_type, device_type_t):
DTy = _enum_to_dpctl_sycl_device_ty(device_type)
else:
raise TypeError(
"device type should be specified as a str or an "
"``enum_types.device_type``."
)
DVRef = DPCTLDeviceMgr_GetDevices(BTy | DTy)
devices = _get_devices(DVRef)
DPCTLDeviceVector_Delete(DVRef)
return devices
cpdef int get_num_devices(
backend=backend_type.all, device_type=device_type_t.all
):
"""
A helper function to return the number of SYCL devices of a given
:class:`dpctl.device_type` and :class:`dpctl.backend_type`.
Args:
backend (optional):
A :class:`dpctl.backend_type` enum value or a string that
specifies a SYCL backend. Currently, accepted values are: "cuda",
"hip", "opencl", "level_zero", or "all".
Default: ``dpctl.backend_type.all``.
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".
Default: ``dpctl.device_type.all``.
Returns:
int:
The number of available SYCL devices that satisfy the provided
:py:class:`dpctl.backend_type` and :py:class:`dpctl.device_type`
values.
"""
cdef _backend_type BTy = _backend_type._ALL_BACKENDS
cdef _device_type DTy = _device_type._ALL_DEVICES
cdef int num_devices = 0
if isinstance(backend, str):
BTy = _string_to_dpctl_sycl_backend_ty(backend)
elif isinstance(backend, backend_type):
BTy = _enum_to_dpctl_sycl_backend_ty(backend)
else:
raise TypeError(
"backend should be specified as a ``str`` or an "
"``enum_types.backend_type``"
)
if isinstance(device_type, str):
DTy = _string_to_dpctl_sycl_device_ty(device_type)
elif isinstance(device_type, device_type_t):
DTy = _enum_to_dpctl_sycl_device_ty(device_type)
else:
raise TypeError(
"device type should be specified as a ``str`` or an "
"``enum_types.device_type``"
)
num_devices = DPCTLDeviceMgr_GetNumDevices(BTy | DTy)
return num_devices
cpdef cpp_bool has_cpu_devices():
""" A helper function to check if there are any SYCL CPU devices available.
Returns:
bool:
``True`` if ``sycl::device_type::cpu`` devices are present,
``False`` otherwise.
"""
cdef int num_cpu_dev = DPCTLDeviceMgr_GetNumDevices(_device_type._CPU)
return <cpp_bool>num_cpu_dev
cpdef cpp_bool has_gpu_devices():
""" A helper function to check if there are any SYCL GPU devices available.
Returns:
bool:
``True`` if ``sycl::device_type::gpu`` devices are present,
``False`` otherwise.
"""
cdef int num_gpu_dev = DPCTLDeviceMgr_GetNumDevices(_device_type._GPU)
return <cpp_bool>num_gpu_dev
cpdef cpp_bool has_accelerator_devices():
""" A helper function to check if there are any SYCL Accelerator devices
available.
Returns:
bool:
``True`` if ``sycl::device_type::accelerator`` devices are
present, ``False`` otherwise.
"""
cdef int num_accelerator_dev = DPCTLDeviceMgr_GetNumDevices(
_device_type._ACCELERATOR
)
return <cpp_bool>num_accelerator_dev
cpdef SyclDevice select_accelerator_device():
"""A wrapper for ``sycl::device{sycl::accelerator_selector_v}`` constructor.
Returns:
dpctl.SyclDevice:
A Python object wrapping the SYCL ``device``
returned by the SYCL ``accelerator_selector``.
Raises:
dpctl.SyclDeviceCreationError:
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 SyclDeviceCreationError("Accelerator device is unavailable.")
Device = SyclDevice._create(DRef)
return Device
cpdef SyclDevice select_cpu_device():
"""A wrapper for ``sycl::device{sycl::cpu_selector_v}`` constructor.
Returns:
dpctl.SyclDevice:
A Python object wrapping the SYCL ``device``
returned by the SYCL ``cpu_selector``.
Raises:
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 SyclDeviceCreationError("CPU device is unavailable.")
Device = SyclDevice._create(DRef)
return Device
cpdef SyclDevice select_default_device():
"""A wrapper for ``sycl::device{sycl::default_selector_v}`` constructor.
Returns:
dpctl.SyclDevice:
A Python object wrapping the SYCL ``device``
returned by the SYCL ``default_selector``.
Raises:
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 SyclDeviceCreationError("Default device is unavailable.")
Device = SyclDevice._create(DRef)
return Device
cpdef SyclDevice select_gpu_device():
"""A wrapper for ``sycl::device{sycl::gpu_selector_v}`` constructor.
Returns:
dpctl.SyclDevice:
A Python object wrapping the SYCL ``device``
returned by the SYCL ``gpu_selector``.
Raises:
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 SyclDeviceCreationError("Device unavailable.")
Device = SyclDevice._create(DRef)
return Device
cdef class _DefaultDeviceCache:
cdef dict __device_map__
def __cinit__(self):
self.__device_map__ = dict()
cdef get_or_create(self):
"""Return instance of SyclDevice and indicator if cache
has been modified"""
key = 0
if key in self.__device_map__:
return self.__device_map__[key], False
dev = select_default_device()
self.__device_map__[key] = dev
return dev, True
cdef _update_map(self, dev_map):
self.__device_map__.update(dev_map)
def __copy__(self):
cdef _DefaultDeviceCache _copy = _DefaultDeviceCache.__new__(
_DefaultDeviceCache)
_copy._update_map(self.__device_map__)
return _copy
_global_default_device_cache = ContextVar(
'global_default_device_cache',
default=_DefaultDeviceCache()
)
cpdef SyclDevice _cached_default_device():
"""Returns a cached device selected by default selector.
Returns:
dpctl.SyclDevice:
A cached default-selected SYCL device.
"""
cdef _DefaultDeviceCache _cache = _global_default_device_cache.get()
d_, changed_ = _cache.get_or_create()
if changed_: _global_default_device_cache.set(_cache)
return d_