Skip to content

Commit c6767ca

Browse files
author
Diptorup Deb
committed
Fixes to ensure we do not expose _SyclDevice to users.
1 parent 8f981ed commit c6767ca

File tree

5 files changed

+122
-25
lines changed

5 files changed

+122
-25
lines changed

dpctl-capi/include/dpctl_sycl_device_interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636

3737
DPCTL_C_EXTERN_C_BEGIN
3838

39+
/*!
40+
* @brief Returns a copy of the DPCTLSyclDeviceRef object.
41+
*
42+
* @param DRef DPCTLSyclDeviceRef object to be copied.
43+
* @return A new DPCTLSyclDeviceRef created by copying the passed in
44+
* DPCTLSyclDeviceRef object.
45+
*/
46+
DPCTL_API
47+
__dpctl_give DPCTLSyclDeviceRef
48+
DPCTLDevice_Copy(__dpctl_keep const DPCTLSyclDeviceRef DRef);
49+
3950
/*!
4051
* @brief Returns a new DPCTLSyclDeviceRef opaque object wrapping a SYCL device
4152
* instance as a host device.

dpctl-capi/source/dpctl_sycl_device_interface.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ void dump_device_info(const device &Device)
6767

6868
} /* end of anonymous namespace */
6969

70+
__dpctl_give DPCTLSyclDeviceRef
71+
DPCTLDevice_Copy(__dpctl_keep const DPCTLSyclDeviceRef DRef)
72+
{
73+
auto Device = unwrap(DRef);
74+
if (!Device) {
75+
std::cerr << "Cannot copy DPCTLSyclDeviceRef as input is a nullptr\n";
76+
return nullptr;
77+
}
78+
try {
79+
auto CopiedDevice = new device(*Device);
80+
return wrap(CopiedDevice);
81+
} catch (std::bad_alloc const &ba) {
82+
// \todo log error
83+
std::cerr << ba.what() << '\n';
84+
return nullptr;
85+
}
86+
}
87+
7088
__dpctl_give DPCTLSyclDeviceRef DPCTLDevice_Create()
7189
{
7290
try {

dpctl/_backend.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ cdef extern from "dpctl_sycl_types.h":
9090

9191

9292
cdef extern from "dpctl_sycl_device_interface.h":
93+
cdef DPCTLSyclDeviceRef DPCTLDevice_Copy(const DPCTLSyclDeviceRef DRef)
9394
cdef DPCTLSyclDeviceRef DPCTLDevice_Create()
9495
cdef DPCTLSyclDeviceRef DPCTLDevice_CreateFromSelector(
9596
const DPCTLSyclDeviceSelectorRef DSRef)

dpctl/_sycl_device.pxd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from libcpp cimport bool
2424
from libc.stdint cimport uint32_t
2525
from ._backend cimport (
26-
DPCTLSyclDeviceRef
26+
DPCTLSyclDeviceRef,
27+
DPCTLSyclDeviceSelectorRef,
2728
)
2829

2930

@@ -70,7 +71,8 @@ cdef class SyclDevice(_SyclDevice):
7071
cdef SyclDevice _create(DPCTLSyclDeviceRef dref)
7172
@staticmethod
7273
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef)
73-
74+
cdef void _init_from__SyclDevice(self, _SyclDevice other)
75+
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef)
7476

7577
cpdef select_accelerator_device()
7678
cpdef select_cpu_device()

dpctl/_sycl_device.pyx

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from ._backend cimport (
2727
DPCTLGPUSelector_Create,
2828
DPCTLHostSelector_Create,
2929
DPCTLCString_Delete,
30+
DPCTLDevice_Copy,
3031
DPCTLDevice_CreateFromSelector,
3132
DPCTLDevice_Delete,
3233
DPCTLDevice_DumpInfo,
@@ -66,7 +67,6 @@ cdef class _SyclDevice:
6667
""" A helper metaclass to abstract a cl::sycl::device instance.
6768
"""
6869

69-
7070
def __dealloc__(self):
7171
DPCTLDevice_Delete(self._device_ref)
7272
DPCTLCString_Delete(self._device_name)
@@ -228,6 +228,11 @@ cdef class _SyclDevice:
228228
return int(<size_t>self._device_ref)
229229

230230

231+
@property
232+
def __name__:
233+
return "SyclDevice"
234+
235+
231236
cdef class SyclDevice(_SyclDevice):
232237
""" Python equivalent for cl::sycl::device class.
233238
@@ -267,7 +272,6 @@ cdef class SyclDevice(_SyclDevice):
267272
gpu.dump_device_info()
268273
269274
"""
270-
271275
@staticmethod
272276
cdef void _init_helper(SyclDevice device, DPCTLSyclDeviceRef DRef):
273277
device._device_ref = DRef
@@ -288,32 +292,83 @@ cdef class SyclDevice(_SyclDevice):
288292
device._gpu_device = DPCTLDevice_IsGPU(DRef)
289293
device._host_device = DPCTLDevice_IsHost(DRef)
290294

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-
311295
@staticmethod
312296
cdef SyclDevice _create(DPCTLSyclDeviceRef dref):
313297
cdef SyclDevice ret = <SyclDevice>_SyclDevice.__new__(_SyclDevice)
314298
# Initialize the attributes of the SyclDevice object
315299
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)))
317372

318373

319374
cpdef select_accelerator_device():
@@ -328,9 +383,11 @@ cpdef select_accelerator_device():
328383
"""
329384
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLAcceleratorSelector_Create()
330385
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
386+
# Free up the device selector
331387
DPCTLDeviceSelector_Delete(DSRef)
332388
if DRef is NULL:
333389
raise ValueError("Device unavailable.")
390+
# The _create call frees DSRef and DRef
334391
Device = SyclDevice._create(DRef)
335392
return Device
336393

@@ -347,9 +404,11 @@ cpdef select_cpu_device():
347404
"""
348405
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLCPUSelector_Create()
349406
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
407+
# Free up the device selector
350408
DPCTLDeviceSelector_Delete(DSRef)
351409
if DRef is NULL:
352410
raise ValueError("Device unavailable.")
411+
# The _create call frees DSRef and DRef
353412
Device = SyclDevice._create(DRef)
354413
return Device
355414

@@ -366,9 +425,11 @@ cpdef select_default_device():
366425
"""
367426
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLDefaultSelector_Create()
368427
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
428+
# Free up the device selector
369429
DPCTLDeviceSelector_Delete(DSRef)
370430
if DRef is NULL:
371431
raise ValueError("Device unavailable.")
432+
# The _create call frees DSRef and DRef
372433
Device = SyclDevice._create(DRef)
373434
return Device
374435

@@ -385,9 +446,11 @@ cpdef select_gpu_device():
385446
"""
386447
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLGPUSelector_Create()
387448
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
449+
# Free up the device selector
388450
DPCTLDeviceSelector_Delete(DSRef)
389451
if DRef is NULL:
390452
raise ValueError("Device unavailable.")
453+
# The _create call frees DSRef and DRef
391454
Device = SyclDevice._create(DRef)
392455
return Device
393456

@@ -404,8 +467,10 @@ cpdef select_host_device():
404467
"""
405468
cdef DPCTLSyclDeviceSelectorRef DSRef = DPCTLHostSelector_Create()
406469
cdef DPCTLSyclDeviceRef DRef = DPCTLDevice_CreateFromSelector(DSRef)
470+
# Free up the device selector
407471
DPCTLDeviceSelector_Delete(DSRef)
408472
if DRef is NULL:
409473
raise ValueError("Device unavailable.")
474+
# The _create call frees DSRef and DRef
410475
Device = SyclDevice._create(DRef)
411476
return Device

0 commit comments

Comments
 (0)