From e0ddad8b4db7af459cd7dba1882831650dc6c439 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 8 Apr 2023 01:57:50 -0500 Subject: [PATCH] Add a unit test to check usability of DpctlSyclQueue type. - Added a test to see if the Numba native object created for a dpctl.SyclQueue is usable inside the compiler. The test extracts the queue_ref pointer from the dpctl.SyclQueue PyObject during unboxing and stores it in a native struct. The test verifies that the queue_ref pointer is valid and can be passed to a C function call generated in the compiled LLVM module. --- .../tests/dpjit_tests/test_box_unbox.py | 27 ------- .../types/DpctlSyclQueue/test_box_unbox.py | 28 +++++++ .../DpctlSyclQueue/test_queue_ref_attr.py | 78 +++++++++++++++++++ 3 files changed, 106 insertions(+), 27 deletions(-) delete mode 100644 numba_dpex/tests/dpjit_tests/test_box_unbox.py create mode 100644 numba_dpex/tests/types/DpctlSyclQueue/test_box_unbox.py create mode 100644 numba_dpex/tests/types/DpctlSyclQueue/test_queue_ref_attr.py diff --git a/numba_dpex/tests/dpjit_tests/test_box_unbox.py b/numba_dpex/tests/dpjit_tests/test_box_unbox.py deleted file mode 100644 index 5350c133f4..0000000000 --- a/numba_dpex/tests/dpjit_tests/test_box_unbox.py +++ /dev/null @@ -1,27 +0,0 @@ -# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation -# -# SPDX-License-Identifier: Apache-2.0 - -""" -Tests for boxing and unboxing of types supported inside dpjit -""" - -import dpctl -import pytest - -from numba_dpex import dpjit - - -@pytest.mark.parametrize( - "obj", - [ - pytest.param(dpctl.SyclQueue()), - ], -) -def test_boxing_unboxing(obj): - @dpjit - def func(a): - return a - - o = func(obj) - assert id(o) == id(obj) diff --git a/numba_dpex/tests/types/DpctlSyclQueue/test_box_unbox.py b/numba_dpex/tests/types/DpctlSyclQueue/test_box_unbox.py new file mode 100644 index 0000000000..3cf3e36379 --- /dev/null +++ b/numba_dpex/tests/types/DpctlSyclQueue/test_box_unbox.py @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +""" +Tests for boxing and unboxing of types supported inside dpjit +""" + +import dpctl +import pytest + +from numba_dpex import dpjit + + +def test_boxing_unboxing(): + """Tests basic boxing and unboxing of a dpctl.SyclQueue object. + + Checks if we can pass in and return a dpctl.SyclQueue object to and + from a dpjit decorated function. + """ + + @dpjit + def func(a): + return a + + q = dpctl.SyclQueue() + o = func(q) + assert id(o) == id(q) diff --git a/numba_dpex/tests/types/DpctlSyclQueue/test_queue_ref_attr.py b/numba_dpex/tests/types/DpctlSyclQueue/test_queue_ref_attr.py new file mode 100644 index 0000000000..466a5ff421 --- /dev/null +++ b/numba_dpex/tests/types/DpctlSyclQueue/test_queue_ref_attr.py @@ -0,0 +1,78 @@ +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +import dpctl +from llvmlite import ir as llvmir +from numba.core import cgutils, types +from numba.extending import intrinsic + +from numba_dpex import dpjit + + +@intrinsic +def are_queues_equal(typingctx, ty_queue1, ty_queue2): + """Calls dpctl's libsyclinterface's DPCTLQueue_AreEq to see if two + dpctl.SyclQueue objects point to the same sycl queue. + + Args: + typingctx: The typing context used during lowering. + ty_queue1: Type of the first queue object, + i.e., numba_dpex.types.DpctlSyclQueue + ty_queue2: Type of the second queue object, + i.e., numba_dpex.types.DpctlSyclQueue + + Returns: + tuple: The signature of the intrinsic function and the codegen function + to lower the intrinsic. + """ + result_type = types.boolean + sig = result_type(ty_queue1, ty_queue2) + + # defines the custom code generation + def codegen(context, builder, sig, args): + fnty = llvmir.FunctionType( + cgutils.bool_t, [cgutils.voidptr_t, cgutils.voidptr_t] + ) + fn = cgutils.get_or_insert_function( + builder.module, fnty, "DPCTLQueue_AreEq" + ) + qref1 = builder.extract_value(args[0], 1) + qref2 = builder.extract_value(args[1], 1) + + ret = builder.call(fn, [qref1, qref2]) + + return ret + + return sig, codegen + + +def test_queue_ref_access_in_dpjit(): + """Tests if we can access the queue_ref attribute of a dpctl.SyclQueue + PyObject inside dpjit and pass it to a native C function, in this case + dpctl's libsyclinterface's DPCTLQueue_AreEq. + + Checks if we can pass in and return a dpctl.SyclQueue object to and + from a dpjit decorated function. + """ + + @dpjit + def test_queue_equality(queue1, queue2): + return are_queues_equal(queue1, queue2) + + q1 = dpctl.SyclQueue() + q2 = dpctl.SyclQueue() + + expected = q1 == q2 + actual = test_queue_equality(q1, q2) + + assert expected == actual + + d = dpctl.SyclDevice() + cq1 = dpctl._sycl_queue_manager.get_device_cached_queue(d) + cq2 = dpctl._sycl_queue_manager.get_device_cached_queue(d) + + expected = cq1 == cq2 + actual = test_queue_equality(cq1, cq2) + + assert expected == actual