Skip to content

Commit a94e63b

Browse files
Fix dpctl.lsplatform() to output in jupyter notebook (#800)
* Add DPCTLPlatformMgr_GetInfo function * Fix lsplatform function to use DPCTLPlatformMgr_GetInfo * Added tests for DPCTLPlatformMgr_GetInfo * Added tests for handlign of null PRef in DPCTLPlatformMgr_GetInfo Co-authored-by: Oleksandr Pavlyk <oleksandr.pavlyk@intel.com>
2 parents a20344b + 49e3419 commit a94e63b

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

dpctl/_backend.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ cdef extern from "syclinterface/dpctl_sycl_platform_manager.h":
263263
DPCTLPlatformVectorRef,
264264
size_t index)
265265
cdef void DPCTLPlatformMgr_PrintInfo(const DPCTLSyclPlatformRef, size_t)
266+
cdef const char *DPCTLPlatformMgr_GetInfo(const DPCTLSyclPlatformRef, size_t)
266267

267268

268269
cdef extern from "syclinterface/dpctl_sycl_platform_interface.h":

dpctl/_sycl_platform.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ from ._backend cimport ( # noqa: E211
3434
DPCTLPlatform_GetPlatforms,
3535
DPCTLPlatform_GetVendor,
3636
DPCTLPlatform_GetVersion,
37+
DPCTLPlatformMgr_GetInfo,
3738
DPCTLPlatformMgr_PrintInfo,
3839
DPCTLPlatformVector_Delete,
3940
DPCTLPlatformVector_GetAt,
@@ -323,6 +324,7 @@ def lsplatform(verbosity=0):
323324
cdef DPCTLPlatformVectorRef PVRef = NULL
324325
cdef size_t v = 0
325326
cdef size_t size = 0
327+
cdef const char * info_str = NULL
326328
cdef DPCTLSyclPlatformRef PRef = NULL
327329

328330
if not isinstance(verbosity, int):
@@ -347,8 +349,11 @@ def lsplatform(verbosity=0):
347349
if v != 0:
348350
print("Platform ", i, "::")
349351
PRef = DPCTLPlatformVector_GetAt(PVRef, i)
350-
DPCTLPlatformMgr_PrintInfo(PRef, v)
352+
info_str = DPCTLPlatformMgr_GetInfo(PRef,v)
353+
py_info = <bytes> info_str
354+
DPCTLCString_Delete(info_str)
351355
DPCTLPlatform_Delete(PRef)
356+
print(py_info.decode("utf-8"),end='')
352357
DPCTLPlatformVector_Delete(PVRef)
353358

354359

libsyclinterface/include/dpctl_sycl_platform_manager.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ DPCTL_API
6464
void DPCTLPlatformMgr_PrintInfo(__dpctl_keep const DPCTLSyclPlatformRef PRef,
6565
size_t verbosity);
6666

67+
/*!
68+
* @brief Returns a set of platform info attributes as a string.
69+
*
70+
* The helper function is used to get metadata about a given platform. The
71+
* amount of information received is controlled by the verbosity level.
72+
*
73+
* Verbosity level 0: Returns only the name of the platform.
74+
* Verbosity level 1: Returns the name, version, vendor, backend, number of
75+
* devices in the platform.
76+
* Verbosity level 2: Returns everything in level 1 and also returns the name,
77+
* version, and filter string for each device in the
78+
* platform.
79+
*
80+
* @param PRef A #DPCTLSyclPlatformRef opaque pointer.
81+
* @param verbosity Verbosilty level to control how much information is
82+
* printed out.
83+
* @return A formatted C string capturing the information about the
84+
* sycl::platform argument.
85+
*/
86+
DPCTL_API
87+
__dpctl_give const char *
88+
DPCTLPlatformMgr_GetInfo(__dpctl_keep const DPCTLSyclPlatformRef PRef,
89+
size_t verbosity);
90+
6791
/*! @} */
6892

6993
DPCTL_C_EXTERN_C_END

libsyclinterface/source/dpctl_sycl_platform_manager.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "dpctl_sycl_platform_manager.h"
2828
#include "Support/CBindingWrapping.h"
2929
#include "dpctl_error_handlers.h"
30+
#include "dpctl_string_utils.hpp"
3031
#include "dpctl_sycl_platform_interface.h"
3132
#include "dpctl_utils_helper.h"
3233
#include <CL/sycl.hpp>
@@ -41,7 +42,7 @@ namespace
4142
{
4243
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef);
4344

44-
void platform_print_info_impl(const platform &p, size_t verbosity)
45+
std::string platform_print_info_impl(const platform &p, size_t verbosity)
4546
{
4647
std::stringstream ss;
4748

@@ -96,7 +97,7 @@ void platform_print_info_impl(const platform &p, size_t verbosity)
9697
}
9798
}
9899

99-
std::cout << ss.str();
100+
return ss.str();
100101
}
101102

102103
} // namespace
@@ -111,10 +112,27 @@ void DPCTLPlatformMgr_PrintInfo(__dpctl_keep const DPCTLSyclPlatformRef PRef,
111112
{
112113
auto p = unwrap(PRef);
113114
if (p) {
114-
platform_print_info_impl(*p, verbosity);
115+
std::cout << platform_print_info_impl(*p, verbosity);
115116
}
116117
else {
117118
error_handler("Platform reference is NULL.", __FILE__, __func__,
118119
__LINE__);
119120
}
120121
}
122+
123+
__dpctl_give const char *
124+
DPCTLPlatformMgr_GetInfo(__dpctl_keep const DPCTLSyclPlatformRef PRef,
125+
size_t verbosity)
126+
{
127+
const char *cstr_info = nullptr;
128+
auto p = unwrap(PRef);
129+
if (p) {
130+
auto infostr = platform_print_info_impl(*p, verbosity);
131+
cstr_info = dpctl::helper::cstring_from_string(infostr);
132+
}
133+
else {
134+
error_handler("Platform reference is NULL.", __FILE__, __func__,
135+
__LINE__);
136+
}
137+
return cstr_info;
138+
}

libsyclinterface/tests/test_sycl_platform_interface.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ TEST_P(TestDPCTLSyclPlatformInterface, ChkCopyNullArg)
224224
EXPECT_NO_FATAL_FAILURE(DPCTLPlatform_Delete(Copied_PRef));
225225
}
226226

227+
TEST_P(TestDPCTLSyclPlatformInterface, ChkGetInfo)
228+
{
229+
const char *info_str = nullptr;
230+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(PRef, 0));
231+
ASSERT_TRUE(info_str != nullptr);
232+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
233+
}
234+
227235
TEST_P(TestDPCTLSyclPlatformInterface, ChkPrintInfo)
228236
{
229237
EXPECT_NO_FATAL_FAILURE(DPCTLPlatformMgr_PrintInfo(PRef, 0));
@@ -255,6 +263,46 @@ TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetBackend)
255263
check_platform_backend(PRef);
256264
}
257265

266+
TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetInfo0)
267+
{
268+
const char *info_str = nullptr;
269+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(PRef, 0));
270+
ASSERT_TRUE(info_str != nullptr);
271+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
272+
}
273+
274+
TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetInfo1)
275+
{
276+
const char *info_str = nullptr;
277+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(PRef, 1));
278+
ASSERT_TRUE(info_str != nullptr);
279+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
280+
}
281+
282+
TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetInfo2)
283+
{
284+
const char *info_str = nullptr;
285+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(PRef, 2));
286+
ASSERT_TRUE(info_str != nullptr);
287+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
288+
}
289+
290+
TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetInfo3)
291+
{
292+
const char *info_str = nullptr;
293+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(PRef, 3));
294+
ASSERT_TRUE(info_str != nullptr);
295+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
296+
}
297+
298+
TEST_F(TestDPCTLSyclDefaultPlatform, ChkGetInfoNull)
299+
{
300+
const char *info_str = nullptr;
301+
DPCTLSyclPlatformRef NullPRef = nullptr;
302+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLPlatformMgr_GetInfo(NullPRef, 0));
303+
ASSERT_TRUE(info_str == nullptr);
304+
}
305+
258306
TEST_F(TestDPCTLSyclDefaultPlatform, ChkPrintInfo0)
259307
{
260308
EXPECT_NO_FATAL_FAILURE(DPCTLPlatformMgr_PrintInfo(PRef, 0));

0 commit comments

Comments
 (0)