mindspore.hal.Event
- class mindspore.hal.Event(enable_timing=False, blocking=False)[source]
Wrapper around a device event, this api will be deprecated and removed in future versions, please use the api
mindspore.runtime.Event()
instead.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
Examples
>>> import mindspore >>> start = mindspore.hal.Event(enable_timing=True) >>> end = mindspore.hal.Event(enable_timing=True) >>> s1 = mindspore.hal.Stream() >>> s2 = mindspore.hal.Stream() >>> a = mindspore.tensor(mindspore.ops.ones([2, 2]), mindspore.float32) >>> b = mindspore.tensor(mindspore.ops.ones([2, 2]), mindspore.float32) >>> c = mindspore.tensor(mindspore.ops.ones([2, 2]), mindspore.float32) >>> with mindspore.hal.StreamCtx(s1): ... d = mindspore.ops.matmul(a, b) ... start.record() >>> c += 2 >>> end.record() >>> with mindspore.hal.StreamCtx(s2): ... start.synchronize() ... end.synchronize() ... e = c + d >>> mindspore.hal.synchronize() >>> print(e) [[5. 5.] [5. 5.]] >>> elapsed_time = start.elapsed_time(end)
- elapsed_time(end_event)[source]
Return 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.
- query()[source]
Check 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 >>> a = mindspore.tensor(mindspore.ops.ones([1024, 2048]), mindspore.float32) >>> b = mindspore.tensor(mindspore.ops.ones([2048, 4096]), mindspore.float32) >>> s1 = mindspore.hal.Stream() >>> with mindspore.hal.StreamCtx(s1): ... c = mindspore.ops.matmul(a, b) ... ev = s1.record_event() >>> s1.synchronize() >>> assert ev.query()
- record(stream=None)[source]
Record 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. DefaultNone
.
- synchronize()[source]
Wait 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]
Make 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. DefaultNone
.
Examples
>>> import mindspore >>> event = mindspore.hal.Event() >>> s1 = mindspore.hal.Stream() >>> s2 = mindspore.hal.Stream() >>> a = mindspore.tensor(mindspore.ops.ones([2, 2]), mindspore.float32) >>> b = mindspore.tensor(mindspore.ops.ones([2, 2]), mindspore.float32) >>> with mindspore.hal.StreamCtx(s1): ... c = mindspore.ops.matmul(a, b) ... event.record() >>> event.wait() >>> d = c + 2 >>> mindspore.hal.synchronize() >>> print(d) [[4. 4.] [4. 4.]]