OpenCL Runtime: Memory¶
- class pyopencl.MemoryObjectHolder¶
- .. attribute:: info
Lower case versions of the
mem_infoconstants may be used as attributes on instances of this class to directly query info attributes.- int_ptr¶
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.
- class pyopencl.MemoryObject¶
- Inherits from :class:`MemoryObjectHolder`.
- hostbuf¶
- release()¶
- get_host_array(shape, dtype, order='C')¶
Return the memory object’s associated host memory area as a
numpy.ndarrayof the given shape, dtype and order.
- 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.
- from_int_ptr(int_ptr_value: int, retain: bool = True) object¶
(static method) Return a new Python object referencing the C-level
cl_memobject 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.
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_flagsfor values of flags. If hostbuf is specified, size defaults to the size of the specified buffer if it is passed as zero.Bufferinherits 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 aBufferor a Python buffer object is needed.Note that actual memory allocation in OpenCL may be deferred. Buffers are attached to a
Contextand 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
sliceobject 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
Bufferpattern – 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.Eventinstances 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_orderfor possible values.
- channel_data_type¶
See
channel_typefor 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:
ImageFormatwas made comparable and hashable
- pyopencl.get_supported_image_formats(context, flags, image_type)¶
See
mem_flagsfor possible values of flags andmem_object_typefor 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_infoandimage_infoconstants may be used as attributes on instances of this class to directly query info attributes.
- get_image_info(param)¶
See
image_infofor 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_flagsfor 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.
Imageinherits fromMemoryObject.Note
If you want to load images from
numpy.ndarrayinstances or read images back into them, be aware that OpenCL images expect the x dimension to vary fastest, whereas in the default (C) order ofnumpyarrays, 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
Imagefrom thenumpy.ndarrayary. 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
ImageFormatis 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_imagefare 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.Eventinstances 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: CommandQueue, dest: Buffer, src: Buffer | ndarray[tuple[Any, ...], dtype[Any]], *, dst_offset: int = 0, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event[source]¶
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer | ndarray[tuple[Any, ...], dtype[Any]], src: Buffer, *, src_offset: int = 0, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer, src: Buffer, *, src_offset: int = 0, dst_offset: int = 0, byte_count: int | None = None, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer, src: Buffer | ndarray[tuple[Any, ...], dtype[Any]], *, origin: tuple[int, ...], host_origin: tuple[int, ...], region: tuple[int, ...], buffer_pitches: tuple[int, ...] | None = None, host_pitches: tuple[int, ...] | None = None, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer | ndarray[tuple[Any, ...], dtype[Any]], src: Buffer, *, origin: tuple[int, ...], host_origin: tuple[int, ...], region: tuple[int, ...], buffer_pitches: tuple[int, ...] | None = None, host_pitches: tuple[int, ...] | None = None, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer, src: Buffer, *, src_origin: tuple[int, ...], dst_origin: tuple[int, ...], region: tuple[int, ...], src_pitches: tuple[int, ...] | None = None, dst_pitches: tuple[int, ...] | None = None, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer | ndarray[tuple[Any, ...], dtype[Any]], src: Image, *, origin: tuple[int, ...], region: tuple[int, ...], pitches: tuple[int, ...] | None = None, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Image, src: Buffer | ndarray[tuple[Any, ...], dtype[Any]], *, origin: tuple[int, ...], region: tuple[int, ...], pitches: tuple[int, ...] | None = None, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Image, src: Buffer, *, origin: tuple[int, ...], region: tuple[int, ...], pitches: tuple[int, ...] | None = None, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Buffer, src: Image, *, origin: tuple[int, ...], region: tuple[int, ...], pitches: tuple[int, ...] | None = None, wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: Image, src: Image, *, src_origin: tuple[int, ...], dest_origin: tuple[int, ...], region: tuple[int, ...], wait_for: Sequence[Event] | None = None) Event
- pyopencl.enqueue_copy(queue: CommandQueue, dest: SVMPointer | Buffer | ndarray[tuple[Any, ...], dtype[Any]], src: SVMPointer | Buffer | ndarray[tuple[Any, ...], dtype[Any]], *, byte_count: int | None = None, src_offset: int = 0, dst_offset: int = 0, is_blocking: bool = True, wait_for: Sequence[Event] | None = None) Event
Copy from
Image,Bufferor the host toImage,Bufferor 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
NannyEventif the transfer involved a host-side buffer, otherwise anEvent.
Note
Be aware that the deletion of the
NannyEventthat 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 thisEventuntil the transfer has completed.Note
Two types of ‘buffer’ occur in the arguments to this function,
Bufferand ‘host-side buffers’. The latter are defined by Python and commonly called buffer objects.numpyarrays are a very common example. Make sure to always be clear on whether aBufferor 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 –
tupleofintof length three or shorter. (mandatory)host_origin –
tupleofintof length three or shorter. (mandatory)region –
tupleofintof length three or shorter. (mandatory)buffer_pitches –
tupleofintof length two or shorter. (optional, “tightly-packed” if unspecified)host_pitches –
tupleofintof length two or shorter. (optional, “tightly-packed” if unspecified)
Rectangular
Buffer↔Buffertransfers (CL 1.1 and newer)- Parameters:
src_origin –
tupleofintof length three or shorter. (mandatory)dst_origin –
tupleofintof length three or shorter. (mandatory)region –
tupleofintof length three or shorter. (mandatory)src_pitches –
tupleofintof length two or shorter. (optional, “tightly-packed” if unspecified)dst_pitches –
tupleofintof 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.Eventinstances 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
Platforminstancequeue – a
CommandQueueinstancesrc – a
Bufferinstancedest – a
Bufferinstancesize – 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.Eventinstances 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
withstatement. 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.Eventinstances for whose completion this command waits before starting execution. shape, dtype, and order have the same meaning as innumpy.empty(). Seemap_flagsfor possible values of flags. strides, if given, overrides order.- Returns:
a tuple (array, event). array is a
numpy.ndarrayrepresenting 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.Eventinstances for whose completion this command waits before starting execution. shape, dtype, and order have the same meaning as innumpy.empty(). Seemap_flagsfor possible values of flags. strides, if given, overrides order.- Returns:
a tuple (array, event, row_pitch, slice_pitch). array is a
numpy.ndarrayrepresenting 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
boolindicating whether to use coordinates between 0 and 1 (True) or the texture’s natural pixel size (False). Seeaddressing_modeandfilter_modefor possible argument values.Also supports an alternate signature
(context, properties).- Parameters:
properties – a sequence of keys and values from
sampler_propertiesas 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_infoconstants may be used as attributes on instances of this class to directly query info attributes.
- get_info(param)¶
See
sampler_infofor 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_samplerobject 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_flagsfor values of flags.- Parameters:
properties – a sequence of keys and values from
pipe_propertiesas 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.