@@ -27,6 +27,7 @@ from ._backend cimport (
27
27
DPCTLGPUSelector_Create,
28
28
DPCTLHostSelector_Create,
29
29
DPCTLCString_Delete,
30
+ DPCTLDevice_Copy,
30
31
DPCTLDevice_CreateFromSelector,
31
32
DPCTLDevice_Delete,
32
33
DPCTLDevice_DumpInfo,
@@ -66,7 +67,6 @@ cdef class _SyclDevice:
66
67
""" A helper metaclass to abstract a cl::sycl::device instance.
67
68
"""
68
69
69
-
70
70
def __dealloc__ (self ):
71
71
DPCTLDevice_Delete(self ._device_ref)
72
72
DPCTLCString_Delete(self ._device_name)
@@ -228,6 +228,11 @@ cdef class _SyclDevice:
228
228
return int (< size_t> self ._device_ref)
229
229
230
230
231
+ @property
232
+ def __name__ :
233
+ return " SyclDevice"
234
+
235
+
231
236
cdef class SyclDevice(_SyclDevice):
232
237
""" Python equivalent for cl::sycl::device class.
233
238
@@ -267,7 +272,6 @@ cdef class SyclDevice(_SyclDevice):
267
272
gpu.dump_device_info()
268
273
269
274
"""
270
-
271
275
@staticmethod
272
276
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef):
273
277
device._device_ref = DRef
@@ -288,32 +292,83 @@ cdef class SyclDevice(_SyclDevice):
288
292
device._gpu_device = DPCTLDevice_IsGPU(DRef)
289
293
device._host_device = DPCTLDevice_IsHost(DRef)
290
294
291
-
292
- def __cinit__ (self , filter_str ):
293
- cdef const char * filter_c_str = NULL
294
- if type (filter_str) is unicode :
295
- string = bytes(< unicode > filter_str, " utf-8" )
296
- filter_c_str = string
297
- elif isinstance (filter_str, unicode ):
298
- string = bytes(unicode (filter_str), " utf-8" )
299
- filter_c_str = < unicode > string
300
- cdef DPCTLSyclDeviceSelectorRef DSRef = (
301
- DPCTLFilterSelector_Create(filter_c_str)
302
- )
303
- cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
304
- if DRef is NULL :
305
- raise ValueError (" Device could not be created from provided filter" )
306
- # Initialize the attributes of the SyclDevice object
307
- SyclDevice._init_helper(self , DRef)
308
- # Free up the device selector
309
- DPCTLDeviceSelector_Delete(DSRef)
310
-
311
295
@staticmethod
312
296
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
313
297
cdef SyclDevice ret = < SyclDevice> _SyclDevice.__new__ (_SyclDevice)
314
298
# Initialize the attributes of the SyclDevice object
315
299
SyclDevice._init_helper(ret, dref)
316
- return ret
300
+ return SyclDevice(ret)
301
+
302
+ cdef void _init_from__SyclDevice(self , _SyclDevice other):
303
+ self ._device_ref = DPCTLDevice_Copy(other._device_ref)
304
+ self ._device_name = DPCTLDevice_GetName(self ._device_ref)
305
+ self ._driver_version = DPCTLDevice_GetDriverInfo(self ._device_ref)
306
+ self ._int64_base_atomics = other._int64_base_atomics
307
+ self ._int64_extended_atomics = other._int64_extended_atomics
308
+ self ._max_compute_units = other._max_compute_units
309
+ self ._max_num_sub_groups = other._max_num_sub_groups
310
+ self ._max_work_group_size = other._max_work_group_size
311
+ self ._max_work_item_dims = other._max_work_item_dims
312
+ self ._max_work_item_sizes = (
313
+ DPCTLDevice_GetMaxWorkItemSizes(self ._device_ref)
314
+ )
315
+ self ._vendor_name = DPCTLDevice_GetVendorName(self ._device_ref)
316
+ self ._accelerator_device = other._accelerator_device
317
+ self ._cpu_device = other._cpu_device
318
+ self ._gpu_device = other._gpu_device
319
+ self ._host_device = other._host_device
320
+
321
+ cdef int _init_from_selector(self , DPCTLSyclDeviceSelectorRef DSRef):
322
+ # Initialize the attributes of the SyclDevice object
323
+ DRef = DPCTLDevice_CreateFromSelector(DSRef)
324
+ if DRef is NULL :
325
+ return - 1
326
+ else :
327
+ SyclDevice._init_helper(self , DRef)
328
+ return 0
329
+
330
+ def __cinit__ (self , arg = None ):
331
+ cdef DPCTLSyclDeviceSelectorRef DSRef = NULL
332
+ cdef DPCTLSyclDeviceRef DRef = NULL
333
+ cdef const char * filter_c_str = NULL
334
+ cdef int ret = 0
335
+
336
+ if type (arg) is unicode :
337
+ string = bytes(< unicode > arg, " utf-8" )
338
+ filter_c_str = string
339
+ DSRef = DPCTLFilterSelector_Create(filter_c_str)
340
+ ret = self ._init_from_selector(DSRef)
341
+ if ret == - 1 :
342
+ raise ValueError (" Could not create a Device with the selector" )
343
+ # Free up the device selector
344
+ DPCTLDeviceSelector_Delete(DSRef)
345
+ elif isinstance (arg, unicode ):
346
+ string = bytes(unicode (arg), " utf-8" )
347
+ filter_c_str = < unicode > string
348
+ DSRef = DPCTLFilterSelector_Create(filter_c_str)
349
+ if ret == - 1 :
350
+ raise ValueError (" Could not create a Device with the selector" )
351
+ # Free up the device selector
352
+ DPCTLDeviceSelector_Delete(DSRef)
353
+ elif isinstance (arg, _SyclDevice):
354
+ self ._init_from__SyclDevice(arg)
355
+ elif arg is None :
356
+ DSRef = DPCTLDefaultSelector_Create()
357
+ self ._init_from_selector(DSRef)
358
+ else :
359
+ raise ValueError (
360
+ " Invalid argument. Argument should be a str object specifying "
361
+ " a SYCL filter selector string."
362
+ )
363
+
364
+
365
+ @property
366
+ def __name__ (self ):
367
+ return " SyclDevice"
368
+
369
+
370
+ def __repr__ (self ):
371
+ return " <dpctl." + self .__name__ + " at {}>" .format(hex (id (self )))
317
372
318
373
319
374
cpdef select_accelerator_device():
@@ -328,9 +383,11 @@ cpdef select_accelerator_device():
328
383
"""
329
384
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLAcceleratorSelector_Create()
330
385
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
386
+ # Free up the device selector
331
387
DPCTLDeviceSelector_Delete(DSRef)
332
388
if DRef is NULL :
333
389
raise ValueError (" Device unavailable." )
390
+ # The _create call frees DSRef and DRef
334
391
Device = SyclDevice._create(DRef)
335
392
return Device
336
393
@@ -347,9 +404,11 @@ cpdef select_cpu_device():
347
404
"""
348
405
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLCPUSelector_Create()
349
406
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
407
+ # Free up the device selector
350
408
DPCTLDeviceSelector_Delete(DSRef)
351
409
if DRef is NULL :
352
410
raise ValueError (" Device unavailable." )
411
+ # The _create call frees DSRef and DRef
353
412
Device = SyclDevice._create(DRef)
354
413
return Device
355
414
@@ -366,9 +425,11 @@ cpdef select_default_device():
366
425
"""
367
426
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLDefaultSelector_Create()
368
427
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
428
+ # Free up the device selector
369
429
DPCTLDeviceSelector_Delete(DSRef)
370
430
if DRef is NULL :
371
431
raise ValueError (" Device unavailable." )
432
+ # The _create call frees DSRef and DRef
372
433
Device = SyclDevice._create(DRef)
373
434
return Device
374
435
@@ -385,9 +446,11 @@ cpdef select_gpu_device():
385
446
"""
386
447
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLGPUSelector_Create()
387
448
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
449
+ # Free up the device selector
388
450
DPCTLDeviceSelector_Delete(DSRef)
389
451
if DRef is NULL :
390
452
raise ValueError (" Device unavailable." )
453
+ # The _create call frees DSRef and DRef
391
454
Device = SyclDevice._create(DRef)
392
455
return Device
393
456
@@ -404,8 +467,10 @@ cpdef select_host_device():
404
467
"""
405
468
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLHostSelector_Create()
406
469
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
470
+ # Free up the device selector
407
471
DPCTLDeviceSelector_Delete(DSRef)
408
472
if DRef is NULL :
409
473
raise ValueError (" Device unavailable." )
474
+ # The _create call frees DSRef and DRef
410
475
Device = SyclDevice._create(DRef)
411
476
return Device
0 commit comments