Skip to content

DPCTLSyclInterface should avoid functions that print to std::cout #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dpctl/_backend.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = <bytes> 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.
Expand Down
28 changes: 14 additions & 14 deletions dpctl/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
""" Defines unit test cases for the SyclQueue class.
"""

import ctypes
import sys

import pytest

import dpctl
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down