/usr/lib/python2.7/dist-packages/pyopencl/compyte/dtypes.py is in python-pyopencl 2016.1+git20161130-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """Type mapping helpers."""
from __future__ import division
__copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import numpy as np
class TypeNameNotKnown(RuntimeError):
pass
# {{{ registry
class DTypeRegistry(object):
def __init__(self):
self.dtype_to_name = {}
self.name_to_dtype = {}
def get_or_register_dtype(self, c_names, dtype=None):
"""Get or register a :class:`numpy.dtype` associated with the C type names
in the string list *c_names*. If *dtype* is `None`, no registration is
performed, and the :class:`numpy.dtype` must already have been registered.
If so, it is returned. If not, :exc:`TypeNameNotKnown` is raised.
If *dtype* is not `None`, registration is attempted. If the *c_names* are
already known and registered to identical :class:`numpy.dtype` objects,
then the previously dtype object of the previously registered type is
returned. If the *c_names* are not yet known, the type is registered. If
one of the *c_names* is known but registered to a different type, an error
is raised. In this latter case, the type may end up partially registered
and any further behavior is undefined.
.. versionadded:: 2012.2
"""
if isinstance(c_names, str):
c_names = [c_names]
if dtype is None:
from pytools import single_valued
return single_valued(self.name_to_dtype[name] for name in c_names)
dtype = np.dtype(dtype)
# check if we've seen an identical dtype, if so retrieve exact dtype object.
try:
existing_name = self.dtype_to_name[dtype]
except KeyError:
existed = False
else:
existed = True
existing_dtype = self.name_to_dtype[existing_name]
assert existing_dtype == dtype
dtype = existing_dtype
for nm in c_names:
try:
name_dtype = self.name_to_dtype[nm]
except KeyError:
self.name_to_dtype[nm] = dtype
else:
if name_dtype != dtype:
raise RuntimeError("name '%s' already registered to "
"different dtype" % nm)
if not existed:
self.dtype_to_name[dtype] = c_names[0]
if not str(dtype) in self.dtype_to_name:
self.dtype_to_name[str(dtype)] = c_names[0]
return dtype
def dtype_to_ctype(self, dtype):
if dtype is None:
raise ValueError("dtype may not be None")
dtype = np.dtype(dtype)
try:
return self.dtype_to_name[dtype]
except KeyError:
raise ValueError("unable to map dtype '%s'" % dtype)
# }}}
# {{{ C types
def fill_registry_with_c_types(reg, respect_windows, include_bool=True):
from sys import platform
import struct
if include_bool:
# bool is of unspecified size in the OpenCL spec and may in fact be
# 4-byte.
reg.get_or_register_dtype("bool", np.bool)
reg.get_or_register_dtype(["signed char", "char"], np.int8)
reg.get_or_register_dtype("unsigned char", np.uint8)
reg.get_or_register_dtype(["short", "signed short",
"signed short int", "short signed int"], np.int16)
reg.get_or_register_dtype(["unsigned short",
"unsigned short int", "short unsigned int"], np.uint16)
reg.get_or_register_dtype(["int", "signed int"], np.int32)
reg.get_or_register_dtype(["unsigned", "unsigned int"], np.uint32)
is_64_bit = struct.calcsize('@P') * 8 == 64
if is_64_bit:
if 'win32' in platform and respect_windows:
i64_name = "long long"
else:
i64_name = "long"
reg.get_or_register_dtype(
[i64_name, "%s int" % i64_name, "signed %s int" % i64_name,
"%s signed int" % i64_name],
np.int64)
reg.get_or_register_dtype(
["unsigned %s" % i64_name, "unsigned %s int" % i64_name,
"%s unsigned int" % i64_name],
np.uint64)
# http://projects.scipy.org/numpy/ticket/2017
if is_64_bit:
reg.get_or_register_dtype(["unsigned %s" % i64_name], np.uintp)
else:
reg.get_or_register_dtype(["unsigned"], np.uintp)
reg.get_or_register_dtype("float", np.float32)
reg.get_or_register_dtype("double", np.float64)
def fill_registry_with_opencl_c_types(reg):
reg.get_or_register_dtype(["char", "signed char"], np.int8)
reg.get_or_register_dtype(["uchar", "unsigned char"], np.uint8)
reg.get_or_register_dtype(["short", "signed short",
"signed short int", "short signed int"], np.int16)
reg.get_or_register_dtype(["ushort", "unsigned short",
"unsigned short int", "short unsigned int"], np.uint16)
reg.get_or_register_dtype(["int", "signed int"], np.int32)
reg.get_or_register_dtype(["uint", "unsigned", "unsigned int"], np.uint32)
reg.get_or_register_dtype(
["long", "long int", "signed long int",
"long signed int"],
np.int64)
reg.get_or_register_dtype(
["ulong", "unsigned long", "unsigned long int",
"long unsigned int"],
np.uint64)
reg.get_or_register_dtype(["intptr_t"], np.intp)
reg.get_or_register_dtype(["uintptr_t"], np.uintp)
reg.get_or_register_dtype("float", np.float32)
reg.get_or_register_dtype("double", np.float64)
# }}}
# {{{ backward compatibility
TYPE_REGISTRY = DTypeRegistry()
# These are deprecated and should no longer be used
DTYPE_TO_NAME = TYPE_REGISTRY.dtype_to_name
NAME_TO_DTYPE = TYPE_REGISTRY.name_to_dtype
dtype_to_ctype = TYPE_REGISTRY.dtype_to_ctype
get_or_register_dtype = TYPE_REGISTRY.get_or_register_dtype
def _fill_dtype_registry(respect_windows, include_bool=True):
fill_registry_with_c_types(
TYPE_REGISTRY, respect_windows, include_bool)
# }}}
# {{{ c declarator parsing
def parse_c_arg_backend(c_arg, scalar_arg_factory, vec_arg_factory,
name_to_dtype=None):
if isinstance(name_to_dtype, DTypeRegistry):
name_to_dtype = name_to_dtype.name_to_dtype__getitem__
elif name_to_dtype is None:
name_to_dtype = NAME_TO_DTYPE.__getitem__
c_arg = c_arg.replace("const", "").replace("volatile", "")
# process and remove declarator
import re
decl_re = re.compile(r"(\**)\s*([_a-zA-Z0-9]+)(\s*\[[ 0-9]*\])*\s*$")
decl_match = decl_re.search(c_arg)
if decl_match is None:
raise ValueError("couldn't parse C declarator '%s'" % c_arg)
name = decl_match.group(2)
if decl_match.group(1) or decl_match.group(3) is not None:
arg_class = vec_arg_factory
else:
arg_class = scalar_arg_factory
tp = c_arg[:decl_match.start()]
tp = " ".join(tp.split())
try:
dtype = name_to_dtype(tp)
except KeyError:
raise ValueError("unknown type '%s'" % tp)
return arg_class(dtype, name)
# }}}
def register_dtype(dtype, c_names, alias_ok=False):
from warnings import warn
warn("register_dtype is deprecated. Use get_or_register_dtype instead.",
DeprecationWarning, stacklevel=2)
if isinstance(c_names, str):
c_names = [c_names]
dtype = np.dtype(dtype)
# check if we've seen this dtype before and error out if a) it was seen before
# and b) alias_ok is False.
if not alias_ok and dtype in TYPE_REGISTRY.dtype_to_name:
raise RuntimeError("dtype '%s' already registered (as '%s', new names '%s')"
% (dtype, TYPE_REGISTRY.dtype_to_name[dtype], ", ".join(c_names)))
TYPE_REGISTRY.get_or_register_dtype(c_names, dtype)
# vim: foldmethod=marker
|