From 83ef6068535b9b68952fa38a35aa5144357208db Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Tue, 7 Sep 2021 12:21:01 -0500 Subject: [PATCH 1/3] Deallocator of SyclEvent calls Wait/Delete only if _event_ref != NULL --- dpctl/_sycl_event.pyx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 From 5a1798a8f468aa89894aaccbb5fed43c7ff01e3f Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Tue, 7 Sep 2021 12:22:23 -0500 Subject: [PATCH 2/3] Added tests to check validation of capsule argument of constructors --- dpctl/tests/_helper.py | 12 ++++++++++++ dpctl/tests/test_sycl_context.py | 8 ++++++++ dpctl/tests/test_sycl_event.py | 8 +++++++- dpctl/tests/test_sycl_queue.py | 29 ++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) 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..8855e59fa0 100644 --- a/dpctl/tests/test_sycl_context.py +++ b/dpctl/tests/test_sycl_context.py @@ -21,6 +21,8 @@ import dpctl +from ._helper import create_invalid_capsule + list_of_valid_filter_selectors = [ "opencl", "opencl:gpu", @@ -210,3 +212,9 @@ 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) 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() From 95972994fac72e0536ff1a738f7d53328d4b7828 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Tue, 7 Sep 2021 12:32:34 -0500 Subject: [PATCH 3/3] Test to expect error creating context from all devices Devices from different platforms can not be used to create a sycl context --- dpctl/tests/test_sycl_context.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dpctl/tests/test_sycl_context.py b/dpctl/tests/test_sycl_context.py index 8855e59fa0..9d09a6117c 100644 --- a/dpctl/tests/test_sycl_context.py +++ b/dpctl/tests/test_sycl_context.py @@ -14,7 +14,7 @@ # 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 @@ -218,3 +218,10 @@ 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)