Repository navigation
Hold queues by weak reference in SequentialOrderManager - #2426
vlad-perevezentsev wants to merge 4 commits into
Conversation
|
View rendered docs @ https://intelpython.github.io/dpctl/pulls/2426/index.html |
10dac33 to
13a721d
Compare
| Queues are held by weak reference, so creating queues does not keep them | ||
| alive. Equal queues, which share a ``sycl::queue``, share an order manager, | ||
| which lives while the queue it was looked up by does. Once that queue is | ||
| gone, the order manager is dropped when the work recorded in it is | ||
| complete and nothing else holds it; until then, an equal queue is given | ||
| the same order manager, so that its tasks still follow that work. Nothing | ||
| waits when a queue is dropped. |
There was a problem hiding this comment.
I think we can trim this down/reduce the verbosity
There was a problem hiding this comment.
Done, I made it shorter. I also shortened the class docstring.
| looked at, as Level Zero v2 reports it submitted until waited on. | ||
| """ | ||
| _local = self._state | ||
| # both prune the complete events first |
There was a problem hiding this comment.
or will short-circuit so cleanup events aren't pruned if submitted events exist, only when submitted events don't exist will the cleanup events be pruned.
It's probably fine to just correct the comment
There was a problem hiding this comment.
Each getter removes the completed events from both lists, so the first call already cleans up the cleanup events. The comment was wrong, I fixed it.
| om = self._add(m, q) | ||
| return om | ||
|
|
||
| def clear(self): |
There was a problem hiding this comment.
An interesting consequence of this change is that _key on a manager that is still held leaves _key set, so if you have another, equivalent queue on a new manager, they don't share order (which is probably ok, but it's worth thinking if we should document that)
There was a problem hiding this comment.
This was the same before this PR: after clear() you get a new manager. clear() waits for all work first, so only tasks you add to the old manager after clear() are not ordered. Now clear() sets _key to None, and I described this in its docstring.
Add a __weakref__ slot to SyclQueue. It follows the vtable pointer, so extensions built against an earlier dpctl find the existing fields and the vtable where they expect them; only the object grows, which they report as a size change at import. SyclQueue gets an empty __dealloc__, so that Cython gives it a deallocator of its own, which clears the weak references. Cython does not count __weakref__ as a field that needs one and would reuse the one of _SyclQueue, leaving weak references to freed memory.
An order manager now holds its sycl::queue in a SyclQueue object of its own, rather than the queue it was looked up by, so that the values of the order manager map do not keep its keys alive. _InOrderManager used to hold its key. The in-order test that counts waits on the queue now makes the manager itself, as the managers of the map wait on their copy.
The order manager map kept every queue it was asked for alive until clear(), so a program creating queues in a loop ran out of device or system memory (up to ~15 MB per live queue on Level Zero v2). Queues are now map keys by weak reference. A queue that is gone is only recorded by its weak reference callback; the owning thread drops its order manager on a later lookup once the work recorded in it is complete, without waiting, as the queue may be dropped on any thread, including inside a host task that the order manager would wait on. Until then, and while anything else holds it, the order manager is handed to an equal queue, so that its work stays ordered. Whether an in-order manager is idle is decided from the events recorded in it only: Level Zero v2 reports the last event of an in-order queue as submitted until it is waited on.
Shorten the description of weak keys in the SequentialOrderManager docs and in the SyclQueueToOrderManagerMap docstring. Correct the comment in _is_idle: each getter of _OrderManager prunes the complete events of both lists, so the cleanup events are pruned even when `or` does not call the second one. clear() now resets _key of the order managers it drops, so that a manager held across clear() no longer looks to be in the map, and its docstring says that such a manager is not given out again.
13a721d to
a527acf
Compare
SequentialOrderManagerkeeps an order manager per queue in a thread-local dict keyed by the queue. Onlyclear()removes entries, so every queue that ran adpnpoperation stays alive, along with its Level Zero command lists and event pools.On Level Zero v2 this costs up to ~15 MB per queue, and a program that keeps creating queues runs out of memory.
On Lunar Lake and Battlemage this often made
dpnpCI fail withUR_RESULT_ERROR_DEVICE_LOST.dpnpCI passes only because itsconftest.pycallsclear()after every test file.This PR makes the map hold queues by weak reference:
SyclQueuesupports weak references. The new slot follows the vtable pointer, so extensions built against an earlierdpctlkeep working.SyclQueuealso gets an empty__dealloc__. Without it, Cython reuses_SyclQueue's deallocator, which does not clear weak references.dpnphost_task, which would then wait for itself.sycl::queue) looked up later gets the same order manager while that manager still has work or is still referenced, so ordering is kept.A lookup costs ~30 ns more for a known queue and ~4 us more for a new one. Creating a queue and running its first operation takes ~1 ms.
Compatibility:
dpnpbuilt against an earlierdpctlworks, but warns at import thatSyclQueuesize changed and fails to import with-W error::RuntimeWarning.dpnpshould be rebuilt against this version.Testing: the
dpctltest suite passes (3870 tests, 10 of them new) on Max 1100 with Level Zero v1 and v2 and on OpenCL CPU, and also passes on free-threaded Python 3.14t. Peak memory of thedpnptest suites, with all tests passing in every run:master,clear()per file (CI)clear()dpnp/tests/tensordpnp/testsdpnp/tests/tensordpnp/testsWithout
clear(),masterreaches 17.6 GB host and 17.3 GB device memory indpnp/tests/tensoron Max 1100, and is killed at 20 GB on Lunar Lake.