diff --git a/dpctl/_sycl_event.pyx b/dpctl/_sycl_event.pyx index a6b909427c..f78e19c326 100644 --- a/dpctl/_sycl_event.pyx +++ b/dpctl/_sycl_event.pyx @@ -88,8 +88,10 @@ cdef class _SyclEvent: """ def __dealloc__(self): - DPCTLEvent_Wait(self._event_ref) - DPCTLEvent_Delete(self._event_ref) + if (self._event_ref): + DPCTLEvent_Wait(self._event_ref) + DPCTLEvent_Delete(self._event_ref) + self._event_ref = NULL self.args = None diff --git a/dpctl/tests/_helper.py b/dpctl/tests/_helper.py index cea9d3d450..aafa845130 100644 --- a/dpctl/tests/_helper.py +++ b/dpctl/tests/_helper.py @@ -11,3 +11,15 @@ def has_cpu(backend="opencl"): def has_sycl_platforms(): return bool(len(dpctl.get_platforms())) + + +def create_invalid_capsule(): + """Creates an invalid capsule for the purpose of testing dpctl + constructors that accept capsules. + """ + import ctypes + + ctor = ctypes.pythonapi.PyCapsule_New + ctor.restype = ctypes.py_object + ctor.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p] + return ctor(id(ctor), b"invalid", 0) diff --git a/dpctl/tests/test_sycl_context.py b/dpctl/tests/test_sycl_context.py index c938caf074..9d09a6117c 100644 --- a/dpctl/tests/test_sycl_context.py +++ b/dpctl/tests/test_sycl_context.py @@ -14,13 +14,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" Defines unit test cases for the SyclContxt class. +""" Defines unit test cases for the :class:`dpctl.SyclContext` class. """ import pytest import dpctl +from ._helper import create_invalid_capsule + list_of_valid_filter_selectors = [ "opencl", "opencl:gpu", @@ -210,3 +212,16 @@ def test_cpython_api(): r2 = ctx.addressof_ref() r1 = get_context_ref_fn(ctx) assert r1 == r2 + + +def test_invalid_capsule(): + cap = create_invalid_capsule() + with pytest.raises(ValueError): + dpctl.SyclContext(cap) + + +def test_multi_device_different_platforms(): + devs = dpctl.get_devices() # all devices + if len(devs) > 1: + with pytest.raises(ValueError): + dpctl.SyclContext(devs) diff --git a/dpctl/tests/test_sycl_event.py b/dpctl/tests/test_sycl_event.py index 62b93dc95d..4e76534bd8 100644 --- a/dpctl/tests/test_sycl_event.py +++ b/dpctl/tests/test_sycl_event.py @@ -25,7 +25,7 @@ import dpctl.program as dpctl_prog from dpctl import event_status_type as esty -from ._helper import has_cpu +from ._helper import create_invalid_capsule, has_cpu def produce_event(profiling=False): @@ -223,6 +223,12 @@ def test_event_capsule(): del cap2 +def test_event_invalid_capsule(): + cap = create_invalid_capsule() + with pytest.raises(TypeError): + dpctl.SyclEvent(cap) + + def test_addressof_ref(): ev = dpctl.SyclEvent() ref = ev.addressof_ref() diff --git a/dpctl/tests/test_sycl_queue.py b/dpctl/tests/test_sycl_queue.py index 5b978153ae..8c31dcc8b0 100644 --- a/dpctl/tests/test_sycl_queue.py +++ b/dpctl/tests/test_sycl_queue.py @@ -21,6 +21,8 @@ import dpctl +from ._helper import create_invalid_capsule + list_of_standard_selectors = [ dpctl.select_accelerator_device, dpctl.select_cpu_device, @@ -359,6 +361,8 @@ def test_context_not_equals(): ctx_cpu = cpuQ.get_sycl_context() assert ctx_cpu != ctx_gpu assert hash(ctx_cpu) != hash(ctx_gpu) + assert gpuQ != cpuQ + assert hash(cpuQ) != hash(gpuQ) def test_context_equals(): @@ -497,11 +501,34 @@ def test_constructor_many_arg(): dpctl.SyclQueue(ctx) +def test_constructor_inconsistent_ctx_dev(): + try: + q = dpctl.SyclQueue("cpu") + except dpctl.SyclQueueCreationError: + pytest.skip("Failed to create CPU queue") + cpuD = q.sycl_device + n_eu = cpuD.max_compute_units + n_half = n_eu // 2 + try: + d0, d1 = cpuD.create_sub_devices(partition=[n_half, n_eu - n_half]) + except Exception: + pytest.skip("Could not create CPU sub-devices") + ctx = dpctl.SyclContext(d0) + with pytest.raises(dpctl.SyclQueueCreationError): + dpctl.SyclQueue(ctx, d1) + + +def test_constructor_invalid_capsule(): + cap = create_invalid_capsule() + with pytest.raises(TypeError): + dpctl.SyclQueue(cap) + + def test_queue_wait(): try: q = dpctl.SyclQueue() except dpctl.SyclQueueCreationError: - pytest.skip("Failed to create device with supported filter") + pytest.skip("Failed to create default queue") q.wait()