Skip to content

Commit 9f06447

Browse files
author
Diptorup Deb
committed
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.
1 parent faa27c3 commit 9f06447

File tree

3 files changed

+106
-27
lines changed

3 files changed

+106
-27
lines changed

numba_dpex/tests/dpjit_tests/test_box_unbox.py

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""
6+
Tests for boxing and unboxing of types supported inside dpjit
7+
"""
8+
9+
import dpctl
10+
import pytest
11+
12+
from numba_dpex import dpjit
13+
14+
15+
def test_boxing_unboxing():
16+
"""Tests basic boxing and unboxing of a dpctl.SyclQueue object.
17+
18+
Checks if we can pass in and return a dpctl.SyclQueue object to and
19+
from a dpjit decorated function.
20+
"""
21+
22+
@dpjit
23+
def func(a):
24+
return a
25+
26+
q = dpctl.SyclQueue()
27+
o = func(q)
28+
assert id(o) == id(q)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpctl
6+
from llvmlite import ir as llvmir
7+
from numba.core import cgutils, types
8+
from numba.extending import intrinsic
9+
10+
from numba_dpex import dpjit
11+
12+
13+
@intrinsic
14+
def are_queues_equal(typingctx, ty_queue1, ty_queue2):
15+
"""Calls dpctl's libsyclinterface's DPCTLQueue_AreEq to see if two
16+
dpctl.SyclQueue objects point to the same sycl queue.
17+
18+
Args:
19+
typingctx: The typing context used during lowering.
20+
ty_queue1: Type of the first queue object,
21+
i.e., numba_dpex.types.DpctlSyclQueue
22+
ty_queue2: Type of the second queue object,
23+
i.e., numba_dpex.types.DpctlSyclQueue
24+
25+
Returns:
26+
tuple: The signature of the intrinsic function and the codegen function
27+
to lower the intrinsic.
28+
"""
29+
result_type = types.boolean
30+
sig = result_type(ty_queue1, ty_queue2)
31+
32+
# defines the custom code generation
33+
def codegen(context, builder, sig, args):
34+
fnty = llvmir.FunctionType(
35+
cgutils.bool_t, [cgutils.voidptr_t, cgutils.voidptr_t]
36+
)
37+
fn = cgutils.get_or_insert_function(
38+
builder.module, fnty, "DPCTLQueue_AreEq"
39+
)
40+
qref1 = builder.extract_value(args[0], 1)
41+
qref2 = builder.extract_value(args[1], 1)
42+
43+
ret = builder.call(fn, [qref1, qref2])
44+
45+
return ret
46+
47+
return sig, codegen
48+
49+
50+
def test_queue_ref_access_in_dpjit():
51+
"""Tests if we can access the queue_ref attribute of a dpctl.SyclQueue
52+
PyObject inside dpjit and pass it to a native C function, in this case
53+
dpctl's libsyclinterface's DPCTLQueue_AreEq.
54+
55+
Checks if the result of queue equality check done inside dpjit is the
56+
same as when done in Python.
57+
"""
58+
59+
@dpjit
60+
def test_queue_equality(queue1, queue2):
61+
return are_queues_equal(queue1, queue2)
62+
63+
q1 = dpctl.SyclQueue()
64+
q2 = dpctl.SyclQueue()
65+
66+
expected = q1 == q2
67+
actual = test_queue_equality(q1, q2)
68+
69+
assert expected == actual
70+
71+
d = dpctl.SyclDevice()
72+
cq1 = dpctl._sycl_queue_manager.get_device_cached_queue(d)
73+
cq2 = dpctl._sycl_queue_manager.get_device_cached_queue(d)
74+
75+
expected = cq1 == cq2
76+
actual = test_queue_equality(cq1, cq2)
77+
78+
assert expected == actual

0 commit comments

Comments
 (0)