pyopencl 2024.2__cp310-cp310-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 (122) hide show
  1. pyopencl/__init__.py +2393 -0
  2. pyopencl/_cl.cpython-310-darwin.so +0 -0
  3. pyopencl/_cluda.py +54 -0
  4. pyopencl/_mymako.py +14 -0
  5. pyopencl/algorithm.py +1444 -0
  6. pyopencl/array.py +3427 -0
  7. pyopencl/bitonic_sort.py +238 -0
  8. pyopencl/bitonic_sort_templates.py +594 -0
  9. pyopencl/cache.py +534 -0
  10. pyopencl/capture_call.py +176 -0
  11. pyopencl/characterize/__init__.py +433 -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 +408 -0
  26. pyopencl/cltypes.py +137 -0
  27. pyopencl/compyte/__init__.py +0 -0
  28. pyopencl/compyte/array.py +214 -0
  29. pyopencl/compyte/dtypes.py +290 -0
  30. pyopencl/compyte/ndarray/__init__.py +0 -0
  31. pyopencl/compyte/ndarray/gen_elemwise.py +1907 -0
  32. pyopencl/compyte/ndarray/gen_reduction.py +1511 -0
  33. pyopencl/compyte/ndarray/setup_opencl.py +101 -0
  34. pyopencl/compyte/ndarray/test_gpu_elemwise.py +411 -0
  35. pyopencl/compyte/ndarray/test_gpu_ndarray.py +487 -0
  36. pyopencl/elementwise.py +1164 -0
  37. pyopencl/invoker.py +418 -0
  38. pyopencl/ipython_ext.py +68 -0
  39. pyopencl/reduction.py +780 -0
  40. pyopencl/scan.py +1898 -0
  41. pyopencl/tools.py +1513 -0
  42. pyopencl/version.py +3 -0
  43. pyopencl-2024.2.data/data/CITATION.cff +74 -0
  44. pyopencl-2024.2.data/data/LICENSE +282 -0
  45. pyopencl-2024.2.data/data/Makefile.in +21 -0
  46. pyopencl-2024.2.data/data/README.rst +70 -0
  47. pyopencl-2024.2.data/data/README_SETUP.txt +34 -0
  48. pyopencl-2024.2.data/data/aksetup_helper.py +1013 -0
  49. pyopencl-2024.2.data/data/configure.py +6 -0
  50. pyopencl-2024.2.data/data/contrib/cldis.py +91 -0
  51. pyopencl-2024.2.data/data/contrib/fortran-to-opencl/README +29 -0
  52. pyopencl-2024.2.data/data/contrib/fortran-to-opencl/translate.py +1441 -0
  53. pyopencl-2024.2.data/data/contrib/pyopencl.vim +84 -0
  54. pyopencl-2024.2.data/data/doc/Makefile +23 -0
  55. pyopencl-2024.2.data/data/doc/algorithm.rst +214 -0
  56. pyopencl-2024.2.data/data/doc/array.rst +305 -0
  57. pyopencl-2024.2.data/data/doc/conf.py +26 -0
  58. pyopencl-2024.2.data/data/doc/howto.rst +105 -0
  59. pyopencl-2024.2.data/data/doc/index.rst +137 -0
  60. pyopencl-2024.2.data/data/doc/make_constants.py +561 -0
  61. pyopencl-2024.2.data/data/doc/misc.rst +885 -0
  62. pyopencl-2024.2.data/data/doc/runtime.rst +51 -0
  63. pyopencl-2024.2.data/data/doc/runtime_const.rst +30 -0
  64. pyopencl-2024.2.data/data/doc/runtime_gl.rst +78 -0
  65. pyopencl-2024.2.data/data/doc/runtime_memory.rst +527 -0
  66. pyopencl-2024.2.data/data/doc/runtime_platform.rst +184 -0
  67. pyopencl-2024.2.data/data/doc/runtime_program.rst +364 -0
  68. pyopencl-2024.2.data/data/doc/runtime_queue.rst +182 -0
  69. pyopencl-2024.2.data/data/doc/subst.rst +36 -0
  70. pyopencl-2024.2.data/data/doc/tools.rst +4 -0
  71. pyopencl-2024.2.data/data/doc/types.rst +42 -0
  72. pyopencl-2024.2.data/data/examples/black-hole-accretion.py +2227 -0
  73. pyopencl-2024.2.data/data/examples/demo-struct-reduce.py +75 -0
  74. pyopencl-2024.2.data/data/examples/demo.py +39 -0
  75. pyopencl-2024.2.data/data/examples/demo_array.py +32 -0
  76. pyopencl-2024.2.data/data/examples/demo_array_svm.py +37 -0
  77. pyopencl-2024.2.data/data/examples/demo_elementwise.py +34 -0
  78. pyopencl-2024.2.data/data/examples/demo_elementwise_complex.py +53 -0
  79. pyopencl-2024.2.data/data/examples/demo_mandelbrot.py +183 -0
  80. pyopencl-2024.2.data/data/examples/demo_meta_codepy.py +56 -0
  81. pyopencl-2024.2.data/data/examples/demo_meta_template.py +55 -0
  82. pyopencl-2024.2.data/data/examples/dump-performance.py +38 -0
  83. pyopencl-2024.2.data/data/examples/dump-properties.py +86 -0
  84. pyopencl-2024.2.data/data/examples/gl_interop_demo.py +84 -0
  85. pyopencl-2024.2.data/data/examples/gl_particle_animation.py +218 -0
  86. pyopencl-2024.2.data/data/examples/ipython-demo.ipynb +203 -0
  87. pyopencl-2024.2.data/data/examples/median-filter.py +99 -0
  88. pyopencl-2024.2.data/data/examples/n-body.py +1070 -0
  89. pyopencl-2024.2.data/data/examples/narray.py +37 -0
  90. pyopencl-2024.2.data/data/examples/noisyImage.jpg +0 -0
  91. pyopencl-2024.2.data/data/examples/pi-monte-carlo.py +1166 -0
  92. pyopencl-2024.2.data/data/examples/svm.py +82 -0
  93. pyopencl-2024.2.data/data/examples/transpose.py +229 -0
  94. pyopencl-2024.2.data/data/pytest.ini +3 -0
  95. pyopencl-2024.2.data/data/src/bitlog.cpp +51 -0
  96. pyopencl-2024.2.data/data/src/bitlog.hpp +83 -0
  97. pyopencl-2024.2.data/data/src/clinfo_ext.h +134 -0
  98. pyopencl-2024.2.data/data/src/mempool.hpp +444 -0
  99. pyopencl-2024.2.data/data/src/pyopencl_ext.h +77 -0
  100. pyopencl-2024.2.data/data/src/tools.hpp +90 -0
  101. pyopencl-2024.2.data/data/src/wrap_cl.cpp +61 -0
  102. pyopencl-2024.2.data/data/src/wrap_cl.hpp +5853 -0
  103. pyopencl-2024.2.data/data/src/wrap_cl_part_1.cpp +369 -0
  104. pyopencl-2024.2.data/data/src/wrap_cl_part_2.cpp +702 -0
  105. pyopencl-2024.2.data/data/src/wrap_constants.cpp +1274 -0
  106. pyopencl-2024.2.data/data/src/wrap_helpers.hpp +213 -0
  107. pyopencl-2024.2.data/data/src/wrap_mempool.cpp +731 -0
  108. pyopencl-2024.2.data/data/test/add-vectors-32.spv +0 -0
  109. pyopencl-2024.2.data/data/test/add-vectors-64.spv +0 -0
  110. pyopencl-2024.2.data/data/test/empty-header.h +1 -0
  111. pyopencl-2024.2.data/data/test/test_algorithm.py +1180 -0
  112. pyopencl-2024.2.data/data/test/test_array.py +2392 -0
  113. pyopencl-2024.2.data/data/test/test_arrays_in_structs.py +100 -0
  114. pyopencl-2024.2.data/data/test/test_clmath.py +529 -0
  115. pyopencl-2024.2.data/data/test/test_clrandom.py +75 -0
  116. pyopencl-2024.2.data/data/test/test_enqueue_copy.py +271 -0
  117. pyopencl-2024.2.data/data/test/test_wrapper.py +1554 -0
  118. pyopencl-2024.2.dist-info/LICENSE +282 -0
  119. pyopencl-2024.2.dist-info/METADATA +105 -0
  120. pyopencl-2024.2.dist-info/RECORD +122 -0
  121. pyopencl-2024.2.dist-info/WHEEL +5 -0
  122. pyopencl-2024.2.dist-info/top_level.txt +1 -0
pyopencl/cache.py ADDED
@@ -0,0 +1,534 @@
1
+ """PyOpenCL compiler cache."""
2
+
3
+
4
+ __copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
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
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+ """
25
+
26
+ import logging
27
+ import os
28
+ import re
29
+ import sys
30
+ from dataclasses import dataclass
31
+ from typing import List, Optional, Tuple
32
+
33
+ import pyopencl._cl as _cl
34
+
35
+
36
+ logger = logging.getLogger(__name__)
37
+
38
+
39
+ import hashlib
40
+
41
+
42
+ new_hash = hashlib.md5
43
+
44
+
45
+ def _erase_dir(dir):
46
+ from os import listdir, rmdir, unlink
47
+ from os.path import join
48
+ for name in listdir(dir):
49
+ unlink(join(dir, name))
50
+ rmdir(dir)
51
+
52
+
53
+ def update_checksum(checksum, obj):
54
+ if isinstance(obj, str):
55
+ checksum.update(obj.encode("utf8"))
56
+ else:
57
+ checksum.update(obj)
58
+
59
+
60
+ # {{{ cleanup
61
+
62
+ class CleanupBase:
63
+ pass
64
+
65
+
66
+ class CleanupManager(CleanupBase):
67
+ def __init__(self):
68
+ self.cleanups = []
69
+
70
+ def register(self, c):
71
+ self.cleanups.insert(0, c)
72
+
73
+ def clean_up(self):
74
+ for c in self.cleanups:
75
+ c.clean_up()
76
+
77
+ def error_clean_up(self):
78
+ for c in self.cleanups:
79
+ c.error_clean_up()
80
+
81
+
82
+ class CacheLockManager(CleanupBase):
83
+ def __init__(self, cleanup_m, cache_dir):
84
+ if cache_dir is not None:
85
+ self.lock_file = os.path.join(cache_dir, "lock")
86
+
87
+ attempts = 0
88
+ while True:
89
+ try:
90
+ self.fd = os.open(self.lock_file,
91
+ os.O_CREAT | os.O_WRONLY | os.O_EXCL)
92
+ break
93
+ except OSError:
94
+ pass
95
+
96
+ # This value was chosen based on the py-filelock package:
97
+ # https://github.com/tox-dev/py-filelock/blob/a6c8fabc4192fa7a4ae19b1875ee842ec5eb4f61/src/filelock/_api.py#L113
98
+ # When running pyopencl in an application with multiple ranks
99
+ # that share a cache_dir, higher timeouts can lead to
100
+ # application stalls even with low numbers of ranks.
101
+ # cf. https://github.com/inducer/pyopencl/pull/504
102
+ wait_time_seconds = 0.05
103
+
104
+ # Warn every 10 seconds if not able to acquire lock
105
+ warn_attempts = int(10/wait_time_seconds)
106
+
107
+ # Exit after 60 seconds if not able to acquire lock
108
+ exit_attempts = int(60/wait_time_seconds)
109
+
110
+ from time import sleep
111
+ sleep(wait_time_seconds)
112
+
113
+ attempts += 1
114
+
115
+ if attempts % warn_attempts == 0:
116
+ from warnings import warn
117
+ warn(
118
+ f"Could not obtain cache lock--delete '{self.lock_file}' "
119
+ "if necessary", stacklevel=2)
120
+
121
+ if attempts > exit_attempts:
122
+ raise RuntimeError("waited more than one minute "
123
+ "on the lock file '%s'"
124
+ "--something is wrong" % self.lock_file)
125
+
126
+ cleanup_m.register(self)
127
+
128
+ def clean_up(self):
129
+ os.close(self.fd)
130
+ os.unlink(self.lock_file)
131
+
132
+ def error_clean_up(self):
133
+ pass
134
+
135
+
136
+ class ModuleCacheDirManager(CleanupBase):
137
+ def __init__(self, cleanup_m, path):
138
+ from os import mkdir
139
+
140
+ self.path = path
141
+ try:
142
+ mkdir(self.path)
143
+ cleanup_m.register(self)
144
+ self.existed = False
145
+ except OSError:
146
+ self.existed = True
147
+
148
+ def sub(self, n):
149
+ from os.path import join
150
+ return join(self.path, n)
151
+
152
+ def reset(self):
153
+ _erase_dir(self.path)
154
+ os.mkdir(self.path)
155
+
156
+ def clean_up(self):
157
+ pass
158
+
159
+ def error_clean_up(self):
160
+ _erase_dir(self.path)
161
+
162
+ # }}}
163
+
164
+
165
+ # {{{ #include dependency handling
166
+
167
+ C_INCLUDE_RE = re.compile(r'^\s*\#\s*include\s+[<"](.+)[">]\s*$',
168
+ re.MULTILINE)
169
+
170
+
171
+ def get_dependencies(src, include_path):
172
+ result = {}
173
+
174
+ from os.path import join, realpath
175
+
176
+ def _inner(src):
177
+ for match in C_INCLUDE_RE.finditer(src):
178
+ included = match.group(1)
179
+
180
+ found = False
181
+ for ipath in include_path:
182
+ included_file_name = realpath(join(ipath, included))
183
+
184
+ if included_file_name not in result:
185
+ try:
186
+ src_file = open(included_file_name)
187
+ except OSError:
188
+ continue
189
+
190
+ try:
191
+ included_src = src_file.read()
192
+ finally:
193
+ src_file.close()
194
+
195
+ # prevent infinite recursion if some header file appears to
196
+ # include itself
197
+ result[included_file_name] = None
198
+
199
+ checksum = new_hash()
200
+ update_checksum(checksum, included_src)
201
+ _inner(included_src)
202
+
203
+ result[included_file_name] = (
204
+ os.stat(included_file_name).st_mtime,
205
+ checksum.hexdigest(),
206
+ )
207
+
208
+ found = True
209
+ break # stop searching the include path
210
+
211
+ if not found:
212
+ pass
213
+
214
+ _inner(src)
215
+
216
+ result = [(name,) + vals for name, vals in result.items()]
217
+ result.sort()
218
+
219
+ return result
220
+
221
+
222
+ def get_file_md5sum(fname):
223
+ checksum = new_hash()
224
+ inf = open(fname)
225
+ try:
226
+ contents = inf.read()
227
+ finally:
228
+ inf.close()
229
+ update_checksum(checksum, contents)
230
+ return checksum.hexdigest()
231
+
232
+
233
+ def check_dependencies(deps):
234
+ for name, date, md5sum in deps:
235
+ try:
236
+ possibly_updated = os.stat(name).st_mtime != date
237
+ except OSError:
238
+ return False
239
+ else:
240
+ if possibly_updated and md5sum != get_file_md5sum(name):
241
+ return False
242
+
243
+ return True
244
+
245
+ # }}}
246
+
247
+
248
+ # {{{ key generation
249
+
250
+ def get_device_cache_id(device):
251
+ from pyopencl.version import VERSION
252
+ platform = device.platform
253
+ return (VERSION,
254
+ platform.vendor, platform.name, platform.version,
255
+ device.vendor, device.name, device.version, device.driver_version)
256
+
257
+
258
+ def get_cache_key(device, options_bytes, src):
259
+ checksum = new_hash()
260
+ update_checksum(checksum, src)
261
+ update_checksum(checksum, options_bytes)
262
+ update_checksum(checksum, str(get_device_cache_id(device)))
263
+ return checksum.hexdigest()
264
+
265
+ # }}}
266
+
267
+
268
+ def retrieve_from_cache(cache_dir, cache_key):
269
+ class _InvalidInfoFile(RuntimeError):
270
+ pass
271
+
272
+ from os.path import isdir, join
273
+ module_cache_dir = join(cache_dir, cache_key)
274
+ if not isdir(module_cache_dir):
275
+ return None
276
+
277
+ cleanup_m = CleanupManager()
278
+ try:
279
+ try:
280
+ CacheLockManager(cleanup_m, cache_dir)
281
+
282
+ mod_cache_dir_m = ModuleCacheDirManager(cleanup_m, module_cache_dir)
283
+ info_path = mod_cache_dir_m.sub("info")
284
+ binary_path = mod_cache_dir_m.sub("binary")
285
+
286
+ # {{{ load info file
287
+
288
+ try:
289
+ from pickle import load
290
+
291
+ try:
292
+ info_file = open(info_path, "rb")
293
+ except OSError:
294
+ raise _InvalidInfoFile()
295
+
296
+ try:
297
+ try:
298
+ info = load(info_file)
299
+ except EOFError:
300
+ raise _InvalidInfoFile()
301
+ finally:
302
+ info_file.close()
303
+
304
+ except _InvalidInfoFile:
305
+ mod_cache_dir_m.reset()
306
+ from warnings import warn
307
+ warn(
308
+ "PyOpenCL encountered an invalid info file for "
309
+ f"cache key '{cache_key}'", stacklevel=2)
310
+ return None
311
+
312
+ # }}}
313
+
314
+ # {{{ load binary
315
+
316
+ binary_file = open(binary_path, "rb")
317
+ try:
318
+ binary = binary_file.read()
319
+ finally:
320
+ binary_file.close()
321
+
322
+ # }}}
323
+
324
+ if check_dependencies(info.dependencies):
325
+ return binary, info.log
326
+ else:
327
+ mod_cache_dir_m.reset()
328
+
329
+ except Exception:
330
+ cleanup_m.error_clean_up()
331
+ raise
332
+ finally:
333
+ cleanup_m.clean_up()
334
+
335
+
336
+ # {{{ top-level driver
337
+
338
+ @dataclass(frozen=True)
339
+ class _SourceInfo:
340
+ dependencies: List[Tuple[str, ...]]
341
+ log: Optional[str]
342
+
343
+
344
+ def _create_built_program_from_source_cached(ctx, src, options_bytes,
345
+ devices, cache_dir, include_path):
346
+ from os.path import join
347
+
348
+ if cache_dir is None:
349
+ import platformdirs
350
+
351
+ # Determine the cache directory in the same way as pytools.PersistentDict,
352
+ # which PyOpenCL uses for invoker caches.
353
+ if sys.platform == "darwin" and os.getenv("XDG_CACHE_HOME") is not None:
354
+ # platformdirs does not handle XDG_CACHE_HOME on macOS
355
+ # https://github.com/platformdirs/platformdirs/issues/269
356
+ cache_dir = join(os.getenv("XDG_CACHE_HOME"), "pyopencl")
357
+ else:
358
+ cache_dir = platformdirs.user_cache_dir("pyopencl", "pyopencl")
359
+
360
+ cache_dir = join(cache_dir,
361
+ "pyopencl-compiler-cache-v2-py{}".format(
362
+ ".".join(str(i) for i in sys.version_info)))
363
+
364
+ os.makedirs(cache_dir, exist_ok=True)
365
+
366
+ if devices is None:
367
+ devices = ctx.devices
368
+
369
+ cache_keys = [get_cache_key(device, options_bytes, src) for device in devices]
370
+
371
+ binaries = []
372
+ to_be_built_indices = []
373
+ logs = []
374
+ for i, (_device, cache_key) in enumerate(zip(devices, cache_keys)):
375
+ cache_result = retrieve_from_cache(cache_dir, cache_key)
376
+
377
+ if cache_result is None:
378
+ logger.debug("build program: binary cache miss (key: %s)" % cache_key)
379
+
380
+ to_be_built_indices.append(i)
381
+ binaries.append(None)
382
+ logs.append(None)
383
+ else:
384
+ logger.debug("build program: binary cache hit (key: %s)" % cache_key)
385
+
386
+ binary, log = cache_result
387
+ binaries.append(binary)
388
+ logs.append(log)
389
+
390
+ message = (75*"="+"\n").join(
391
+ f"Build on {dev} succeeded, but said:\n\n{log}"
392
+ for dev, log in zip(devices, logs)
393
+ if log is not None and log.strip())
394
+
395
+ if message:
396
+ from pyopencl import compiler_output
397
+ compiler_output(
398
+ "Built kernel retrieved from cache. Original from-source "
399
+ "build had warnings:\n"+message)
400
+
401
+ # {{{ build on the build-needing devices, in one go
402
+
403
+ result = None
404
+ already_built = False
405
+ was_cached = not to_be_built_indices
406
+
407
+ if to_be_built_indices:
408
+ # defeat implementation caches:
409
+ from uuid import uuid4
410
+ src = src + "\n\n__constant int pyopencl_defeat_cache_%s = 0;" % (
411
+ uuid4().hex)
412
+
413
+ logger.debug("build program: start building program from source on %s"
414
+ % ", ".join(str(devices[i]) for i in to_be_built_indices))
415
+
416
+ prg = _cl._Program(ctx, src)
417
+ prg.build(options_bytes, [devices[i] for i in to_be_built_indices])
418
+
419
+ logger.debug("build program: from-source build complete")
420
+
421
+ prg_devs = prg.get_info(_cl.program_info.DEVICES)
422
+ prg_bins = prg.get_info(_cl.program_info.BINARIES)
423
+ prg_logs = prg._get_build_logs()
424
+
425
+ for dest_index in to_be_built_indices:
426
+ dev = devices[dest_index]
427
+ src_index = prg_devs.index(dev)
428
+ binaries[dest_index] = prg_bins[src_index]
429
+ _, logs[dest_index] = prg_logs[src_index]
430
+
431
+ if len(to_be_built_indices) == len(devices):
432
+ # Important special case: if code for all devices was built,
433
+ # then we may simply use the program that we just built as the
434
+ # final result.
435
+
436
+ result = prg
437
+ already_built = True
438
+
439
+ if result is None:
440
+ result = _cl._Program(ctx, devices, binaries)
441
+
442
+ # }}}
443
+
444
+ # {{{ save binaries to cache
445
+
446
+ if to_be_built_indices:
447
+ cleanup_m = CleanupManager()
448
+ try:
449
+ try:
450
+ CacheLockManager(cleanup_m, cache_dir)
451
+
452
+ for i in to_be_built_indices:
453
+ cache_key = cache_keys[i]
454
+ binary = binaries[i]
455
+
456
+ mod_cache_dir_m = ModuleCacheDirManager(cleanup_m,
457
+ join(cache_dir, cache_key))
458
+ info_path = mod_cache_dir_m.sub("info")
459
+ binary_path = mod_cache_dir_m.sub("binary")
460
+ source_path = mod_cache_dir_m.sub("source.cl")
461
+
462
+ outf = open(source_path, "wt")
463
+ outf.write(src)
464
+ outf.close()
465
+
466
+ outf = open(binary_path, "wb")
467
+ outf.write(binary)
468
+ outf.close()
469
+
470
+ from pickle import dump
471
+ info_file = open(info_path, "wb")
472
+ dump(_SourceInfo(
473
+ dependencies=get_dependencies(src, include_path),
474
+ log=logs[i]), info_file)
475
+ info_file.close()
476
+
477
+ except Exception:
478
+ cleanup_m.error_clean_up()
479
+ raise
480
+ finally:
481
+ cleanup_m.clean_up()
482
+
483
+ # }}}
484
+
485
+ return result, already_built, was_cached
486
+
487
+
488
+ def create_built_program_from_source_cached(ctx, src, options_bytes, devices=None,
489
+ cache_dir=None, include_path=None):
490
+ try:
491
+ was_cached = False
492
+ already_built = False
493
+ if cache_dir is not False:
494
+ prg, already_built, was_cached = \
495
+ _create_built_program_from_source_cached(
496
+ ctx, src, options_bytes, devices, cache_dir,
497
+ include_path=include_path)
498
+ if was_cached and not already_built:
499
+ prg.build(options_bytes, devices)
500
+ already_built = True
501
+ else:
502
+ prg = _cl._Program(ctx, src)
503
+
504
+ except Exception as e:
505
+ from pyopencl import Error
506
+ build_program_failure = (isinstance(e, Error)
507
+ and e.code == _cl.status_code.BUILD_PROGRAM_FAILURE) # noqa pylint:disable=no-member
508
+
509
+ # Mac error on intel CPU driver: can't build from cached version.
510
+ # If we get a build_program_failure from the cached version then
511
+ # build from source instead, otherwise report the failure.
512
+ if build_program_failure and not was_cached:
513
+ raise
514
+
515
+ if not build_program_failure:
516
+ from traceback import format_exc
517
+ from warnings import warn
518
+ warn(
519
+ "PyOpenCL compiler caching failed with an exception:\n"
520
+ f"[begin exception]\n{format_exc()}[end exception]",
521
+ stacklevel=2)
522
+
523
+ prg = _cl._Program(ctx, src)
524
+ was_cached = False
525
+ already_built = False
526
+
527
+ if not already_built:
528
+ prg.build(options_bytes, devices)
529
+
530
+ return prg, was_cached
531
+
532
+ # }}}
533
+
534
+ # vim: foldmethod=marker
@@ -0,0 +1,176 @@
1
+ __copyright__ = "Copyright (C) 2013 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
+
24
+ import numpy as np
25
+ from pytools.py_codegen import Indentation, PythonCodeGenerator
26
+
27
+ import pyopencl as cl
28
+
29
+
30
+ def capture_kernel_call(kernel, output_file, queue, g_size, l_size, *args, **kwargs):
31
+ try:
32
+ source = kernel._source
33
+ except AttributeError:
34
+ raise RuntimeError("cannot capture call, kernel source not available")
35
+
36
+ if source is None:
37
+ raise RuntimeError("cannot capture call, kernel source not available")
38
+
39
+ cg = PythonCodeGenerator()
40
+
41
+ cg("# generated by pyopencl.capture_call")
42
+ cg("")
43
+ cg("import numpy as np")
44
+ cg("import pyopencl as cl")
45
+ cg("from base64 import b64decode")
46
+ cg("from zlib import decompress")
47
+ cg("mf = cl.mem_flags")
48
+ cg("")
49
+
50
+ cg('CODE = r"""//CL//')
51
+ for line in source.split("\n"):
52
+ cg(line)
53
+ cg('"""')
54
+
55
+ # {{{ invocation
56
+
57
+ arg_data = []
58
+
59
+ cg("")
60
+ cg("")
61
+ cg("def main():")
62
+ with Indentation(cg):
63
+ cg("ctx = cl.create_some_context()")
64
+ cg("queue = cl.CommandQueue(ctx)")
65
+ cg("")
66
+
67
+ kernel_args = []
68
+
69
+ for i, arg in enumerate(args):
70
+ if isinstance(arg, cl.Buffer):
71
+ buf = bytearray(arg.size)
72
+ cl.enqueue_copy(queue, buf, arg)
73
+ arg_data.append(("arg%d_data" % i, buf))
74
+ cg("arg%d = cl.Buffer(ctx, "
75
+ "mf.READ_WRITE | cl.mem_flags.COPY_HOST_PTR,"
76
+ % i)
77
+ cg(" hostbuf=decompress(b64decode(arg%d_data)))"
78
+ % i)
79
+ kernel_args.append("arg%d" % i)
80
+ elif isinstance(arg, (int, float)):
81
+ kernel_args.append(repr(arg))
82
+ elif isinstance(arg, np.integer):
83
+ kernel_args.append("np.{}({})".format(
84
+ arg.dtype.type.__name__, repr(int(arg))))
85
+ elif isinstance(arg, np.floating):
86
+ kernel_args.append("np.{}({})".format(
87
+ arg.dtype.type.__name__, repr(float(arg))))
88
+ elif isinstance(arg, np.complexfloating):
89
+ kernel_args.append("np.{}({})".format(
90
+ arg.dtype.type.__name__, repr(complex(arg))))
91
+ else:
92
+ try:
93
+ arg_buf = memoryview(arg)
94
+ except Exception:
95
+ raise RuntimeError("cannot capture: "
96
+ "unsupported arg nr %d (0-based)" % i)
97
+
98
+ arg_data.append(("arg%d_data" % i, arg_buf))
99
+ kernel_args.append("decompress(b64decode(arg%d_data))" % i)
100
+
101
+ cg("")
102
+
103
+ g_times_l = kwargs.get("g_times_l", False)
104
+ if g_times_l:
105
+ dim = max(len(g_size), len(l_size))
106
+ l_size = l_size + (1,) * (dim-len(l_size))
107
+ g_size = g_size + (1,) * (dim-len(g_size))
108
+ g_size = tuple(
109
+ gs*ls for gs, ls in zip(g_size, l_size))
110
+
111
+ global_offset = kwargs.get("global_offset", None)
112
+ if global_offset is not None:
113
+ kernel_args.append("global_offset=%s" % repr(global_offset))
114
+
115
+ cg("prg = cl.Program(ctx, CODE).build()")
116
+ cg("knl = prg.%s" % kernel.function_name)
117
+ if hasattr(kernel, "_scalar_arg_dtypes"):
118
+ def strify_dtype(d):
119
+ if d is None:
120
+ return "None"
121
+
122
+ d = np.dtype(d)
123
+ s = repr(d)
124
+ if s.startswith("dtype"):
125
+ s = "np."+s
126
+
127
+ return s
128
+
129
+ cg("knl.set_scalar_arg_dtypes((%s,))"
130
+ % ", ".join(
131
+ strify_dtype(dt) for dt in kernel._scalar_arg_dtypes))
132
+
133
+ cg("knl(queue, {}, {},".format(repr(g_size), repr(l_size)))
134
+ cg(" %s)" % ", ".join(kernel_args))
135
+ cg("")
136
+ cg("queue.finish()")
137
+
138
+ # }}}
139
+
140
+ # {{{ data
141
+
142
+ from base64 import b64encode
143
+ from zlib import compress
144
+ cg("")
145
+ line_len = 70
146
+
147
+ for name, val in arg_data:
148
+ cg("%s = (" % name)
149
+ with Indentation(cg):
150
+ val = b64encode(compress(memoryview(val))).decode()
151
+ i = 0
152
+ while i < len(val):
153
+ cg(repr(val[i:i+line_len]))
154
+ i += line_len
155
+
156
+ cg(")")
157
+
158
+ # }}}
159
+
160
+ # {{{ file trailer
161
+
162
+ cg("")
163
+ cg('if __name__ == "__main__":')
164
+ with Indentation(cg):
165
+ cg("main()")
166
+ cg("")
167
+
168
+ cg("# vim: filetype=pyopencl")
169
+
170
+ # }}}
171
+
172
+ if isinstance(output_file, str):
173
+ with open(output_file, "w") as outf:
174
+ outf.write(cg.get())
175
+ else:
176
+ output_file.write(cg.get())