pyopencl 2024.3__cp39-cp39-musllinux_1_2_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyopencl might be problematic. Click here for more details.
- pyopencl/.libs/libOpenCL-1ef0e16e.so.1.0.0 +0 -0
- pyopencl/__init__.py +2410 -0
- pyopencl/_cl.cpython-39-x86_64-linux-gnu.so +0 -0
- pyopencl/_cluda.py +54 -0
- pyopencl/_mymako.py +14 -0
- pyopencl/algorithm.py +1449 -0
- pyopencl/array.py +3437 -0
- pyopencl/bitonic_sort.py +242 -0
- pyopencl/bitonic_sort_templates.py +594 -0
- pyopencl/cache.py +535 -0
- pyopencl/capture_call.py +177 -0
- pyopencl/characterize/__init__.py +456 -0
- pyopencl/characterize/performance.py +237 -0
- pyopencl/cl/pyopencl-airy.cl +324 -0
- pyopencl/cl/pyopencl-bessel-j-complex.cl +238 -0
- pyopencl/cl/pyopencl-bessel-j.cl +1084 -0
- pyopencl/cl/pyopencl-bessel-y.cl +435 -0
- pyopencl/cl/pyopencl-complex.h +303 -0
- pyopencl/cl/pyopencl-eval-tbl.cl +120 -0
- pyopencl/cl/pyopencl-hankel-complex.cl +444 -0
- pyopencl/cl/pyopencl-random123/array.h +325 -0
- pyopencl/cl/pyopencl-random123/openclfeatures.h +93 -0
- pyopencl/cl/pyopencl-random123/philox.cl +486 -0
- pyopencl/cl/pyopencl-random123/threefry.cl +864 -0
- pyopencl/clmath.py +280 -0
- pyopencl/clrandom.py +409 -0
- pyopencl/cltypes.py +137 -0
- pyopencl/compyte/.gitignore +21 -0
- pyopencl/compyte/__init__.py +0 -0
- pyopencl/compyte/array.py +214 -0
- pyopencl/compyte/dtypes.py +290 -0
- pyopencl/compyte/pyproject.toml +54 -0
- pyopencl/elementwise.py +1171 -0
- pyopencl/invoker.py +421 -0
- pyopencl/ipython_ext.py +68 -0
- pyopencl/reduction.py +786 -0
- pyopencl/scan.py +1915 -0
- pyopencl/tools.py +1527 -0
- pyopencl/version.py +9 -0
- pyopencl-2024.3.dist-info/METADATA +108 -0
- pyopencl-2024.3.dist-info/RECORD +43 -0
- pyopencl-2024.3.dist-info/WHEEL +5 -0
- pyopencl-2024.3.dist-info/licenses/LICENSE +104 -0
pyopencl/cltypes.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
__copyright__ = "Copyright (C) 2016 Jonathan Mackenzie"
|
|
2
|
+
|
|
3
|
+
__license__ = """
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
15
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
16
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
18
|
+
THE SOFTWARE.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import warnings
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
|
|
25
|
+
from pyopencl.tools import get_or_register_dtype
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __file__.endswith("array.py"):
|
|
29
|
+
warnings.warn(
|
|
30
|
+
"pyopencl.array.vec is deprecated. Please use pyopencl.cltypes.",
|
|
31
|
+
stacklevel=2)
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
This file provides a type mapping from OpenCl type names to their numpy equivalents
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
char = np.int8
|
|
38
|
+
uchar = np.uint8
|
|
39
|
+
short = np.int16
|
|
40
|
+
ushort = np.uint16
|
|
41
|
+
int = np.int32
|
|
42
|
+
uint = np.uint32
|
|
43
|
+
long = np.int64
|
|
44
|
+
ulong = np.uint64
|
|
45
|
+
half = np.float16
|
|
46
|
+
float = np.float32
|
|
47
|
+
double = np.float64
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# {{{ vector types
|
|
51
|
+
|
|
52
|
+
def _create_vector_types():
|
|
53
|
+
_mapping = [(k, globals()[k]) for k in
|
|
54
|
+
["char", "uchar", "short", "ushort", "int",
|
|
55
|
+
"uint", "long", "ulong", "float", "double"]]
|
|
56
|
+
|
|
57
|
+
def set_global(key, val):
|
|
58
|
+
globals()[key] = val
|
|
59
|
+
|
|
60
|
+
vec_types = {}
|
|
61
|
+
vec_type_to_scalar_and_count = {}
|
|
62
|
+
|
|
63
|
+
field_names = ["x", "y", "z", "w"]
|
|
64
|
+
|
|
65
|
+
counts = [2, 3, 4, 8, 16]
|
|
66
|
+
|
|
67
|
+
for base_name, base_type in _mapping:
|
|
68
|
+
for count in counts:
|
|
69
|
+
name = "%s%d" % (base_name, count)
|
|
70
|
+
|
|
71
|
+
titles = field_names[:count]
|
|
72
|
+
|
|
73
|
+
padded_count = count
|
|
74
|
+
if count == 3:
|
|
75
|
+
padded_count = 4
|
|
76
|
+
|
|
77
|
+
names = ["s%d" % i for i in range(count)]
|
|
78
|
+
while len(names) < padded_count:
|
|
79
|
+
names.append("padding%d" % (len(names) - count))
|
|
80
|
+
|
|
81
|
+
if len(titles) < len(names):
|
|
82
|
+
titles.extend((len(names) - len(titles)) * [None])
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
dtype = np.dtype({
|
|
86
|
+
"names": names,
|
|
87
|
+
"formats": [base_type] * padded_count,
|
|
88
|
+
"titles": titles})
|
|
89
|
+
except NotImplementedError:
|
|
90
|
+
try:
|
|
91
|
+
dtype = np.dtype([((n, title), base_type)
|
|
92
|
+
for (n, title) in zip(names, titles)])
|
|
93
|
+
except TypeError:
|
|
94
|
+
dtype = np.dtype([(n, base_type) for (n, title)
|
|
95
|
+
in zip(names, titles)])
|
|
96
|
+
|
|
97
|
+
get_or_register_dtype(name, dtype)
|
|
98
|
+
|
|
99
|
+
set_global(name, dtype)
|
|
100
|
+
|
|
101
|
+
def create_array(dtype, count, padded_count, *args, **kwargs):
|
|
102
|
+
if len(args) < count:
|
|
103
|
+
from warnings import warn
|
|
104
|
+
warn("default values for make_xxx are deprecated;"
|
|
105
|
+
" instead specify all parameters or use"
|
|
106
|
+
" cltypes.zeros_xxx",
|
|
107
|
+
DeprecationWarning, stacklevel=4)
|
|
108
|
+
|
|
109
|
+
padded_args = tuple(list(args) + [0] * (padded_count - len(args)))
|
|
110
|
+
array = eval("array(padded_args, dtype=dtype)",
|
|
111
|
+
{"array": np.array,
|
|
112
|
+
"padded_args": padded_args,
|
|
113
|
+
"dtype": dtype})
|
|
114
|
+
for key, val in list(kwargs.items()):
|
|
115
|
+
array[key] = val
|
|
116
|
+
return array
|
|
117
|
+
|
|
118
|
+
set_global("make_" + name, eval(
|
|
119
|
+
"lambda *args, **kwargs: create_array(dtype, %i, %i, "
|
|
120
|
+
"*args, **kwargs)" % (count, padded_count),
|
|
121
|
+
{"create_array": create_array, "dtype": dtype}))
|
|
122
|
+
set_global("filled_" + name, eval(
|
|
123
|
+
"lambda val: make_%s(*[val]*%i)" % (name, count)))
|
|
124
|
+
set_global("zeros_" + name, eval("lambda: filled_%s(0)" % (name)))
|
|
125
|
+
set_global("ones_" + name, eval("lambda: filled_%s(1)" % (name)))
|
|
126
|
+
|
|
127
|
+
vec_types[np.dtype(base_type), count] = dtype
|
|
128
|
+
vec_type_to_scalar_and_count[dtype] = np.dtype(base_type), count
|
|
129
|
+
|
|
130
|
+
return vec_types, vec_type_to_scalar_and_count
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
vec_types, vec_type_to_scalar_and_count = _create_vector_types()
|
|
134
|
+
|
|
135
|
+
# }}}
|
|
136
|
+
|
|
137
|
+
# vim: foldmethod=marker
|
|
File without changes
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
__copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
|
|
2
|
+
|
|
3
|
+
__license__ = """
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def f_contiguous_strides(itemsize, shape):
|
|
27
|
+
if shape:
|
|
28
|
+
strides = [itemsize]
|
|
29
|
+
for s in shape[:-1]:
|
|
30
|
+
# NOTE: max(1, s) is used to handle 0-sized axes in `shape`;
|
|
31
|
+
# the stride for `shape[i] <= 1` doesn't matter, but letting it be 0
|
|
32
|
+
# is not a good idea: https://github.com/inducer/arraycontext/pull/91
|
|
33
|
+
strides.append(strides[-1]*max(1, s))
|
|
34
|
+
return tuple(strides)
|
|
35
|
+
else:
|
|
36
|
+
return ()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def c_contiguous_strides(itemsize, shape):
|
|
40
|
+
if shape:
|
|
41
|
+
strides = [itemsize]
|
|
42
|
+
for s in shape[:0:-1]:
|
|
43
|
+
# NOTE: max(1, s) is used to handle 0-sized axes in `shape`;
|
|
44
|
+
# the stride for `shape[i] <= 1` doesn't matter, but letting it be 0
|
|
45
|
+
# is not a good idea: https://github.com/inducer/arraycontext/pull/91
|
|
46
|
+
strides.append(strides[-1]*max(1, s))
|
|
47
|
+
return tuple(strides[::-1])
|
|
48
|
+
else:
|
|
49
|
+
return ()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def equal_strides(strides1, strides2, shape):
|
|
53
|
+
if strides1 == strides2:
|
|
54
|
+
return True
|
|
55
|
+
|
|
56
|
+
if len(strides1) != len(strides2) or len(strides2) != len(shape):
|
|
57
|
+
return False
|
|
58
|
+
|
|
59
|
+
for s, st1, st2 in zip(shape, strides1, strides2):
|
|
60
|
+
if s != 1 and st1 != st2:
|
|
61
|
+
return False
|
|
62
|
+
|
|
63
|
+
return True
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def is_f_contiguous_strides(strides, itemsize, shape):
|
|
67
|
+
from pytools import product
|
|
68
|
+
return (
|
|
69
|
+
equal_strides(strides, f_contiguous_strides(itemsize, shape), shape)
|
|
70
|
+
or product(shape) == 0) # noqa: W503
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def is_c_contiguous_strides(strides, itemsize, shape):
|
|
74
|
+
from pytools import product
|
|
75
|
+
return (equal_strides(strides, c_contiguous_strides(itemsize, shape), shape)
|
|
76
|
+
or product(shape) == 0) # noqa: W503
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class ArrayFlags:
|
|
80
|
+
def __init__(self, ary):
|
|
81
|
+
self.f_contiguous = is_f_contiguous_strides(
|
|
82
|
+
ary.strides, ary.dtype.itemsize, ary.shape)
|
|
83
|
+
self.c_contiguous = is_c_contiguous_strides(
|
|
84
|
+
ary.strides, ary.dtype.itemsize, ary.shape)
|
|
85
|
+
self.forc = self.f_contiguous or self.c_contiguous
|
|
86
|
+
|
|
87
|
+
def __repr__(self):
|
|
88
|
+
return (
|
|
89
|
+
f" C_CONTIGUOUS : {self.c_contiguous}\n"
|
|
90
|
+
f" F_CONTIGUOUS : {self.f_contiguous}"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def __str__(self):
|
|
94
|
+
return repr(self)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def get_common_dtype(obj1, obj2, allow_double):
|
|
98
|
+
# Yes, numpy behaves differently depending on whether
|
|
99
|
+
# we're dealing with arrays or scalars.
|
|
100
|
+
|
|
101
|
+
zero1 = np.zeros(1, dtype=obj1.dtype)
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
zero2 = np.zeros(1, dtype=obj2.dtype)
|
|
105
|
+
except AttributeError:
|
|
106
|
+
zero2 = obj2
|
|
107
|
+
|
|
108
|
+
result = (zero1 + zero2).dtype
|
|
109
|
+
|
|
110
|
+
if not allow_double:
|
|
111
|
+
if result == np.float64:
|
|
112
|
+
result = np.dtype(np.float32)
|
|
113
|
+
elif result == np.complex128:
|
|
114
|
+
result = np.dtype(np.complex64)
|
|
115
|
+
|
|
116
|
+
return result
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def bound(a):
|
|
120
|
+
high = a.bytes
|
|
121
|
+
low = a.bytes
|
|
122
|
+
|
|
123
|
+
for stri, shp in zip(a.strides, a.shape):
|
|
124
|
+
if stri < 0:
|
|
125
|
+
low += (stri)*(shp-1)
|
|
126
|
+
else:
|
|
127
|
+
high += (stri)*(shp-1)
|
|
128
|
+
return low, high
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def may_share_memory(a, b):
|
|
132
|
+
# When this is called with a an ndarray and b
|
|
133
|
+
# a sparse matrix, numpy.may_share_memory fails.
|
|
134
|
+
if a is b:
|
|
135
|
+
return True
|
|
136
|
+
if a.__class__ is b.__class__:
|
|
137
|
+
a_l, a_h = bound(a)
|
|
138
|
+
b_l, b_h = bound(b)
|
|
139
|
+
if b_l >= a_h or a_l >= b_h:
|
|
140
|
+
return False
|
|
141
|
+
return True
|
|
142
|
+
else:
|
|
143
|
+
return False
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# {{{ as_strided implementation
|
|
147
|
+
|
|
148
|
+
try:
|
|
149
|
+
from numpy.lib.stride_tricks import as_strided as _as_strided
|
|
150
|
+
_test_dtype = np.dtype(
|
|
151
|
+
[("a", np.float64), ("b", np.float64)], align=True)
|
|
152
|
+
_test_result = _as_strided(np.zeros(10, dtype=_test_dtype))
|
|
153
|
+
if _test_result.dtype != _test_dtype:
|
|
154
|
+
raise RuntimeError("numpy's as_strided is broken")
|
|
155
|
+
|
|
156
|
+
as_strided = _as_strided
|
|
157
|
+
except Exception:
|
|
158
|
+
# stolen from numpy to be compatible with older versions of numpy
|
|
159
|
+
class _DummyArray:
|
|
160
|
+
""" Dummy object that just exists to hang __array_interface__ dictionaries
|
|
161
|
+
and possibly keep alive a reference to a base array.
|
|
162
|
+
"""
|
|
163
|
+
def __init__(self, interface, base=None):
|
|
164
|
+
self.__array_interface__ = interface
|
|
165
|
+
self.base = base
|
|
166
|
+
|
|
167
|
+
def as_strided(x, shape=None, strides=None):
|
|
168
|
+
""" Make an ndarray from the given array with the given shape and strides.
|
|
169
|
+
"""
|
|
170
|
+
# work around Numpy bug 1873 (reported by Irwin Zaid)
|
|
171
|
+
# Since this is stolen from numpy, this implementation has the same bug.
|
|
172
|
+
# http://projects.scipy.org/numpy/ticket/1873
|
|
173
|
+
# == https://github.com/numpy/numpy/issues/2466
|
|
174
|
+
|
|
175
|
+
# Do not recreate the array if nothing need to be changed.
|
|
176
|
+
# This fixes a lot of errors on pypy since DummyArray hack does not
|
|
177
|
+
# currently (2014/May/17) on pypy.
|
|
178
|
+
|
|
179
|
+
if ((shape is None or x.shape == shape)
|
|
180
|
+
and (strides is None or x.strides == strides)): # noqa: W503
|
|
181
|
+
return x
|
|
182
|
+
if not x.dtype.isbuiltin:
|
|
183
|
+
if shape is None:
|
|
184
|
+
shape = x.shape
|
|
185
|
+
strides = tuple(strides)
|
|
186
|
+
|
|
187
|
+
from pytools import product
|
|
188
|
+
if strides is not None and shape is not None \
|
|
189
|
+
and product(shape) == product(x.shape) \
|
|
190
|
+
and x.flags.forc:
|
|
191
|
+
# Workaround: If we're being asked to do what amounts to a
|
|
192
|
+
# contiguous reshape, at least do that.
|
|
193
|
+
|
|
194
|
+
if strides == f_contiguous_strides(x.dtype.itemsize, shape):
|
|
195
|
+
result = x.reshape(-1).reshape(*shape, order="F")
|
|
196
|
+
assert result.strides == strides
|
|
197
|
+
return result
|
|
198
|
+
elif strides == c_contiguous_strides(x.dtype.itemsize, shape):
|
|
199
|
+
result = x.reshape(-1).reshape(*shape, order="C")
|
|
200
|
+
assert result.strides == strides
|
|
201
|
+
return result
|
|
202
|
+
|
|
203
|
+
raise NotImplementedError(
|
|
204
|
+
"as_strided won't work on non-builtin arrays for now. "
|
|
205
|
+
"See https://github.com/numpy/numpy/issues/2466")
|
|
206
|
+
|
|
207
|
+
interface = dict(x.__array_interface__)
|
|
208
|
+
if shape is not None:
|
|
209
|
+
interface["shape"] = tuple(shape)
|
|
210
|
+
if strides is not None:
|
|
211
|
+
interface["strides"] = tuple(strides)
|
|
212
|
+
return np.asarray(_DummyArray(interface, base=x))
|
|
213
|
+
|
|
214
|
+
# }}}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""Type mapping helpers."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
__copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
|
|
5
|
+
|
|
6
|
+
__license__ = """
|
|
7
|
+
Permission is hereby granted, free of charge, to any person
|
|
8
|
+
obtaining a copy of this software and associated documentation
|
|
9
|
+
files (the "Software"), to deal in the Software without
|
|
10
|
+
restriction, including without limitation the rights to use,
|
|
11
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the
|
|
13
|
+
Software is furnished to do so, subject to the following
|
|
14
|
+
conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
21
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
23
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
24
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
25
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
26
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
import numpy as np
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TypeNameNotKnown(RuntimeError): # noqa: N818
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# {{{ registry
|
|
37
|
+
|
|
38
|
+
class DTypeRegistry:
|
|
39
|
+
def __init__(self):
|
|
40
|
+
self.dtype_to_name = {}
|
|
41
|
+
self.name_to_dtype = {}
|
|
42
|
+
|
|
43
|
+
def get_or_register_dtype(self, c_names, dtype=None):
|
|
44
|
+
"""Get or register a :class:`numpy.dtype` associated with the C type names
|
|
45
|
+
in the string list *c_names*. If *dtype* is `None`, no registration is
|
|
46
|
+
performed, and the :class:`numpy.dtype` must already have been registered.
|
|
47
|
+
If so, it is returned. If not, :exc:`TypeNameNotKnown` is raised.
|
|
48
|
+
|
|
49
|
+
If *dtype* is not `None`, registration is attempted. If the *c_names* are
|
|
50
|
+
already known and registered to identical :class:`numpy.dtype` objects,
|
|
51
|
+
then the previously dtype object of the previously registered type is
|
|
52
|
+
returned. If the *c_names* are not yet known, the type is registered. If
|
|
53
|
+
one of the *c_names* is known but registered to a different type, an error
|
|
54
|
+
is raised. In this latter case, the type may end up partially registered
|
|
55
|
+
and any further behavior is undefined.
|
|
56
|
+
|
|
57
|
+
.. versionadded:: 2012.2
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
if isinstance(c_names, str):
|
|
61
|
+
c_names = [c_names]
|
|
62
|
+
|
|
63
|
+
if dtype is None:
|
|
64
|
+
from pytools import single_valued
|
|
65
|
+
return single_valued(self.name_to_dtype[name] for name in c_names)
|
|
66
|
+
|
|
67
|
+
dtype = np.dtype(dtype)
|
|
68
|
+
|
|
69
|
+
# check if we've seen an identical dtype, if so retrieve exact dtype object.
|
|
70
|
+
try:
|
|
71
|
+
existing_name = self.dtype_to_name[dtype]
|
|
72
|
+
except KeyError:
|
|
73
|
+
existed = False
|
|
74
|
+
else:
|
|
75
|
+
existed = True
|
|
76
|
+
existing_dtype = self.name_to_dtype[existing_name]
|
|
77
|
+
assert existing_dtype == dtype
|
|
78
|
+
dtype = existing_dtype
|
|
79
|
+
|
|
80
|
+
for nm in c_names:
|
|
81
|
+
try:
|
|
82
|
+
name_dtype = self.name_to_dtype[nm]
|
|
83
|
+
except KeyError:
|
|
84
|
+
self.name_to_dtype[nm] = dtype
|
|
85
|
+
else:
|
|
86
|
+
if name_dtype != dtype:
|
|
87
|
+
raise RuntimeError("name '%s' already registered to "
|
|
88
|
+
"different dtype" % nm)
|
|
89
|
+
|
|
90
|
+
if not existed:
|
|
91
|
+
self.dtype_to_name[dtype] = c_names[0]
|
|
92
|
+
if str(dtype) not in self.dtype_to_name:
|
|
93
|
+
self.dtype_to_name[str(dtype)] = c_names[0]
|
|
94
|
+
|
|
95
|
+
return dtype
|
|
96
|
+
|
|
97
|
+
def dtype_to_ctype(self, dtype):
|
|
98
|
+
if dtype is None:
|
|
99
|
+
raise ValueError("dtype may not be None")
|
|
100
|
+
|
|
101
|
+
dtype = np.dtype(dtype)
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
return self.dtype_to_name[dtype]
|
|
105
|
+
except KeyError:
|
|
106
|
+
raise ValueError("unable to map dtype '%s'" % dtype) from None
|
|
107
|
+
|
|
108
|
+
# }}}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# {{{ C types
|
|
112
|
+
|
|
113
|
+
def fill_registry_with_c_types(reg, respect_windows, include_bool=True):
|
|
114
|
+
import struct
|
|
115
|
+
from sys import platform
|
|
116
|
+
|
|
117
|
+
if include_bool:
|
|
118
|
+
# bool is of unspecified size in the OpenCL spec and may in fact be
|
|
119
|
+
# 4-byte.
|
|
120
|
+
reg.get_or_register_dtype("bool", np.bool_)
|
|
121
|
+
|
|
122
|
+
reg.get_or_register_dtype(["signed char", "char"], np.int8)
|
|
123
|
+
reg.get_or_register_dtype("unsigned char", np.uint8)
|
|
124
|
+
reg.get_or_register_dtype(["short", "signed short",
|
|
125
|
+
"signed short int", "short signed int"], np.int16)
|
|
126
|
+
reg.get_or_register_dtype(["unsigned short",
|
|
127
|
+
"unsigned short int", "short unsigned int"], np.uint16)
|
|
128
|
+
reg.get_or_register_dtype(["int", "signed int"], np.int32)
|
|
129
|
+
reg.get_or_register_dtype(["unsigned", "unsigned int"], np.uint32)
|
|
130
|
+
|
|
131
|
+
is_64_bit = struct.calcsize("@P") * 8 == 64
|
|
132
|
+
if is_64_bit:
|
|
133
|
+
if "win32" in platform and respect_windows:
|
|
134
|
+
i64_name = "long long"
|
|
135
|
+
else:
|
|
136
|
+
i64_name = "long"
|
|
137
|
+
|
|
138
|
+
reg.get_or_register_dtype(
|
|
139
|
+
[i64_name, "%s int" % i64_name, "signed %s int" % i64_name,
|
|
140
|
+
"%s signed int" % i64_name],
|
|
141
|
+
np.int64)
|
|
142
|
+
reg.get_or_register_dtype(
|
|
143
|
+
["unsigned %s" % i64_name, "unsigned %s int" % i64_name,
|
|
144
|
+
"%s unsigned int" % i64_name],
|
|
145
|
+
np.uint64)
|
|
146
|
+
|
|
147
|
+
# http://projects.scipy.org/numpy/ticket/2017
|
|
148
|
+
if is_64_bit:
|
|
149
|
+
reg.get_or_register_dtype(["unsigned %s" % i64_name], np.uintp)
|
|
150
|
+
else:
|
|
151
|
+
reg.get_or_register_dtype(["unsigned"], np.uintp)
|
|
152
|
+
|
|
153
|
+
reg.get_or_register_dtype("float", np.float32)
|
|
154
|
+
reg.get_or_register_dtype("double", np.float64)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def fill_registry_with_opencl_c_types(reg):
|
|
158
|
+
reg.get_or_register_dtype(["char", "signed char"], np.int8)
|
|
159
|
+
reg.get_or_register_dtype(["uchar", "unsigned char"], np.uint8)
|
|
160
|
+
reg.get_or_register_dtype(["short", "signed short",
|
|
161
|
+
"signed short int", "short signed int"], np.int16)
|
|
162
|
+
reg.get_or_register_dtype(["ushort", "unsigned short",
|
|
163
|
+
"unsigned short int", "short unsigned int"], np.uint16)
|
|
164
|
+
reg.get_or_register_dtype(["int", "signed int"], np.int32)
|
|
165
|
+
reg.get_or_register_dtype(["uint", "unsigned", "unsigned int"], np.uint32)
|
|
166
|
+
|
|
167
|
+
reg.get_or_register_dtype(
|
|
168
|
+
["long", "long int", "signed long int",
|
|
169
|
+
"long signed int"],
|
|
170
|
+
np.int64)
|
|
171
|
+
reg.get_or_register_dtype(
|
|
172
|
+
["ulong", "unsigned long", "unsigned long int",
|
|
173
|
+
"long unsigned int"],
|
|
174
|
+
np.uint64)
|
|
175
|
+
|
|
176
|
+
reg.get_or_register_dtype(["intptr_t"], np.intp)
|
|
177
|
+
reg.get_or_register_dtype(["uintptr_t"], np.uintp)
|
|
178
|
+
|
|
179
|
+
reg.get_or_register_dtype("float", np.float32)
|
|
180
|
+
reg.get_or_register_dtype("double", np.float64)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def fill_registry_with_c99_stdint_types(reg):
|
|
184
|
+
reg.get_or_register_dtype("bool", np.bool_)
|
|
185
|
+
|
|
186
|
+
reg.get_or_register_dtype("int8_t", np.int8)
|
|
187
|
+
reg.get_or_register_dtype("uint8_t", np.uint8)
|
|
188
|
+
reg.get_or_register_dtype("int16_t", np.int16)
|
|
189
|
+
reg.get_or_register_dtype("uint16_t", np.uint16)
|
|
190
|
+
reg.get_or_register_dtype("int32_t", np.int32)
|
|
191
|
+
reg.get_or_register_dtype("uint32_t", np.uint32)
|
|
192
|
+
reg.get_or_register_dtype("int64_t", np.int64)
|
|
193
|
+
reg.get_or_register_dtype("uint64_t", np.uint64)
|
|
194
|
+
reg.get_or_register_dtype("uintptr_t", np.uintp)
|
|
195
|
+
|
|
196
|
+
reg.get_or_register_dtype("float", np.float32)
|
|
197
|
+
reg.get_or_register_dtype("double", np.float64)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def fill_registry_with_c99_complex_types(reg):
|
|
201
|
+
reg.get_or_register_dtype("float complex", np.complex64)
|
|
202
|
+
reg.get_or_register_dtype("double complex", np.complex128)
|
|
203
|
+
reg.get_or_register_dtype("long double complex", np.clongdouble)
|
|
204
|
+
|
|
205
|
+
# }}}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
# {{{ backward compatibility
|
|
209
|
+
|
|
210
|
+
TYPE_REGISTRY = DTypeRegistry()
|
|
211
|
+
|
|
212
|
+
# These are deprecated and should no longer be used
|
|
213
|
+
DTYPE_TO_NAME = TYPE_REGISTRY.dtype_to_name
|
|
214
|
+
NAME_TO_DTYPE = TYPE_REGISTRY.name_to_dtype
|
|
215
|
+
|
|
216
|
+
dtype_to_ctype = TYPE_REGISTRY.dtype_to_ctype
|
|
217
|
+
get_or_register_dtype = TYPE_REGISTRY.get_or_register_dtype
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _fill_dtype_registry(respect_windows, include_bool=True):
|
|
221
|
+
fill_registry_with_c_types(
|
|
222
|
+
TYPE_REGISTRY, respect_windows, include_bool)
|
|
223
|
+
|
|
224
|
+
# }}}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# {{{ c declarator parsing
|
|
228
|
+
|
|
229
|
+
def parse_c_arg_backend(c_arg, scalar_arg_factory, vec_arg_factory,
|
|
230
|
+
name_to_dtype=None):
|
|
231
|
+
if isinstance(name_to_dtype, DTypeRegistry):
|
|
232
|
+
name_to_dtype = name_to_dtype.name_to_dtype__getitem__
|
|
233
|
+
elif name_to_dtype is None:
|
|
234
|
+
name_to_dtype = NAME_TO_DTYPE.__getitem__
|
|
235
|
+
|
|
236
|
+
c_arg = (c_arg
|
|
237
|
+
.replace("const", "")
|
|
238
|
+
.replace("volatile", "")
|
|
239
|
+
.replace("__restrict__", "")
|
|
240
|
+
.replace("restrict", ""))
|
|
241
|
+
|
|
242
|
+
# process and remove declarator
|
|
243
|
+
import re
|
|
244
|
+
decl_re = re.compile(r"(\**)\s*([_a-zA-Z0-9]+)(\s*\[[ 0-9]*\])*\s*$")
|
|
245
|
+
decl_match = decl_re.search(c_arg)
|
|
246
|
+
|
|
247
|
+
if decl_match is None:
|
|
248
|
+
raise ValueError("couldn't parse C declarator '%s'" % c_arg)
|
|
249
|
+
|
|
250
|
+
name = decl_match.group(2)
|
|
251
|
+
|
|
252
|
+
if decl_match.group(1) or decl_match.group(3) is not None:
|
|
253
|
+
arg_class = vec_arg_factory
|
|
254
|
+
else:
|
|
255
|
+
arg_class = scalar_arg_factory
|
|
256
|
+
|
|
257
|
+
tp = c_arg[:decl_match.start()]
|
|
258
|
+
tp = " ".join(tp.split())
|
|
259
|
+
|
|
260
|
+
try:
|
|
261
|
+
dtype = name_to_dtype(tp)
|
|
262
|
+
except KeyError:
|
|
263
|
+
raise ValueError("unknown type '%s'" % tp) from None
|
|
264
|
+
|
|
265
|
+
return arg_class(dtype, name)
|
|
266
|
+
|
|
267
|
+
# }}}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def register_dtype(dtype, c_names, alias_ok=False):
|
|
271
|
+
from warnings import warn
|
|
272
|
+
warn("register_dtype is deprecated. Use get_or_register_dtype instead.",
|
|
273
|
+
DeprecationWarning, stacklevel=2)
|
|
274
|
+
|
|
275
|
+
if isinstance(c_names, str):
|
|
276
|
+
c_names = [c_names]
|
|
277
|
+
|
|
278
|
+
dtype = np.dtype(dtype)
|
|
279
|
+
|
|
280
|
+
# check if we've seen this dtype before and error out if a) it was seen before
|
|
281
|
+
# and b) alias_ok is False.
|
|
282
|
+
|
|
283
|
+
if not alias_ok and dtype in TYPE_REGISTRY.dtype_to_name:
|
|
284
|
+
raise RuntimeError("dtype '%s' already registered (as '%s', new names '%s')"
|
|
285
|
+
% (dtype, TYPE_REGISTRY.dtype_to_name[dtype], ", ".join(c_names)))
|
|
286
|
+
|
|
287
|
+
TYPE_REGISTRY.get_or_register_dtype(c_names, dtype)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
# vim: foldmethod=marker
|