pyopencl 2025.1__cp313-cp313-macosx_10_14_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.

Files changed (42) hide show
  1. pyopencl/__init__.py +2410 -0
  2. pyopencl/_cl.cpython-313-darwin.so +0 -0
  3. pyopencl/_cluda.py +54 -0
  4. pyopencl/_mymako.py +14 -0
  5. pyopencl/algorithm.py +1449 -0
  6. pyopencl/array.py +3362 -0
  7. pyopencl/bitonic_sort.py +242 -0
  8. pyopencl/bitonic_sort_templates.py +594 -0
  9. pyopencl/cache.py +535 -0
  10. pyopencl/capture_call.py +177 -0
  11. pyopencl/characterize/__init__.py +456 -0
  12. pyopencl/characterize/performance.py +237 -0
  13. pyopencl/cl/pyopencl-airy.cl +324 -0
  14. pyopencl/cl/pyopencl-bessel-j-complex.cl +238 -0
  15. pyopencl/cl/pyopencl-bessel-j.cl +1084 -0
  16. pyopencl/cl/pyopencl-bessel-y.cl +435 -0
  17. pyopencl/cl/pyopencl-complex.h +303 -0
  18. pyopencl/cl/pyopencl-eval-tbl.cl +120 -0
  19. pyopencl/cl/pyopencl-hankel-complex.cl +444 -0
  20. pyopencl/cl/pyopencl-random123/array.h +325 -0
  21. pyopencl/cl/pyopencl-random123/openclfeatures.h +93 -0
  22. pyopencl/cl/pyopencl-random123/philox.cl +486 -0
  23. pyopencl/cl/pyopencl-random123/threefry.cl +864 -0
  24. pyopencl/clmath.py +280 -0
  25. pyopencl/clrandom.py +409 -0
  26. pyopencl/cltypes.py +137 -0
  27. pyopencl/compyte/.gitignore +21 -0
  28. pyopencl/compyte/__init__.py +0 -0
  29. pyopencl/compyte/array.py +214 -0
  30. pyopencl/compyte/dtypes.py +290 -0
  31. pyopencl/compyte/pyproject.toml +54 -0
  32. pyopencl/elementwise.py +1171 -0
  33. pyopencl/invoker.py +421 -0
  34. pyopencl/ipython_ext.py +68 -0
  35. pyopencl/reduction.py +786 -0
  36. pyopencl/scan.py +1915 -0
  37. pyopencl/tools.py +1527 -0
  38. pyopencl/version.py +9 -0
  39. pyopencl-2025.1.dist-info/METADATA +108 -0
  40. pyopencl-2025.1.dist-info/RECORD +42 -0
  41. pyopencl-2025.1.dist-info/WHEEL +5 -0
  42. pyopencl-2025.1.dist-info/licenses/LICENSE +282 -0
@@ -0,0 +1,242 @@
1
+ __copyright__ = """
2
+ Copyright (c) 2011, Eric Bainville
3
+ Copyright (c) 2015, Ilya Efimoff
4
+ All rights reserved.
5
+ """
6
+
7
+ # based on code at http://www.bealto.com/gpu-sorting_intro.html
8
+
9
+ __license__ = """
10
+ Redistribution and use in source and binary forms, with or without
11
+ modification, are permitted provided that the following conditions are met:
12
+
13
+ 1. Redistributions of source code must retain the above copyright notice, this
14
+ list of conditions and the following disclaimer.
15
+
16
+ 2. Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+
20
+ 3. Neither the name of the copyright holder nor the names of its contributors
21
+ may be used to endorse or promote products derived from this software without
22
+ specific prior written permission.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+ """
35
+
36
+ from functools import reduce
37
+ from operator import mul
38
+ from typing import ClassVar, Dict
39
+
40
+ from mako.template import Template
41
+
42
+ from pytools import memoize_method
43
+
44
+ import pyopencl as cl
45
+ import pyopencl.bitonic_sort_templates as _tmpl
46
+ from pyopencl.tools import dtype_to_ctype
47
+
48
+
49
+ def _is_power_of_2(n):
50
+ from pyopencl.tools import bitlog2
51
+ return n == 0 or 2**bitlog2(n) == n
52
+
53
+
54
+ class BitonicSort:
55
+ """Sort an array (or one axis of one) using a sorting network.
56
+
57
+ Will only work if the axis of the array to be sorted has a length
58
+ that is a power of 2.
59
+
60
+ .. versionadded:: 2015.2
61
+
62
+ .. seealso:: :class:`pyopencl.algorithm.RadixSort`
63
+
64
+ .. automethod:: __call__
65
+ """
66
+
67
+ kernels_srcs: ClassVar[Dict[str, str]] = {
68
+ "B2": _tmpl.ParallelBitonic_B2,
69
+ "B4": _tmpl.ParallelBitonic_B4,
70
+ "B8": _tmpl.ParallelBitonic_B8,
71
+ "B16": _tmpl.ParallelBitonic_B16,
72
+ "C4": _tmpl.ParallelBitonic_C4,
73
+ "BL": _tmpl.ParallelBitonic_Local,
74
+ "BLO": _tmpl.ParallelBitonic_Local_Optim,
75
+ "PML": _tmpl.ParallelMerge_Local
76
+ }
77
+
78
+ def __init__(self, context):
79
+ self.context = context
80
+
81
+ def __call__(self, arr, idx=None, queue=None, wait_for=None, axis=0):
82
+ """
83
+ :arg arr: the array to be sorted. Will be overwritten with the sorted array.
84
+ :arg idx: an array of indices to be tracked along with the sorting of *arr*
85
+ :arg queue: a :class:`pyopencl.CommandQueue`, defaults to the array's queue
86
+ if None
87
+ :arg wait_for: a list of :class:`pyopencl.Event` instances or None
88
+ :arg axis: the axis of the array by which to sort
89
+
90
+ :returns: a tuple (sorted_array, event)
91
+ """
92
+
93
+ if queue is None:
94
+ queue = arr.queue
95
+
96
+ if wait_for is None:
97
+ wait_for = []
98
+ wait_for = wait_for + arr.events
99
+
100
+ last_evt = cl.enqueue_marker(queue, wait_for=wait_for)
101
+
102
+ if arr.shape[axis] == 0:
103
+ return arr, last_evt
104
+
105
+ if not _is_power_of_2(arr.shape[axis]):
106
+ raise ValueError("sorted array axis length must be a power of 2")
107
+
108
+ if idx is None:
109
+ argsort = 0
110
+ else:
111
+ argsort = 1
112
+
113
+ run_queue = self.sort_b_prepare_wl(
114
+ argsort,
115
+ arr.dtype,
116
+ idx.dtype if idx is not None else None, arr.shape,
117
+ axis)
118
+
119
+ knl, nt, wg, aux = run_queue[0]
120
+
121
+ if idx is not None:
122
+ if aux:
123
+ last_evt = knl(
124
+ queue, (nt,), wg, arr.data, idx.data,
125
+ cl.LocalMemory(
126
+ _tmpl.LOCAL_MEM_FACTOR*wg[0]*arr.dtype.itemsize),
127
+ cl.LocalMemory(
128
+ _tmpl.LOCAL_MEM_FACTOR*wg[0]*idx.dtype.itemsize),
129
+ wait_for=[last_evt])
130
+ for knl, nt, wg, _ in run_queue[1:]:
131
+ last_evt = knl(
132
+ queue, (nt,), wg, arr.data, idx.data,
133
+ wait_for=[last_evt])
134
+
135
+ else:
136
+ if aux:
137
+ last_evt = knl(
138
+ queue, (nt,), wg, arr.data,
139
+ cl.LocalMemory(
140
+ _tmpl.LOCAL_MEM_FACTOR*wg[0]*4*arr.dtype.itemsize),
141
+ wait_for=[last_evt])
142
+ for knl, nt, wg, _ in run_queue[1:]:
143
+ last_evt = knl(queue, (nt,), wg, arr.data, wait_for=[last_evt])
144
+
145
+ return arr, last_evt
146
+
147
+ @memoize_method
148
+ def get_program(self, letter, argsort, params):
149
+ defstpl = Template(_tmpl.defines)
150
+
151
+ defs = defstpl.render(
152
+ NS="\\", argsort=argsort, inc=params[0], dir=params[1],
153
+ dtype=params[2], idxtype=params[3],
154
+ dsize=params[4], nsize=params[5])
155
+
156
+ kid = Template(self.kernels_srcs[letter]).render(argsort=argsort)
157
+
158
+ prg = cl.Program(self.context, defs + kid).build()
159
+ return prg
160
+
161
+ @memoize_method
162
+ def sort_b_prepare_wl(self, argsort, key_dtype, idx_dtype, shape, axis):
163
+ key_ctype = dtype_to_ctype(key_dtype)
164
+
165
+ if idx_dtype is None:
166
+ idx_ctype = "uint" # Dummy
167
+
168
+ else:
169
+ idx_ctype = dtype_to_ctype(idx_dtype)
170
+
171
+ run_queue = []
172
+ ds = int(shape[axis])
173
+ size = reduce(mul, shape)
174
+ ndim = len(shape)
175
+
176
+ ns = reduce(mul, shape[(axis+1):]) if axis < ndim-1 else 1
177
+
178
+ ds = int(shape[axis])
179
+ allowb4 = True
180
+ allowb8 = True
181
+ allowb16 = True
182
+
183
+ dev = self.context.devices[0]
184
+
185
+ # {{{ find workgroup size
186
+
187
+ wg = min(ds, dev.max_work_group_size)
188
+
189
+ available_lmem = dev.local_mem_size
190
+ while True:
191
+ lmem_size = _tmpl.LOCAL_MEM_FACTOR*wg*key_dtype.itemsize
192
+ if argsort:
193
+ lmem_size += _tmpl.LOCAL_MEM_FACTOR*wg*idx_dtype.itemsize
194
+
195
+ if lmem_size + 512 > available_lmem:
196
+ wg //= 2
197
+
198
+ if not wg:
199
+ raise RuntimeError(
200
+ "too little local memory available on '%s'"
201
+ % dev)
202
+
203
+ else:
204
+ break
205
+
206
+ # }}}
207
+
208
+ length = wg >> 1
209
+ prg = self.get_program(
210
+ "BLO", argsort, (1, 1, key_ctype, idx_ctype, ds, ns))
211
+ run_queue.append((prg.run, size, (wg,), True))
212
+
213
+ while length < ds:
214
+ inc = length
215
+ while inc > 0:
216
+ ninc = 0
217
+ direction = length << 1
218
+ if allowb16 and inc >= 8 and ninc == 0:
219
+ letter = "B16"
220
+ ninc = 4
221
+ elif allowb8 and inc >= 4 and ninc == 0:
222
+ letter = "B8"
223
+ ninc = 3
224
+ elif allowb4 and inc >= 2 and ninc == 0:
225
+ letter = "B4"
226
+ ninc = 2
227
+ elif inc >= 0:
228
+ letter = "B2"
229
+ ninc = 1
230
+ else:
231
+ raise AssertionError("Should not happen")
232
+
233
+ nthreads = size >> ninc
234
+
235
+ prg = self.get_program(letter, argsort,
236
+ (inc, direction, key_ctype, idx_ctype, ds, ns))
237
+ run_queue.append((prg.run, nthreads, None, False,))
238
+ inc >>= ninc
239
+
240
+ length <<= 1
241
+
242
+ return run_queue