1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
.. include:: subst.rst
OpenCL Runtime: Programs and Kernels
====================================
.. currentmodule:: pyopencl
Program
-------
.. envvar:: PYOPENCL_NO_CACHE
By default, PyOpenCL will use cached (on disk) "binaries" returned by the
OpenCL runtime when calling :meth:`Program.build` on a program
constructed with source. (It will depend on the ICD in use how much
compilation work is saved by this.)
By setting the environment variable :envvar:`PYOPENCL_NO_CACHE` to any
string that :func:`pytools.strtobool` evaluates as ``True``, this caching
is suppressed. No additional in-memory caching is performed. To retain the
compiled version of a kernel in memory, simply retain the :class:`Program`
and/or :class:`Kernel` objects.
PyOpenCL will also cache "invokers", which are short snippets of Python
that are generated to accelerate passing arguments to and enqueuing
a kernel.
.. versionadded:: 2013.1
.. envvar:: PYOPENCL_COMPILER_OUTPUT
When setting the environment variable :envvar:`PYOPENCL_COMPILER_OUTPUT`
to any string that :func:`pytools.strtobool` evaluates as ``True``,
PyOpenCL will show compiler messages emitted during program build.
.. envvar:: PYOPENCL_BUILD_OPTIONS
Any options found in the environment variable
:envvar:`PYOPENCL_BUILD_OPTIONS` will be appended to *options*
in :meth:`Program.build`.
.. versionadded:: 2013.1
.. class:: Program(context, src)
Program(context, devices, binaries)
*binaries* must contain one binary for each entry in *devices*.
If *src* is a :class:`bytes` object starting with a valid `SPIR-V
<https://www.khronos.org/spir>`__ magic number, it will be handed
off to the OpenCL implementation as such, rather than as OpenCL C source
code. (SPIR-V support requires OpenCL 2.1.)
.. versionchanged:: 2016.2
Add support for SPIR-V.
.. attribute:: info
Lower case versions of the :class:`program_info` constants
may be used as attributes on instances of this class
to directly query info attributes.
.. method:: get_info(param)
See :class:`program_info` for values of *param*.
.. method:: get_build_info(device, param)
See :class:`program_build_info` for values of *param*.
.. method:: build(options=[], devices=None, cache_dir=None)
*options* is a string of compiler flags.
Returns *self*.
If *cache_dir* is not None - built binaries are cached in an on-disk cache
with given path.
If passed *cache_dir* is None, but context of this program was created with
not-None cache_dir - it will be used as cache directory.
If passed *cache_dir* is None and context was created with None cache_dir:
built binaries will be cached in an on-disk cache called
:file:`pyopencl-compiler-cache-vN-uidNAME-pyVERSION` in the directory
returned by :func:`tempfile.gettempdir`.
See also :envvar:`PYOPENCL_NO_CACHE`, :envvar:`PYOPENCL_BUILD_OPTIONS`.
.. versionchanged:: 2011.1
*options* may now also be a :class:`list` of :class:`str`.
.. method:: compile(self, options=[], devices=None, headers=[])
:param headers: a list of tuples *(name, program)*.
Only available with CL 1.2.
.. versionadded:: 2011.2
.. attribute:: kernel_name
You may use ``program.kernel_name`` to obtain a :class:`Kernel`
object from a program. Note that every lookup of this type
produces a new kernel object, so that this **won't** work::
prg.sum.set_args(a_g, b_g, res_g)
ev = cl.enqueue_nd_range_kernel(queue, prg.sum, a_np.shape, None)
Instead, either use the (recommended, stateless) calling interface::
sum_knl = prg.sum
sum_knl(queue, a_np.shape, None, a_g, b_g, res_g)
or the long, stateful way around, if you prefer::
sum_knl.set_args(a_g, b_g, res_g)
ev = cl.enqueue_nd_range_kernel(queue, sum_knl, a_np.shape, None)
The following will also work, however note that a number of caches that
are important for efficient kernel enqueue are attached to the :class:`Kernel`
object, and these caches will be ineffective in this usage pattern::
prg.sum(queue, a_np.shape, None, a_g, b_g, res_g)
Note that the :class:`Program` has to be built (see :meth:`build`) in
order for this to work simply by attribute lookup.
.. note::
The :class:`program_info` attributes live
in the same name space and take precedence over
:class:`Kernel` names.
.. note::
If you need to retrieve a kernel whose name includes non-identifier
characters, retrieving it as an attribute of
:class:`~pyopencl.Program` will
not work, for obvious reasons. In that case, you can use
the :class:`~pyopencl.Kernel` constructor directly.
.. method:: all_kernels()
Returns a list of all :class:`Kernel` objects in the :class:`Program`.
.. method:: set_specialization_constant(spec_id, buffer)
Only available with CL 2.2 and newer.
.. versionadded:: 2020.3
.. automethod:: from_int_ptr
.. autoattribute:: int_ptr
|comparable|
.. function:: create_program_with_built_in_kernels(context, devices, kernel_names)
Only available with CL 1.2.
.. versionadded:: 2011.2
.. function:: link_program(context, programs, options=[], devices=None)
Only available with CL 1.2.
.. versionadded:: 2011.2
.. function:: unload_platform_compiler(platform)
Only available with CL 1.2.
.. versionadded:: 2011.2
Kernel
------
.. class:: Kernel(program, name)
.. attribute:: info
Lower case versions of the :class:`kernel_info` constants
may be used as attributes on instances of this class
to directly query info attributes.
.. method:: clone()
Only available with CL 2.1.
.. versionadded:: 2020.3
.. method:: get_info(param)
See :class:`kernel_info` for values of *param*.
.. method:: get_work_group_info(param, device)
See :class:`kernel_work_group_info` for values of *param*.
.. method:: get_arg_info(arg_index, param)
See :class:`kernel_arg_info` for values of *param*.
Only available in OpenCL 1.2 and newer.
.. method:: get_sub_group_info(self, device, param, input_value=None)
When the OpenCL spec requests *input_value* to be of type ``size_t``,
these may be passed directly as a number. When it requests
*input_value* to be of type ``size_t *``, a tuple of integers
may be passed.
Only available in OpenCL 2.1 and newer.
.. versionadded:: 2020.3
.. method:: set_arg(self, index, arg)
*arg* may be
* *None*: This may be passed for ``__global`` memory references
to pass a *NULL* pointer to the kernel.
* Anything that satisfies the Python buffer interface,
in particular :class:`numpy.ndarray`, :class:`str`,
or :mod:`numpy`'s sized scalars, such as :class:`numpy.int32`
or :class:`numpy.float64`.
.. note::
Note that Python's own :class:`int` or :class:`float`
objects will not work out of the box. See
:meth:`Kernel.set_scalar_arg_dtypes` for a way to make
them work. Alternatively, the standard library module
:mod:`struct` can be used to convert Python's native
number types to binary data in a :class:`str`.
* An instance of :class:`MemoryObject`. (e.g. :class:`Buffer`,
:class:`Image`, etc.)
* An instance of :class:`LocalMemory`.
* An instance of :class:`Sampler`.
* An instance of :class:`CommandQueue`. (CL 2.0 and higher only)
.. method:: set_args(self, *args)
Invoke :meth:`set_arg` on each element of *args* in turn.
.. versionadded:: 0.92
.. method:: set_scalar_arg_dtypes(arg_dtypes)
Inform the wrapper about the sized types of scalar
:class:`Kernel` arguments. For each argument,
*arg_dtypes* contains an entry. For non-scalars,
this must be *None*. For scalars, it must be an
object acceptable to the :class:`numpy.dtype`
constructor, indicating that the corresponding
scalar argument is of that type.
After invoking this function with the proper information,
most suitable number types will automatically be
cast to the right type for kernel invocation.
.. note ::
The information set by this method is attached to a single kernel
instance. A new kernel instance is created every time you use
`program.kernel` attribute access. The following will therefore not
work::
prg = cl.Program(...).build()
prg.kernel.set_scalar_arg_dtypes(...)
prg.kernel(queue, n_globals, None, args)
.. method:: __call__(queue, global_size, local_size, *args, global_offset=None, wait_for=None, g_times_l=False, allow_empty_ndrange=False)
Use :func:`enqueue_nd_range_kernel` to enqueue a kernel execution, after using
:meth:`set_args` to set each argument in turn. See the documentation for
:meth:`set_arg` to see what argument types are allowed.
|glsize|
|empty-nd-range|
|std-enqueue-blurb|
.. note::
:meth:`__call__` is *not* thread-safe. It sets the arguments using :meth:`set_args`
and then runs :func:`enqueue_nd_range_kernel`. Another thread could race it
in doing the same things, with undefined outcome. This issue is inherited
from the C-level OpenCL API. The recommended solution is to make a kernel
(i.e. access ``prg.kernel_name``, which corresponds to making a new kernel)
for every thread that may enqueue calls to the kernel.
A solution involving implicit locks was discussed and decided against on the
mailing list in `October 2012
<https://lists.tiker.net/pipermail/pyopencl/2012-October/001311.html>`__.
.. versionchanged:: 0.92
*local_size* was promoted to third positional argument from being a
keyword argument. The old keyword argument usage will continue to
be accepted with a warning throughout the 0.92 release cycle.
This is a backward-compatible change (just barely!) because
*local_size* as third positional argument can only be a
:class:`tuple` or *None*. :class:`tuple` instances are never valid
:class:`Kernel` arguments, and *None* is valid as an argument, but
its treatment in the wrapper had a bug (now fixed) that prevented
it from working.
.. versionchanged:: 2011.1
Added the *g_times_l* keyword arg.
.. versionchanged:: 2020.2
Added the *allow_empty_ndrange* keyword argument.
.. method:: capture_call(output_file, queue, global_size, local_size, *args, global_offset=None, wait_for=None, g_times_l=False)
This method supports the exact same interface as :meth:`__call__`, but
instead of invoking the kernel, it writes a self-contained PyOpenCL program
to *filename* that reproduces this invocation. Data and kernel source code
will be packaged up in *filename*'s source code.
This is mainly intended as a debugging aid. For example, it can be used
to automate the task of creating a small, self-contained test case for
an observed problem. It can also help separate a misbehaving kernel from
a potentially large or time-consuming outer code.
:arg output_file: a a filename or a file-like to which the generated
code is to be written.
To use, simply change::
evt = my_kernel(queue, gsize, lsize, arg1, arg2, ...)
to::
evt = my_kernel.capture_call("bug.py", queue, gsize, lsize, arg1, arg2, ...)
.. versionadded:: 2013.1
.. automethod:: from_int_ptr
.. autoattribute:: int_ptr
|comparable|
.. class:: LocalMemory(size)
A helper class to pass ``__local`` memory arguments to kernels.
.. versionadded:: 0.91.2
.. attribute:: size
The size of local buffer in bytes to be provided.
.. function:: enqueue_nd_range_kernel(queue, kernel, global_work_size, local_work_size, global_work_offset=None, wait_for=None, g_times_l=False, allow_empty_ndrange=False)
|glsize|
|empty-nd-range|
|std-enqueue-blurb|
.. versionchanged:: 2011.1
Added the *g_times_l* keyword arg.
.. versionchanged:: 2020.2
Added the *allow_empty_ndrange* keyword argument.
|