pyopencl 2026.1.1__cp314-cp314t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.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.
- pyopencl/.libs/libOpenCL-34a55fe4.so.1.0.0 +0 -0
- pyopencl/__init__.py +1995 -0
- pyopencl/_cl.cpython-314t-aarch64-linux-gnu.so +0 -0
- pyopencl/_cl.pyi +2009 -0
- pyopencl/_cluda.py +57 -0
- pyopencl/_monkeypatch.py +1104 -0
- pyopencl/_mymako.py +17 -0
- pyopencl/algorithm.py +1454 -0
- pyopencl/array.py +3530 -0
- pyopencl/bitonic_sort.py +245 -0
- pyopencl/bitonic_sort_templates.py +597 -0
- pyopencl/cache.py +553 -0
- pyopencl/capture_call.py +200 -0
- pyopencl/characterize/__init__.py +461 -0
- pyopencl/characterize/performance.py +240 -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 +281 -0
- pyopencl/clrandom.py +412 -0
- pyopencl/cltypes.py +217 -0
- pyopencl/compyte/.gitignore +21 -0
- pyopencl/compyte/__init__.py +0 -0
- pyopencl/compyte/array.py +211 -0
- pyopencl/compyte/dtypes.py +314 -0
- pyopencl/compyte/pyproject.toml +49 -0
- pyopencl/elementwise.py +1288 -0
- pyopencl/invoker.py +417 -0
- pyopencl/ipython_ext.py +70 -0
- pyopencl/py.typed +0 -0
- pyopencl/reduction.py +829 -0
- pyopencl/scan.py +1921 -0
- pyopencl/tools.py +1680 -0
- pyopencl/typing.py +61 -0
- pyopencl/version.py +11 -0
- pyopencl-2026.1.1.dist-info/METADATA +108 -0
- pyopencl-2026.1.1.dist-info/RECORD +47 -0
- pyopencl-2026.1.1.dist-info/WHEEL +6 -0
- pyopencl-2026.1.1.dist-info/licenses/LICENSE +104 -0
pyopencl/cltypes.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
__copyright__ = "Copyright (C) 2016 Jonathan Mackenzie"
|
|
5
|
+
|
|
6
|
+
__license__ = """
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import warnings
|
|
25
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
|
|
29
|
+
from pyopencl.tools import get_or_register_dtype
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if TYPE_CHECKING:
|
|
33
|
+
import builtins
|
|
34
|
+
from collections.abc import MutableSequence
|
|
35
|
+
|
|
36
|
+
if __file__.endswith("array.py"):
|
|
37
|
+
warnings.warn(
|
|
38
|
+
"pyopencl.array.vec is deprecated. Please use pyopencl.cltypes.",
|
|
39
|
+
stacklevel=2)
|
|
40
|
+
|
|
41
|
+
"""
|
|
42
|
+
This file provides a type mapping from OpenCl type names to their numpy equivalents
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
char = np.int8
|
|
46
|
+
uchar = np.uint8
|
|
47
|
+
short = np.int16
|
|
48
|
+
ushort = np.uint16
|
|
49
|
+
int = np.int32
|
|
50
|
+
uint = np.uint32
|
|
51
|
+
long = np.int64
|
|
52
|
+
ulong = np.uint64
|
|
53
|
+
half = np.float16
|
|
54
|
+
float = np.float32
|
|
55
|
+
double = np.float64
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# {{{ vector types
|
|
59
|
+
|
|
60
|
+
def _create_vector_types() -> tuple[
|
|
61
|
+
dict[tuple[np.dtype[Any], builtins.int], np.dtype[Any]],
|
|
62
|
+
dict[np.dtype[Any], tuple[np.dtype[Any], builtins.int]]]:
|
|
63
|
+
mapping = [(k, globals()[k]) for k in
|
|
64
|
+
["char", "uchar", "short", "ushort", "int",
|
|
65
|
+
"uint", "long", "ulong", "float", "double"]]
|
|
66
|
+
|
|
67
|
+
def set_global(key: str, val: np.dtype[Any]) -> None:
|
|
68
|
+
globals()[key] = val
|
|
69
|
+
|
|
70
|
+
vec_types: dict[tuple[np.dtype[Any], builtins.int], np.dtype[Any]] = {}
|
|
71
|
+
vec_type_to_scalar_and_count: dict[np.dtype[Any],
|
|
72
|
+
tuple[np.dtype[Any], builtins.int]] = {}
|
|
73
|
+
|
|
74
|
+
field_names = ["x", "y", "z", "w"]
|
|
75
|
+
|
|
76
|
+
counts = [2, 3, 4, 8, 16]
|
|
77
|
+
|
|
78
|
+
for base_name, base_type in mapping:
|
|
79
|
+
for count in counts:
|
|
80
|
+
name = f"{base_name}{count}"
|
|
81
|
+
titles = cast("MutableSequence[str | None]", field_names[:count])
|
|
82
|
+
|
|
83
|
+
padded_count = count
|
|
84
|
+
if count == 3:
|
|
85
|
+
padded_count = 4
|
|
86
|
+
|
|
87
|
+
names = [f"s{i}" for i in range(count)]
|
|
88
|
+
while len(names) < padded_count:
|
|
89
|
+
pad = len(names) - count
|
|
90
|
+
names.append(f"padding{pad}")
|
|
91
|
+
|
|
92
|
+
if len(titles) < len(names):
|
|
93
|
+
pad = len(names) - len(titles)
|
|
94
|
+
titles.extend([None] * pad)
|
|
95
|
+
|
|
96
|
+
try:
|
|
97
|
+
dtype = np.dtype({
|
|
98
|
+
"names": names,
|
|
99
|
+
"formats": [base_type] * padded_count,
|
|
100
|
+
"titles": titles})
|
|
101
|
+
except NotImplementedError:
|
|
102
|
+
try:
|
|
103
|
+
dtype = np.dtype([((n, title), base_type)
|
|
104
|
+
for (n, title)
|
|
105
|
+
in zip(names, titles, strict=True)])
|
|
106
|
+
except TypeError:
|
|
107
|
+
dtype = np.dtype([(n, base_type) for n in names])
|
|
108
|
+
|
|
109
|
+
assert isinstance(dtype, np.dtype)
|
|
110
|
+
get_or_register_dtype(name, dtype)
|
|
111
|
+
set_global(name, dtype)
|
|
112
|
+
|
|
113
|
+
def create_array(dtype: np.dtype[Any],
|
|
114
|
+
count: int,
|
|
115
|
+
padded_count: int,
|
|
116
|
+
*args: Any, **kwargs: Any) -> dict[str, Any]:
|
|
117
|
+
if len(args) < count:
|
|
118
|
+
from warnings import warn
|
|
119
|
+
warn("default values for make_xxx are deprecated;"
|
|
120
|
+
" instead specify all parameters or use"
|
|
121
|
+
" cltypes.zeros_xxx",
|
|
122
|
+
DeprecationWarning, stacklevel=4)
|
|
123
|
+
|
|
124
|
+
padded_args = tuple(list(args) + [0] * (padded_count - len(args)))
|
|
125
|
+
array = eval("array(padded_args, dtype=dtype)",
|
|
126
|
+
{"array": np.array,
|
|
127
|
+
"padded_args": padded_args,
|
|
128
|
+
"dtype": dtype})
|
|
129
|
+
|
|
130
|
+
for key, val in kwargs.items():
|
|
131
|
+
array[key] = val
|
|
132
|
+
|
|
133
|
+
return array
|
|
134
|
+
|
|
135
|
+
set_global(
|
|
136
|
+
f"make_{name}",
|
|
137
|
+
eval("lambda *args, **kwargs: "
|
|
138
|
+
f"create_array(dtype, {count}, {padded_count}, *args, **kwargs)",
|
|
139
|
+
{"create_array": create_array, "dtype": dtype}))
|
|
140
|
+
set_global(
|
|
141
|
+
f"filled_{name}",
|
|
142
|
+
eval(f"lambda val: make_{name}(*[val]*{count})"))
|
|
143
|
+
set_global(f"zeros_{name}", eval(f"lambda: filled_{name}(0)"))
|
|
144
|
+
set_global(f"ones_{name}", eval(f"lambda: filled_{name}(1)"))
|
|
145
|
+
|
|
146
|
+
base_dtype = np.dtype(base_type)
|
|
147
|
+
vec_types[base_dtype, count] = dtype
|
|
148
|
+
vec_type_to_scalar_and_count[dtype] = base_dtype, count
|
|
149
|
+
|
|
150
|
+
return vec_types, vec_type_to_scalar_and_count
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
vec_types, vec_type_to_scalar_and_count = _create_vector_types()
|
|
154
|
+
|
|
155
|
+
# }}}
|
|
156
|
+
|
|
157
|
+
char2: np.dtype[Any]
|
|
158
|
+
char3: np.dtype[Any]
|
|
159
|
+
char4: np.dtype[Any]
|
|
160
|
+
char8: np.dtype[Any]
|
|
161
|
+
char16: np.dtype[Any]
|
|
162
|
+
|
|
163
|
+
uchar2: np.dtype[Any]
|
|
164
|
+
uchar3: np.dtype[Any]
|
|
165
|
+
uchar4: np.dtype[Any]
|
|
166
|
+
uchar8: np.dtype[Any]
|
|
167
|
+
uchar16: np.dtype[Any]
|
|
168
|
+
|
|
169
|
+
short2: np.dtype[Any]
|
|
170
|
+
short3: np.dtype[Any]
|
|
171
|
+
short4: np.dtype[Any]
|
|
172
|
+
short8: np.dtype[Any]
|
|
173
|
+
short16: np.dtype[Any]
|
|
174
|
+
|
|
175
|
+
ushort2: np.dtype[Any]
|
|
176
|
+
ushort3: np.dtype[Any]
|
|
177
|
+
ushort4: np.dtype[Any]
|
|
178
|
+
ushort8: np.dtype[Any]
|
|
179
|
+
ushort16: np.dtype[Any]
|
|
180
|
+
|
|
181
|
+
int2: np.dtype[Any]
|
|
182
|
+
int3: np.dtype[Any]
|
|
183
|
+
int4: np.dtype[Any]
|
|
184
|
+
int8: np.dtype[Any]
|
|
185
|
+
int16: np.dtype[Any]
|
|
186
|
+
|
|
187
|
+
uint2: np.dtype[Any]
|
|
188
|
+
uint3: np.dtype[Any]
|
|
189
|
+
uint4: np.dtype[Any]
|
|
190
|
+
uint8: np.dtype[Any]
|
|
191
|
+
uint16: np.dtype[Any]
|
|
192
|
+
|
|
193
|
+
long2: np.dtype[Any]
|
|
194
|
+
long3: np.dtype[Any]
|
|
195
|
+
long4: np.dtype[Any]
|
|
196
|
+
long8: np.dtype[Any]
|
|
197
|
+
long16: np.dtype[Any]
|
|
198
|
+
|
|
199
|
+
ulong2: np.dtype[Any]
|
|
200
|
+
ulong3: np.dtype[Any]
|
|
201
|
+
ulong4: np.dtype[Any]
|
|
202
|
+
ulong8: np.dtype[Any]
|
|
203
|
+
ulong16: np.dtype[Any]
|
|
204
|
+
|
|
205
|
+
float2: np.dtype[Any]
|
|
206
|
+
float3: np.dtype[Any]
|
|
207
|
+
float4: np.dtype[Any]
|
|
208
|
+
float8: np.dtype[Any]
|
|
209
|
+
float16: np.dtype[Any]
|
|
210
|
+
|
|
211
|
+
double2: np.dtype[Any]
|
|
212
|
+
double3: np.dtype[Any]
|
|
213
|
+
double4: np.dtype[Any]
|
|
214
|
+
double8: np.dtype[Any]
|
|
215
|
+
double16: np.dtype[Any]
|
|
216
|
+
|
|
217
|
+
# vim: foldmethod=marker
|
|
File without changes
|
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
from typing import Any, Protocol
|
|
24
|
+
|
|
25
|
+
import numpy as np
|
|
26
|
+
from typing_extensions import override
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def f_contiguous_strides(itemsize: int, shape: tuple[int, ...]) -> tuple[int, ...]:
|
|
30
|
+
if shape:
|
|
31
|
+
strides = [itemsize]
|
|
32
|
+
for s in shape[:-1]:
|
|
33
|
+
# NOTE: max(1, s) is used to handle 0-sized axes in `shape`;
|
|
34
|
+
# the stride for `shape[i] <= 1` doesn't matter, but letting it be 0
|
|
35
|
+
# is not a good idea: https://github.com/inducer/arraycontext/pull/91
|
|
36
|
+
strides.append(strides[-1]*max(1, s))
|
|
37
|
+
return tuple(strides)
|
|
38
|
+
else:
|
|
39
|
+
return ()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def c_contiguous_strides(itemsize: int, shape: tuple[int, ...]) -> tuple[int, ...]:
|
|
43
|
+
if shape:
|
|
44
|
+
strides = [itemsize]
|
|
45
|
+
for s in shape[:0:-1]:
|
|
46
|
+
# NOTE: max(1, s) is used to handle 0-sized axes in `shape`;
|
|
47
|
+
# the stride for `shape[i] <= 1` doesn't matter, but letting it be 0
|
|
48
|
+
# is not a good idea: https://github.com/inducer/arraycontext/pull/91
|
|
49
|
+
strides.append(strides[-1]*max(1, s))
|
|
50
|
+
return tuple(strides[::-1])
|
|
51
|
+
else:
|
|
52
|
+
return ()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def equal_strides(
|
|
56
|
+
strides1: tuple[int, ...],
|
|
57
|
+
strides2: tuple[int, ...],
|
|
58
|
+
shape: tuple[int, ...]
|
|
59
|
+
) -> bool:
|
|
60
|
+
if strides1 == strides2:
|
|
61
|
+
return True
|
|
62
|
+
|
|
63
|
+
if len(strides1) != len(strides2) or len(strides2) != len(shape):
|
|
64
|
+
return False
|
|
65
|
+
|
|
66
|
+
for s, st1, st2 in zip(shape, strides1, strides2, strict=True):
|
|
67
|
+
if s != 1 and st1 != st2:
|
|
68
|
+
return False
|
|
69
|
+
|
|
70
|
+
return True
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def is_f_contiguous_strides(
|
|
74
|
+
strides: tuple[int, ...],
|
|
75
|
+
itemsize: int,
|
|
76
|
+
shape: tuple[int, ...]
|
|
77
|
+
) -> bool:
|
|
78
|
+
from pytools import product
|
|
79
|
+
return (
|
|
80
|
+
equal_strides(strides, f_contiguous_strides(itemsize, shape), shape)
|
|
81
|
+
or product(shape) == 0)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def is_c_contiguous_strides(
|
|
85
|
+
strides: tuple[int, ...],
|
|
86
|
+
itemsize: int,
|
|
87
|
+
shape: tuple[int, ...]
|
|
88
|
+
) -> bool:
|
|
89
|
+
from pytools import product
|
|
90
|
+
return (equal_strides(strides, c_contiguous_strides(itemsize, shape), shape)
|
|
91
|
+
or product(shape) == 0)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class ArrayIsh(Protocol):
|
|
95
|
+
shape: tuple[int, ...]
|
|
96
|
+
strides: tuple[int, ...]
|
|
97
|
+
dtype: np.dtype[Any]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class ArrayFlags:
|
|
101
|
+
def __init__(self, ary: ArrayIsh) -> None:
|
|
102
|
+
self.f_contiguous: bool = is_f_contiguous_strides(
|
|
103
|
+
ary.strides, ary.dtype.itemsize, ary.shape)
|
|
104
|
+
self.c_contiguous: bool = is_c_contiguous_strides(
|
|
105
|
+
ary.strides, ary.dtype.itemsize, ary.shape)
|
|
106
|
+
self.forc: bool = self.f_contiguous or self.c_contiguous
|
|
107
|
+
|
|
108
|
+
@override
|
|
109
|
+
def __repr__(self) -> str:
|
|
110
|
+
return (
|
|
111
|
+
f" C_CONTIGUOUS : {self.c_contiguous}\n"
|
|
112
|
+
f" F_CONTIGUOUS : {self.f_contiguous}"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
@override
|
|
116
|
+
def __str__(self) -> str:
|
|
117
|
+
return repr(self)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def get_common_dtype(obj1: ArrayIsh, obj2: ArrayIsh,
|
|
121
|
+
allow_double: bool) -> np.dtype[Any]:
|
|
122
|
+
# Yes, numpy behaves differently depending on whether
|
|
123
|
+
# we're dealing with arrays or scalars.
|
|
124
|
+
|
|
125
|
+
zero1 = np.zeros(1, dtype=obj1.dtype)
|
|
126
|
+
|
|
127
|
+
try:
|
|
128
|
+
zero2 = np.zeros(1, dtype=obj2.dtype)
|
|
129
|
+
except AttributeError:
|
|
130
|
+
zero2 = obj2
|
|
131
|
+
|
|
132
|
+
result = (zero1 + zero2).dtype
|
|
133
|
+
|
|
134
|
+
if not allow_double:
|
|
135
|
+
if result == np.float64:
|
|
136
|
+
result = np.dtype(np.float32)
|
|
137
|
+
elif result == np.complex128:
|
|
138
|
+
result = np.dtype(np.complex64)
|
|
139
|
+
|
|
140
|
+
return result
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
# {{{ as_strided implementation
|
|
144
|
+
|
|
145
|
+
try:
|
|
146
|
+
from numpy.lib.stride_tricks import as_strided as _as_strided
|
|
147
|
+
_test_dtype = np.dtype(
|
|
148
|
+
[("a", np.float64), ("b", np.float64)], align=True)
|
|
149
|
+
_test_result = _as_strided(np.zeros(10, dtype=_test_dtype))
|
|
150
|
+
if _test_result.dtype != _test_dtype:
|
|
151
|
+
raise RuntimeError("numpy's as_strided is broken")
|
|
152
|
+
|
|
153
|
+
as_strided = _as_strided
|
|
154
|
+
except Exception:
|
|
155
|
+
# stolen from numpy to be compatible with older versions of numpy
|
|
156
|
+
class _DummyArray:
|
|
157
|
+
""" Dummy object that just exists to hang __array_interface__ dictionaries
|
|
158
|
+
and possibly keep alive a reference to a base array.
|
|
159
|
+
"""
|
|
160
|
+
def __init__(self, interface, base=None):
|
|
161
|
+
self.__array_interface__ = interface
|
|
162
|
+
self.base = base
|
|
163
|
+
|
|
164
|
+
def as_strided(x, shape=None, strides=None):
|
|
165
|
+
""" Make an ndarray from the given array with the given shape and strides.
|
|
166
|
+
"""
|
|
167
|
+
# work around Numpy bug 1873 (reported by Irwin Zaid)
|
|
168
|
+
# Since this is stolen from numpy, this implementation has the same bug.
|
|
169
|
+
# http://projects.scipy.org/numpy/ticket/1873
|
|
170
|
+
# == https://github.com/numpy/numpy/issues/2466
|
|
171
|
+
|
|
172
|
+
# Do not recreate the array if nothing need to be changed.
|
|
173
|
+
# This fixes a lot of errors on pypy since DummyArray hack does not
|
|
174
|
+
# currently (2014/May/17) on pypy.
|
|
175
|
+
|
|
176
|
+
if ((shape is None or x.shape == shape)
|
|
177
|
+
and (strides is None or x.strides == strides)):
|
|
178
|
+
return x
|
|
179
|
+
if not x.dtype.isbuiltin:
|
|
180
|
+
if shape is None:
|
|
181
|
+
shape = x.shape
|
|
182
|
+
strides = tuple(strides)
|
|
183
|
+
|
|
184
|
+
from pytools import product
|
|
185
|
+
if strides is not None and shape is not None \
|
|
186
|
+
and product(shape) == product(x.shape) \
|
|
187
|
+
and x.flags.forc:
|
|
188
|
+
# Workaround: If we're being asked to do what amounts to a
|
|
189
|
+
# contiguous reshape, at least do that.
|
|
190
|
+
|
|
191
|
+
if strides == f_contiguous_strides(x.dtype.itemsize, shape):
|
|
192
|
+
result = x.reshape(-1).reshape(*shape, order="F")
|
|
193
|
+
assert result.strides == strides
|
|
194
|
+
return result
|
|
195
|
+
elif strides == c_contiguous_strides(x.dtype.itemsize, shape):
|
|
196
|
+
result = x.reshape(-1).reshape(*shape, order="C")
|
|
197
|
+
assert result.strides == strides
|
|
198
|
+
return result
|
|
199
|
+
|
|
200
|
+
raise NotImplementedError(
|
|
201
|
+
"as_strided won't work on non-builtin arrays for now. "
|
|
202
|
+
"See https://github.com/numpy/numpy/issues/2466")
|
|
203
|
+
|
|
204
|
+
interface = dict(x.__array_interface__)
|
|
205
|
+
if shape is not None:
|
|
206
|
+
interface["shape"] = tuple(shape)
|
|
207
|
+
if strides is not None:
|
|
208
|
+
interface["strides"] = tuple(strides)
|
|
209
|
+
return np.asarray(_DummyArray(interface, base=x))
|
|
210
|
+
|
|
211
|
+
# }}}
|