OpenCL for Python API

opencl.Platform

class opencl.Platform

opencl.Platform not constructable

use opencl.get_platforms() to get a list of

devices

list of all devices attached to this platform

extensions

platform extensions as a string

get_devices

plat.get_devices(device_type=opencl.Device.ALL)

return a list of devices by type.

name

platform name

profile

return the plafrom profile info

vendor

platform vendor

version

return the version string of the platform

opencl.Device

class opencl.Device
DEFAULT

device type default.

address_bits
available
compiler_available
driver_version
extensions
global_mem_size
has_image_support

test if this device supports the openc.Image class

has_local_mem
has_native_kernel

test if this device supports native python kernels

has_queue_out_of_order_exec_mode

test if this device supports out_of_order_exec_mode for queues

has_queue_profiling

test if this device supports profiling for queues

host_unified_memory
local_mem_size
max_clock_frequency
max_compute_units

The number of parallel compute cores on the OpenCL device. The minimum value is 1.

max_const_buffer_size
max_image2d_shape
max_image3d_shape
max_mem_alloc_size
max_parameter_size
max_read_image_args
max_work_group_size
max_work_item_dimensions

Maximum dimensions that specify the global and local work-item IDs used by the data parallel execution model. (Refer to clEnqueueNDRangeKernel).

The minimum value is 3.

max_work_item_sizes

Maximum number of work-items that can be specified in each dimension to opencl.Queue.enqueue_nd_range_kernel.

Returns:n entries, where n is the value returned by the query for opencl.Device.max_work_item_dimensions
max_write_image_args
name

the name of this device

platform

return the platform this device is associated with.

profile
profiling_timer_resolution
queue_properties

return queue properties as a bitfield

see also has_queue_out_of_order_exec_mode and has_queue_profiling

type

return device type: one of [Device.DEFAULT, Device.ALL, Device.GPU or Device.CPU]

vendor_id

return the vendor ID

version

opencl.Event

class opencl.Event

An event object can be used to track the execution status of a command. The API calls that enqueue commands to a command-queue create a new event object that is returned in the event argument.

add_callback

event.add_callback(callback) Registers a user callback function for on completion of the event.

Parameters:callback – must be of the signature callback(event, status)
status

the current status of the event.

wait

event.wait()

Waits on the host thread for commands identified by event objects in event_list to complete. A command is considered complete if its execution status is CL_COMPLETE or a negative value.

opencl.UserEvent

class opencl.UserEvent

Creates a user event object. User events allow applications to enqueue commands that wait on a user event to finish before the command is executed by the device.

complete

Set this event status to complete.

opencl.Program

class opencl.Program(context, source=None, binaries=None, devices=None)

Create an opencl program.

Parameters:
  • context – opencl.Context object.
  • source – program source to compile.
  • binaries – dict of pre-compiled binaries. of the form {device:bytes, ..}
  • devices – list of devices to compile on.
binaries
binary_sizes
build
context
devices
kernel

Return a kernel object.

logs
num_devices
source

opencl.MemoryObject

class opencl.MemoryObject

Memory objects are categorized into two types: buffer objects, and image objects. A buffer object stores a one-dimensional collection of elements whereas an image object is used to store a two- or three- dimensional texture, frame-buffer or image.

add_destructor_callback

memobj.add_destructor_callback(callback)

Registers a user callback function with a memory object. Each call to add_destructor_callback registers the specified user callback function on a callback stack associated with memobj.

The registered user callback functions are called in the reverse order in which they were registered. The user callback functions are called and then the memory object’s resources are freed and the memory object is deleted. This provides a mechanism for the application (and libraries) using memobj to be notified when the memory referenced by host_ptr, specified when the memory object is created and used as the storage bits for the memory object, can be reused or freed.

Parameters:callback – function with the signature callback(memobj)
base

Return the original memobject if this is a sub-buffer.

base_offset

Return the offset from base of this is a subbuffer.

context

Return the context that this object was created with.

get_buffer_id

Return the pointer to the opencl memory object.

mem_size

Return the size in bytes of this memory object.

type

return the enumed type of this object. One of MemoryObject.BUFFER, MemoryObject.IMAGE2D or MemoryObject.IMAGE3D

opencl.DeviceMemoryView

class opencl.DeviceMemoryView

A buffer object stores a one-dimensional collection of elements. Elements of a buffer object can be a scalar data type (such as an int, float), vector data type, or a user-defined structure

array_info
copy

Copy this buffer into a new object

ctype
format

Is ctype format of the elements

static from_host()

DeviceMemoryView.from_host(context, host, copy=True, readable=True, writeable=True)

Create an OpenCL buffer from a Python memoryview object.

is_contiguous

is this array C-contiguous

item

Return a python object if the size of this array is 1.

itemsize

Is size of each element

map

DeviceMemoryView.map(queue, blocking=True, readable=True, writeable=True)

enqueues a command to map a region of the buffer object given by buffer into the host address space and returns a pointer to this mapped region.

nbytes

Total number of bytes

ndim

number of dimensions

read

buf.read(queue, out, wait_on=(), blocking=False)

Read this buffer into a memory view

readonly

Is this memory writable

shape

Shape of the array

size

Total number of elements

strides

Strides of the array

write

view.write(queue, buf, wait_on=(), blocking=False)

Write data from a memoryview to the device.

opencl.ImageFormat

class opencl.ImageFormat
channel_data_type
channel_order
ctype
format
static from_ctype()
static supported_formats()

opencl.Image

class opencl.Image
format
image_depth
image_format
image_height
image_width
map
shape
strides

opencl.ContextProperties

class opencl.ContextProperties

Store of key value pairs that can be used to initialize an opencl.Context object.

as_dict
platform
properties_dict
set_property

opencl.Context

class opencl.Context(devices=(), device_type=cl.Device.DEFAULT, ContextProperties properties=None, callback=None)

opencl.Context(devices=(), device_type=cl.Device.DEFAULT, ContextProperties properties=None, callback=print_context_error)

Creates an OpenCL context. An OpenCL context is created with one or more devices. Contexts are used by the OpenCL runtime for managing objects such as command-queues, memory, program and kernel objects and for executing kernels on one or more devices specified in the context.

Parameters:
  • devices – list of opencl devices
  • device_type – type of device to create context from. used only if devices is empty.
  • properties – cl.ContextProperties object
  • callback – This callback function will be used by the OpenCL implementation to report information on errors that occur in this context. the function signature must be callback(str, bytes)
devices

return a list of devices associated with this context

num_devices

return the number of devices

properties

return a ContextProperties object

ref_count

return opencl internal refrence count of this object

opencl.OpenCLException

class opencl.OpenCLException

Base opencl exception object.

opencl.contextual_memory

class opencl.contextual_memory(ctype=None, shape=None, flat=False)

Memory ‘type’ descriptor.

array_info
ctype_string()
derefrence()

Return the type that this object is a pointer to.

from_param()

Return a ctypes.c_void_p from arg.

Parameters:arg – must be a MemoryObject.
nbytes
size

opencl.global_memory

class opencl.global_memory(ctype=None, shape=None, flat=False)

opencl.Kernel

class opencl.Kernel

openCl kernel object.

argnames
argtypes
compile_work_group_size
context
global_work_offset
global_work_size
local_mem_size
local_work_size
name
nargs
preferred_work_group_size_multiple
private_mem_size
program
set_args
work_group_size

opencl.Queue

class opencl.Queue

opencl.Queue(context, device=None, out_of_order_exec_mode=False, profiling=False)

OpenCL objects such as memory, program and kernel objects are created using a context. Operations on these objects are performed using a command-queue. The command-queue can be used to queue a set of operations (referred to as commands) in order. Having multiple command-queues allows applications to queue multiple independent commands without requiring synchronization. Note that this should work as long as these objects are not being shared. Sharing of objects across multiple command-queues will require the application to perform appropriate synchronization

Parameters:
  • context – An opencl.Context object
  • device – if None use the first device in the context [default None]
  • out_of_order_exec_mode – enable out_of_order_exec_mode [default False]
  • profiling – enable profiling [default False]
barrier

queue.barrier()

Enqueues a barrier operation. The queue.barrier command ensures that all queued commands in command_queue have finished execution before the next batch of commands can begin execution. The queue.barrier command is a synchronization point

context

Return the context that this queue was created with

copy
device

Return the device associated with this queue

enqueue_copy_buffer
enqueue_copy_buffer_rect
enqueue_native_kernel

queue.enqueue_native_kernel(function [, arg, ..., kwarg=, ...])

Enqueues a command to execute a python function.

Parameters:
  • function – A callable python object
  • args – Arguments for function
  • kwargs – Keywords for function
enqueue_nd_range_kernel

queue.enqueue_nd_range_kernel(kernel, work_dim, global_work_size, global_work_offset=None, local_work_size=None, wait_on=())

Enqueues a command to execute a kernel on a device

Parameters:
  • kernel – an opencl.Kernel object
  • work_dim – is the number of dimensions used to specify the global work-items and work-items in the work-group.
  • global_work_size – A list of length work_dim that describe the number of global work-items in work_dim dimensions that will execute the kernel function. T
  • global_work_offset – Can be used to specify an array of work_dim unsigned values that describe the offset used to calculate the global ID of a work-item.
:param local_work_size:f A list of length work_dim unsigned values that describe the number of
work-items that make up a work-group. If None, the OpenCL implementation will determine how to be break the global work-items into appropriate work-group instances
Parameters:wait_on – A list of events
enqueue_read_buffer
enqueue_task

queue.enqueue_task(kernel, wait_on=())

Enqueues a command to execute a kernel on a device. The kernel is executed using a single work-item.

Parameters:
  • kernel – an opencl kernel.
  • wait_on – a list of events
enqueue_wait_for_events

queue.enqueue_wait_for_events(self, event, event2, ...) queue.enqueue_wait_for_events(self, eventlist)

Enqueues a wait for a specific event or a list of events to complete before any future commands queued in the command-queue are executed. num_events specifies the number of events given by event_list.

enqueue_write_buffer
finish

queue.finish()

Blocks until all previously queued OpenCL commands in command_queue are issued to the associated device and have completed. clFinish does not return until all queued commands in command_queue have been processed and completed. clFinish is also a synchronization point

flush

queue.flush()

Issues all previously queued OpenCL commands in command_queue to the device associated with command_queue. clFlush only guarantees that all queued commands to command_queue will eventually be submitted to the appropriate device. There is no guarantee that they will be complete after clFlush returns.

marker

queue.marker()

Enqueues a marker command to command_queue. The marker command is not completed until all commands enqueued before it have completed. The marker command returns an event which can be waited on, i.e. this event can be waited on to insure that all commands, which have been queued before the marker command, have been completed.

Functions