-
Andreas Klöckner authoredAndreas Klöckner authored
Multi-dimensional arrays
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)).
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)
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:
#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.
The :class:`Array` Class
Constructing :class:`Array` Instances
Manipulating :class:`Array` instances
Conditionals
Reductions
See also :ref:`custom-reductions`.
Elementwise Functions on :class:`Arrray` Instances
The :mod:`pyopencl.clmath` module contains exposes array versions of the C functions available in the OpenCL standard. (See table 6.8 in the spec.)