Differences from PyOpenCL

import opencl


import pyopencl as cl

OpenCL for Python

import opencl as cl

Properties and Flags

In PyOpenCL one would would write:

ctx = pyopencl.Context()
devices = ctx.get_info(pyopencl.context_info.DEVICES)

OpenCL for Python:

ctx = pyopencl.Context()
devices = ctx.devices


OpenCL for Python does not require numpy and has no external dependencies. It only relies hevily on the memoryview object and ctypes data structures.

The main memory object in OpenCL for Python is a opencl.DeviceMemoryView. a device memoryview supports slicing and copying. To create a view of device memory you can use opencl.from_host() or opencl.empty()


na = np.arange(20)
a =  opencl.from_host(na, copy=True)

# and
b =  opencl.empty(shape, ctype='f')

The argument ctype may be a valid ctype or subclass from the ctypes module or a valid data format descriptor.


Kernels may follow the ctypes convention. and define an argtypes attribute. argnames and __defaults__ may also be defined.


program = cl.Program( '''__kernel void foo(__global *a, float b, int x) ... ''').build()

foo = program.foo
foo.argnames = 'a', 'b', 'x'
foo.argtypes = cl.global_memory('f'), cl.cl_float, cl.cl_int

#global_work_size is either a function or sequence of integers.
foo.global_work_size = lambda a: a.shape

#Equivalent to b=2.0, x=1
foo.__defaults__ = 10.0, 1

#The following invocations of foo are all equivalent.
foo(queue, a=cl_memory)
foo(queue, a=cl_memory, x=1, b=10)
foo(queue, cl_memory, 10, 1)
foo(queue, a=cl_memory, b=10, x=1, global_work_size=[cl_memory.size])


Can be referred to as opencl.Queue.

all enqueue_* functions are now methods on the opencl.Queue class

Table Of Contents

Previous topic

Getting Started

Next topic

OpenCL for Python API

This Page