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, retain=True)¶ Constructs a
pyopencl
handle from a C-level pointer (given as the integer int_ptr_value). If retain is True (the defauult)pyopencl
will callclRetainXXX
on the provided object. If the previous owner of the object will not release the reference, retain should be set to False, to effectively transfer ownership topyopencl
.Changed in version 2016.1: retain added
-
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.
-
Memory Migration¶
-
pyopencl.
enqueue_migrate_mem_objects
(queue, mem_objects, flags=0, wait_for=None)¶ Parameters: flags – from mem_migration_flags
New in version 2011.2.
Only available with CL 1.2.
-
pyopencl.
enqueue_migrate_mem_object_ext
(queue, mem_objects, flags=0, wait_for=None)¶ Parameters: flags – from migrate_mem_object_flags_ext
New in version 2011.2.
Only available with the cl_ext_migrate_memobject extension.
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)¶ Parameters: pattern – 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 exeuction.Only available with CL 1.2.
New 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
¶ New in version 0.91.5.
-
dtype_size
¶ New in version 0.91.5.
-
itemsize
¶ New 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.
-
Image(context, flags, format, shape=None, pitches=None, hostbuf=None, is_array=False, buffer=None):
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.
New in version 0.91.
Changed in version 2011.2: Added is_array and buffer, which are only available on CL 1.2 and newer.
-
pyopencl.
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.
-
pyopencl.
get_image_info
(param)¶ See
image_info
for values of param.
-
pyopencl.
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.
- Convert the array to Fortran (column-major) order using
-
pyopencl.
image_from_array
(ctx, ary, num_channels=None, mode="r", norm_int=False)¶ 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.
New 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 exeuction.Only available with CL 1.2.
New in version 2011.2.
Transfers¶
-
pyopencl.
enqueue_copy
(queue, dest, src, **kwargs)¶ 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
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
↔ hostParameters: device_offset – offset in bytes (optional) 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 transfering 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)
- dest_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
↔ hostParameters: Transfer
Buffer
↔Image
Parameters: Transfer
Image
↔Image
Parameters: 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 exeuction.New in version 2011.1.
Mapping Memory into Host Address Space¶
-
class
pyopencl.
MemoryMap
¶ ” .. automethod:: release
This class may also be used as a context manager in a
with
statement.
-
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 exeuction. 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.
-
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 exeuction. 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
(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.-
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, retain=True)¶ Constructs a
pyopencl
handle from a C-level pointer (given as the integer int_ptr_value). If retain is True (the defauult)pyopencl
will callclRetainXXX
on the provided object. If the previous owner of the object will not release the reference, retain should be set to False, to effectively transfer ownership topyopencl
.Changed in version 2016.1: retain added
-
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.
-