Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1e713b8
Add a caching memory pool for dpctl.memory allocations
ndgrigorian Nov 2, 2026
6062965
Add C API for caching memory pool
ndgrigorian Nov 2, 2026
9000cd2
Use memory pool in MKL example
ndgrigorian Nov 2, 2026
dfb60e6
Implement splitting of allocations in memory pool
ndgrigorian Nov 2, 2026
4247e4c
memory pool changelog
ndgrigorian Nov 2, 2026
0c01ad4
wait for held objects' work when an allocation fails
ndgrigorian Nov 5, 2026
cc6963b
drop memory pools left with no allocation
ndgrigorian Nov 5, 2026
02a699a
record keep-alive kernels as uses of pooled allocations
ndgrigorian Nov 5, 2026
6c71211
add a maximal split size to the memory pool
ndgrigorian Nov 5, 2026
8f7d171
keep statistics per memory pool
ndgrigorian Nov 5, 2026
b79be40
wait for released memory a step at a time when an allocation fails
ndgrigorian Nov 5, 2026
05f15cd
don't miss releases in the allocation-failure wait
ndgrigorian Nov 5, 2026
bde23e1
wait only for held objects on the failing allocation's device
ndgrigorian Nov 5, 2026
7e91240
round large memory pool allocations up to 2 MiB
ndgrigorian Nov 6, 2026
49165bc
pool allocations aligned to at most 512 bytes
ndgrigorian Nov 6, 2026
eb0992f
report the memory pool's usage when an allocation fails
ndgrigorian Nov 6, 2026
6798e44
read the memory pool's cache limit and maximal split size from the en…
ndgrigorian Nov 6, 2026
9c1caab
document the memory pool
ndgrigorian Nov 6, 2026
3dbf96f
add get_device_cached_in_order_queue
ndgrigorian Dec 4, 2026
1ae0443
reuse device blocks of an in-order queue on that queue at once
ndgrigorian Dec 4, 2026
afca3b2
submit nothing in keep_args_alive for blocks the queue owns
ndgrigorian Dec 4, 2026
169f1b7
add record_queue for pooled memory used on another queue
ndgrigorian Dec 4, 2026
ca36435
recognize objects carrying pooled memory in keep_args_alive
ndgrigorian Dec 4, 2026
f1afd69
iterate released blocks by index, as freeing one may fence it
ndgrigorian Dec 4, 2026
5155afc
submit the memory pool's barriers through libsyclinterface
ndgrigorian Dec 4, 2026
7723717
query released blocks only when an allocation misses the cache
ndgrigorian Dec 4, 2026
29015dd
define DPCTL_KEEP_ARGS_ALIVE_HAS_GET_OWNER
ndgrigorian Dec 5, 2026
040efad
limit the memory pending on allocations not reused in queue order
ndgrigorian Dec 5, 2026
e805de7
cap the pending memory by the device memory left
ndgrigorian Dec 5, 2026
0234fc6
raise the default pending floor to 512 MiB
ndgrigorian Dec 6, 2026
1d4bc38
default the pending floor to 128 MiB on CPU devices
ndgrigorian Dec 6, 2026
e1c7360
free the oldest cached allocations above a device memory threshold
ndgrigorian Dec 6, 2026
1f4f6c0
link gh-2446 in the changelog
ndgrigorian Dec 6, 2026
9aaee12
align the pool's USM allocations to the block granularity
ndgrigorian Dec 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `dpctl.backend_type.openmp` for the AdaptiveCpp OpenMP host device [gh-2384](https://github.com/IntelPython/dpctl/pull/2384)
* Added `DPCTLService_GetSyclProvider` C-API function and `dpctl._diagnostics.sycl_provider`, which report the SYCL implementation `dpctl` was built with [gh-2384](https://github.com/IntelPython/dpctl/pull/2384)
* Added support for weak references to `dpctl.SyclQueue` [gh-2426](https://github.com/IntelPython/dpctl/pull/2426)
* Added a memory pool that caches USM allocations made by `dpctl.memory` for reuse, with `dpctl.memory.empty_cache`, `get_cache_limit`, `set_cache_limit`, `get_max_split_size`, `set_max_split_size`, `get_pool_stats`, which gives totals or the statistics of the pool for a queue and USM kind, and `reset_peak_pool_stats`; setting `DPCTL_USM_POOL=0` disables caching, and `DPCTL_USM_POOL_CACHE_LIMIT` and `DPCTL_USM_POOL_MAX_SPLIT_SIZE` set the initial cache limit and maximal split size in bytes [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* Added `Memory_PoolAllocate`, `Memory_PoolGetPointer`, `Memory_PoolRelease`, `Memory_PoolEmptyCache`, `Memory_PoolOwns`, `Memory_PoolRecordUse`, `Memory_PoolIsOwnedBy`, `Memory_PoolRecordQueue` and `SyclQueue_SubmitKeepAliveMarker` to the C-API, and `dpctl::memory::pool_allocate`, `dpctl::memory::pool_empty_cache`, `dpctl::memory::record_queue` and `dpctl::utils::keep_usm_alive` to `dpctl4pybind11.hpp`, for allocating from the memory pool in native extensions [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* Added `dpctl.get_device_cached_in_order_queue`, which returns a cached in-order queue for a device, separate from the one `dpctl.get_device_cached_queue` returns. Pickled queues with only the `"in_order"` property are restored as that queue [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* Device allocations from the memory pool made for an in-order queue are owned by that queue, which reuses them as soon as they are released, as its order puts the new work after the old. Other queues get them after a barrier on the owning queue [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* Added `record_queue` to `dpctl.memory.MemoryUSMShared`, `MemoryUSMDevice` and `MemoryUSMHost`, which records that work on a queue uses an allocation from the memory pool, so that the pool does not reuse it before that work completes [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* An allocation from the memory pool that cannot be reused in queue order waits for blocks released while offloaded work may still use them, rather than allocating more, once they exceed a limit relative to the memory in use and the device memory left, set with `DPCTL_USM_POOL_PENDING_RATIO`, `DPCTL_USM_POOL_PENDING_FLOOR` and `DPCTL_USM_POOL_PENDING_HEADROOM`. Above `DPCTL_USM_POOL_GC_THRESHOLD` of a device's memory, 0.9 by default, the oldest cached allocations of the device are freed first [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)

### Changed
* Object lifetime during offload is now managed by a list of releases deferred until the events that gate them have completed, rather than by a `host_task`. `dpctl` keeps one thread for looking over the list and running the releases that have come due, started by the first release deferred and again in the child of a `fork`, which takes the GIL to do so and lets a release call into the interpreter and free USM allocations [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
Expand All @@ -26,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `dpctl.utils.SequentialOrderManager[q].submitted_events` includes the queue's last event for in-order queues when `sycl_ext_oneapi_in_order_queue_events` is available, and `wait()` waits for all work on an in-order queue [gh-2299](https://github.com/IntelPython/dpctl/pull/2299)
* Pickled `dpctl.memory.MemoryUSMShared`, `MemoryUSMDevice`, and `MemoryUSMHost` objects keep their queue instead of being restored on the default device. Pickling memory whose queue can not be pickled raises `pickle.PicklingError` [gh-2406](https://github.com/IntelPython/dpctl/pull/2406)
* NumPy is no longer a build dependency of `dpctl`, only a run-time dependency [gh-2436](https://github.com/IntelPython/dpctl/pull/2436)
* `dpctl.SyclQueue.keep_args_alive`, `dpctl::utils::keep_args_alive` and `dpctl::utils::keep_usm_alive` no longer hold `dpctl.memory` objects and allocations from the memory pool, but record the kernel as a use of the allocation, which the pool does not reuse before that kernel completes [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* `dpctl.SyclQueue.keep_args_alive`, `dpctl::utils::keep_args_alive` and `dpctl::utils::keep_usm_alive` submit nothing for allocations owned by the in-order queue they are called on, whose work must then be on that queue, and return an already complete event if every argument is such an allocation [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)
* `dpctl.SyclQueue.keep_args_alive` does not hold objects carrying memory from the pool, either as a `dpctl.memory` object in their `usm_data` attribute or through a `__sycl_usm_array_interface__` pointing into a pooled block, and `dpctl::utils::keep_args_alive` takes a function that finds the allocation an object carries, which `DPCTL_KEEP_ARGS_ALIVE_HAS_GET_OWNER` signals [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)

### Deprecated
* Deprecated `dpctl.SyclQueue._submit_keep_args_alive` in favor of `dpctl.SyclQueue.keep_args_alive`, which it now calls and which behaves identically [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
Expand All @@ -45,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fixed building with the open source DPC++ compiler and CMake 4.5 or newer, which identifies it as `DPClang` instead of `Clang` [gh-2433](https://github.com/IntelPython/dpctl/pull/2433)
* Building with the open source DPC++ toolchain no longer requires the toolchain to bundle the OpenCL ICD loader, which `dpctl` loads at run time [gh-2435](https://github.com/IntelPython/dpctl/pull/2435)
* `dpctl.utils.SequentialOrderManager` no longer keeps every queue alive until `clear()`: it holds queues by weak reference and drops the order manager of a queue once the queue is gone and its work is complete, so a program that keeps creating queues no longer runs out of memory [gh-2426](https://github.com/IntelPython/dpctl/pull/2426)
* A `dpctl.memory` allocation that still fails then waits for the blocks released to its memory pool while in use, and for the work on its device that objects are held for, or on any device for host memory, a growing number at a time, trying again after each wait. `Memory_PoolAllocate` waits only when called without the GIL [gh-2446](https://github.com/IntelPython/dpctl/pull/2446)

### Maintenance
* Removed the `-fno-sycl-id-queries-fit-in-int` compile option, which none of the `dpctl` extensions need since `dpctl.tensor` moved to `dpnp` [gh-2413](https://github.com/IntelPython/dpctl/pull/2413)
Expand Down
9 changes: 9 additions & 0 deletions docs/doc_sources/api_reference/dpctl/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
select_accelerator_device
select_device_with_aspects

.. rubric:: Cached queues

.. autosummary::
:toctree: generated
:nosignatures:

get_device_cached_queue
get_device_cached_in_order_queue

.. rubric:: Platform discovery

.. autosummary::
Expand Down
105 changes: 105 additions & 0 deletions docs/doc_sources/api_reference/dpctl/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,111 @@ Should the USM allocation fail, the following Python exception will be raised:

USMAllocationError

.. _dpctl_memory_pool:

.. rubric:: Memory pool

USM allocations made with no alignment, or with a power of two up to 512 bytes, come from a
memory pool that carves them out of larger USM allocations and caches them for reuse once they
are freed, so that most allocations do not reach the SYCL runtime. There is a pool for each SYCL
context, device and USM type, and one for host allocations per context.

- Requests of up to 1 MiB are carved out of 2 MiB USM allocations, and larger requests come from
USM allocations rounded up to a multiple of 2 MiB. Sizes are rounded up to 512 bytes.
- A request is served from the smallest cached block that fits. The block is split if at least
512 bytes would be left of a block up to 1 MiB, or more than 1 MiB of a larger one. Cached
blocks of at least :func:`get_max_split_size` are neither split nor used for smaller requests.
- A USM allocation is freed by :func:`empty_cache` once none of its blocks is in use, or as it is
released if the cached bytes exceed :func:`get_cache_limit`, once the work that may use it has
completed. Cached memory is freed at exit, apart from memory still used by queued work, which is
left to the SYCL runtime.

Setting ``DPCTL_USM_POOL=0`` disables caching, and the cache limit and the maximal split size can
be set with ``DPCTL_USM_POOL_CACHE_LIMIT`` and ``DPCTL_USM_POOL_MAX_SPLIT_SIZE``. See
:ref:`Environment variables <user_guides_env_variables>`.

.. rubric:: Limiting the memory held for offloaded work

On a queue that does not order its work, a block released while offloaded work may still use it
is held until that work completes. When the host runs ahead, an allocation that misses the cache
waits for the oldest such blocks rather than allocating more once they exceed a limit relative to
the memory in use and to the device memory left. Separately, once the device's pools hold more
than 90% of its memory, the oldest cached allocations are freed before allocating more. See
:ref:`Environment variables <user_guides_env_variables>`.

.. rubric:: Allocations owned by an in-order queue

Device allocations made for an in-order queue, such as the one
:func:`dpctl.get_device_cached_in_order_queue` returns, are owned by that queue. Once released,
the queue reuses them right away, without waiting for the work on them: work submitted to an
in-order queue runs after the work submitted to it before, so a new use of the memory is ordered
after the old ones.

- Another queue gets such a block only after a barrier on the owning queue completes. That
barrier is submitted when a request on another queue could have used the block.
:func:`empty_cache` waits for the barriers on owning queues before freeing their blocks.
- Host and shared allocations, and allocations for out-of-order queues, are not owned, because
the host and those queues do not follow the order of any one queue.
- A reused block is ready for work on its owning queue. Work on another queue that uses it must
be ordered after work on the owning queue, or after an event of it.

Work on a queue other than the owner, or on any queue for an allocation no queue owns, is covered
by :meth:`dpctl.SyclQueue.keep_args_alive` called on that queue, or by ``record_queue(queue)`` on
the memory object, the counterpart of PyTorch's ``Tensor.record_stream``. Once released, a block
with recorded queues is not reused before a barrier submitted to each of them completes.
``dpctl::memory::record_queue`` and :c:func:`Memory_PoolRecordQueue` do the same from native
code.

.. rubric:: Memory used by offloaded work

:meth:`dpctl.SyclQueue.keep_args_alive` does not hold :mod:`dpctl.memory` objects whose
allocation comes from the pool. It records its kernel as a use of the allocation instead, and an
allocation released before its uses have completed is not reused or freed until they have, so
the object may be deleted while the work runs. ``dpctl::utils::keep_args_alive`` and
``dpctl::utils::keep_usm_alive`` behave the same. Work that is neither covered by one of these
nor waited for before the object is deleted may find its memory reused.

:meth:`dpctl.SyclQueue.keep_args_alive` treats an object that carries pooled memory the same way:
an object whose ``usm_data`` attribute is a :mod:`dpctl.memory` object, as arrays have, or one
whose ``__sycl_usm_array_interface__`` points into a block of the pool that is in use. Other
objects, and carriers of memory that does not come from the pool, are held.
``dpctl::utils::keep_args_alive`` takes a function finding the ``std::shared_ptr<void>`` an
object carries, for native extensions that know their array types.

An allocation owned by the in-order queue that these are called on needs nothing, as that queue
reuses it only for later work. ``depends`` is not consulted for it, so the work using it must be
on that queue, and work on another queue is covered by calling them on that queue. If every
argument is such an allocation, nothing is submitted and the returned event is already complete,
so with the cached in-order queue the call costs little more than checking the arguments.

.. rubric:: When an allocation fails

An allocation that fails is attempted again after releasing the objects held for offloaded work
that has completed, and after emptying the cache. If it still fails, it waits for the uses of the
blocks released to its pool while in use, and then for the work on its device that objects are
held for, on any device for host memory, a growing number at a time and trying again after each
wait. There is no time limit, so a failing allocation can wait as long as that work runs. The
:class:`USMAllocationError` raised in the end gives the bytes the pool has reserved, in use,
released while in use and cached.

.. rubric:: Shared allocations

A shared allocation from the pool may have been used before, and keeps the memory advice and the
residency its previous user left it with. Advice that the program relies on should be given after
allocating.

.. autosummary::
:toctree: generated
:nosignatures:

empty_cache
get_cache_limit
set_cache_limit
get_max_split_size
set_max_split_size
get_pool_stats
reset_peak_pool_stats

.. rubric:: IPC (Inter-Process Communication) Memory

Classes and functions for sharing USM device allocations across processes
Expand Down
84 changes: 84 additions & 0 deletions docs/doc_sources/api_reference/dpctl_capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ API for :c:struct:`PySyclQueueObject`
or on one draining the completed releases. Calling the function does not
require the GIL.

.. c:function:: int SyclQueue_SubmitKeepAliveMarker(DPCTLSyclQueueRef QRef, DPCTLSyclEventRef *depERefs, size_t nDepERefs, DPCTLSyclEventRef markerERef)

:param QRef: queue running the work that uses pooled allocations
:param depERefs: array of events for that work
:param nDepERefs: number of events in ``depERefs``
:param markerERef: existing event the marker event is assigned to
:returns: 0 on success, and 1 on failure.

Submits an empty kernel to ``QRef`` after ``depERefs`` and assigns its event
to the ``sycl::event`` behind ``markerERef``, for passing to
:c:func:`Memory_PoolRecordUse`. Calling the function does not require the
GIL.

API for :c:struct:`PySyclEventObject`
-------------------------------------

Expand Down Expand Up @@ -178,6 +191,77 @@ API for :c:struct:`Py_MemoryObject`
or a `nullptr` if the USM allocation represented by `o` is not managed by the smart
pointer.

Memory pool API
---------------

.. c:function:: void * Memory_PoolAllocate(size_t nbytes, DPCTLSyclUSMType kind, DPCTLSyclQueueRef QRef)

:param nbytes: The size of allocation in bytes
:param kind: USM allocation kind, ``DPCTL_USM_DEVICE``, ``DPCTL_USM_SHARED`` or ``DPCTL_USM_HOST``
:param QRef: Queue whose context and device the allocation is bound to
:returns: Opaque pointer to `std::shared_ptr<void>` which manages a USM allocation from
the memory pool of :py:mod:`dpctl.memory`, or a `nullptr` if it could not be allocated.
Does not need the GIL.

If the allocation fails, the pool's cache is emptied and the allocation tried again. Called
without the GIL, it then also waits for the blocks released to the pool while work may still
use them, and tries again after each wait.

.. c:function:: DPCTLSyclUSMRef Memory_PoolGetPointer(void *opaque)

:param opaque: Opaque pointer returned by :c:func:`Memory_PoolAllocate`
:returns: Pointer of the USM allocation.

.. c:function:: void Memory_PoolRelease(void *opaque)

:param opaque: Opaque pointer returned by :c:func:`Memory_PoolAllocate`

Deletes the opaque pointer. The USM allocation returns to the pool once nothing else
owns it, so no work may still be using it.

.. c:function:: int Memory_PoolOwns(void *opaque)

:param opaque: Opaque pointer to a ``std::shared_ptr<void>`` which manages a USM allocation
:returns: 1 if the allocation comes from the memory pool, and 0 otherwise.

.. c:function:: int Memory_PoolRecordUse(void *opaque, DPCTLSyclEventRef ERef)

:param opaque: Opaque pointer to a ``std::shared_ptr<void>`` which manages a USM allocation
:param ERef: Event of work that may use the allocation
:returns: 1 if the use was recorded, and 0 if the allocation is not from the
memory pool or the use could not be recorded.

Once released, the allocation is not reused before the work behind ``ERef``
has completed, so the owner need not be kept alive for that work.

.. c:function:: int Memory_PoolIsOwnedBy(void *opaque, DPCTLSyclQueueRef QRef)

:param opaque: Opaque pointer to a ``std::shared_ptr<void>`` which manages a USM allocation
:param QRef: Queue running the work that uses the allocation
:returns: 1 if the allocation comes from the memory pool and is owned by the
in-order queue behind ``QRef``, and 0 otherwise.

Such an allocation is reused by that queue only for work submitted after
the work using it, so neither the owner nor a recorded use is needed for
work on that queue.

.. c:function:: int Memory_PoolRecordQueue(void *opaque, DPCTLSyclQueueRef QRef)

:param opaque: Opaque pointer to a ``std::shared_ptr<void>`` which manages a USM allocation
:param QRef: Queue whose work may use the allocation
:returns: 1 if the use was recorded, and 0 if the allocation is not from the
memory pool or the use could not be recorded.

Once released, the allocation is not reused before the work submitted to
``QRef`` until then has completed, which a barrier submitted to ``QRef``
on release tracks.

.. c:function:: void Memory_PoolEmptyCache(void)

Frees the USM allocations cached by the memory pool. Waits for the work on
in-order queues that may still use cached blocks only if called without
the GIL.

API for :c:struct:`PySyclKernelObject`
--------------------------------------

Expand Down
Loading
Loading