3
OpenCL Platform/Runtime Documentation
4
=====================================
10
.. moduleauthor:: Andreas Kloeckner <inform@tiker.net>
14
Gives the numeric version of PyOpenCL as a variable-length tuple
15
of integers. Enables easy version checks such as
18
.. data:: VERSION_STATUS
20
A text string such as `"rc4"` or `"beta"` qualifying the status
23
.. data:: VERSION_TEXT
25
The full release name (such as `"0.93rc4"`) in string form.
27
.. function:: get_cl_header_version()
29
Return a variable-length tuple of integers representing the
30
version of the OpenCL header against which PyOpenCL was
33
.. versionadded:: 0.92
42
Base class for all PyOpenCL exceptions.
44
.. class:: MemoryError
48
.. class:: RuntimeError
53
.. include:: constants.inc
55
Platforms, Devices and Contexts
56
-------------------------------
58
.. |comparable| replace:: Two instances of this class may be compared
59
using *"=="* and *"!="*.
60
.. |buf-iface| replace:: must implement the Python buffer interface.
61
(e.g. by being an :class:`numpy.ndarray`)
62
.. |explain-waitfor| replace:: *wait_for*
63
may either be *None* or a list of :class:`Event` instances for
64
whose completion this command waits before starting exeuction.
65
.. |std-enqueue-blurb| replace:: Returns a new :class:`Event`. |explain-waitfor|
67
.. function:: get_platforms()
69
Return a list of :class:`Platform` instances.
75
Lower case versions of the :class:`platform_info` constants
76
may be used as attributes on instances of this class
77
to directly query info attributes.
79
.. method:: get_info(param)
81
See :class:`platform_info` for values of *param*.
83
.. method:: get_devices(device_type=device_type.ALL)
85
Return a list of devices matching *device_type*.
86
See :class:`device_type` for values of *device_type*.
94
Lower case versions of the :class:`device_info` constants
95
may be used as attributes on instances of this class
96
to directly query info attributes.
98
.. method:: get_info(param)
100
See :class:`device_info` for values of *param*.
102
Two instances of this class may be compared using *=="* and *"!="*.
104
.. class:: Context(devices=None, properties=None, dev_type=None)
106
Create a new context. *properties* is a list of key-value
107
tuples, where each key must be one of :class:`context_properties`.
108
At most one of *devices* and *dev_type* may be not `None`, where
109
*devices* is a list of :class:`Device` instances, and
110
*dev_type* is one of the :class:`device_type` constants.
111
If neither is specified, a context with a *dev_type* of
112
:attr:`device_type.DEFAULT` is created.
116
Calling the constructor with no arguments will fail for recent
117
CL drivers that support the OpenCL ICD. If you want similar,
118
just-give-me-a-context-already behavior, we recommend
119
:func:`create_some_context`. See, e.g. this
120
`explanation by AMD <http://developer.amd.com/support/KnowledgeBase/Lists/KnowledgeBase/DispForm.aspx?ID=71>`_.
125
:attr:`context_properties.CL_GL_CONTEXT_KHR`,
126
:attr:`context_properties.CL_EGL_DISPLAY_KHR`,
127
:attr:`context_properties.CL_GLX_DISPLAY_KHR`,
128
:attr:`context_properties.CL_WGL_HDC_KHR`, and
129
:attr:`context_properties.CL_CGL_SHAREGROUP_KHR`
130
the value in the key-value pair is a PyOpenGL context or display
133
.. versionchanged:: 0.91.2
134
Constructor arguments *dev_type* added.
138
Lower case versions of the :class:`context_info` constants
139
may be used as attributes on instances of this class
140
to directly query info attributes.
142
.. method:: get_info(param)
144
See :class:`context_info` for values of *param*.
148
.. function:: create_some_context(interactive=True)
150
Create a :class:`Context` 'somehow'.
152
If multiple choices for platform and/or device exist, *interactive*
153
is True, and *sys.stdin.isatty()* is also True,
154
then the user is queried about which device should be chosen.
155
Otherwise, a device is chosen in an implementation-defined manner.
157
Command Queues and Events
158
-------------------------
160
.. class:: CommandQueue(context, device=None, properties=None)
162
Create a new command queue. *properties* is a bit field
163
consisting of :class:`command_queue_properties` values.
165
if *device* is None, one of the devices in *context* is chosen
166
in an implementation-defined manner.
170
Lower case versions of the :class:`command_queue_info` constants
171
may be used as attributes on instances of this class
172
to directly query info attributes.
174
.. method:: get_info(param)
176
See :class:`command_queue_info` for values of *param*.
178
.. method:: set_property(prop, enable)
180
See :class:`command_queue_properties` for possible values of *prop*.
181
*enable* is a :class:`bool`.
183
Unavailable in OpenCL 1.1 and newer.
194
Lower case versions of the :class:`event_info` constants
195
may be used as attributes on instances of this class
196
to directly query info attributes.
198
.. attribute:: profile.info
200
Lower case versions of the :class:`profiling_info` constants
201
may be used as attributes on the attribute `profile` of this
202
class to directly query profiling info.
204
For example, you may use *evt.profile.end* instead of
205
*evt.get_profiling_info(pyopencl.profiling_info.END)*.
207
.. method:: get_info(param)
209
See :class:`event_info` for values of *param*.
211
.. method:: get_profiling_info(param)
213
See :class:`profiling_info` for values of *param*.
214
See :attr:`profile` for an easier way of obtaining
215
the same information.
221
.. function:: wait_for_events(events)
222
.. function:: enqueue_marker(queue)
224
Returns an :class:`Event`.
226
.. function:: enqueue_wait_for_events(queue, events)
228
Returns an :class:`Event`.
230
.. function:: enqueue_barrier(queue)
232
Enqueues a barrier operation. which ensures that all queued commands in
233
command_queue have finished execution. This command is a synchronization
236
.. versionadded:: 0.91.5
238
.. class:: UserEvent(context)
240
A subclass of :class:`Event`. Only available with OpenCL 1.1 and newer.
242
.. versionadded:: 0.92
244
.. method:: set_status(status)
246
See :class:`command_execution_status` for possible values of *status*.
251
.. class:: MemoryObject
255
Lower case versions of the :class:`mem_info` constants
256
may be used as attributes on instances of this class
257
to directly query info attributes.
259
.. method:: get_info(param)
261
See :class:`mem_info` for values of *param*.
263
.. attribute:: hostbuf
265
Contains the *hostbuf* parameter if the MemoryObject was constructed
266
with :attr:`mem_flags.USE_HOST_PTR`.
268
.. method:: release()
275
.. class:: Buffer(context, flags, size=0, hostbuf=None)
277
Create a :class:`Buffer`.
278
See :class:`mem_flags` for values of *flags*.
279
If *hostbuf* is specified, *size* defaults to the size of
280
the specified buffer if it is passed as zero.
282
:class:`Buffer` is a subclass of :class:`MemoryObject`.
284
.. method:: get_sub_region(origin, size, flags=0)
286
.. method:: __getitem__(slc)
288
*slc* is a :class:`slice` object indicating from which byte index range
289
a sub-buffer is to be created. The *flags* argument of
290
:meth:`get_sub_region` is set to the same flags with which *self* was
293
.. function:: enqueue_read_buffer(queue, mem, hostbuf, device_offset=0, wait_for=None, is_blocking=False)
297
*hostbuf* |buf-iface|
299
.. function:: enqueue_write_buffer(queue, mem, hostbuf, device_offset=0, wait_for=None, is_blocking=False)
303
*hostbuf* |buf-iface|
305
.. function:: enqueue_copy_buffer(queue, src, dst, byte_count=0, src_offset=0, dst_offset=0, wait_for=None)
307
If *byte_count* is passed as 0 (the default), the size of the
308
:class:`Buffer` *src* is used instead.
312
.. versionadded:: 0.91.5
314
.. function:: enqueue_read_buffer_rect(queue, mem, hostbuf, buffer_origin, host_origin, region, buffer_pitches=None, host_pitches=None, wait_for=None, is_blocking=False)
316
The *origin* and *region* parameters are :class:`tuple` instances of length
317
three or shorter. The *pitches* parameters are :class:`tuple` instances of
318
length two or shorter, which may be zero to indicate 'tight packing'.
322
*hostbuf* |buf-iface|
324
Only available in OpenCL 1.1 and newer.
326
.. versionadded:: 0.92
328
.. function:: enqueue_write_buffer_rect(queue, mem, hostbuf, buffer_origin, host_origin, region, buffer_pitches=None, host_pitches=None, wait_for=None, is_blocking=False)
330
The *origin* and *region* parameters are :class:`tuple` instances of length
331
three or shorter. The *pitches* parameters are :class:`tuple` instances of
332
length two or shorter, which may be zero to indicate 'tight packing'.
336
*hostbuf* |buf-iface|
338
Only available in OpenCL 1.1 and newer.
340
.. versionadded:: 0.92
342
.. function:: enqueue_copy_buffer_rect(queue, src, dst, src_origin, dst_origin, region, src_pitches=None, dst_pitches=None, wait_for=None)
344
The *origin* and *region* parameters are :class:`tuple` instances of length
345
three or shorter. The *pitches* parameters are :class:`tuple` instances of
346
length two or shorter, which may be zero to indicate 'tight packing'.
350
Only available in OpenCL 1.1 and newer.
352
.. versionadded:: 0.92
357
.. class:: ImageFormat([channel_order, channel_type])
359
.. versionchanged:: 0.91
360
Constructor arguments added.
362
.. attribute:: channel_order
364
See :class:`channel_order` for possible values.
366
.. attribute:: channel_data_type
368
See :class:`channel_type` for possible values.
370
.. attribute:: channel_count
372
.. versionadded:: 0.91.5
374
.. attribute:: dtype_size
376
.. versionadded:: 0.91.5
378
.. attribute:: itemsize
380
.. versionadded:: 0.91.5
384
Returns a :class:`str` representation of the image format.
386
.. versionadded:: 0.91
388
.. function:: get_supported_image_formats(context, flags, image_type)
390
See :class:`mem_flags` for possible values of *flags*
391
and :class:`mem_object_type` for possible values of *image_type*.
395
.. class:: Image(context, flags, format, shape=None, pitches=None, hostbuf=None)
397
*shape* is a 2- or 3-tuple.
399
If *hostbuf* is given and *shape* is `None`, then *hostbuf.shape* is
400
used as the *shape* parameter.
402
:class:`Image` is a subclass of :class:`MemoryObject`.
404
.. versionadded:: 0.91
408
Lower case versions of the :class:`mem_info`
409
and :class:`image_info` constants
410
may be used as attributes on instances of this class
411
to directly query info attributes.
415
Return the value of the *shape* constructor argument as a :class:`tuple`.
417
.. method:: get_image_info(param)
419
See :class:`image_info` for values of *param*.
421
.. method:: release()
425
.. function:: enqueue_read_image(queue, mem, origin, region, hostbuf, row_pitch=0, slice_pitch=0, wait_for=None, is_blocking=False)
429
.. versionchanged:: 0.91
430
*pitch* arguments defaults to zero, moved.
432
.. function:: enqueue_write_image(queue, mem, origin, region, hostbuf, row_pitch=0, slice_pitch=0, wait_for=None, is_blocking=False)
436
.. versionchanged:: 0.91
437
*pitch* arguments defaults to zero, moved.
439
.. function:: enqueue_copy_image(queue, src, dest, src_origin, dest_origin, region, wait_for=None)
443
.. function:: enqueue_copy_image_to_buffer(queue, src, dest, origin, region, offset, wait_for=None)
447
.. function:: enqueue_copy_buffer_to_image(queue, src, dest, offset, origin, region, wait_for=None)
451
Mapping Memory into Host Address Space
452
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
456
.. method:: release(queue=None, wait_for=None)
458
.. function:: enqueue_map_buffer(queue, buf, flags, offset, shape, dtype, order, wait_for=None, is_blocking=False)
461
*shape*, *dtype*, and *order* have the same meaning
462
as in :func:`numpy.empty`.
463
See :class:`map_flags` for possible values of *flags*.
465
:return: a tuple *(array, event)*. *array* is a
466
:class:`numpy.ndarray` representing the host side
467
of the map. Its *.base* member contains a
470
.. function:: enqueue_map_image(queue, buf, flags, origin, region, shape, dtype, order, wait_for=None, is_blocking=False)
473
*shape*, *dtype*, and *order* have the same meaning
474
as in :func:`numpy.empty`.
475
See :class:`map_flags` for possible values of *flags*.
477
:return: a tuple *(array, event)*. *array* is a
478
:class:`numpy.ndarray` representing the host side
479
of the map. Its *.base* member contains a
486
.. class:: Sampler(context, normalized_coords, addressing_mode, filter_mode)
488
*normalized_coords* is a :class:`bool` indicating whether
489
to use coordinates between 0 and 1 (*True*) or the texture's
490
natural pixel size (*False*).
491
See :class:`addressing_mode` and :class:`filter_mode` for possible
496
Lower case versions of the :class:`sampler_info` constants
497
may be used as attributes on instances of this class
498
to directly query info attributes.
500
.. method:: get_info(param)
502
See :class:`sampler_info` for values of *param*.
509
.. class:: Program(context, src)
510
Program(context, devices, binaries)
512
*binaries* must contain one binary for each entry in *devices*.
516
Lower case versions of the :class:`program_info` constants
517
may be used as attributes on instances of this class
518
to directly query info attributes.
520
.. method:: get_info(param)
522
See :class:`program_info` for values of *param*.
524
.. method:: get_build_info(device, param)
526
See :class:`program_build_info` for values of *param*.
528
.. method:: build(options="", devices=None)
530
*options* is a string of compiler flags.
533
.. attribute:: kernel_name
535
:class:`Kernel` objects can be produced from a built
536
(see :meth:`build`) program simply by attribute lookup.
540
The :class:`program_info` attributes live
541
in the same name space and take precedence over
542
:class:`Kernel` names.
546
.. method:: all_kernels()
548
Returns a list of all :class:`Kernel` objects in the :class:`Program`.
550
.. function:: unload_compiler()
552
.. class:: Kernel(program, name)
556
Lower case versions of the :class:`kernel_info` constants
557
may be used as attributes on instances of this class
558
to directly query info attributes.
560
.. method:: get_info(param)
562
See :class:`kernel_info` for values of *param*.
564
.. method:: get_work_group_info(param, device)
566
See :class:`kernel_work_group_info` for values of *param*.
568
.. method:: set_arg(self, arg)
572
* `None`: This may be passed for `__global` memory references
573
to pass a NULL pointer to the kernel.
574
* Anything that satisfies the Python buffer interface,
575
in particular :class:`numpy.ndarray`, :class:`str`,
576
or :mod:`numpy`'s sized scalars, such as :class:`numpy.int32`
577
or :class:`numpy.float64`.
581
Note that Python's own :class:`int` or :class:`float`
582
objects will not work out of the box. See
583
:meth:`Kernel.set_scalar_arg_dtypes` for a way to make
584
them work. Alternatively, the standard library module
585
:mod:`struct` can be used to convert Python's native
586
number types to binary data in a :class:`str`.
588
* An instance of :class:`MemoryObject`. (e.g. :class:`Buffer`,
589
:class:`Image`, etc.)
590
* An instance of :class:`LocalMemory`.
591
* An instance of :class:`Sampler`.
593
.. method:: set_args(self, *args)
595
Invoke :meth:`set_arg` on each element of *args* in turn.
597
.. method:: set_scalar_arg_dtypes(arg_dtypes)
599
Inform the wrapper about the sized types of scalar
600
:class:`Kernel` arguments. For each argument,
601
*arg_dtypes* contains an entry. For non-scalars,
602
this must be *None*. For scalars, it must be an
603
object acceptable to the :class:`numpy.dtype`
604
constructor, indicating that the corresponding
605
scalar argument is of that type.
607
After invoking this function with the proper information,
608
most suitable number types will automatically be
609
cast to the right type for kernel invocation.
611
.. method:: __call__(queue, global_size, local_size, *args, global_offset=None, wait_for=None)
613
Use :func:`enqueue_nd_range_kernel` to enqueue a kernel execution, after using
614
:meth:`set_args` to set each argument in turn. See the documentation for
615
:meth:`set_arg` to see what argument types are allowed.
618
*None* may be passed for local_size
620
.. versionchanged:: 0.92
621
*local_size* was promoted to third positional argument from being a
622
keyword argument. The old keyword argument usage will continue to
623
be accepted with a warning throughout the 0.92 release cycle.
624
This is a backward-compatible change (just barely!) because
625
*local_size* as third positional argument can only be a
626
:class:`tuple` or *None*. :class:`tuple` instances are never valid
627
:class:`Kernel` arguments, and *None* is valid as an argument, but
628
its treatment in the wrapper had a bug (now fixed) that prevented
633
.. class:: LocalMemory(size)
635
A helper class to pass `__local` memory arguments to kernels.
637
.. versionadded:: 0.91.2
641
The size of local buffer in bytes to be provided.
643
.. function:: enqueue_nd_range_kernel(queue, kernel, global_work_size, local_work_size, global_work_offset=None, wait_for=None)
647
.. function:: enqueue_task(queue, kernel, wait_for=None)
656
Functionality in this section is only available when PyOpenCL is compiled
657
with GL support. See :func:`have_gl`.
659
.. versionadded:: 0.91
661
.. function:: have_gl()
663
Return *True* if PyOpenCL was compiled with OpenGL interoperability, otherwise *False*.
665
.. class:: GLBuffer(context, flags, bufobj)
667
:class:`GLBuffer` is a subclass of :class:`MemoryObject`.
669
.. attribute:: gl_object
671
.. class:: GLRenderBuffer(context, flags, bufobj)
673
:class:`GLRenderBuffer` is a subclass of :class:`MemoryObject`.
675
.. attribute:: gl_object
677
.. class:: GLTexture(context, flags, texture_target, miplevel, texture, dims)
679
*dims* is either 2 or 3.
680
:class:`GLTexture` is a subclass of :class:`Image`.
682
.. attribute:: gl_object
684
.. method:: get_gl_texture_info(param)
686
See :class:`gl_texture_info` for values of *param*. Only available when PyOpenCL is compiled with GL support. See :func:`have_gl`.
688
.. function:: enqueue_acquire_gl_objects(queue, mem_objects, wait_for=None)
690
*mem_objects* is a list of :class:`MemoryObject` instances.
693
.. function:: enqueue_release_gl_objects(queue, mem_objects, wait_for=None)
695
*mem_objects* is a list of :class:`MemoryObject` instances. |std-enqueue-blurb|
697
.. function:: get_gl_context_info_khr(properties, param_name)
699
Get information on which CL device corresponds to a given
700
GL/EGL/WGL/CGL device.
702
See the :class:`Context` constructor for the meaning of
703
*properties* and :class:`gl_context_info` for *param_name*.