mindspore.hal.Stream

class mindspore.hal.Stream(priority=0, **kwargs)[source]

Wrapper around a device stream.

A device stream is a linear sequence of execution that belongs to a specific device, independent from other streams.

For a quick start of using Stream, please refer to Illustration of stream management .

Parameters
  • priority (int, optional) – priority of the stream, lower numbers represent higher priorities. By default, streams have priority 0.

  • kwargs (dict) – keyword arguments.

query()[source]

Checks if all the work submitted has been completed.

Returns

A boolean indicating if all kernels in this stream are 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)
>>> s1.synchronize()
>>> assert s1.query()
record_event(event=None)[source]

Records an event.

Parameters

event (Event, optional) – event to record. If not given, a new one will be allocated.

Returns

Event, recorded event. If this argument is None, a new one will be allocated. Default is None.

Raises

TypeError – If 'event' is neither a mindspore.hal.Event nor a None.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> a = Tensor(np.ones([3, 3]), ms.float32)
>>> b = Tensor(np.ones([3, 3]), ms.float32)
>>> s1 = ms.hal.Stream()
>>> with ms.hal.StreamCtx(s1):
...     c = a + b
...     event = s1.record_event()
...     d = a * b
>>> cur_stream = ms.hal.current_stream()
>>> cur_stream.wait_event(event)
>>> e = c + 3
>>> print(e)
[[5. 5. 5.]
 [5. 5. 5.]
 [5. 5. 5.]]
synchronize()[source]

Wait for all the kernels in this stream to complete.

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)
>>> s1.synchronize()
>>> assert s1.query()
wait_event(event)[source]

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

Parameters

event (Event) – an event to wait for.

Raises

TypeError – If 'event' is not a mindspore.hal.Event.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> a = Tensor(np.ones([3, 3]), ms.float32)
>>> b = Tensor(np.ones([3, 3]), ms.float32)
>>> s1 = ms.hal.Stream()
>>> with ms.hal.StreamCtx(s1):
...     c = a + b
...     event = s1.record_event()
...     d = a * b
>>> cur_stream = ms.hal.current_stream()
>>> cur_stream.wait_event(event)
>>> e = c + 3
>>> print(e)
[[5. 5. 5.]
 [5. 5. 5.]
 [5. 5. 5.]]
wait_stream(stream)[source]

Synchronizes with another stream.

All future work submitted to this stream will wait until all kernels submitted to a given stream at the time of call complete.

Parameters

stream (Stream) – a stream to synchronize.

Raises

TypeError – If 'stream' is not a mindspore.hal.Stream.

Examples

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