mindspore.ops.Barrier

View Source On Gitee
class mindspore.ops.Barrier(group=GlobalComm.WORLD_COMM_GROUP)[source]

Synchronizes all processes in the specified group. Once the process call this operation, it will be blocked until all processes call this operation. After all processes finish calling the operations, the blocked processes will be waken and continue their task.

Parameters

group (str, optional) – The communication group to work on. Default: GlobalComm.WORLD_COMM_GROUP.

Raises
  • TypeError – If group is not a str.

  • RuntimeError – If backend is invalid, or distributed initialization fails.

  • ValueError – If the local rank id of the calling process in the group is larger than the group's rank size.

Supported Platforms:

Ascend

Examples

Note

Before running the following examples, you need to configure the communication environment variables.

For Ascend/GPU/CPU devices, it is recommended to use the msrun startup method without any third-party or configuration file dependencies. Please see the msrun start up for more details.

This example should be run with 2 devices.

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore.communication import init
>>> from mindspore import Tensor
>>> from mindspore import ops
>>> # Launch 4 processes.
>>> init()
>>> class BarrierNet(nn.Cell):
>>>     def __init__(self):
>>>         super(BarrierNet, self).__init__()
>>>         self.barrier = ops.Barrier()
>>>
>>>     def construct(self):
>>>         self.barrier()
>>> net = BarrierNet()
>>> net()
Tutorial Examples: