Multi-dimensional arrays

The functionality in this module provides something of a work-alike for numpy arrays, but with all operations executed on the CL compute device.

Data Types

PyOpenCL provides some amount of integration between the numpy type system, as represented by numpy.dtype, and the types available in OpenCL. All the simple scalar types map straightforwardly to their CL counterparts.

Vector Types

class pyopencl.array.vec

All of OpenCL’s supported vector types, such as float3 and long4 are available as numpy data types within this class. These numpy.dtype instances have field names of x, y, z, and w just like their OpenCL counterparts. They will work both for parameter passing to kernels as well as for passing data back and forth between kernels and Python code. For each type, a make_type function is also provided (e.g. make_float3(x,y,z)).

If you want to construct a pre-initialized vector type you have three new functions to choose from:

  • zeros_type()

  • ones_type()

  • filled_type(fill_value)

New in version 2014.1.

Changed in version 2014.1: The make_type functions have a default value (0) for each component. Relying on the default values has been deprecated. Either specify all components or use one of th new flavors mentioned above for constructing a vector.

Custom data types

If you would like to use your own (struct/union/whatever) data types in array operations where you supply operation source code, define those types in the preamble passed to pyopencl.elementwise.ElementwiseKernel, pyopencl.reduction.ReductionKernel (or similar), and let PyOpenCL know about them using this function:

exception pyopencl.tools.TypeNameNotKnown

New in version 2013.1.

pyopencl.tools.register_dtype(dtype, name)

Changed in version 2013.1: This function has been deprecated. It is recommended that you develop against the new interface, get_or_register_dtype().

pyopencl.tools.dtype_to_ctype(dtype)

Returns a C name registered for dtype.

This function helps with producing C/OpenCL declarations for structured numpy.dtype instances:

A more complete example of how to use custom structured types can be found in examples/demo-struct-reduce.py in the PyOpenCL distribution.

Complex Numbers

PyOpenCL’s Array type supports complex numbers out of the box, by simply using the corresponding numpy types.

If you would like to use this support in your own kernels, here’s how to proceed: Since OpenCL 1.2 (and earlier) do not specify native complex number support, PyOpenCL works around that deficiency. By saying:

#include <pyopencl-complex.h>

in your kernel, you get complex types cfloat_t and cdouble_t, along with functions defined on them such as cfloat_mul(a, b) or cdouble_log(z). Elementwise kernels automatically include the header if your kernel has complex input or output. See the source file for a precise list of what’s available.

If you need double precision support, please:

#define PYOPENCL_DEFINE_CDOUBLE

before including the header, as DP support apparently cannot be reliably autodetected.

Under the hood, the complex types are struct types as defined in the header. Ideally, you should only access the structs through the provided functions, never directly.

New in version 2012.1.

Changed in version 2015.2: [INCOMPATIBLE] Changed PyOpenCL’s complex numbers from float2 and double2 OpenCL vector types to custom struct. This was changed because it very easily introduced bugs where

  • complex*complex

  • real+complex

look like they may do the right thing, but silently do the wrong thing.

The Array Class

Constructing Array Instances

pyopencl.array.empty(queue, shape, dtype, order='C', allocator=None, data=None)

A synonym for the Array constructor.

Manipulating Array instances

Conditionals

Reductions

See also Sums and counts (“reduce”).

Elementwise Functions on Array Instances

The pyopencl.clmath module contains exposes array versions of the C functions available in the OpenCL standard. (See table 6.8 in the spec.)

pyopencl.clmath.acos(array, queue=None)
pyopencl.clmath.acosh(array, queue=None)
pyopencl.clmath.acospi(array, queue=None)
pyopencl.clmath.asin(array, queue=None)
pyopencl.clmath.asinh(array, queue=None)
pyopencl.clmath.asinpi(array, queue=None)
pyopencl.clmath.atan(array, queue=None)
pyopencl.clmath.atanh(array, queue=None)
pyopencl.clmath.atanpi(array, queue=None)
pyopencl.clmath.cbrt(array, queue=None)
pyopencl.clmath.ceil(array, queue=None)
pyopencl.clmath.cos(array, queue=None)
pyopencl.clmath.cosh(array, queue=None)
pyopencl.clmath.cospi(array, queue=None)
pyopencl.clmath.erfc(array, queue=None)
pyopencl.clmath.erf(array, queue=None)
pyopencl.clmath.exp(array, queue=None)
pyopencl.clmath.exp2(array, queue=None)
pyopencl.clmath.exp10(array, queue=None)
pyopencl.clmath.expm1(array, queue=None)
pyopencl.clmath.fabs(array, queue=None)
pyopencl.clmath.floor(array, queue=None)
pyopencl.clmath.fmod(arg, mod, queue=None)

Return the floating point remainder of the division arg/mod, for each element in arg and mod.

pyopencl.clmath.frexp(arg, queue=None)

Return a tuple (significands, exponents) such that arg == significand * 2**exponent.

pyopencl.clmath.ilogb(array, queue=None)
pyopencl.clmath.ldexp(significand, exponent, queue=None)

Return a new array of floating point values composed from the entries of significand and exponent, paired together as result = significand * 2**exponent.

pyopencl.clmath.lgamma(array, queue=None)
pyopencl.clmath.log(array, queue=None)
pyopencl.clmath.log2(array, queue=None)
pyopencl.clmath.log10(array, queue=None)
pyopencl.clmath.log1p(array, queue=None)
pyopencl.clmath.logb(array, queue=None)
pyopencl.clmath.modf(arg, queue=None)

Return a tuple (fracpart, intpart) of arrays containing the integer and fractional parts of arg.

pyopencl.clmath.nan(array, queue=None)
pyopencl.clmath.rint(array, queue=None)
pyopencl.clmath.round(array, queue=None)
pyopencl.clmath.sin(array, queue=None)
pyopencl.clmath.sinh(array, queue=None)
pyopencl.clmath.sinpi(array, queue=None)
pyopencl.clmath.sqrt(array, queue=None)
pyopencl.clmath.tan(array, queue=None)
pyopencl.clmath.tanh(array, queue=None)
pyopencl.clmath.tanpi(array, queue=None)
pyopencl.clmath.tgamma(array, queue=None)
pyopencl.clmath.trunc(array, queue=None)

Generating Arrays of Random Numbers