Skip to content

Commit 38d5a1f

Browse files
author
Diptorup Deb
committed
Add a new utility function to return the device info as a C string object.
1 parent a8f4254 commit 38d5a1f

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

dpctl-capi/include/dpctl_sycl_device_manager.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ DPCTL_API
7676
__dpctl_give DPCTLDeviceVectorRef
7777
DPCTLDeviceMgr_GetDevices(int device_identifier);
7878

79+
/*!
80+
* @brief Returns a set of device info attributes as a string.
81+
*
82+
* @param DRef Opaque pointer to a ``sycl::device``
83+
* @return A formatted C string capturing the following attributes:
84+
* - device name
85+
* - driver version
86+
* - vendor
87+
* - profiler support
88+
* - oneapi filter string
89+
* @ingroup DeviceManager
90+
*/
91+
DPCTL_API
92+
__dpctl_give const char *
93+
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef);
94+
7995
/*!
8096
* @brief Returns an index on the given device in the vector returned by
8197
* #DPCTLDeviceMgr_GetDevices if found, -1 otherwise.
@@ -91,6 +107,7 @@ DPCTLDeviceMgr_GetDevices(int device_identifier);
91107
* @return If found, returns the position of the given device in the
92108
* vector that would be returned by #DPCTLDeviceMgr_GetDevices if called
93109
* with the same device_identifier argument.
110+
* @ingroup DeviceManager
94111
*/
95112
DPCTL_API
96113
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
@@ -122,6 +139,7 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef);
122139
* the enum values or a bitwise OR-ed combination.
123140
* @return The number of available devices satisfying the condition specified
124141
* by the device_identifier bit flag.
142+
* @ingroup DeviceManager
125143
*/
126144
DPCTL_API
127145
size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);
@@ -131,6 +149,7 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);
131149
* currently supported by dpctl.
132150
*
133151
* @param DRef A #DPCTLSyclDeviceRef opaque pointer.
152+
* @ingroup DeviceManager
134153
*/
135154
DPCTL_API
136155
void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef);

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
4444
/*
4545
* Helper function to print the metadata for a sycl::device.
4646
*/
47-
void print_device_info(const device &Device)
47+
std::string get_device_info_str(const device &Device)
4848
{
4949
std::stringstream ss;
5050

@@ -61,7 +61,7 @@ void print_device_info(const device &Device)
6161
<< DPCTL_DeviceTypeToStr(Device.get_info<info::device::device_type>())
6262
<< ":" << DPCTL_GetRelativeDeviceId(Device) << '\n';
6363

64-
std::cout << ss.str();
64+
return ss.str();
6565
}
6666

6767
struct DeviceCacheBuilder
@@ -168,6 +168,32 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
168168
return wrap(Devices);
169169
}
170170

171+
__dpctl_give const char *
172+
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
173+
{
174+
char *cstr_info = nullptr;
175+
auto D = unwrap(DRef);
176+
if (D) {
177+
try {
178+
auto infostr = get_device_info_str(*D);
179+
auto cstr_len = infostr.length() + 1;
180+
cstr_info = new char[cstr_len];
181+
#ifdef _WIN32
182+
strncpy_s(cstr_info, cstr_len, infostr.c_str(), cstr_len);
183+
#else
184+
std::strncpy(cstr_info, infostr.c_str(), cstr_len);
185+
#endif
186+
} catch (std::bad_alloc const &ba) {
187+
// \todo log error
188+
std::cerr << ba.what() << '\n';
189+
} catch (runtime_error const &re) {
190+
// \todo log error
191+
std::cerr << re.what() << '\n';
192+
}
193+
}
194+
return cstr_info;
195+
}
196+
171197
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
172198
int device_identifier)
173199
{
@@ -226,7 +252,7 @@ void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
226252
{
227253
auto Device = unwrap(DRef);
228254
if (Device)
229-
print_device_info(*Device);
255+
std::cout << get_device_info_str(*Device);
230256
else
231257
std::cout << "Device is not valid (NULL). Cannot print device info.\n";
232258
}

dpctl-capi/tests/test_sycl_device_manager.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "dpctl_sycl_device_interface.h"
2929
#include "dpctl_sycl_device_manager.h"
3030
#include "dpctl_sycl_device_selector_interface.h"
31+
#include "dpctl_utils.h"
3132
#include <gtest/gtest.h>
3233
#include <string>
3334

@@ -70,6 +71,14 @@ TEST_P(TestDPCTLDeviceManager, ChkPrintDeviceInfo)
7071
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
7172
}
7273

74+
TEST_P(TestDPCTLDeviceManager, ChkGetDeviceInfoStr)
75+
{
76+
const char *info_str = nullptr;
77+
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLDeviceMgr_GetDeviceInfoStr(DRef));
78+
ASSERT_TRUE(info_str != nullptr);
79+
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
80+
}
81+
7382
TEST_P(TestDPCTLDeviceManager, ChkGetCachedContext)
7483
{
7584
DPCTLSyclContextRef CRef = nullptr;

0 commit comments

Comments
 (0)