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.
Files changed (47) hide show
  1. pyopencl/.libs/libOpenCL-34a55fe4.so.1.0.0 +0 -0
  2. pyopencl/__init__.py +1995 -0
  3. pyopencl/_cl.cpython-314t-aarch64-linux-gnu.so +0 -0
  4. pyopencl/_cl.pyi +2009 -0
  5. pyopencl/_cluda.py +57 -0
  6. pyopencl/_monkeypatch.py +1104 -0
  7. pyopencl/_mymako.py +17 -0
  8. pyopencl/algorithm.py +1454 -0
  9. pyopencl/array.py +3530 -0
  10. pyopencl/bitonic_sort.py +245 -0
  11. pyopencl/bitonic_sort_templates.py +597 -0
  12. pyopencl/cache.py +553 -0
  13. pyopencl/capture_call.py +200 -0
  14. pyopencl/characterize/__init__.py +461 -0
  15. pyopencl/characterize/performance.py +240 -0
  16. pyopencl/cl/pyopencl-airy.cl +324 -0
  17. pyopencl/cl/pyopencl-bessel-j-complex.cl +238 -0
  18. pyopencl/cl/pyopencl-bessel-j.cl +1084 -0
  19. pyopencl/cl/pyopencl-bessel-y.cl +435 -0
  20. pyopencl/cl/pyopencl-complex.h +303 -0
  21. pyopencl/cl/pyopencl-eval-tbl.cl +120 -0
  22. pyopencl/cl/pyopencl-hankel-complex.cl +444 -0
  23. pyopencl/cl/pyopencl-random123/array.h +325 -0
  24. pyopencl/cl/pyopencl-random123/openclfeatures.h +93 -0
  25. pyopencl/cl/pyopencl-random123/philox.cl +486 -0
  26. pyopencl/cl/pyopencl-random123/threefry.cl +864 -0
  27. pyopencl/clmath.py +281 -0
  28. pyopencl/clrandom.py +412 -0
  29. pyopencl/cltypes.py +217 -0
  30. pyopencl/compyte/.gitignore +21 -0
  31. pyopencl/compyte/__init__.py +0 -0
  32. pyopencl/compyte/array.py +211 -0
  33. pyopencl/compyte/dtypes.py +314 -0
  34. pyopencl/compyte/pyproject.toml +49 -0
  35. pyopencl/elementwise.py +1288 -0
  36. pyopencl/invoker.py +417 -0
  37. pyopencl/ipython_ext.py +70 -0
  38. pyopencl/py.typed +0 -0
  39. pyopencl/reduction.py +829 -0
  40. pyopencl/scan.py +1921 -0
  41. pyopencl/tools.py +1680 -0
  42. pyopencl/typing.py +61 -0
  43. pyopencl/version.py +11 -0
  44. pyopencl-2026.1.1.dist-info/METADATA +108 -0
  45. pyopencl-2026.1.1.dist-info/RECORD +47 -0
  46. pyopencl-2026.1.1.dist-info/WHEEL +6 -0
  47. pyopencl-2026.1.1.dist-info/licenses/LICENSE +104 -0
pyopencl/cache.py ADDED
@@ -0,0 +1,553 @@
1
+ """PyOpenCL compiler cache."""
2
+ from __future__ import annotations
3
+
4
+
5
+ __copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
6
+
7
+ __license__ = """
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+ """
26
+
27
+ import hashlib
28
+ import logging
29
+ import os
30
+ import re
31
+ import sys
32
+ from dataclasses import dataclass
33
+ from typing import TYPE_CHECKING, Literal
34
+
35
+ import pyopencl._cl as _cl
36
+
37
+
38
+ logger = logging.getLogger(__name__)
39
+
40
+
41
+ if TYPE_CHECKING:
42
+ from collections.abc import Sequence
43
+
44
+
45
+ new_hash = hashlib.md5
46
+
47
+
48
+ def _erase_dir(directory: str):
49
+ from os import listdir, rmdir, unlink
50
+ from os.path import join
51
+
52
+ for name in listdir(directory):
53
+ unlink(join(directory, name))
54
+
55
+ rmdir(directory)
56
+
57
+
58
+ def update_checksum(checksum, obj):
59
+ if isinstance(obj, str):
60
+ checksum.update(obj.encode("utf8"))
61
+ else:
62
+ checksum.update(obj)
63
+
64
+
65
+ # {{{ cleanup
66
+
67
+ class CleanupBase:
68
+ pass
69
+
70
+
71
+ class CleanupManager(CleanupBase):
72
+ def __init__(self):
73
+ self.cleanups = []
74
+
75
+ def register(self, c):
76
+ self.cleanups.insert(0, c)
77
+
78
+ def clean_up(self):
79
+ for c in self.cleanups:
80
+ c.clean_up()
81
+
82
+ def error_clean_up(self):
83
+ for c in self.cleanups:
84
+ c.error_clean_up()
85
+
86
+
87
+ class CacheLockManager(CleanupBase):
88
+ def __init__(self, cleanup_m, cache_dir):
89
+ if cache_dir is not None:
90
+ self.lock_file = os.path.join(cache_dir, "lock")
91
+
92
+ attempts = 0
93
+ while True:
94
+ try:
95
+ self.fd = os.open(self.lock_file,
96
+ os.O_CREAT | os.O_WRONLY | os.O_EXCL)
97
+ break
98
+ except OSError:
99
+ pass
100
+
101
+ # This value was chosen based on the py-filelock package:
102
+ # https://github.com/tox-dev/py-filelock/blob/a6c8fabc4192fa7a4ae19b1875ee842ec5eb4f61/src/filelock/_api.py#L113
103
+ # When running pyopencl in an application with multiple ranks
104
+ # that share a cache_dir, higher timeouts can lead to
105
+ # application stalls even with low numbers of ranks.
106
+ # cf. https://github.com/inducer/pyopencl/pull/504
107
+ wait_time_seconds = 0.05
108
+
109
+ # Warn every 10 seconds if not able to acquire lock
110
+ warn_attempts = int(10/wait_time_seconds)
111
+
112
+ # Exit after 60 seconds if not able to acquire lock
113
+ exit_attempts = int(60/wait_time_seconds)
114
+
115
+ from time import sleep
116
+ sleep(wait_time_seconds)
117
+
118
+ attempts += 1
119
+
120
+ if attempts % warn_attempts == 0:
121
+ from warnings import warn
122
+ warn(
123
+ f"Could not obtain cache lock--delete '{self.lock_file}' "
124
+ "if necessary", stacklevel=2)
125
+
126
+ if attempts > exit_attempts:
127
+ raise RuntimeError("waited more than one minute "
128
+ "on the lock file '%s'"
129
+ "--something is wrong" % self.lock_file)
130
+
131
+ cleanup_m.register(self)
132
+
133
+ def clean_up(self):
134
+ os.close(self.fd)
135
+ os.unlink(self.lock_file)
136
+
137
+ def error_clean_up(self):
138
+ pass
139
+
140
+
141
+ class ModuleCacheDirManager(CleanupBase):
142
+ def __init__(self, cleanup_m, path):
143
+ from os import mkdir
144
+
145
+ self.path = path
146
+ try:
147
+ mkdir(self.path)
148
+ cleanup_m.register(self)
149
+ self.existed = False
150
+ except OSError:
151
+ self.existed = True
152
+
153
+ def sub(self, n):
154
+ from os.path import join
155
+ return join(self.path, n)
156
+
157
+ def reset(self):
158
+ _erase_dir(self.path)
159
+ os.mkdir(self.path)
160
+
161
+ def clean_up(self):
162
+ pass
163
+
164
+ def error_clean_up(self):
165
+ _erase_dir(self.path)
166
+
167
+ # }}}
168
+
169
+
170
+ # {{{ #include dependency handling
171
+
172
+ C_INCLUDE_RE = re.compile(r'^\s*\#\s*include\s+[<"](.+)[">]\s*$',
173
+ re.MULTILINE)
174
+
175
+
176
+ def get_dependencies(src, include_path):
177
+ result = {}
178
+
179
+ from os.path import join, realpath
180
+
181
+ def _inner(src):
182
+ for match in C_INCLUDE_RE.finditer(src):
183
+ included = match.group(1)
184
+
185
+ found = False
186
+ for ipath in include_path:
187
+ included_file_name = realpath(join(ipath, included))
188
+
189
+ if included_file_name not in result:
190
+ try:
191
+ src_file = open(included_file_name)
192
+ except OSError:
193
+ continue
194
+
195
+ try:
196
+ included_src = src_file.read()
197
+ finally:
198
+ src_file.close()
199
+
200
+ # prevent infinite recursion if some header file appears to
201
+ # include itself
202
+ result[included_file_name] = None
203
+
204
+ checksum = new_hash()
205
+ update_checksum(checksum, included_src)
206
+ _inner(included_src)
207
+
208
+ result[included_file_name] = (
209
+ os.stat(included_file_name).st_mtime,
210
+ checksum.hexdigest(),
211
+ )
212
+
213
+ found = True
214
+ break # stop searching the include path
215
+
216
+ if not found:
217
+ pass
218
+
219
+ _inner(src)
220
+
221
+ result = [(name, *vals) for name, vals in result.items()]
222
+ result.sort()
223
+
224
+ return result
225
+
226
+
227
+ def get_file_md5sum(fname):
228
+ checksum = new_hash()
229
+ inf = open(fname)
230
+ try:
231
+ contents = inf.read()
232
+ finally:
233
+ inf.close()
234
+ update_checksum(checksum, contents)
235
+ return checksum.hexdigest()
236
+
237
+
238
+ def check_dependencies(deps):
239
+ for name, date, md5sum in deps:
240
+ try:
241
+ possibly_updated = os.stat(name).st_mtime != date
242
+ except OSError:
243
+ return False
244
+ else:
245
+ if possibly_updated and md5sum != get_file_md5sum(name):
246
+ return False
247
+
248
+ return True
249
+
250
+ # }}}
251
+
252
+
253
+ # {{{ key generation
254
+
255
+ def get_device_cache_id(device):
256
+ from pyopencl.version import VERSION
257
+ platform = device.platform
258
+ return (VERSION,
259
+ platform.vendor, platform.name, platform.version,
260
+ device.vendor, device.name, device.version, device.driver_version)
261
+
262
+
263
+ def get_cache_key(device, options_bytes, src):
264
+ checksum = new_hash()
265
+ update_checksum(checksum, src)
266
+ update_checksum(checksum, options_bytes)
267
+ update_checksum(checksum, str(get_device_cache_id(device)))
268
+ return checksum.hexdigest()
269
+
270
+ # }}}
271
+
272
+
273
+ def retrieve_from_cache(cache_dir, cache_key):
274
+ class _InvalidInfoFileError(RuntimeError):
275
+ pass
276
+
277
+ from os.path import isdir, join
278
+ module_cache_dir = join(cache_dir, cache_key)
279
+ if not isdir(module_cache_dir):
280
+ return None
281
+
282
+ cleanup_m = CleanupManager()
283
+ try:
284
+ try:
285
+ CacheLockManager(cleanup_m, cache_dir)
286
+
287
+ mod_cache_dir_m = ModuleCacheDirManager(cleanup_m, module_cache_dir)
288
+ info_path = mod_cache_dir_m.sub("info")
289
+ binary_path = mod_cache_dir_m.sub("binary")
290
+
291
+ # {{{ load info file
292
+
293
+ try:
294
+ from pickle import load
295
+
296
+ try:
297
+ info_file = open(info_path, "rb")
298
+ except OSError as err:
299
+ raise _InvalidInfoFileError() from err
300
+
301
+ try:
302
+ try:
303
+ info = load(info_file)
304
+ except EOFError as err:
305
+ raise _InvalidInfoFileError() from err
306
+ finally:
307
+ info_file.close()
308
+
309
+ except _InvalidInfoFileError:
310
+ mod_cache_dir_m.reset()
311
+ from warnings import warn
312
+ warn(
313
+ "PyOpenCL encountered an invalid info file for "
314
+ f"cache key '{cache_key}'", stacklevel=2)
315
+ return None
316
+
317
+ # }}}
318
+
319
+ # {{{ load binary
320
+
321
+ binary_file = open(binary_path, "rb")
322
+ try:
323
+ binary = binary_file.read()
324
+ finally:
325
+ binary_file.close()
326
+
327
+ # }}}
328
+
329
+ if check_dependencies(info.dependencies):
330
+ return binary, info.log
331
+ else:
332
+ mod_cache_dir_m.reset()
333
+
334
+ except Exception:
335
+ cleanup_m.error_clean_up()
336
+ raise
337
+ finally:
338
+ cleanup_m.clean_up()
339
+
340
+
341
+ # {{{ top-level driver
342
+
343
+ @dataclass(frozen=True)
344
+ class _SourceInfo:
345
+ dependencies: list[tuple[str, ...]]
346
+ log: str | None
347
+
348
+
349
+ def _create_built_program_from_source_cached(
350
+ ctx: _cl.Context,
351
+ src: str | bytes,
352
+ options_bytes: bytes,
353
+ devices: Sequence[_cl.Device] | None,
354
+ cache_dir: str | None,
355
+ include_path: Sequence[str] | None):
356
+ from os.path import join
357
+
358
+ if cache_dir is None:
359
+ import platformdirs
360
+
361
+ # Determine the cache directory in the same way as pytools.PersistentDict,
362
+ # which PyOpenCL uses for invoker caches.
363
+ xdg_cache_home = os.getenv("XDG_CACHE_HOME")
364
+ if sys.platform == "darwin" and xdg_cache_home is not None:
365
+ # platformdirs does not handle XDG_CACHE_HOME on macOS
366
+ # https://github.com/platformdirs/platformdirs/issues/269
367
+ cache_dir = join(xdg_cache_home, "pyopencl")
368
+ else:
369
+ cache_dir = platformdirs.user_cache_dir("pyopencl", "pyopencl")
370
+
371
+ cache_dir = join(cache_dir,
372
+ "pyopencl-compiler-cache-v2-py{}".format(
373
+ ".".join(str(i) for i in sys.version_info)))
374
+
375
+ os.makedirs(cache_dir, exist_ok=True)
376
+
377
+ if devices is None:
378
+ devices = ctx.devices
379
+
380
+ cache_keys = [get_cache_key(device, options_bytes, src) for device in devices]
381
+
382
+ binaries = []
383
+ to_be_built_indices: list[int] = []
384
+ logs = []
385
+ for i, (_device, cache_key) in enumerate(zip(devices, cache_keys, strict=True)):
386
+ cache_result = retrieve_from_cache(cache_dir, cache_key)
387
+
388
+ if cache_result is None:
389
+ logger.debug("build program: binary cache miss (key: %s)", cache_key)
390
+
391
+ to_be_built_indices.append(i)
392
+ binaries.append(None)
393
+ logs.append(None)
394
+ else:
395
+ logger.debug("build program: binary cache hit (key: %s)", cache_key)
396
+
397
+ binary, log = cache_result
398
+ binaries.append(binary)
399
+ logs.append(log)
400
+
401
+ message = (75*"="+"\n").join(
402
+ f"Build on {dev} succeeded, but said:\n\n{log}"
403
+ for dev, log in zip(devices, logs, strict=True)
404
+ if log is not None and log.strip())
405
+
406
+ if message:
407
+ from pyopencl import compiler_output
408
+ compiler_output(
409
+ "Built kernel retrieved from cache. Original from-source "
410
+ "build had warnings:\n"+message)
411
+
412
+ # {{{ build on the build-needing devices, in one go
413
+
414
+ result = None
415
+ already_built = False
416
+ was_cached = not to_be_built_indices
417
+
418
+ if isinstance(src, str):
419
+ src = src.encode()
420
+
421
+ if to_be_built_indices:
422
+ # defeat implementation caches:
423
+ from uuid import uuid4
424
+ src = src + b"\n\n__constant int pyopencl_defeat_cache_%s = 0;" % (
425
+ uuid4().hex)
426
+
427
+ logger.debug(
428
+ "build program: start building program from source on %s",
429
+ ", ".join(str(devices[i]) for i in to_be_built_indices))
430
+
431
+ prg = _cl._Program(ctx, src)
432
+ prg.build(options_bytes, [devices[i] for i in to_be_built_indices])
433
+
434
+ logger.debug("build program: from-source build complete")
435
+
436
+ prg_devs = prg.get_info(_cl.program_info.DEVICES)
437
+ prg_bins = prg.get_info(_cl.program_info.BINARIES)
438
+ prg_logs = prg._get_build_logs()
439
+
440
+ for dest_index in to_be_built_indices:
441
+ dev = devices[dest_index]
442
+ src_index = prg_devs.index(dev)
443
+ binaries[dest_index] = prg_bins[src_index]
444
+ _, logs[dest_index] = prg_logs[src_index]
445
+
446
+ if len(to_be_built_indices) == len(devices):
447
+ # Important special case: if code for all devices was built,
448
+ # then we may simply use the program that we just built as the
449
+ # final result.
450
+
451
+ result = prg
452
+ already_built = True
453
+
454
+ if result is None:
455
+ result = _cl._Program(ctx, devices, binaries)
456
+
457
+ # }}}
458
+
459
+ # {{{ save binaries to cache
460
+
461
+ if to_be_built_indices:
462
+ cleanup_m = CleanupManager()
463
+ try:
464
+ try:
465
+ CacheLockManager(cleanup_m, cache_dir)
466
+
467
+ for i in to_be_built_indices:
468
+ cache_key = cache_keys[i]
469
+ binary = binaries[i]
470
+
471
+ mod_cache_dir_m = ModuleCacheDirManager(cleanup_m,
472
+ join(cache_dir, cache_key))
473
+ info_path = mod_cache_dir_m.sub("info")
474
+ binary_path = mod_cache_dir_m.sub("binary")
475
+ source_path = mod_cache_dir_m.sub("source.cl")
476
+
477
+ with open(source_path, "wb") as outf:
478
+ outf.write(src)
479
+
480
+ with open(binary_path, "wb") as outf:
481
+ outf.write(binary)
482
+
483
+ from pickle import dump
484
+ info_file = open(info_path, "wb")
485
+ dump(_SourceInfo(
486
+ dependencies=get_dependencies(src, include_path),
487
+ log=logs[i]), info_file)
488
+ info_file.close()
489
+
490
+ except Exception:
491
+ cleanup_m.error_clean_up()
492
+ raise
493
+ finally:
494
+ cleanup_m.clean_up()
495
+
496
+ # }}}
497
+
498
+ return result, already_built, was_cached
499
+
500
+
501
+ def create_built_program_from_source_cached(
502
+ ctx: _cl.Context,
503
+ src: str | bytes,
504
+ options_bytes: bytes,
505
+ devices: Sequence[_cl.Device] | None = None,
506
+ cache_dir: str | Literal[False] | None = None,
507
+ include_path: Sequence[str] | None = None
508
+ ):
509
+ try:
510
+ was_cached = False
511
+ already_built = False
512
+ if cache_dir is not False:
513
+ prg, already_built, was_cached = \
514
+ _create_built_program_from_source_cached(
515
+ ctx, src, options_bytes, devices, cache_dir,
516
+ include_path=include_path)
517
+ if was_cached and not already_built:
518
+ prg.build(options_bytes, devices)
519
+ already_built = True
520
+ else:
521
+ prg = _cl._Program(ctx, src)
522
+
523
+ except Exception as e:
524
+ from pyopencl import Error
525
+ build_program_failure = (isinstance(e, Error)
526
+ and e.code == _cl.status_code.BUILD_PROGRAM_FAILURE)
527
+
528
+ # Mac error on intel CPU driver: can't build from cached version.
529
+ # If we get a build_program_failure from the cached version then
530
+ # build from source instead, otherwise report the failure.
531
+ if build_program_failure and not was_cached:
532
+ raise
533
+
534
+ if not build_program_failure:
535
+ from traceback import format_exc
536
+ from warnings import warn
537
+ warn(
538
+ "PyOpenCL compiler caching failed with an exception:\n"
539
+ f"[begin exception]\n{format_exc()}[end exception]",
540
+ stacklevel=2)
541
+
542
+ prg = _cl._Program(ctx, src)
543
+ was_cached = False
544
+ already_built = False
545
+
546
+ if not already_built:
547
+ prg.build(options_bytes, devices)
548
+
549
+ return prg, was_cached
550
+
551
+ # }}}
552
+
553
+ # vim: foldmethod=marker