Skip to content

Commit 8e50395

Browse files
author
Diptorup Deb
authored
Add a new utility function to return the device info as a C string object. (#620)
* Add a new utility function to return the device info as a C string object. * Add a helper to convert a C++ string to and a C string.
2 parents d469c84 + a58afe9 commit 8e50395

8 files changed

+131
-94
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- dpctl_string_utils.hpp - C++ to C string converted -*-C++-*- ===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// Helper function to convert a C++ string to a C string.
23+
//===----------------------------------------------------------------------===//
24+
#include <cstring>
25+
#include <iostream>
26+
#include <string>
27+
28+
#pragma once
29+
30+
namespace dpctl
31+
{
32+
namespace helper
33+
{
34+
/*!
35+
* @brief Convert a C++ std::string to a const char* and return the string to
36+
* caller.
37+
*
38+
* @param str A C++ string that has to be converted to a C string.
39+
* @return A const char* string representation of the C++ string.
40+
*/
41+
static inline __dpctl_give const char *
42+
cstring_from_string(const std::string &str)
43+
{
44+
char *cstr = nullptr;
45+
try {
46+
auto cstr_len = str.length() + 1;
47+
cstr = new char[cstr_len];
48+
#ifdef _WIN32
49+
strncpy_s(cstr, cstr_len, str.c_str(), cstr_len);
50+
#else
51+
std::strncpy(cstr, str.c_str(), cstr_len);
52+
#endif
53+
} catch (std::bad_alloc const &ba) {
54+
// \todo log error
55+
std::cerr << ba.what() << '\n';
56+
}
57+
58+
return cstr;
59+
}
60+
} // namespace helper
61+
} // namespace dpctl

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_service.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,13 @@
2626
#include "dpctl_service.h"
2727
#include "Config/dpctl_config.h"
2828

29+
#include "../helper/include/dpctl_string_utils.hpp"
2930
#include <algorithm>
3031
#include <cstring>
3132
#include <iostream>
3233

3334
__dpctl_give const char *DPCTLService_GetDPCPPVersion(void)
3435
{
3536
std::string version = DPCTL_DPCPP_VERSION;
36-
char *version_cstr = nullptr;
37-
try {
38-
auto cstr_len = version.length() + 1;
39-
version_cstr = new char[cstr_len];
40-
#ifdef _WIN32
41-
strncpy_s(version_cstr, cstr_len, version.c_str(), cstr_len);
42-
#else
43-
std::strncpy(version_cstr, version.c_str(), cstr_len);
44-
#endif
45-
} catch (std::bad_alloc const &ba) {
46-
// \todo log error
47-
std::cerr << ba.what() << '\n';
48-
}
49-
return version_cstr;
37+
return dpctl::helper::cstring_from_string(version);
5038
}

dpctl-capi/source/dpctl_sycl_device_interface.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_device_interface.h"
28+
#include "../helper/include/dpctl_string_utils.hpp"
2829
#include "../helper/include/dpctl_utils_helper.h"
2930
#include "Support/CBindingWrapping.h"
3031
#include "dpctl_sycl_device_manager.h"
@@ -308,21 +309,12 @@ DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef)
308309
__dpctl_give const char *
309310
DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
310311
{
311-
char *cstr_name = nullptr;
312+
const char *cstr_name = nullptr;
312313
auto D = unwrap(DRef);
313314
if (D) {
314315
try {
315316
auto name = D->get_info<info::device::name>();
316-
auto cstr_len = name.length() + 1;
317-
cstr_name = new char[cstr_len];
318-
#ifdef _WIN32
319-
strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len);
320-
#else
321-
std::strncpy(cstr_name, name.c_str(), cstr_len);
322-
#endif
323-
} catch (std::bad_alloc const &ba) {
324-
// \todo log error
325-
std::cerr << ba.what() << '\n';
317+
cstr_name = dpctl::helper::cstring_from_string(name);
326318
} catch (runtime_error const &re) {
327319
// \todo log error
328320
std::cerr << re.what() << '\n';
@@ -334,21 +326,12 @@ DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
334326
__dpctl_give const char *
335327
DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
336328
{
337-
char *cstr_vendor = nullptr;
329+
const char *cstr_vendor = nullptr;
338330
auto D = unwrap(DRef);
339331
if (D) {
340332
try {
341333
auto vendor = D->get_info<info::device::vendor>();
342-
auto cstr_len = vendor.length() + 1;
343-
cstr_vendor = new char[cstr_len];
344-
#ifdef _WIN32
345-
strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len);
346-
#else
347-
std::strncpy(cstr_vendor, vendor.c_str(), cstr_len);
348-
#endif
349-
} catch (std::bad_alloc const &ba) {
350-
// \todo log error
351-
std::cerr << ba.what() << '\n';
334+
cstr_vendor = dpctl::helper::cstring_from_string(vendor);
352335
} catch (runtime_error const &re) {
353336
// \todo log error
354337
std::cerr << re.what() << '\n';
@@ -360,21 +343,12 @@ DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
360343
__dpctl_give const char *
361344
DPCTLDevice_GetDriverVersion(__dpctl_keep const DPCTLSyclDeviceRef DRef)
362345
{
363-
char *cstr_driver = nullptr;
346+
const char *cstr_driver = nullptr;
364347
auto D = unwrap(DRef);
365348
if (D) {
366349
try {
367350
auto driver = D->get_info<info::device::driver_version>();
368-
auto cstr_len = driver.length() + 1;
369-
cstr_driver = new char[cstr_len];
370-
#ifdef _WIN32
371-
strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len);
372-
#else
373-
std::strncpy(cstr_driver, driver.c_str(), cstr_len);
374-
#endif
375-
} catch (std::bad_alloc const &ba) {
376-
// \todo log error
377-
std::cerr << ba.what() << '\n';
351+
cstr_driver = dpctl::helper::cstring_from_string(driver);
378352
} catch (runtime_error const &re) {
379353
// \todo log error
380354
std::cerr << re.what() << '\n';

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//===----------------------------------------------------------------------===//
2525

2626
#include "dpctl_sycl_device_manager.h"
27+
#include "../helper/include/dpctl_string_utils.hpp"
2728
#include "../helper/include/dpctl_utils_helper.h"
2829
#include "Support/CBindingWrapping.h"
2930
#include "dpctl_sycl_enum_types.h"
@@ -45,7 +46,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
4546
/*
4647
* Helper function to print the metadata for a sycl::device.
4748
*/
48-
void print_device_info(const device &Device)
49+
std::string get_device_info_str(const device &Device)
4950
{
5051
std::stringstream ss;
5152

@@ -62,7 +63,7 @@ void print_device_info(const device &Device)
6263
<< DPCTL_DeviceTypeToStr(Device.get_info<info::device::device_type>())
6364
<< ":" << DPCTL_GetRelativeDeviceId(Device) << '\n';
6465

65-
std::cout << ss.str();
66+
return ss.str();
6667
}
6768

6869
struct DeviceCacheBuilder
@@ -169,6 +170,23 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
169170
return wrap(Devices);
170171
}
171172

173+
__dpctl_give const char *
174+
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
175+
{
176+
const char *cstr_info = nullptr;
177+
auto D = unwrap(DRef);
178+
if (D) {
179+
try {
180+
auto infostr = get_device_info_str(*D);
181+
cstr_info = dpctl::helper::cstring_from_string(infostr);
182+
} catch (runtime_error const &re) {
183+
// \todo log error
184+
std::cerr << re.what() << '\n';
185+
}
186+
}
187+
return cstr_info;
188+
}
189+
172190
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
173191
int device_identifier)
174192
{
@@ -227,7 +245,7 @@ void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
227245
{
228246
auto Device = unwrap(DRef);
229247
if (Device)
230-
print_device_info(*Device);
248+
std::cout << get_device_info_str(*Device);
231249
else
232250
std::cout << "Device is not valid (NULL). Cannot print device info.\n";
233251
}

dpctl-capi/source/dpctl_sycl_kernel_interface.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_kernel_interface.h"
28+
#include "../helper/include/dpctl_string_utils.hpp"
2829
#include "Support/CBindingWrapping.h"
2930
#include <CL/sycl.hpp> /* Sycl headers */
3031

@@ -49,14 +50,7 @@ DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel)
4950
auto kernel_name = SyclKernel->get_info<info::kernel::function_name>();
5051
if (kernel_name.empty())
5152
return nullptr;
52-
auto cstr_len = kernel_name.length() + 1;
53-
auto cstr_name = new char[cstr_len];
54-
#ifdef _WIN32
55-
strncpy_s(cstr_name, cstr_len, kernel_name.c_str(), cstr_len);
56-
#else
57-
std::strncpy(cstr_name, kernel_name.c_str(), cstr_len);
58-
#endif
59-
return cstr_name;
53+
return dpctl::helper::cstring_from_string(kernel_name);
6054
}
6155

6256
size_t DPCTLKernel_GetNumArgs(__dpctl_keep const DPCTLSyclKernelRef Kernel)

0 commit comments

Comments
 (0)