OpenCL Type Mapping

.. module:: pyopencl.types

.. _type-mappings:

Scalar Types

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:

.. doctest::

  1. >>> import numpy as np
  2. >>> import pyopencl as cl
  3. >>> import pyopencl.cltypes
  4. >>> cl_uint = cl.cltypes.uint(42) # maps to numpy.uint32
  5. >>> cl_long = cl.cltypes.long(1235) # maps to numpy.int64
  6. >>> floats = np.empty((128,), dtype=cl.cltypes.float) # array of numpy.float32

Vector Types

The corresponding vector types are also made available in the same package, allowing you to easily create numpy arrays with the appropriate memory layout.

.. doctest::

  1. >>> import numpy as np
  2. >>> array_of_float16 = np.empty((128,), dtype=cl.cltypes.float16) # array of float16