OpenCL Runtime: 内存(Memory)

原作者: Andreas Kloeckner inform@tiker.net

原文地址

翻译:CycleUser


.. currentmodule:: pyopencl

.. class:: MemoryObject

  1. .. attribute:: info
  2. Lower case versions of the :class:`mem_info` constants
  3. may be used as attributes on instances of this class
  4. to directly query info attributes.
  5. .. attribute:: hostbuf
  6. .. method:: get_info(param)
  7. See :class:`mem_info` for values of *param*.
  8. .. method:: release()
  9. .. method:: get_host_array(shape, dtype, order="C")
  10. Return the memory object's associated host memory
  11. area as a :class:`numpy.ndarray` of the given *shape*,
  12. *dtype* and *order*.
  13. .. automethod:: from_int_ptr
  14. .. autoattribute:: int_ptr
  15. |comparable|

Memory Migration

.. function:: enqueue_migrate_mem_objects(queue, mem_objects, flags=0, wait_for=None)

  1. :param flags: from :class:`mem_migration_flags`
  2. .. versionadded:: 2011.2
  3. Only available with CL 1.2.

Buffer

.. class:: Buffer(context, flags, size=0, hostbuf=None)

  1. Create a :class:`Buffer`.
  2. See :class:`mem_flags` for values of *flags*.
  3. If *hostbuf* is specified, *size* defaults to the size of
  4. the specified buffer if it is passed as zero.
  5. :class:`Buffer` inherits from :class:`MemoryObject`.
  6. .. note::
  7. Python also defines a type of `buffer object
  8. <https://docs.python.org/3.4/c-api/buffer.html>`_,
  9. and PyOpenCL interacts with those, too, as the host-side
  10. target of :func:`enqueue_copy`. Make sure to always be
  11. clear on whether a :class:`Buffer` or a Python buffer
  12. object is needed.
  13. Note that actual memory allocation in OpenCL may be deferred.
  14. Buffers are attached to a :class:`Context` and are only
  15. moved to a device once the buffer is used on that device.
  16. That is also the point when out-of-memory errors will occur.
  17. If you'd like to be sure that there's enough memory for
  18. your allocation, either use :func:`enqueue_migrate_mem_objects`
  19. (if available) or simply perform a small transfer to the
  20. buffer. See also :class:`pyopencl.tools.ImmediateAllocator`.
  21. .. method:: get_sub_region(origin, size, flags=0)
  22. Only available in OpenCL 1.1 and newer.
  23. .. method:: __getitem__(slc)
  24. *slc* is a :class:`slice` object indicating from which byte index range
  25. a sub-buffer is to be created. The *flags* argument of
  26. :meth:`get_sub_region` is set to the same flags with which *self* was
  27. created.

.. function:: enqueue_fill_buffer(queue, mem, pattern, offset, size, wait_for=None)

  1. :arg mem: the on device :class:`Buffer`
  2. :arg pattern: a buffer object (likely a :class:`numpy.ndarray`, eg. `np.uint32(0)`)
  3. The memory associated with *pattern* can be reused or freed once the function
  4. completes.
  5. :arg size: The size in bytes of the region to be filled. Must be a multiple of the
  6. size of the pattern.
  7. :arg offset: The location in bytes of the region being filled in *mem*.
  8. Must be a multiple of the size of the pattern.
  9. Fills a buffer with the provided pattern
  10. |std-enqueue-blurb|
  11. Only available with CL 1.2.
  12. .. versionadded:: 2011.2

.. _svm:

Shared Virtual Memory (SVM)

Shared virtual memory allows the host and the compute device to share address space, so that pointers on the host and on the device may have the same meaning. In addition, it allows the same memory to be accessed by both the host and the device. Coarse-grain SVM requires that buffers be mapped before being accessed on the host, fine-grain SVM does away with that requirement.

SVM requires OpenCL 2.0.

.. autoclass:: SVM

.. autoclass:: SVMMap

Allocating SVM ^^^^^^^^^^^^^^

.. autofunction:: svm_empty .. autofunction:: svm_empty_like .. autofunction:: csvm_empty .. autofunction:: csvm_empty_like .. autofunction:: fsvm_empty .. autofunction:: fsvm_empty_like

Operations on SVM ^^^^^^^^^^^^^^^^^

(See also :ref:mem-transfer.)

.. autofunction:: enqueue_svm_memfill .. autofunction:: enqueue_svm_migratemem

SVM Allocation Holder ^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: SVMAllocation

Image

.. class:: ImageFormat([channel_order, channel_type])

  1. .. attribute:: channel_order
  2. See :class:`channel_order` for possible values.
  3. .. attribute:: channel_data_type
  4. See :class:`channel_type` for possible values.
  5. .. attribute:: channel_count
  6. .. versionadded:: 0.91.5
  7. .. attribute:: dtype_size
  8. .. versionadded:: 0.91.5
  9. .. attribute:: itemsize
  10. .. versionadded:: 0.91.5
  11. .. method:: __repr__
  12. Returns a :class:`str` representation of the image format.
  13. .. versionadded:: 0.91
  14. |comparable|
  15. .. versionchanged:: 0.91
  16. Constructor arguments added.
  17. .. versionchanged:: 2013.2
  18. :class:`ImageFormat` was made comparable and hashable

.. function:: get_supported_image_formats(context, flags, image_type)

  1. See :class:`mem_flags` for possible values of *flags*
  2. and :class:`mem_object_type` for possible values of *image_type*.

.. class:: Image(context, flags, format, shape=None, pitches=None, hostbuf=None, is_array=False, buffer=None):

  1. See :class:`mem_flags` for values of *flags*.
  2. *shape* is a 2- or 3-tuple. *format* is an instance of :class:`ImageFormat`.
  3. *pitches* is a 1-tuple for 2D images and a 2-tuple for 3D images, indicating
  4. the distance in bytes from one scan line to the next, and from one 2D image
  5. slice to the next.
  6. If *hostbuf* is given and *shape* is `None`, then *hostbuf.shape* is
  7. used as the *shape* parameter.
  8. :class:`Image` inherits from :class:`MemoryObject`.
  9. .. note::
  10. If you want to load images from :mod:`numpy.ndarray` instances or read images
  11. back into them, be aware that OpenCL images expect the *x* dimension to vary
  12. fastest, whereas in the default (C) order of :mod:`numpy` arrays, the last index
  13. varies fastest. If your array is arranged in the wrong order in memory,
  14. there are two possible fixes for this:
  15. * Convert the array to Fortran (column-major) order using :func:`numpy.asarray`.
  16. * Pass *ary.T.copy()* to the image creation function.
  17. .. versionadded:: 0.91
  18. .. versionchanged:: 2011.2
  19. Added *is_array* and *buffer*, which are only available on CL 1.2 and newer.
  20. .. attribute:: info
  21. Lower case versions of the :class:`mem_info`
  22. and :class:`image_info` constants
  23. may be used as attributes on instances of this class
  24. to directly query info attributes.
  25. .. attribute:: shape
  26. Return the value of the *shape* constructor argument as a :class:`tuple`.
  27. .. method:: get_image_info(param)
  28. See :class:`image_info` for values of *param*.
  29. .. method:: release()
  30. |comparable|

.. function:: image_from_array(ctx, ary, num_channels=None, mode=”r”, norm_int=False)

  1. Build a 2D or 3D :class:`Image` from the :class:`numpy.ndarray` *ary*. If
  2. *num_channels* is greater than one, the last dimension of *ary* must be
  3. identical to *num_channels*. *ary* must be in C order. If *num_channels* is
  4. not given, it defaults to 1 for scalar types and the number of entries
  5. for :ref:`vector-types`.
  6. The :class:`ImageFormat` is chosen as the first *num_channels* components
  7. of "RGBA".
  8. :param mode: "r" or "w" for read/write
  9. .. note::
  10. When reading from the image object, the indices passed to `read_imagef` are
  11. in the reverse order from what they would be when accessing *ary* from
  12. Python.
  13. If *norm_int* is `True`, then the integer values are normalized to a floating
  14. point scale of 0..1 when read.
  15. .. versionadded:: 2011.2

.. function:: enqueue_fill_image(queue, mem, color, origin, region, wait_for=None)

  1. :arg color: a buffer object (likely a :class:`numpy.ndarray`)
  2. |std-enqueue-blurb|
  3. Only available with CL 1.2.
  4. .. versionadded:: 2011.2

.. _mem-transfer:

Transfers

.. autofunction:: enqueue_copy(queue, dest, src, **kwargs)

Mapping Memory into Host Address Space

.. autoclass:: MemoryMap

.. function:: enqueue_map_buffer(queue, buf, flags, offset, shape, dtype, order=”C”, strides=None, wait_for=None, is_blocking=True)

  1. |explain-waitfor|
  2. *shape*, *dtype*, and *order* have the same meaning
  3. as in :func:`numpy.empty`.
  4. See :class:`map_flags` for possible values of *flags*.
  5. *strides*, if given, overrides *order*.
  6. :return: a tuple *(array, event)*. *array* is a
  7. :class:`numpy.ndarray` representing the host side
  8. of the map. Its *.base* member contains a
  9. :class:`MemoryMap`.
  10. .. versionchanged:: 2011.1
  11. *is_blocking* now defaults to True.
  12. .. versionchanged:: 2013.1
  13. *order* now defaults to "C".
  14. .. versionchanged:: 2013.2
  15. Added *strides* argument.
  16. Sample usage::
  17. mapped_buf = cl.enqueue_map_buffer(queue, buf, ...)
  18. with mapped_buf.base:
  19. # work with mapped_buf
  20. ...
  21. # memory will be unmapped here

.. function:: enqueue_map_image(queue, buf, flags, origin, region, shape, dtype, order=”C”, strides=None, wait_for=None, is_blocking=True)

  1. |explain-waitfor|
  2. *shape*, *dtype*, and *order* have the same meaning
  3. as in :func:`numpy.empty`.
  4. See :class:`map_flags` for possible values of *flags*.
  5. *strides*, if given, overrides *order*.
  6. :return: a tuple *(array, event)*. *array* is a
  7. :class:`numpy.ndarray` representing the host side
  8. of the map. Its *.base* member contains a
  9. :class:`MemoryMap`.
  10. .. versionchanged:: 2011.1
  11. *is_blocking* now defaults to True.
  12. .. versionchanged:: 2013.1
  13. *order* now defaults to "C".
  14. .. versionchanged:: 2013.2
  15. Added *strides* argument.

Samplers

.. class:: Sampler

  1. .. method:: __init__(context, normalized_coords, addressing_mode, filter_mode)
  2. *normalized_coords* is a :class:`bool` indicating whether
  3. to use coordinates between 0 and 1 (*True*) or the texture's
  4. natural pixel size (*False*).
  5. See :class:`addressing_mode` and :class:`filter_mode` for possible
  6. argument values.
  7. .. method:: __init__(context, properties)
  8. :arg properties: a sequence
  9. of keys and values from :class:`sampler_properties` as accepted
  10. by :c:func:`clCreateSamplerWithProperties` (see the OpenCL
  11. spec for details). The trailing *0* is added automatically
  12. and does not need to be included.
  13. Requires OpenCL 2 or newer.
  14. .. versionadded:: 2018.2
  15. .. attribute:: info
  16. Lower case versions of the :class:`sampler_info` constants
  17. may be used as attributes on instances of this class
  18. to directly query info attributes.
  19. .. method:: get_info(param)
  20. See :class:`sampler_info` for values of *param*.
  21. .. automethod:: from_int_ptr
  22. .. autoattribute:: int_ptr
  23. |comparable|