Skip to content
Snippets Groups Projects
types.rst 1.22 KiB
Newer Older
OpenCL Type Mapping
===================

.. module:: pyopencl.cltypes
For ease of use, a the :mod:`pyopencl.cltypes` module provides convenient mapping
from OpenCL type names to their equivalent :mod:`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::

    >>> 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 :mod:`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 :mod:`numpy` arrays with the appropriate memory
layout.

.. doctest::

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