Skip to content

Commit 4141e6b

Browse files
Make SyclTimer accumulative
``` In [9]: timer = dpctl.SyclTimer() In [10]: with timer(q): ...: y = dpt.linspace(1, 2, num=10**6, sycl_queue=q) ...: In [11]: timer.dt Out[11]: (0.0022024469999450957, 0.002116712) In [12]: with timer(q): ...: x = dpt.linspace(0, 1, num=10**6, sycl_queue=q) ...: In [13]: timer.dt Out[13]: (0.004531950999989931, 0.004239664000000001) ```
1 parent 34c5935 commit 4141e6b

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

dpctl/_sycl_timer.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ def __init__(self, host_timer=timeit.default_timer, time_scale=1):
6767
self.timer = host_timer
6868
self.time_scale = time_scale
6969
self.queue = None
70-
self.host_start = None
71-
self.host_finish = None
72-
self.event_start = None
73-
self.event_finish = None
70+
self.host_times = []
71+
self.bracketing_events = []
7472

7573
def __call__(self, queue=None):
7674
if isinstance(queue, SyclQueue):
@@ -89,27 +87,30 @@ def __call__(self, queue=None):
8987
return self
9088

9189
def __enter__(self):
92-
self.event_start = self.queue.submit_barrier()
93-
self.host_start = self.timer()
90+
self._event_start = self.queue.submit_barrier()
91+
self._host_start = self.timer()
9492
return self
9593

9694
def __exit__(self, *args):
97-
self.event_finish = self.queue.submit_barrier()
98-
self.host_finish = self.timer()
95+
self.host_times.append((self._host_start, self.timer()))
96+
self.bracketing_events.append(
97+
(self._event_start, self.queue.submit_barrier())
98+
)
99+
del self._event_start
100+
del self._host_start
99101

100102
@property
101103
def dt(self):
102104
"""Returns a tuple of elapsed times where first
103105
element is the duration as measured by the host timer,
104106
while the second element is the duration as measured by
105107
the device timer and encoded in profiling events"""
106-
self.event_start.wait()
107-
self.event_finish.wait()
108-
return (
109-
(self.host_finish - self.host_start) * self.time_scale,
110-
(
111-
self.event_finish.profiling_info_start
112-
- self.event_start.profiling_info_end
113-
)
114-
* (1e-9 * self.time_scale),
115-
)
108+
for es, ef in self.bracketing_events:
109+
es.wait()
110+
ef.wait()
111+
host_dt = sum(tf - ts for ts, tf in self.host_times) * self.time_scale
112+
dev_dt = sum(
113+
ef.profiling_info_start - es.profiling_info_end
114+
for es, ef in self.bracketing_events
115+
) * (1e-9 * self.time_scale)
116+
return (host_dt, dev_dt)

0 commit comments

Comments
 (0)