Repository navigation
Expand file tree
/
Copy path_order_manager.py
More file actions
252 lines (198 loc) · 7.12 KB
/
Copy path_order_manager.py
File metadata and controls
252 lines (198 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sys
import threading
import weakref
from collections.abc import Sequence
from typing import Protocol
from .._sycl_event import SyclEvent
from .._sycl_queue import SyclQueue, _drain_deferred_releases
from ._seq_order_keeper import _OrderManager
class OrderManagerProtocol(Protocol):
"""Interface of order managers returned by ``SequentialOrderManager``."""
def add_event_pair(
self,
host_task_ev: SyclEvent | Sequence[SyclEvent],
comp_ev: SyclEvent | Sequence[SyclEvent],
) -> None: ...
def add_event(self, comp_ev: SyclEvent | Sequence[SyclEvent]) -> None: ...
def add_cleanup_event(
self, cleanup_ev: SyclEvent | Sequence[SyclEvent]
) -> None: ...
@property
def num_host_task_events(self) -> int: ...
@property
def num_cleanup_events(self) -> int: ...
@property
def num_submitted_events(self) -> int: ...
@property
def host_task_events(self) -> list[SyclEvent]: ...
@property
def cleanup_events(self) -> list[SyclEvent]: ...
@property
def submitted_events(self) -> list[SyclEvent]: ...
def wait(self) -> None: ...
def __copy__(self) -> "OrderManagerProtocol": ...
class _SequentialOrderManager:
"""
Class to orchestrate default sequential order
of the tasks offloaded from Python.
Record offloaded tasks with :meth:`add_event` and use
:attr:`submitted_events` as the dependencies of the tasks that follow
them. Record events that gate the release of objects used by a task with
:meth:`add_cleanup_event`: they are waited on, but never become
dependencies of later tasks.
"""
def __init__(self):
self._state = _OrderManager(16)
def __del__(self):
if sys.is_finalizing():
return
_local = self._state
SyclEvent.wait_for(_local.get_submitted_events())
SyclEvent.wait_for(_local.get_cleanup_events())
def add_event_pair(self, host_task_ev, comp_ev):
_local = self._state
if isinstance(host_task_ev, SyclEvent) and isinstance(
comp_ev, SyclEvent
):
_local.add_to_both_events(host_task_ev, comp_ev)
else:
if not isinstance(host_task_ev, (list, tuple)):
host_task_ev = (host_task_ev,)
if not isinstance(comp_ev, (list, tuple)):
comp_ev = (comp_ev,)
_local.add_vector_to_both_events(host_task_ev, comp_ev)
def add_event(self, comp_ev):
_local = self._state
if isinstance(comp_ev, SyclEvent):
_local.add_to_submitted_events(comp_ev)
else:
if not isinstance(comp_ev, (list, tuple)):
comp_ev = (comp_ev,)
for ev in comp_ev:
_local.add_to_submitted_events(ev)
def add_cleanup_event(self, cleanup_ev):
_local = self._state
if isinstance(cleanup_ev, SyclEvent):
_local.add_to_cleanup_events(cleanup_ev)
else:
if not isinstance(cleanup_ev, (list, tuple)):
cleanup_ev = (cleanup_ev,)
for ev in cleanup_ev:
_local.add_to_cleanup_events(ev)
@property
def num_host_task_events(self):
return self.num_cleanup_events
@property
def num_cleanup_events(self):
_local = self._state
return _local.get_num_cleanup_events()
@property
def num_submitted_events(self):
_local = self._state
return _local.get_num_submitted_events()
@property
def host_task_events(self):
return self.cleanup_events
@property
def cleanup_events(self):
_local = self._state
return _local.get_cleanup_events()
@property
def submitted_events(self):
_local = self._state
return _local.get_submitted_events()
def wait(self):
_local = self._state
res = _local.wait()
# the events are complete, so what was held for those tasks is released
# here, unless dpctl's thread has already taken it to release; a release
# runs Python code, which finalization rules out
if not sys.is_finalizing():
_drain_deferred_releases()
return res
def __copy__(self):
res = _SequentialOrderManager.__new__(_SequentialOrderManager)
res._state = _OrderManager(self._state)
return res
_use_last_event = SyclQueue.is_get_last_event_available()
class _InOrderManager(_SequentialOrderManager):
"""
Order manager for an in-order queue.
Holds the added events which are not complete, as
:class:`_SequentialOrderManager` does. ``submitted_events`` also has the
event of the last command submitted to the queue if
:meth:`dpctl.SyclQueue.get_last_event` is available. ``wait`` waits on
the queue.
"""
def __init__(self, q: SyclQueue):
super().__init__()
self._queue = q
def __del__(self):
if sys.is_finalizing():
return
self._queue.wait()
super().__del__()
@property
def num_submitted_events(self):
return len(self.submitted_events)
@property
def submitted_events(self):
evs = self._state.get_submitted_events()
if _use_last_event:
ev = self._queue.get_last_event()
if ev is not None:
evs.append(ev)
return evs
def wait(self):
self._queue.wait()
return super().wait()
def __copy__(self):
res = _InOrderManager.__new__(_InOrderManager)
res._state = _OrderManager(self._state)
res._queue = self._queue
return res
class SyclQueueToOrderManagerMap:
"""
Utility class used to ensure sequential ordering of offloaded tasks
when passed to order manager.
Maintains a thread-local dictionary mapping SyclQueue instances to
order managers: _InOrderManager for in-order queues,
_SequentialOrderManager otherwise.
"""
def __init__(self):
# each thread gets its own dictionary of order managers
self._tls = threading.local()
def _get_map(self):
"""
Factory method to get or create an order manager map for the
current thread.
"""
try:
return self._tls.order_manager_map
except AttributeError:
m = {}
self._tls.order_manager_map = m
return m
def __getitem__(self, q: SyclQueue) -> OrderManagerProtocol:
"""Get order manager for given SyclQueue"""
if not isinstance(q, SyclQueue):
raise TypeError(f"Expected `dpctl.SyclQueue`, got {type(q)}")
_local = self._get_map()
om = _local.get(q)
if om is None:
if q.is_in_order:
om = _InOrderManager(q)
else:
om = _SequentialOrderManager()
_local[q] = om
return om
def clear(self):
"""Clear content of internal dictionary"""
_local = self._get_map()
for v in _local.values():
v.wait()
_local.clear()
SequentialOrderManager = SyclQueueToOrderManagerMap()
def _callback(som):
som.clear()
f = weakref.finalize(SequentialOrderManager, _callback, SequentialOrderManager)