Skip to content
Snippets Groups Projects
array.rst 26.83 KiB

Multi-dimensional arrays on the Compute Device

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

Data Types

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

Vector Types

All of OpenCL's supported vector types, such as float3 and long4 are available as :mod:`numpy` data types within this class. These :class:`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)).

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 :class:`pyopencl.elementwise.ElementwiseKernel`, :class:`pyopencl.reduction.ReductionKernel` (or similar), and let PyOpenCL know about them using this function:

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

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

Complex Numbers

PyOpenCL's :class:`Array` type supports complex numbers out of the box, by simply using the corresponding :mod:`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: