Built-in Utilities

.. module:: pyopencl.tools

.. _memory-pools:

Memory Pools

The constructor :func:pyopencl.Buffer can consume a fairly large amount of processing time if it is invoked very frequently. For example, code based on :class:pyopencl.array.Array can easily run into this issue because a fresh memory area is allocated for each intermediate result. Memory pools are a remedy for this problem based on the observation that often many of the block allocations are of the same sizes as previously used ones.

Then, instead of fully returning the memory to the system and incurring the associated reallocation overhead, the pool holds on to the memory and uses it to satisfy future allocations of similarly-sized blocks. The pool reacts appropriately to out-of-memory conditions as long as all memory allocations are made through it. Allocations performed from outside of the pool may run into spurious out-of-memory conditions due to the pool owning much or all of the available memory.

Using :class:pyopencl.array.Array instances with a :class:MemoryPool is not complicated::

  1. mem_pool = pyopencl.tools.MemoryPool(pyopencl.tools.ImmediateAllocator(queue))
  2. a_dev = cl_array.arange(queue, 2000, dtype=np.float32, allocator=mem_pool)

.. class:: PooledBuffer

  1. An object representing a :class:`MemoryPool`-based allocation of
  2. device memory. Once this object is deleted, its associated device
  3. memory is returned to the pool. This supports the same interface
  4. as :class:`pyopencl.Buffer`.

.. class:: DeferredAllocator(context, mem_flags=pyopencl.mem_flags.READ_WRITE)

  1. *mem_flags* takes its values from :class:`pyopencl.mem_flags` and corresponds
  2. to the *flags* argument of :class:`pyopencl.Buffer`. DeferredAllocator
  3. has the same semantics as regular OpenCL buffer allocation, i.e. it may
  4. promise memory to be available that may (in any call to a buffer-using
  5. CL function) turn out to not exist later on. (Allocations in CL are
  6. bound to contexts, not devices, and memory availability depends on which
  7. device the buffer is used with.)
  8. .. versionchanged::
  9. In version 2013.1, :class:`CLAllocator` was deprecated and replaced
  10. by :class:`DeferredAllocator`.
  11. .. method:: __call__(size)
  12. Allocate a :class:`pyopencl.Buffer` of the given *size*.

.. class:: ImmediateAllocator(queue, mem_flags=pyopencl.mem_flags.READ_WRITE)

  1. *mem_flags* takes its values from :class:`pyopencl.mem_flags` and corresponds
  2. to the *flags* argument of :class:`pyopencl.Buffer`.
  3. :class:`ImmediateAllocator` will attempt to ensure at allocation time that
  4. allocated memory is actually available. If no memory is available, an out-of-memory
  5. error is reported at allocation time.
  6. .. versionadded:: 2013.1
  7. .. method:: __call__(size)
  8. Allocate a :class:`pyopencl.Buffer` of the given *size*.

.. class:: MemoryPool(allocator)

  1. A memory pool for OpenCL device memory. *allocator* must be an instance of
  2. one of the above classes, and should be an :class:`ImmediateAllocator`.
  3. The memory pool assumes that allocation failures are reported
  4. by the allocator immediately, and not in the OpenCL-typical
  5. deferred manner.
  6. .. attribute:: held_blocks
  7. The number of unused blocks being held by this pool.
  8. .. attribute:: active_blocks
  9. The number of blocks in active use that have been allocated
  10. through this pool.
  11. .. method:: allocate(size)
  12. Return a :class:`PooledBuffer` of the given *size*.
  13. .. method:: __call__(size)
  14. Synonym for :meth:`allocate` to match :class:`CLAllocator` interface.
  15. .. versionadded: 2011.2
  16. .. method:: free_held
  17. Free all unused memory that the pool is currently holding.
  18. .. method:: stop_holding
  19. Instruct the memory to start immediately freeing memory returned
  20. to it, instead of holding it for future allocations.
  21. Implicitly calls :meth:`free_held`.
  22. This is useful as a cleanup action when a memory pool falls out
  23. of use.

CL-Object-dependent Caching

.. autofunction:: first_arg_dependent_memoize .. autofunction:: clear_first_arg_caches

Testing

.. function:: pytest_generate_tests_for_pyopencl(metafunc)

  1. Using the line::
  2. from pyopencl.tools import pytest_generate_tests_for_pyopencl \
  3. as pytest_generate_tests
  4. in your `pytest <http://pytest.org>`_ test scripts allows you to use the
  5. arguments *ctx_factory*, *device*, or *platform* in your test functions,
  6. and they will automatically be run for each OpenCL device/platform in the
  7. system, as appropriate.
  8. The following two environment variables are also supported to control
  9. device/platform choice::
  10. PYOPENCL_TEST=0:0,1;intel=i5,i7

Device Characterization

.. automodule:: pyopencl.characterize :members: