mindspore.hal.Event

View Source On Gitee
class mindspore.hal.Event(enable_timing=False, blocking=False)[source]

Wrapper around a device event.

Device events are synchronization markers that can be used to monitor the device’s progress, to accurately measure timing, and to synchronize device streams.

The underlying device events are lazily initialized when the event is first recorded.

Parameters
  • enable_timing (bool, optional) – indicates if the event should measure time (default: False)

  • blocking (bool, optional) – if True, wait will be blocking (default: False)

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> start = ms.hal.Event(enable_timing=True)
>>> end = ms.hal.Event(enable_timing=True)
>>> s1 = ms.hal.Stream()
>>> s2 = ms.hal.Stream()
>>> a = Tensor(np.ones([2, 2]), ms.float32)
>>> b = Tensor(np.ones([2, 2]), ms.float32)
>>> c = Tensor(np.ones([2, 2]), ms.float32)
>>> with ms.hal.StreamCtx(s1):
...     d = ops.matmul(a, b)
...     start.record()
>>> c += 2
>>> end.record()
>>> with ms.hal.StreamCtx(s2):
...     start.synchronize()
...     end.synchronize()
...     e = c + d
>>> ms.hal.synchronize()
>>> print(e)
[[5. 5.]
 [5. 5.]]
>>> elapsed_time = start.elapsed_time(end)
elapsed_time(end_event)[source]

Returns the time elapsed in milliseconds after the event was recorded and before the end_event was recorded.

Parameters

end_event (Event) – end event.

Returns

float, the time elapsed in milliseconds.

Raises

TypeError – If ‘end_event’ is not a mindspore.hal.Event.

query()[source]

Checks if all work currently captured by event has completed.

Returns

A boolean indicating if all work currently captured by event has completed.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> a = Tensor(np.ones([1024, 2048]), ms.float32)
>>> b = Tensor(np.ones([2048, 4096]), ms.float32)
>>> s1 = ms.hal.Stream()
>>> with ms.hal.StreamCtx(s1):
...     c = ops.matmul(a, b)
...     ev = s1.record_event()
>>> s1.synchronize()
>>> assert ev.query()
record(stream=None)[source]

Records the event in a given stream.

Uses mindspore.hal.current_stream() if no stream is specified. The stream’s device must match the event’s device.

Parameters

stream (Stream, optional) – a stream to record. If this argument is None, current stream will be used. Default value: None.

Raises

TypeError – If ‘stream’ is neither a mindspore.hal.Stream nor a None.

synchronize()[source]

Waits for the event to complete.

Waits until the completion of all work currently captured in this event. This prevents the CPU thread from proceeding until the event completes.

wait(stream=None)[source]

Makes all future work submitted to the given stream wait for this event.

Use mindspore.hal.current_stream() if no stream is specified.

Parameters

stream (Stream, optional) – a stream to record. If this argument is None, current stream will be used. Default value: None.

Raises

TypeError – If ‘stream’ is neither a mindspore.hal.Stream nor a None.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> event = ms.hal.Event()
>>> s1 = ms.hal.Stream()
>>> s2 = ms.hal.Stream()
>>> a = Tensor(np.ones([2, 2]), ms.float32)
>>> b = Tensor(np.ones([2, 2]), ms.float32)
>>> with ms.hal.StreamCtx(s1):
...     c = ops.matmul(a, b)
...     event.record()
>>> event.wait()
>>> d = c + 2
>>> ms.hal.synchronize()
>>> print(d)
[[4. 4.]
 [4. 4.]]