Skip to content

Commit

Permalink
Add a new utility function to return the device info as a C string ob…
Browse files Browse the repository at this point in the history
…ject.
  • Loading branch information
Diptorup Deb committed Oct 5, 2021
1 parent a8f4254 commit 50f6935
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
19 changes: 19 additions & 0 deletions dpctl-capi/include/dpctl_sycl_device_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ DPCTL_API
__dpctl_give DPCTLDeviceVectorRef
DPCTLDeviceMgr_GetDevices(int device_identifier);

/*!
* @brief Returns as a string a set of device info attributes.
*
* @param DRef Opaque pointer to a ``sycl::device``
* @return A formatted C string capturing the following attributes:
* - device name
* - driver version
* - vendor
* - profiler support
* - oneapi filter string
* @ingroup DeviceManager
*/
DPCTL_API
__dpctl_give const char *
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef);

/*!
* @brief Returns an index on the given device in the vector returned by
* #DPCTLDeviceMgr_GetDevices if found, -1 otherwise.
Expand All @@ -91,6 +107,7 @@ DPCTLDeviceMgr_GetDevices(int device_identifier);
* @return If found, returns the position of the given device in the
* vector that would be returned by #DPCTLDeviceMgr_GetDevices if called
* with the same device_identifier argument.
* @ingroup DeviceManager
*/
DPCTL_API
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
Expand Down Expand Up @@ -122,6 +139,7 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef);
* the enum values or a bitwise OR-ed combination.
* @return The number of available devices satisfying the condition specified
* by the device_identifier bit flag.
* @ingroup DeviceManager
*/
DPCTL_API
size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);
Expand All @@ -131,6 +149,7 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);
* currently supported by dpctl.
*
* @param DRef A #DPCTLSyclDeviceRef opaque pointer.
* @ingroup DeviceManager
*/
DPCTL_API
void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef);
Expand Down
32 changes: 29 additions & 3 deletions dpctl-capi/source/dpctl_sycl_device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
/*
* Helper function to print the metadata for a sycl::device.
*/
void print_device_info(const device &Device)
std::string get_device_info_str(const device &Device)
{
std::stringstream ss;

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

std::cout << ss.str();
return ss.str();
}

struct DeviceCacheBuilder
Expand Down Expand Up @@ -168,6 +168,32 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
return wrap(Devices);
}

__dpctl_give const char *
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
char *cstr_info = nullptr;
auto D = unwrap(DRef);
if (D) {
try {
auto infostr = get_device_info_str(*D);
auto cstr_len = infostr.length() + 1;
cstr_info = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_info, cstr_len, infostr.c_str(), cstr_len);
#else
std::strncpy(cstr_info, infostr.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
}
}
return cstr_info;
}

int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
int device_identifier)
{
Expand Down Expand Up @@ -226,7 +252,7 @@ void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
auto Device = unwrap(DRef);
if (Device)
print_device_info(*Device);
std::cout << get_device_info_str(*Device);
else
std::cout << "Device is not valid (NULL). Cannot print device info.\n";
}
Expand Down
9 changes: 9 additions & 0 deletions dpctl-capi/tests/test_sycl_device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "dpctl_sycl_device_interface.h"
#include "dpctl_sycl_device_manager.h"
#include "dpctl_sycl_device_selector_interface.h"
#include "dpctl_utils.h"
#include <gtest/gtest.h>
#include <string>

Expand Down Expand Up @@ -70,6 +71,14 @@ TEST_P(TestDPCTLDeviceManager, ChkPrintDeviceInfo)
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
}

TEST_P(TestDPCTLDeviceManager, ChkGetDeviceInfoStr)
{
const char *info_str = nullptr;
EXPECT_NO_FATAL_FAILURE(info_str = DPCTLDeviceMgr_GetDeviceInfoStr(DRef));
ASSERT_TRUE(info_str != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(info_str));
}

TEST_P(TestDPCTLDeviceManager, ChkGetCachedContext)
{
DPCTLSyclContextRef CRef = nullptr;
Expand Down

0 comments on commit 50f6935

Please sign in to comment.