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;
std::string extString = get_device_extensions_string(device);
std::istringstream ss(extString);
while (ss)
{
std::string found;
ss >> found;
if (found == extensionName) return true;
}
return false;
}
/* 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)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_EXTENSIONS, "extensions string");
return get_device_info_string(device, CL_DEVICE_EXTENSIONS);
}
/* 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)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_IL_VERSION, "IL version string");
return get_device_info_string(device, CL_DEVICE_IL_VERSION);
}
/* 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)
{
return (char *) alloc_and_get_device_info(device, CL_DEVICE_VERSION, "version string");
return get_device_info_string(device, CL_DEVICE_VERSION);
}