|
23 | 23 | import threading |
24 | 24 | import time |
25 | 25 | import warnings |
| 26 | +import weakref |
26 | 27 |
|
27 | 28 | import numpy as np |
28 | 29 | import pytest |
@@ -686,3 +687,138 @@ def wait(self): |
686 | 687 | # clear() runs at exit, and must wait on in-order queues as well |
687 | 688 | _som.clear() |
688 | 689 | 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