From 821e6a1289da5c7291a26548b8df5d7803ba2c81 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Tue, 5 Oct 2021 16:37:34 -0500 Subject: [PATCH 1/2] use GetDeviceInfoStr dpctl.SyclDevice.print_device_info gets CString from DPCTL-CAPI and prints it using Python's stream. --- dpctl/_backend.pxd | 2 +- dpctl/_sycl_device.pyx | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dpctl/_backend.pxd b/dpctl/_backend.pxd index d968a7d42f..eefdd4da85 100644 --- a/dpctl/_backend.pxd +++ b/dpctl/_backend.pxd @@ -207,7 +207,7 @@ cdef extern from "dpctl_sycl_device_manager.h": const DPCTLSyclDeviceRef DRef, int device_identifier) cdef size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier) - cdef void DPCTLDeviceMgr_PrintDeviceInfo(const DPCTLSyclDeviceRef DRef) + cdef const char * DPCTLDeviceMgr_GetDeviceInfoStr(const DPCTLSyclDeviceRef DRef) cdef DPCTLSyclContextRef DPCTLDeviceMgr_GetCachedContext( const DPCTLSyclDeviceRef DRef) cdef int64_t DPCTLDeviceMgr_GetRelativeId(const DPCTLSyclDeviceRef DRef) diff --git a/dpctl/_sycl_device.pyx b/dpctl/_sycl_device.pyx index 4246bcde25..e27aaf8f77 100644 --- a/dpctl/_sycl_device.pyx +++ b/dpctl/_sycl_device.pyx @@ -65,10 +65,10 @@ from ._backend cimport ( # noqa: E211 DPCTLDevice_IsCPU, DPCTLDevice_IsGPU, DPCTLDevice_IsHost, + DPCTLDeviceMgr_GetDeviceInfoStr, DPCTLDeviceMgr_GetDevices, DPCTLDeviceMgr_GetPositionInDevices, DPCTLDeviceMgr_GetRelativeId, - DPCTLDeviceMgr_PrintDeviceInfo, DPCTLDeviceSelector_Delete, DPCTLDeviceSelector_Score, DPCTLDeviceVector_Delete, @@ -286,7 +286,12 @@ cdef class SyclDevice(_SyclDevice): def print_device_info(self): """ Print information about the SYCL device. """ - DPCTLDeviceMgr_PrintDeviceInfo(self._device_ref) + cdef const char * info_str = DPCTLDeviceMgr_GetDeviceInfoStr( + self._device_ref + ) + py_info = info_str + DPCTLCString_Delete(info_str) + print(py_info.decode("utf-8")) cdef DPCTLSyclDeviceRef get_device_ref(self): """ Returns the DPCTLSyclDeviceRef pointer for this class. From b6e7e237ff0220f658b2b6d896d54501fe5ec230 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 20 Aug 2021 14:03:01 -0500 Subject: [PATCH 2/2] Used pytest stream capture to test q.print_device_info --- dpctl/tests/test_sycl_queue.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/dpctl/tests/test_sycl_queue.py b/dpctl/tests/test_sycl_queue.py index b7e4c982e7..bf35d76ae2 100644 --- a/dpctl/tests/test_sycl_queue.py +++ b/dpctl/tests/test_sycl_queue.py @@ -17,6 +17,9 @@ """ Defines unit test cases for the SyclQueue class. """ +import ctypes +import sys + import pytest import dpctl @@ -395,22 +398,22 @@ def test_hashing_of_queue(): assert queue_dict -def test_channeling_device_properties(): +def test_channeling_device_properties(capsys): try: q = dpctl.SyclQueue() dev = q.sycl_device except dpctl.SyclQueueCreationError: pytest.fail("Failed to create device from default selector") - import io - from contextlib import redirect_stdout - - f1 = io.StringIO() - with redirect_stdout(f1): - q.print_device_info() # should execute without raising - f2 = io.StringIO() - with redirect_stdout(f2): - dev.print_device_info() - assert f1.getvalue() == f2.getvalue(), "Mismatch in print_device_info" + + q.print_device_info() # should execute without raising + q_captured = capsys.readouterr() + q_output = q_captured.out + dev.print_device_info() + d_captured = capsys.readouterr() + d_output = d_captured.out + assert q_output, "No output captured" + assert q_output == d_output, "Mismatch in print_device_info" + assert q_captured.err == "" and d_captured.err == "" for pr in ["backend", "name", "driver_version"]: assert getattr(q, pr) == getattr( dev, pr @@ -468,9 +471,6 @@ def test_queue_capsule(): def test_cpython_api(): - import ctypes - import sys - q = dpctl.SyclQueue() mod = sys.modules[q.__class__.__module__] # get capsule storign get_context_ref function ptr