Skip to content

Commit 711f868

Browse files
DPCTLSyclInterface should avoid functions that print to std::cout (#542)
* use GetDeviceInfoStr dpctl.SyclDevice.print_device_info gets CString from DPCTL-CAPI and prints it using Python's stream. * Used pytest stream capture to test q.print_device_info
2 parents 782340f + b6e7e23 commit 711f868

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

dpctl/_backend.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ cdef extern from "dpctl_sycl_device_manager.h":
207207
const DPCTLSyclDeviceRef DRef,
208208
int device_identifier)
209209
cdef size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
210-
cdef void DPCTLDeviceMgr_PrintDeviceInfo(const DPCTLSyclDeviceRef DRef)
210+
cdef const char * DPCTLDeviceMgr_GetDeviceInfoStr(const DPCTLSyclDeviceRef DRef)
211211
cdef DPCTLSyclContextRef DPCTLDeviceMgr_GetCachedContext(
212212
const DPCTLSyclDeviceRef DRef)
213213
cdef int64_t DPCTLDeviceMgr_GetRelativeId(const DPCTLSyclDeviceRef DRef)

dpctl/_sycl_device.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ from ._backend cimport ( # noqa: E211
6565
DPCTLDevice_IsCPU,
6666
DPCTLDevice_IsGPU,
6767
DPCTLDevice_IsHost,
68+
DPCTLDeviceMgr_GetDeviceInfoStr,
6869
DPCTLDeviceMgr_GetDevices,
6970
DPCTLDeviceMgr_GetPositionInDevices,
7071
DPCTLDeviceMgr_GetRelativeId,
71-
DPCTLDeviceMgr_PrintDeviceInfo,
7272
DPCTLDeviceSelector_Delete,
7373
DPCTLDeviceSelector_Score,
7474
DPCTLDeviceVector_Delete,
@@ -286,7 +286,12 @@ cdef class SyclDevice(_SyclDevice):
286286
def print_device_info(self):
287287
""" Print information about the SYCL device.
288288
"""
289-
DPCTLDeviceMgr_PrintDeviceInfo(self._device_ref)
289+
cdef const char * info_str = DPCTLDeviceMgr_GetDeviceInfoStr(
290+
self._device_ref
291+
)
292+
py_info = <bytes> info_str
293+
DPCTLCString_Delete(info_str)
294+
print(py_info.decode("utf-8"))
290295

291296
cdef DPCTLSyclDeviceRef get_device_ref(self):
292297
""" Returns the DPCTLSyclDeviceRef pointer for this class.

dpctl/tests/test_sycl_queue.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
""" Defines unit test cases for the SyclQueue class.
1818
"""
1919

20+
import ctypes
21+
import sys
22+
2023
import pytest
2124

2225
import dpctl
@@ -395,22 +398,22 @@ def test_hashing_of_queue():
395398
assert queue_dict
396399

397400

398-
def test_channeling_device_properties():
401+
def test_channeling_device_properties(capsys):
399402
try:
400403
q = dpctl.SyclQueue()
401404
dev = q.sycl_device
402405
except dpctl.SyclQueueCreationError:
403406
pytest.fail("Failed to create device from default selector")
404-
import io
405-
from contextlib import redirect_stdout
406-
407-
f1 = io.StringIO()
408-
with redirect_stdout(f1):
409-
q.print_device_info() # should execute without raising
410-
f2 = io.StringIO()
411-
with redirect_stdout(f2):
412-
dev.print_device_info()
413-
assert f1.getvalue() == f2.getvalue(), "Mismatch in print_device_info"
407+
408+
q.print_device_info() # should execute without raising
409+
q_captured = capsys.readouterr()
410+
q_output = q_captured.out
411+
dev.print_device_info()
412+
d_captured = capsys.readouterr()
413+
d_output = d_captured.out
414+
assert q_output, "No output captured"
415+
assert q_output == d_output, "Mismatch in print_device_info"
416+
assert q_captured.err == "" and d_captured.err == ""
414417
for pr in ["backend", "name", "driver_version"]:
415418
assert getattr(q, pr) == getattr(
416419
dev, pr
@@ -468,9 +471,6 @@ def test_queue_capsule():
468471

469472

470473
def test_cpython_api():
471-
import ctypes
472-
import sys
473-
474474
q = dpctl.SyclQueue()
475475
mod = sys.modules[q.__class__.__module__]
476476
# get capsule storign get_context_ref function ptr

0 commit comments

Comments
 (0)