# Data Parallel Control (dpctl)## Copyright 2020-2024 Intel Corporation## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.importtimeitfrom.importSyclQueue__doc__="This module implements :class:`dpctl.SyclTimer`."classHostDeviceDuration:def__init__(self,host_dt,device_dt):self._host_dt=host_dtself._device_dt=device_dtdef__repr__(self):returnf"(host_dt={self._host_dt}, device_dt={self._device_dt})"def__str__(self):returnf"(host_dt={self._host_dt}, device_dt={self._device_dt})"def__iter__(self):yield from[self._host_dt,self._device_dt]@propertydefhost_dt(self):returnself._host_dt@propertydefdevice_dt(self):returnself._device_dt
[docs]classSyclTimer:""" Context to measure device time and host wall-time of execution of commands submitted to :class:`dpctl.SyclQueue`. :Example: .. code-block:: python import dpctl # Create a default SyclQueue q = dpctl.SyclQueue(property="enable_profiling") # create the timer milliseconds_sc = 1e-3 timer = dpctl.SyclTimer(time_scale = milliseconds_sc) # use the timer with timer(queue=q): code_block1 # use the timer with timer(queue=q): code_block2 # retrieve elapsed times in milliseconds wall_dt, device_dt = timer.dt .. note:: The timer submits barriers to the queue at the entrance and the exit of the context and uses profiling information from events associated with these submissions to perform the timing. Thus :class:`dpctl.SyclTimer` requires the queue with ``"enable_profiling"`` property. In order to be able to collect the profiling information, the ``dt`` property ensures that both submitted barriers complete their execution and thus effectively synchronizes the queue. Args: host_timer (callable, optional): A callable such that host_timer() returns current host time in seconds. Default: :py:func:`timeit.default_timer`. time_scale (Union[int, float], optional): Ratio of the unit of time of interest and one second. Default: ``1``. """
[docs]def__init__(self,host_timer=timeit.default_timer,time_scale=1):""" Create new instance of :class:`.SyclTimer`. Args: host_timer (callable, optional) A function that takes no arguments and returns a value measuring time. Default: :meth:`timeit.default_timer`. time_scale (Union[int, float], optional): Scaling factor applied to durations measured by the host_timer. Default: ``1``. """self.timer=host_timerself.time_scale=time_scaleself.queue=Noneself.host_times=[]self.bracketing_events=[]
def__call__(self,queue=None):ifisinstance(queue,SyclQueue):ifqueue.has_enable_profiling:self.queue=queueelse:raiseValueError("The given queue was not created with the ""enable_profiling property")else:raiseTypeError("The passed queue must have type dpctl.SyclQueue, "f"got {type(queue)}")returnselfdef__enter__(self):self._event_start=self.queue.submit_barrier()self._host_start=self.timer()returnselfdef__exit__(self,*args):self.host_times.append((self._host_start,self.timer()))self.bracketing_events.append((self._event_start,self.queue.submit_barrier()))delself._event_startdelself._host_start@propertydefdt(self):"""Returns a pair of elapsed times ``host_dt`` and ``device_dt``. The ``host_dt`` is the duration as measured by the host timer, while the ``device_dt`` is the duration as measured by the device timer and encoded in profiling events. Returns: HostDeviceDuration: Data class with ``host_dt`` and ``device_dt`` members which supports unpacking into a 2-tuple. :Example: .. code-block:: python import dpctl from dpctl import tensor q = dpctl.SyclQueue(property="enable_profiling") device = tensor.Device.create_device(q) timer = dpctl.SyclTimer() with timer(q): x = tensor.linspace(-4, 4, num=10**6, dtype="float32") e = tensor.exp(-0.5 * tensor.square(x)) s = tensor.sin(2.3 * x + 0.11) f = e * s host_dt, device_dt = timer.dt .. note:: Since different timers are used to measure host and device durations, one should not expect that ``host_dt`` is always strictly greater than ``device_dt``. Use tracing tools like ``onetrace``, or ``unitrace`` from `intel/pti-gpu <https://github.com/intel/pti-gpu>`_ repository for more accurate measurements. """fores,efinself.bracketing_events:es.wait()ef.wait()host_dt=sum(tf-tsforts,tfinself.host_times)*self.time_scaledev_dt=sum(ef.profiling_info_start-es.profiling_info_endfores,efinself.bracketing_events)*(1e-9*self.time_scale)returnHostDeviceDuration(host_dt,dev_dt)