OpenCL Runtime: Memory¶
- class pyopencl.MemoryObject¶
- info¶
Lower case versions of the
mem_info
constants may be used as attributes on instances of this class to directly query info attributes.
- hostbuf¶
- release()¶
- get_host_array(shape, dtype, order='C')¶
Return the memory object’s associated host memory area as a
numpy.ndarray
of the given shape, dtype and order.
- from_int_ptr(int_ptr_value: int, retain: bool = True) object ¶
(static method) Return a new Python object referencing the C-level
cl_mem
object at the location pointed to by int_ptr_value. The relevantclRetain*
function will be called if retain is True.If the previous owner of the object will not release the reference, retain should be set to False, to effectively transfer ownership topyopencl
.Added in version 2013.2.
Changed in version 2016.1: retain added.
- int_ptr¶
Return an integer corresponding to the pointer value of the underlying
cl_mem
. Usefrom_int_ptr()
to turn back into a Python object.Added in version 2013.2.
Instances of this class are hashable, and two instances of this class may be compared using “==” and “!=”. (Hashability was added in version 2011.2.) Two objects are considered the same if the underlying OpenCL object is the same, as established by C pointer equality.
Memory Migration¶
- pyopencl.enqueue_migrate_mem_objects(queue, mem_objects, flags=0, wait_for=None)¶
- Parameters:
flags – from
mem_migration_flags
Added in version 2011.2.
Only available with CL 1.2.
Buffer¶
- class pyopencl.Buffer(context, flags, size=0, hostbuf=None)¶
Create a
Buffer
. Seemem_flags
for values of flags. If hostbuf is specified, size defaults to the size of the specified buffer if it is passed as zero.Buffer
inherits fromMemoryObject
.Note
Python also defines a type of buffer object, and PyOpenCL interacts with those, too, as the host-side target of
enqueue_copy()
. Make sure to always be clear on whether aBuffer
or a Python buffer object is needed.Note that actual memory allocation in OpenCL may be deferred. Buffers are attached to a
Context
and are only moved to a device once the buffer is used on that device. That is also the point when out-of-memory errors will occur. If you’d like to be sure that there’s enough memory for your allocation, either useenqueue_migrate_mem_objects()
(if available) or simply perform a small transfer to the buffer. See alsopyopencl.tools.ImmediateAllocator
.- get_sub_region(origin, size, flags=0)¶
Only available in OpenCL 1.1 and newer.
- __getitem__(slc)¶
slc is a
slice
object indicating from which byte index range a sub-buffer is to be created. The flags argument ofget_sub_region()
is set to the same flags with which self was created.
- pyopencl.enqueue_fill_buffer(queue, mem, pattern, offset, size, wait_for=None)[source]¶
- Parameters:
mem – the on device
Buffer
pattern – a buffer object (likely a
numpy.ndarray
, eg.np.uint32(0)
). The memory associated with pattern can be reused or freed once the function completes.size – The size in bytes of the region to be filled. Must be a multiple of the size of the pattern.
offset – The location in bytes of the region being filled in mem. Must be a multiple of the size of the pattern.
Fills a buffer with the provided pattern
Returns a new
pyopencl.Event
. wait_for may either be None or a list ofpyopencl.Event
instances for whose completion this command waits before starting execution.Only available with CL 1.2.
Added in version 2011.2.
Image¶
- class pyopencl.ImageFormat([channel_order, channel_type])¶
- channel_order¶
See
channel_order
for possible values.
- channel_data_type¶
See
channel_type
for possible values.
- channel_count¶
Added in version 0.91.5.
- dtype_size¶
Added in version 0.91.5.
- itemsize¶
Added in version 0.91.5.
Instances of this class are hashable, and two instances of this class may be compared using “==” and “!=”. (Hashability was added in version 2011.2.) Two objects are considered the same if the underlying OpenCL object is the same, as established by C pointer equality.
Changed in version 0.91: Constructor arguments added.
Changed in version 2013.2:
ImageFormat
was made comparable and hashable
- pyopencl.get_supported_image_formats(context, flags, image_type)¶
See
mem_flags
for possible values of flags andmem_object_type
for possible values of image_type.
- class pyopencl.Image¶
- Use :func:`create_image` to create images.
Added in version 0.91.
- info¶
Lower case versions of the
mem_info
andimage_info
constants may be used as attributes on instances of this class to directly query info attributes.
- get_image_info(param)¶
See
image_info
for values of param.
- release()¶
Instances of this class are hashable, and two instances of this class may be compared using “==” and “!=”. (Hashability was added in version 2011.2.) Two objects are considered the same if the underlying OpenCL object is the same, as established by C pointer equality.
- pyopencl.create_image(context, flags, format, shape=None, pitches=None, hostbuf=None, is_array=False, buffer=None) Image [source]¶
See
mem_flags
for values of flags. shape is a 2- or 3-tuple. format is an instance ofImageFormat
. pitches is a 1-tuple for 2D images and a 2-tuple for 3D images, indicating the distance in bytes from one scan line to the next, and from one 2D image slice to the next.If hostbuf is given and shape is None, then hostbuf.shape is used as the shape parameter.
Image
inherits fromMemoryObject
.Note
If you want to load images from
numpy.ndarray
instances or read images back into them, be aware that OpenCL images expect the x dimension to vary fastest, whereas in the default (C) order ofnumpy
arrays, the last index varies fastest. If your array is arranged in the wrong order in memory, there are two possible fixes for this:Convert the array to Fortran (column-major) order using
numpy.asarray()
.Pass ary.T.copy() to the image creation function.
Added in version 2024.3.
- pyopencl.image_from_array(ctx, ary, num_channels=None, mode='r', norm_int=False)[source]¶
Build a 2D or 3D
Image
from thenumpy.ndarray
ary. If num_channels is greater than one, the last dimension of ary must be identical to num_channels. ary must be in C order. If num_channels is not given, it defaults to 1 for scalar types and the number of entries for Vector Types.The
ImageFormat
is chosen as the first num_channels components of “RGBA”.- Parameters:
mode – “r” or “w” for read/write
Note
When reading from the image object, the indices passed to
read_imagef
are in the reverse order from what they would be when accessing ary from Python.If norm_int is True, then the integer values are normalized to a floating point scale of 0..1 when read.
Added in version 2011.2.
- pyopencl.enqueue_fill_image(queue, mem, color, origin, region, wait_for=None)¶
- Parameters:
color – a buffer object (likely a
numpy.ndarray
)
Returns a new
pyopencl.Event
. wait_for may either be None or a list ofpyopencl.Event
instances for whose completion this command waits before starting execution.Only available with CL 1.2.
Added in version 2011.2.
Transfers¶
- pyopencl.enqueue_copy(queue, dest, src, **kwargs)[source]¶
Copy from
Image
,Buffer
or the host toImage
,Buffer
or the host. (Note: host-to-host copies are unsupported.)The following keyword arguments are available:
- Parameters:
wait_for – (optional, default empty)
is_blocking – Wait for completion. Defaults to True. (Available on any copy involving host memory)
- Returns:
A
NannyEvent
if the transfer involved a host-side buffer, otherwise anEvent
.
Note
Be aware that the deletion of the
NannyEvent
that is returned by the function if the transfer involved a host-side buffer will block until the transfer is complete, so be sure to keep a reference to thisEvent
until the transfer has completed.Note
Two types of ‘buffer’ occur in the arguments to this function,
Buffer
and ‘host-side buffers’. The latter are defined by Python and commonly called buffer objects.numpy
arrays are a very common example. Make sure to always be clear on whether aBuffer
or a Python buffer object is needed.Transfer
Buffer
↔ host- Parameters:
src_offset –
offset in bytes (optional)
May only be nonzero if applied on the device side.
dst_offset –
offset in bytes (optional)
May only be nonzero if applied on the device side.
Note
The size of the transfer is controlled by the size of the of the host-side buffer. If the host-side buffer is a
numpy.ndarray
, you can control the transfer size by transferring into a smaller ‘view’ of the target array, like this:cl.enqueue_copy(queue, large_dest_numpy_array[:15], src_buffer)
- Parameters:
byte_count – (optional) If not specified, defaults to the size of the source in versions 2012.x and earlier, and to the minimum of the size of the source and target from 2013.1 on.
src_offset – (optional)
dst_offset – (optional)
Rectangular
Buffer
↔ host transfers (CL 1.1 and newer)- Parameters:
buffer_origin –
tuple
ofint
of length three or shorter. (mandatory)host_origin –
tuple
ofint
of length three or shorter. (mandatory)region –
tuple
ofint
of length three or shorter. (mandatory)buffer_pitches –
tuple
ofint
of length two or shorter. (optional, “tightly-packed” if unspecified)host_pitches –
tuple
ofint
of length two or shorter. (optional, “tightly-packed” if unspecified)
Rectangular
Buffer
↔Buffer
transfers (CL 1.1 and newer)- Parameters:
src_origin –
tuple
ofint
of length three or shorter. (mandatory)dst_origin –
tuple
ofint
of length three or shorter. (mandatory)region –
tuple
ofint
of length three or shorter. (mandatory)src_pitches –
tuple
ofint
of length two or shorter. (optional, “tightly-packed” if unspecified)dst_pitches –
tuple
ofint
of length two or shorter. (optional, “tightly-packed” if unspecified)
Transfer
Image
↔ host- Parameters:
- Parameters:
- Parameters:
Transfer
SVMPointer
/host ↔SVMPointer
/host- Parameters:
byte_count – (optional) If not specified, defaults to the size of the source in versions 2012.x and earlier, and to the minimum of the size of the source and target from 2013.1 on.
Returns a new
pyopencl.Event
. wait_for may either be None or a list ofpyopencl.Event
instances for whose completion this command waits before starting execution.Added in version 2011.1.
- pyopencl.enqueue_copy_buffer_p2p_amd(platform, queue, src, dest, size=None, wait_for=None)¶
AMD extension to perform a peer-to-peer copy between two buffers on two different devices. The two devices must be in different contexts. The queue must be where the source buffer is located.
- Parameters:
platform – a
Platform
instancequeue – a
CommandQueue
instancesrc – a
Buffer
instancedest – a
Buffer
instancesize – the number of bytes to copy. If None, the minimum of the sizes of the two buffers is used.
Returns a new
pyopencl.Event
. wait_for may either be None or a list ofpyopencl.Event
instances for whose completion this command waits before starting execution.Only available on AMD platforms.
Added in version 2023.1.2.
Mapping Memory into Host Address Space¶
- class pyopencl.MemoryMap¶
This class may also be used as a context manager in a
with
statement. The memory corresponding to this object will be unmapped when this object is deleted orrelease()
is called.- release(self, queue: pyopencl._cl.CommandQueue | None = None, wait_for: object | None = None) pyopencl._cl.Event ¶
- pyopencl.enqueue_map_buffer(queue, buf, flags, offset, shape, dtype, order='C', strides=None, wait_for=None, is_blocking=True)¶
wait_for may either be None or a list of
pyopencl.Event
instances for whose completion this command waits before starting execution. shape, dtype, and order have the same meaning as innumpy.empty()
. Seemap_flags
for possible values of flags. strides, if given, overrides order.- Returns:
a tuple (array, event). array is a
numpy.ndarray
representing the host side of the map. Its .base member contains aMemoryMap
.
Changed in version 2011.1: is_blocking now defaults to True.
Changed in version 2013.1: order now defaults to “C”.
Changed in version 2013.2: Added strides argument.
Sample usage:
mapped_buf = cl.enqueue_map_buffer(queue, buf, ...) with mapped_buf.base: # work with mapped_buf ... # memory will be unmapped here
- pyopencl.enqueue_map_image(queue, buf, flags, origin, region, shape, dtype, order='C', strides=None, wait_for=None, is_blocking=True)¶
wait_for may either be None or a list of
pyopencl.Event
instances for whose completion this command waits before starting execution. shape, dtype, and order have the same meaning as innumpy.empty()
. Seemap_flags
for possible values of flags. strides, if given, overrides order.- Returns:
a tuple (array, event). array is a
numpy.ndarray
representing the host side of the map. Its .base member contains aMemoryMap
.
Changed in version 2011.1: is_blocking now defaults to True.
Changed in version 2013.1: order now defaults to “C”.
Changed in version 2013.2: Added strides argument.
Samplers¶
- class pyopencl.Sampler¶
- __init__(context, normalized_coords, addressing_mode, filter_mode)¶
normalized_coords is a
bool
indicating whether to use coordinates between 0 and 1 (True) or the texture’s natural pixel size (False). Seeaddressing_mode
andfilter_mode
for possible argument values.Also supports an alternate signature
(context, properties)
.- Parameters:
properties – a sequence of keys and values from
sampler_properties
as accepted byclCreateSamplerWithProperties()
(see the OpenCL spec for details). The trailing 0 is added automatically and does not need to be included.
This signature Requires OpenCL 2 or newer.
Changed in version 2018.2: The properties-based signature was added.
- info¶
Lower case versions of the
sampler_info
constants may be used as attributes on instances of this class to directly query info attributes.
- get_info(param)¶
See
sampler_info
for values of param.
- from_int_ptr(int_ptr_value: int, retain: bool = True) pyopencl._cl.Sampler ¶
(static method) Return a new Python object referencing the C-level
cl_sampler
object at the location pointed to by int_ptr_value. The relevantclRetain*
function will be called if retain is True.If the previous owner of the object will not release the reference, retain should be set to False, to effectively transfer ownership topyopencl
.Added in version 2013.2.
Changed in version 2016.1: retain added.
- int_ptr¶
Return an integer corresponding to the pointer value of the underlying
cl_sampler
. Usefrom_int_ptr()
to turn back into a Python object.Added in version 2013.2.
Instances of this class are hashable, and two instances of this class may be compared using “==” and “!=”. (Hashability was added in version 2011.2.) Two objects are considered the same if the underlying OpenCL object is the same, as established by C pointer equality.
Pipes¶
- class pyopencl.Pipe(context, flags, packet_size, max_packets, properties=())¶
See
mem_flags
for values of flags.- Parameters:
properties – a sequence of keys and values from
pipe_properties
as accepted byclCreatePipe()
. The trailing 0 is added automatically and does not need to be included. (This argument must currently be empty.)
This function requires OpenCL 2 or newer.
Added in version 2020.3.
Changed in version 2021.1.7: properties now defaults to an empty tuple.
Type aliases¶
- class pyopencl._cl.Buffer¶
See
pyopencl.Buffer
.
- class pyopencl._cl.Image¶
See
pyopencl.Image
.