Skip to content

Commit

Permalink
Add a unit test to check usability of DpctlSyclQueue type.
Browse files Browse the repository at this point in the history
   - 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.
  • Loading branch information
Diptorup Deb committed Apr 8, 2023
1 parent faa27c3 commit 9f06447
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 27 deletions.
27 changes: 0 additions & 27 deletions numba_dpex/tests/dpjit_tests/test_box_unbox.py

This file was deleted.

28 changes: 28 additions & 0 deletions numba_dpex/tests/types/DpctlSyclQueue/test_box_unbox.py
Original file line number Diff line number Diff line change
@@ -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)
78 changes: 78 additions & 0 deletions numba_dpex/tests/types/DpctlSyclQueue/test_queue_ref_attr.py
Original file line number Diff line number Diff line change
@@ -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 the result of queue equality check done inside dpjit is the
same as when done in Python.
"""

@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

0 comments on commit 9f06447

Please sign in to comment.