An Allocator that uses pyopencl.Buffer with the given flags.
A numpy.ndarray work-alike 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) If the
allocator is a callable that, upon being called with an argument of the number of bytes to be allocated, returns an object that can be cast to an int representing the address of the newly allocated memory. (See DefaultAllocator.)
The pyopencl.MemoryObject instance created for the memory that backs this Array.
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.
The total number of entries, including padding, that are present in the array.
Returns the size of the leading dimension of self.
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.
Return selffac*self + otherfac*other.
Fill the array with scalar.
Return self, cast to dtype.
Return a Array that is an exact copy of the numpy.ndarray instance ary.
See Array for the meaning of allocator.
A synonym for the Array constructor.
Same as empty(), but the Array is zero-initialized before being returned.
Make a new, uninitialized Array having the same properties as other_ary.
Make a new, zero-initialized 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.
Return the Array [a[indices[0]], ..., a[indices[n]]]. For the moment, a must be a type that can be bound to a texture.
Return an array like then_, which, for the element at index i, contains then_[i] if criterion[i]>0, else else_[i]. (added in 0.94)
Return the elementwise maximum of a and b. (added in 0.94)
Return the elementwise minimum of a and b. (added in 0.94)
See also Custom Reductions.
The pyopencl.clmath module contains exposes array versions of the C functions available in the OpenCL standard. (See table 6.8 in the spec.)
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.
Return an array of shape filled with random values of dtype in the range [0,1).
Evaluating involved expressions on pyopencl.array.Array instances can be somewhat inefficient, because a new temporary is created for each intermediate result. The functionality in the module pyopencl.elementwise contains tools to help generate kernels that evaluate multi-stage expressions on one or several operands in a single pass.
Generate a kernel that takes a number of scalar or vector arguments and performs the scalar operation on each entry of its arguments, if that argument is a vector.
arguments is specified as a string formatted as a C argument list. operation is specified as a C assignment statement, without a semicolon. Vectors in operation should be indexed by the variable i.
name specifies the name as which the kernel is compiled, and options are passed unmodified to pyopencl.Program.build().
preamble is a piece of C source code that gets inserted outside of the function context in the elementwise operation’s kernel source code.
Invoke the generated scalar kernel. The arguments may either be scalars or GPUArray instances.
Here’s a usage example:
import pyopencl as cl
import pyopencl.array as cl_array
import numpy
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
n = 10
a_gpu = cl_array.to_device(
ctx, queue, numpy.random.randn(n).astype(numpy.float32))
b_gpu = cl_array.to_device(
ctx, queue, numpy.random.randn(n).astype(numpy.float32))
from pyopencl.elementwise import ElementwiseKernel
lin_comb = ElementwiseKernel(ctx,
"float a, float *x, "
"float b, float *y, "
"float *z",
"z[i] = a*x[i] + b*y[i]",
"linear_combination")
c_gpu = cl_array.empty_like(a_gpu)
lin_comb(5, a_gpu, 6, b_gpu, c_gpu)
import numpy.linalg as la
assert la.norm((c_gpu - (5*a_gpu+6*b_gpu)).get()) < 1e-5
(You can find this example as examples/demo_elementwise.py in the PyOpenCL distribution.)
Generate a kernel that takes a number of scalar or vector arguments (at least one vector argument), performs the map_expr on each entry of the vector argument and then the reduce_expr on the outcome of that. neutral serves as an initial value. preamble offers the possibility to add preprocessor directives and other code (such as helper functions) to be added before the actual reduction kernel code.
Vectors in map_expr should be indexed by the variable i. reduce_expr uses the formal values “a” and “b” to indicate two operands of a binary reduction operation. If you do not specify a map_expr, “in[i]” – and therefore the presence of only one input argument – is automatically assumed.
dtype_out specifies the numpy.dtype in which the reduction is performed and in which the result is returned. neutral is specified as float or integer formatted as string. reduce_expr and map_expr are specified as string formatted operations and arguments is specified as a string formatted as a C argument list. name specifies the name as which the kernel is compiled. options are passed unmodified to pyopencl.Program.build(). preamble is specified as a string of code.
Here’s a usage example:
a = pyopencl.array.arange(400, dtype=numpy.float32)
b = pyopencl.array.arange(400, dtype=numpy.float32)
krnl = ReductionKernel(ctx, numpy.float32, neutral="0",
reduce_expr="a+b", map_expr="a[i]*b[i]",
arguments="__global float *a, __global float *b")
my_dot_prod = krnl(a, b).get()