mindspore.runtime.Stream

View Source On Gitee
class mindspore.runtime.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.

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
>>> ms.set_device("Ascend", 0)
>>> a = Tensor(np.ones([1024, 2048]), ms.float32)
>>> b = Tensor(np.ones([2048, 4096]), ms.float32)
>>> s1 = ms.runtime.Stream()
>>> with ms.runtime.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. Default is None.

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.runtime.Event nor a None.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> ms.set_device("Ascend", 0)
>>> a = Tensor(np.ones([3, 3]), ms.float32)
>>> b = Tensor(np.ones([3, 3]), ms.float32)
>>> s1 = ms.runtime.Stream()
>>> with ms.runtime.StreamCtx(s1):
...     c = a + b
...     event = s1.record_event()
...     d = a * b
>>> cur_stream = ms.runtime.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
>>> ms.set_device("Ascend", 0)
>>> a = Tensor(np.ones([1024, 2048]), ms.float32)
>>> b = Tensor(np.ones([2048, 4096]), ms.float32)
>>> s1 = ms.runtime.Stream()
>>> with ms.runtime.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.runtime.Event.

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> ms.set_device("Ascend", 0)
>>> a = Tensor(np.ones([3, 3]), ms.float32)
>>> b = Tensor(np.ones([3, 3]), ms.float32)
>>> s1 = ms.runtime.Stream()
>>> with ms.runtime.StreamCtx(s1):
...     c = a + b
...     event = s1.record_event()
...     d = a * b
>>> cur_stream = ms.runtime.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.runtime.Stream.

Examples

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