1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkOpenCLStringUtils.h"
#include <iosfwd>
#include <sstream>
#include <cstring>
namespace itk
{
inline bool
opencl_isspace(const char c)
{
switch (c)
{
case '\t':
case '\n':
case '\r':
case '\f':
case ' ':
return true;
default:
return false;
}
}
//------------------------------------------------------------------------------
std::string
opencl_simplified(const std::string & str)
{
if (str.empty())
{
return str;
}
const std::size_t size = str.size();
std::string result(size, '\0');
const char * from = &str[0];
const char * fromend = from + size;
int outc = 0;
char * to = &result[0];
for (;;)
{
while (from != fromend && opencl_isspace(char(*from)))
{
++from;
}
while (from != fromend && !opencl_isspace(char(*from)))
{
to[outc++] = *from++;
}
if (from != fromend)
{
to[outc++] = ' ';
}
else
{
break;
}
}
if (outc > 0 && to[outc - 1] == ' ')
{
--outc;
}
result.resize(outc);
return result;
}
//------------------------------------------------------------------------------
bool
opencl_has_extension(const std::string & list, const std::string & name)
{
if (list.empty() || name.empty())
{
return false;
}
const std::size_t found = list.find(name);
if (found != std::string::npos)
{
return true;
}
else
{
return false;
}
}
//------------------------------------------------------------------------------
std::string
opencl_get_platform_info_string(const cl_platform_id id, const cl_platform_info name)
{
std::size_t size;
if (!id || clGetPlatformInfo(id, name, 0, 0, &size) != CL_SUCCESS)
{
return std::string();
}
std::string buffer(size, '\0');
clGetPlatformInfo(id, name, size, &buffer[0], &size);
return buffer;
}
//------------------------------------------------------------------------------
std::string
opencl_get_device_info_string(const cl_device_id id, const cl_device_info name)
{
std::size_t size;
if (!id || clGetDeviceInfo(id, name, 0, 0, &size) != CL_SUCCESS)
{
return std::string();
}
std::string buffer(size, '\0');
clGetDeviceInfo(id, name, size, &buffer[0], &size);
return buffer;
}
//------------------------------------------------------------------------------
bool
opencl_is_platform(cl_platform_id id, cl_platform_info name, const char * str)
{
std::size_t len = strlen(str);
std::size_t size;
if (!id || clGetPlatformInfo(id, name, 0, 0, &size) != CL_SUCCESS)
{
return false;
}
if (size <= len)
{
return false;
}
std::string buffer(size, '\0');
clGetPlatformInfo(id, name, size, &buffer[0], &size);
if (strncmp(&buffer[0], str, len) != 0)
{
return false;
}
return buffer[len] == '\0';
}
//------------------------------------------------------------------------------
int
opencl_version_flags(const std::string & /*version*/)
{
// not implemented.
return 1;
}
//------------------------------------------------------------------------------
std::list<std::string>
opencl_split_string(const std::string & str, const char separator)
{
std::list<std::string> strings;
if (str.empty())
{
return strings;
}
std::istringstream f(str);
std::string s;
while (std::getline(f, s, separator))
{
strings.push_back(s);
}
return strings;
}
//------------------------------------------------------------------------------
unsigned int
opencl_get_device_info_uint(const cl_device_id id, const cl_device_info name)
{
cl_uint value;
if (!id || clGetDeviceInfo(id, name, sizeof(value), &value, 0) != CL_SUCCESS)
{
return 0;
}
else
{
const unsigned int result = value;
return result;
}
}
//------------------------------------------------------------------------------
int
opencl_get_device_info_int(const cl_device_id id, const cl_device_info name)
{
cl_int value;
if (!id || clGetDeviceInfo(id, name, sizeof(value), &value, 0) != CL_SUCCESS)
{
return 0;
}
else
{
const int result = value;
return result;
}
}
//------------------------------------------------------------------------------
unsigned long
opencl_get_device_info_ulong(const cl_device_id id, const cl_device_info name)
{
cl_ulong value;
if (!id || clGetDeviceInfo(id, name, sizeof(value), &value, 0) != CL_SUCCESS)
{
return 0;
}
else
{
const unsigned long result = value;
return result;
}
}
//------------------------------------------------------------------------------
std::size_t
opencl_get_device_info_size(const cl_device_id id, const cl_device_info name)
{
std::size_t value;
if (!id || clGetDeviceInfo(id, name, sizeof(value), &value, 0) != CL_SUCCESS)
{
return 0;
}
else
{
return value;
}
}
//------------------------------------------------------------------------------
bool
opencl_get_device_info_bool(const cl_device_id id, const cl_device_info name)
{
cl_bool value;
if (!id || clGetDeviceInfo(id, name, sizeof(value), &value, 0) != CL_SUCCESS)
{
return false;
}
else
{
return value != 0;
}
}
//------------------------------------------------------------------------------
bool
opencl_get_device_info_is_string(const cl_device_id id, const cl_device_info name, const char * str)
{
std::size_t len = strlen(str);
std::size_t size;
if (!id || clGetDeviceInfo(id, name, 0, 0, &size) != CL_SUCCESS)
{
return false;
}
if (size <= len)
{
return false;
}
std::string buffer(size, '\0');
clGetDeviceInfo(id, name, size, &buffer[0], &size);
if (strncmp(&buffer[0], str, len) != 0)
{
return false;
}
return buffer[len] == '\0';
}
} // end of namespace itk
|