Use std::string for get_device_*_string helpers (#737)

* Use std::string for get_device_*_string helpers

Removes need to use BufferOwningPtr to avoid memory leaks.

* Rename get_device_info to get_device_info_string

Makes it more obvious that it should only be called for device queries
that return strings.

* Tokenize extensions in is_extension_available

Avoids the potential issue where one extension name is a prefix of
another.

* Throw exception when device info size is 0
This commit is contained in:
James Price
2020-04-16 14:27:40 -04:00
committed by GitHub
parent c911046f5b
commit 2ac3c2c1ef
3 changed files with 44 additions and 53 deletions

View File

@@ -13,60 +13,69 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <sstream>
#include <stdexcept>
#include <vector>
#include "deviceInfo.h"
#include "errorHelpers.h"
#include "typeWrappers.h"
/* Helper to allocate and return a buffer containing device information for the specified device info parameter. */
static void *alloc_and_get_device_info( cl_device_id device, cl_device_info param_name, const char *param_description )
/* Helper to return a string containing device information for the specified
* device info parameter. */
static std::string get_device_info_string(cl_device_id device,
cl_device_info param_name)
{
size_t size = 0;
int err;
if ((err = clGetDeviceInfo(device, param_name, 0, NULL, &size)) != CL_SUCCESS)
if ((err = clGetDeviceInfo(device, param_name, 0, NULL, &size))
!= CL_SUCCESS
|| size == 0)
{
throw std::runtime_error("clGetDeviceInfo failed\n");
}
if (0 == size)
return NULL;
std::vector<char> info(size);
auto buffer = new uint8_t[size];
if ((err = clGetDeviceInfo(device, param_name, size, buffer, NULL)) != CL_SUCCESS)
if ((err = clGetDeviceInfo(device, param_name, size, info.data(), NULL))
!= CL_SUCCESS)
{
delete [] buffer;
throw std::runtime_error("clGetDeviceInfo failed\n");
}
return buffer;
return std::string(info.begin(), info.end());
}
/* Determines if an extension is supported by a device. */
int is_extension_available(cl_device_id device, const char *extensionName)
{
char *extString = alloc_and_get_device_extensions_string(device);
BufferOwningPtr<char> extStringBuf(extString);
return strstr(extString, extensionName) != NULL;
}
/* Returns a newly allocated C string containing the supported extensions list for a device. */
char *alloc_and_get_device_extensions_string(cl_device_id device)
std::string extString = get_device_extensions_string(device);
std::istringstream ss(extString);
while (ss)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_EXTENSIONS, "extensions string");
std::string found;
ss >> found;
if (found == extensionName) return true;
}
return false;
}
/* Returns a newly allocated C string containing the supported IL version(s) for a device. */
char *alloc_and_get_device_il_version_string(cl_device_id device)
/* Returns a string containing the supported extensions list for a device. */
std::string get_device_extensions_string(cl_device_id device)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_IL_VERSION, "IL version string");
return get_device_info_string(device, CL_DEVICE_EXTENSIONS);
}
/* Returns a newly allocated C string containing the supported OpenCL version for a device. */
char *alloc_and_get_device_version_string(cl_device_id device)
/* Returns a string containing the supported IL version(s) for a device. */
std::string get_device_il_version_string(cl_device_id device)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_VERSION, "version string");
return get_device_info_string(device, CL_DEVICE_IL_VERSION);
}
/* Returns a string containing the supported OpenCL version for a device. */
std::string get_device_version_string(cl_device_id device)
{
return get_device_info_string(device, CL_DEVICE_VERSION);
}

View File

@@ -19,6 +19,8 @@
// Configuration
#include "../config.hpp"
#include <string>
#include <CL/opencl.h>
#ifdef __cplusplus
@@ -28,14 +30,14 @@ extern "C" {
/* Determines if an extension is supported by a device. */
int is_extension_available(cl_device_id device, const char *extensionName);
/* Returns a newly allocated C string containing the supported extensions list for a device. */
char *alloc_and_get_device_extensions_string(cl_device_id device);
/* Returns a string containing the supported extensions list for a device. */
std::string get_device_extensions_string(cl_device_id device);
/* Returns a newly allocated C string containing the supported IL version(s) for a device. */
char *alloc_and_get_device_il_version_string(cl_device_id device);
/* Returns a string containing the supported IL version(s) for a device. */
std::string get_device_il_version_string(cl_device_id device);
/* Returns a newly allocated C string containing the supported OpenCL version for a device. */
char *alloc_and_get_device_version_string(cl_device_id device);
/* Returns a string containing the supported OpenCL version for a device. */
std::string get_device_version_string(cl_device_id device);
#ifdef __cplusplus
}

View File

@@ -356,21 +356,8 @@ static std::string get_khronos_compiler_command(const cl_uint device_address_spa
static cl_int get_cl_device_info_str(const cl_device_id device, const cl_uint device_address_space_size,
const CompilationMode compilationMode, std::string &clDeviceInfo)
{
char *extensionsString = alloc_and_get_device_extensions_string(device);
if ( NULL == extensionsString )
{
return -1;
}
BufferOwningPtr<char> extensionsStringBuf(extensionsString);
char *versionString = alloc_and_get_device_version_string(device);
if ( NULL == versionString )
{
return -1;
}
BufferOwningPtr<char> versionStringBuf(versionString);
std::string extensionsString = get_device_extensions_string(device);
std::string versionString = get_device_version_string(device);
std::ostringstream clDeviceInfoStream;
std::string file_type = get_offline_compilation_file_type_str(compilationMode);
@@ -381,14 +368,7 @@ static cl_int get_cl_device_info_str(const cl_device_id device, const cl_uint de
* that will be loaded with clCreateProgramWithIL() */
if (compilationMode == kSpir_v)
{
char *ilVersionString = alloc_and_get_device_il_version_string(device);
if ( NULL == ilVersionString )
{
return -1;
}
BufferOwningPtr<char> versionStringBuf(ilVersionString);
std::string ilVersionString = get_device_il_version_string(device);
clDeviceInfoStream << "CL_DEVICE_IL_VERSION=\"" << ilVersionString << "\"" << std::endl;
}
clDeviceInfoStream << "CL_DEVICE_VERSION=\"" << versionString << "\"" << std::endl;