mindspore.communication.comm_func.recv
- mindspore.communication.comm_func.recv(tensor, src=0, group=GlobalComm.WORLD_COMM_GROUP, tag=0)[源代码]
同步接收张量到指定线程。
说明
Send 和 Receive 算子需组合使用,且有同一个 tag。 输入的 tensor 的shape和dtype将用于接收张量,但 tensor 的数据值不起作用。 当前支持PyNative模式,不支持Graph模式。
- 参数:
tensor (Tensor) - 输入Tensor。Tensor的shape为 \((x_1, x_2, ..., x_R)\) 。 输入的 tensor 的shape和dtype将用于接收张量,但 tensor 的数据值不起作用。
src (int,可选) - 表示发送源的进程编号。只会接收来自源进程的张量。默认值:0。
group (str,可选) - 工作的通信组。(默认值:Ascend平台为
"hccl_world_group"
,GPU平台为"nccl_world_group"
)。tag (int,可选) - 用于区分发送、接收消息的标签。该消息将被接收来自相同 tag 的Send发送的张量。默认值:0。
- 返回:
Tensor,其shape为 \((x_1, x_2, ..., x_R)\)。
- 异常:
TypeError - src不是int或group不是str。
ValueError - 如果该线程的rank id 大于通信组的rank size。
- 支持平台:
Ascend
样例:
>>> import numpy as np >>> import mindspore as ms >>> from mindspore.communication import init >>> from mindspore.communication.comm_func import send, recv >>> from mindspore.communication import get_rank, get_group_size >>> >>> np.random.seed(1) >>> init() >>> rank = get_rank() >>> size = get_group_size() >>> x = np.ones([2, 2]).astype(np.float32) * 0.01 * (rank + 1) >>> x2 = np.ones([2, 2]).astype(np.float32) >>> >>> >>> if rank < size / 2: >>> _x = ms.Tensor(x) >>> send(_x, rank + size // 2) >>> else: >>> _x2 = ms.Tensor(x2) >>> output = recv(_x2, rank - size // 2) >>> print(output) [[0.01 0.01] [0.01 0.01]]