OpenCL Type Mapping#
For ease of use, a the cltypes module provides convenient mapping from OpenCL type names to their equivalent numpy types. This saves you from referring back to the OpenCL spec to see that a cl_long is 64 bit unsigned integer. Use the module as follows:
>>> import numpy as np >>> import pyopencl as cl >>> import pyopencl.cltypes >>> cl_uint = cl.cltypes.uint(42) # maps to numpy.uint32 >>> cl_long = cl.cltypes.long(1235) # maps to numpy.int64 >>> floats = np.empty((128,), dtype=cl.cltypes.float) # array of numpy.float32
The OpenCL type
bool does not have a corresponding
numpy type defined here,
because OpenCL does not specify the in-memory representation (or even the storage
size) for this type.
The corresponding vector types are also made available in the same package, allowing you to easily create numpy arrays with the appropriate memory layout.
>>> import numpy as np >>> array_of_float16 = np.empty((128,), dtype=cl.cltypes.float16) # array of float16