The functionality in this module provides something of a workalike for numpy arrays, but with all operations executed on the CL compute device.
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.
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 preinitialized vector type you have three new functions to choose from:
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.
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:
Get or register a numpy.dtype associated with the C type names in the string list c_names. If dtype is None, no registration is performed, and the numpy.dtype must already have been registered. If so, it is returned. If not, TypeNameNotKnown is raised.
If dtype is not None, registration is attempted. If the c_names are already known and registered to identical numpy.dtype objects, then the previously dtype object of the previously registered type is returned. If the c_names are not yet known, the type is registered. If one of the c_names is known but registered to a different type, an error is raised. In this latter case, the type may end up partially registered and any further behavior is undefined.
New in version 2012.2.
New in version 2013.1.
Changed in version 2013.1: This function has been deprecated. It is recommended that you develop against the new interface, get_or_register_dtype().
Returns a C name registered for dtype.
This function helps with producing C/OpenCL declarations for structured numpy.dtype instances:
Return a tuple (dtype, c_decl) such that the C struct declaration in c_decl and the structure numpy.dtype instance dtype have the same memory layout.
Note that dtype may be modified from the value that was passed in, for example to insert padding.
(As a remark on implementation, this routine runs a small kernel on the given device to ensure that numpy and C offsets and sizes match.)
This example explains the use of this function:
>>> import numpy as np
>>> import pyopencl as cl
>>> import pyopencl.tools
>>> ctx = cl.create_some_context()
>>> dtype = np.dtype([("id", np.uint32), ("value", np.float32)])
>>> dtype, c_decl = pyopencl.tools.match_dtype_to_c_struct(
... ctx.devices[0], 'id_val', dtype)
>>> print c_decl
typedef struct {
unsigned id;
float value;
} id_val;
>>> print dtype
[('id', '<u4'), ('value', '<f4')]
>>> cl.tools.get_or_register_dtype('id_val', dtype)
As this example shows, it is important to call get_or_register_dtype() on the modified dtype returned by this function, not the original one.
A more complete example of how to use custom structured types can be found in examples/demostructreduce.py in the PyOpenCL distribution.
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 <pyopenclcomplex.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 simply float2 and double2.
Warning
Note that, at the OpenCL source code level, addition (real + complex) and multiplication (complex*complex) are defined for e.g. float2, but yield wrong results, so that you need to use the corresponding functions. (The Array type implements complex arithmetic as you remember it, without any idiotic quirks like this.)
New in version 2012.1.
A numpy.ndarray workalike that stores its data and performs its computations on the compute device. shape and dtype work exactly as in numpy. Arithmetic methods in Array support the broadcasting of scalars. (e.g. array+5)
cqa must be a pyopencl.CommandQueue or a pyopencl.Context.
If it is a queue, cqa specifies the queue in which the array carries out its computations by default. If a default queue (and thereby overloaded operators and many other niceties) are not desired, pass a Context.
cqa will at some point be renamed cq, so it should be considered ‘positionalonly’. Arguments starting from ‘order’ should be considered keywordonly.
allocator may be None or a callable that, upon being called with an argument of the number of bytes to be allocated, returns an pyopencl.Buffer object. (A pyopencl.tools.MemoryPool instance is one useful example of an object to pass here.)
Changed in version 2011.1: Renamed context to cqa, made it generalpurpose.
All arguments beyond order should be considered keywordonly.
The pyopencl.MemoryObject instance created for the memory that backs this Array.
Changed in version 2013.1: If a nonzero offset has been specified for this array, this will fail with ArrayHasOffsetError.
The pyopencl.MemoryObject instance created for the memory that backs this Array. Unlike data, the base address of base_data is allowed to be different from the beginning of the array. The actual beginning is the base address of base_data plus offset in units of dtype.
Unlike data, retrieving base_data always succeeds.
New in version 2013.1.
The tuple of lengths of each dimension in the array.
The numpy.dtype of the items in the GPU array.
The number of meaningful entries in the array. Can also be computed by multiplying up the numbers in shape.
Tuple of bytes to step in each dimension when traversing an array.
Return an object with attributes c_contiguous, f_contiguous and forc, which may be used to query contiguity properties in analogy to numpy.ndarray.flags.
Methods
Return a copy of self with the default queue set to queue.
None is allowed as a value for queue.
New in version 2013.1.
Returns the size of the leading dimension of self.
Returns an array containing the same data with a new shape.
Returns flattened array containing the same data.
Returns view of array with the same data. If dtype is different from current dtype, the actual bytes of memory will be reinterpreted.
Transfer the contents the numpy.ndarray object ary onto the device.
ary must have the same dtype and size (not necessarily shape) as self.
Transfer the contents of self into ary or a newly allocated numpy.ndarray. If ary is given, it must have the right size (not necessarily shape) and dtype.
New in version 2013.1.
Return selffac * self + otherfac*other.
Add an array with an array or an array with a scalar.
Substract an array from an array or a scalar from an array.
Divides an array by an array or a scalar, i.e. self / other.
Divides an array by a scalar or an array, i.e. other / self.
Return a Array of the absolute values of the elements of self.
Fill the array with scalar.
Returns:  self. 

Return a copy of self, cast to dtype.
New in version 2012.1.
New in version 2012.1.
New in version 2012.1.
New in version 2013.1.
Set the slice of self identified subscript to value.
value is allowed to be:
Nonscalar broadcasting is not currently supported.
New in version 2013.1.
Like __setitem__(), but with the ability to specify a queue and wait_for.
New in version 2013.1.
Changed in version 2013.2: Added wait_for.
If is_blocking, return a numpy.ndarray corresponding to the same memory as self.
If is_blocking is not true, return a tuple (ary, evt), where ary is the abovementioned array.
The host array is obtained using pyopencl.enqueue_map_buffer(). See there for further details.
Parameters:  flags – A combination of pyopencl.map_flags. Defaults to readwrite. 

New in version 2013.2.
Comparisons, conditionals, any, all
New in version 2013.2.
Boolean arrays are stored as numpy.int8 because bool has an unspecified size in the OpenCL spec.
Only works for device scalars. (i.e. “arrays” with shape == ().)
Event management
If an array is used from within an outoforder queue, it needs to take care of its own operation ordering. The facilities in this section make this possible.
New in version 2014.1.1.
A list of pyopencl.Event instances that the current content of this array depends on. User code may read, but should never modify this list directly. To update this list, instead use the following methods.
New in version 2013.1.
Return a Array that is an exact copy of the numpy.ndarray instance ary.
See Array for the meaning of allocator.
Changed in version 2011.1: context argument was deprecated.
A synonym for the Array constructor.
Same as empty(), but the Array is zeroinitialized before being returned.
Changed in version 2011.1: context argument was deprecated.
Make a new, uninitialized Array having the same properties as other_ary.
Make a new, zeroinitialized Array having the same properties as other_ary.
Create a Array filled with numbers spaced step apart, starting from start and ending at stop.
For floating point arguments, the length of the result is ceil((stop  start)/step). This rule may result in the last element of the result being greater than stop.
dtype, if not specified, is taken as the largest common type of start, stop and step.
Changed in version 2011.1: context argument was deprecated.
Changed in version 2011.2: allocator keyword argument was added.
Return the Array [a[indices[0]], ..., a[indices[n]]]. For the moment, a must be a type that can be bound to a texture.
New in version 2013.1.
Return an array like then_, which, for the element at index i, contains then_[i] if criterion[i]>0, else else_[i].
Return the elementwise maximum of a and b.
Return the elementwise minimum of a and b.
New in version 2011.1.
New in version 2011.1.
Like numpy.vdot().
New in version 2013.1.
New in version 2011.1.
New in version 2011.1.
New in version 2011.1.
New in version 2011.1.
New in version 2011.1.
See also Sums and counts (“reduce”).
The pyopencl.clmath module contains exposes array versions of the C functions available in the OpenCL standard. (See table 6.8 in the spec.)
New in version 2013.1.
New in version 2013.1.
Return the floating point remainder of the division arg/mod, for each element in arg and mod.
Return a tuple (significands, exponents) such that arg == significand * 2**exponent.
Return a new array of floating point values composed from the entries of significand and exponent, paired together as result = significand * 2**exponent.
Return a tuple (fracpart, intpart) of arrays containing the integer and fractional parts of arg.
PyOpenCL now includes and uses the RANLUXCL random number generator by Ivar Ursin Nikolaisen. In addition to being usable through the convenience functions above, it is available in any piece of code compiled through PyOpenCL by:
#include <pyopenclranluxcl.cl>
See the source for some documentation if you’re planning on using RANLUXCL directly.
The RANLUX generator is described in the following two articles. If you use the generator for scientific purposes, please consider citing them:
New in version 2011.2.
A pyopencl.array.Array containing the state of the generator.
nskip is an integer which can (optionally) be defined in the kernel code as RANLUXCL_NSKIP. If this is done the generator will be faster for luxury setting 0 and 1, or when the pvalue is manually set to a multiple of 24.
Parameters: 


Changed in version 2013.1: Added default value for num_work_items.
Fill ary with uniformly distributed random numbers in the interval (a, b), endpoints excluded.
Returns:  a pyopencl.Event 

Changed in version 2014.1.1: Added return value.
Make a new empty array, apply fill_uniform() to it.
Fill ary with normally distributed numbers with mean mu and standard deviation sigma.
Changed in version 2014.1.1: Added return value.
Make a new empty array, apply fill_normal() to it.
The generator gets inefficient when different work items invoke the generator a differing number of times. This function ensures efficiency.
Return an array of shape filled with random values of dtype in the range [a,b).
Fill result with random values of dtype in the range [0,1).