17
17
18
18
import timeit
19
19
20
- import dpctl
20
+ from . import SyclQueue
21
21
22
22
23
23
class SyclTimer :
24
- def __init__ (self , host_time = timeit .default_timer , time_scale = 1 ):
25
- self .timer = host_time
24
+ """
25
+ SyclTimer(host_timer=timeit.default_timer, time_scale=1)
26
+ Python class to measure device time of execution of commands submitted to
27
+ :class:`dpctl.SyclQueue` as well as the wall-time.
28
+
29
+ :Example:
30
+ .. code-block:: python
31
+
32
+ import dpctl
33
+
34
+ # Create a default SyclQueue
35
+ q = dpctl.SyclQueue(property='enable_profiling')
36
+
37
+ # create the timer
38
+ miliseconds_sc = 1e-3
39
+ timer = dpctl.SyclTimer(time_scale = miliseconds_sc)
40
+
41
+ # use the timer
42
+ with timer(queue=q):
43
+ code_block
44
+
45
+ # retrieve elapsed times in miliseconds
46
+ sycl_dt, wall_dt = timer.dt
47
+
48
+ Remark:
49
+ The timer synchronizes the queue at the entrance and the
50
+ exit of the context.
51
+
52
+ Args:
53
+ host_timer (callable): A callable such that host_timer() returns current
54
+ host time in seconds.
55
+ time_scale (int, float): Ratio of the unit of time of interest and
56
+ one second.
57
+ """
58
+
59
+ def __init__ (self , host_timer = timeit .default_timer , time_scale = 1 ):
60
+ self .timer = host_timer
26
61
self .time_scale = time_scale
62
+ self .queue = None
27
63
28
64
def __call__ (self , queue = None ):
29
- if isinstance (queue , dpctl . SyclQueue ):
65
+ if isinstance (queue , SyclQueue ):
30
66
if queue .has_enable_profiling :
31
67
self .queue = queue
32
68
else :
33
69
raise ValueError (
34
- "The queue does not contain the enable_profiling property"
70
+ "The given queue was not created with the "
71
+ "enable_profiling property"
35
72
)
36
73
else :
37
- raise ValueError (
38
- "The passed queue must be <class 'dpctl._sycl_queue.SyclQueue'>"
74
+ raise TypeError (
75
+ "The passed queue must have type dpctl.SyclQueue, "
76
+ "got {}" .format (type (queue ))
39
77
)
40
- return self . __enter__ ()
78
+ return self
41
79
42
80
def __enter__ (self ):
43
81
self .event_start = self .queue .submit_barrier ()
@@ -48,6 +86,7 @@ def __exit__(self, *args):
48
86
self .event_finish = self .queue .submit_barrier ()
49
87
self .host_finish = self .timer ()
50
88
89
+ @property
51
90
def dt (self ):
52
91
self .event_start .wait ()
53
92
self .event_finish .wait ()
0 commit comments