Skip to content

Commit 10dac33

Browse files
Hold queues by weak reference in SequentialOrderManager
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.
1 parent 1addd52 commit 10dac33

4 files changed

Lines changed: 300 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
* Removed stale `select_host_device` and `has_host_device` names from `dpctl.__all__`, which made `from dpctl import *` fail with `AttributeError` [gh-2410](https://github.com/IntelPython/dpctl/pull/2410)
3333
* A USM allocation that fails now drops the references held for offloaded tasks that have completed, and is attempted once more if that released anything, so that memory only waiting to be given up is not reported as unavailable [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
3434
* `dpctl.SyclPlatform.get_devices` no longer prints Unified Runtime warnings for `device_type="custom"` on non-OpenCL platforms, where custom devices are not supported [gh-2306](https://github.com/IntelPython/dpctl/pull/2306)
35+
* `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)
3536

3637
### Maintenance
3738
* 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)

‎docs/doc_sources/api_reference/dpctl/utils.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
Thread-local object mapping each :class:`dpctl.SyclQueue` to an order
1717
manager, used to ensure sequential ordering of offloaded tasks.
1818

19+
Queues are held by weak reference, so creating queues does not keep them
20+
alive. Equal queues, which share a ``sycl::queue``, share an order manager,
21+
which lives while the queue it was looked up by does. Once that queue is
22+
gone, the order manager is dropped when the work recorded in it is
23+
complete and nothing else holds it; until then, an equal queue is given
24+
the same order manager, so that its tasks still follow that work. Nothing
25+
waits when a queue is dropped.
26+
1927
Record submitted tasks with ``add_event`` and use ``submitted_events``
2028
as the dependency list of subsequent submissions. To keep Python objects
2129
referenced by a task alive until it completes, use

‎dpctl/tests/test_utils.py‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import threading
2424
import time
2525
import warnings
26+
import weakref
2627

2728
import numpy as np
2829
import pytest
@@ -686,3 +687,138 @@ def wait(self):
686687
# clear() runs at exit, and must wait on in-order queues as well
687688
_som.clear()
688689
assert any(m is _mngr for m in waited)
690+
691+
692+
def _thread_map():
693+
return dpctl.utils.SequentialOrderManager._get_thread_map()
694+
695+
696+
@pytest.mark.parametrize("in_order", [False, True], ids=["ooo", "in_order"])
697+
def test_order_manager_does_not_keep_queues_alive(in_order):
698+
_som = dpctl.utils.SequentialOrderManager
699+
_som.clear()
700+
refs = []
701+
for _ in range(16):
702+
q = _in_order_queue() if in_order else _queue()
703+
_mngr = _som[q]
704+
buf = dpctl.memory.MemoryUSMDevice(1024, queue=q)
705+
_mngr.add_event(q.memset_async(buf, 1, buf.nbytes))
706+
# holds nothing: buf would keep q alive until dpctl's thread drops it
707+
_mngr.add_cleanup_event(q.keep_args_alive(()))
708+
_mngr.wait()
709+
refs.append(weakref.ref(q))
710+
del q, _mngr, buf
711+
# each lookup drops the managers of the queues gone before it
712+
assert len(_thread_map().managers) <= 2
713+
assert all(r() is None for r in refs)
714+
m = _thread_map()
715+
assert len(m.dead) == 1
716+
q = _in_order_queue() if in_order else _queue()
717+
_som[q]
718+
assert len(m.managers) == 1 and len(m.owned) == 1
719+
assert not m.retired and not m.dead and not m.dropped
720+
_som.clear()
721+
722+
723+
def test_order_manager_hands_pending_work_to_equal_queue():
724+
_som = dpctl.utils.SequentialOrderManager
725+
_som.clear()
726+
q1 = _queue()
727+
# the same sycl::queue in another object
728+
q2 = dpctl.SyclQueue(q1._get_capsule())
729+
assert q1 == q2 and q1 is not q2
730+
_mngr = _som[q1]
731+
assert _som[q2] is _mngr
732+
# as if the work recorded through q1 were not complete when q1 is gone
733+
_mngr._is_idle = lambda: False
734+
_mngr_ref = weakref.ref(_mngr)
735+
del q1, _mngr
736+
_som[_queue()]
737+
# the map holds it, as its work is not complete
738+
assert _mngr_ref() is not None
739+
assert _som[q2] is _mngr_ref()
740+
_som.clear()
741+
742+
743+
def test_order_manager_drops_idle_manager_of_queue_gone():
744+
_som = dpctl.utils.SequentialOrderManager
745+
_som.clear()
746+
q1 = _queue()
747+
q2 = dpctl.SyclQueue(q1._get_capsule())
748+
_mngr = _som[q1]
749+
_mngr.wait()
750+
_mngr_ref = weakref.ref(_mngr)
751+
del q1, _mngr
752+
# nothing holds it once the map drops it
753+
_som[q2]
754+
assert _mngr_ref() is None
755+
m = _thread_map()
756+
assert len(m.owned) == 1 and not m.retired and not m.dropped
757+
_som.clear()
758+
759+
760+
def test_order_manager_keeps_manager_held_for_equal_queue():
761+
_som = dpctl.utils.SequentialOrderManager
762+
_som.clear()
763+
q1 = _queue()
764+
q2 = dpctl.SyclQueue(q1._get_capsule())
765+
_som[q1]
766+
# looked up by q2, but the map has it under q1
767+
_mngr = _som[q2]
768+
_mngr.wait()
769+
del q1
770+
# the map drops it, its work being complete, while it is still in use
771+
_som[_queue()]
772+
assert _mngr._dropped
773+
# work recorded in it now has to be ordered before the work on q2
774+
assert _som[q2] is _mngr
775+
assert not _mngr._dropped
776+
_som.clear()
777+
778+
779+
def test_order_manager_orders_work_of_queue_gone():
780+
_som = dpctl.utils.SequentialOrderManager
781+
_som.clear()
782+
q2 = _queue()
783+
buf = dpctl.memory.MemoryUSMDevice(64 * 1024 * 1024, queue=q2)
784+
host = np.empty(buf.nbytes, dtype=np.uint8)
785+
for _ in range(5):
786+
q2.memset(buf, 0, buf.nbytes)
787+
_som.clear()
788+
# the same sycl::queue in another object, which is the key
789+
q1 = dpctl.SyclQueue(q2._get_capsule())
790+
_mngr = _som[q1]
791+
_fill_in_steps(q1, buf, 8, _mngr)
792+
del _mngr
793+
q1_ref = weakref.ref(q1)
794+
del q1
795+
assert q1_ref() is None
796+
_mngr = _som[q2]
797+
ev = q2.memset_async(buf, 9, buf.nbytes, _mngr.submitted_events)
798+
_mngr.add_event(ev)
799+
q2.memcpy_async(host, buf, buf.nbytes, [ev]).wait()
800+
assert np.all(host == 9)
801+
del _mngr
802+
_som.clear()
803+
804+
805+
def test_order_manager_queue_gone_on_other_thread(monkeypatch):
806+
_som = dpctl.utils.SequentialOrderManager
807+
_som.clear()
808+
q = _queue()
809+
_mngr = _som[q]
810+
monkeypatch.setattr(_mngr, "_is_idle", lambda: False)
811+
holder = [q]
812+
del q
813+
# the last reference is dropped on another thread, which neither waits
814+
# nor changes this thread's map
815+
t = threading.Thread(target=holder.clear)
816+
t.start()
817+
t.join(timeout=60)
818+
assert not t.is_alive()
819+
m = _thread_map()
820+
assert len(m.dead) == 1 and len(m.managers) == 1
821+
_som[_queue()]
822+
assert [om for om in m.retired.values()] == [_mngr]
823+
_som.clear()
824+
assert not m.retired

0 commit comments

Comments
 (0)