mindspore.ops.communication.recv

查看源文件
mindspore.ops.communication.recv(tensor, src=0, group=None, tag=0)[源代码]

从src接收张量。

说明

当前仅支持PyNative模式,不支持Graph模式。

参数:
  • tensor (Tensor) - 如果函数以in-place模式运行,则用于填充接收数据的张量。否则,表示用于接收张量的shape和dtype,但输入 tensor 的值不会生效。

  • src (int, 可选) - 标识源rank(全局rank)的整数。默认值: 0

  • group (str,可选) - 通信组名称。默认值: None ,即Ascend平台表示为 "hccl_world_group"

  • tag (int, 可选) - 标识发送/接收消息标签的整数。消息将被具有相同 tag 的Send操作接收。默认值: 0。当前为预留参数。

返回:
  • 若函数以in-place模式运行,返回int。如果成功,返回 0

  • 若函数以非in-place模式运行,返回Tensor。输出shape为 \((x_1, x_2, ..., x_R)\)

异常:
  • TypeError - tensor 不是Tensor,src 不是int或 group 不是str。

  • ValueError - 如果进程的rank ID大于通信组的rank size。

支持平台:

Ascend CPU

样例:

说明

运行以下样例之前,需要配置好通信环境变量。

针对Ascend设备,推荐使用msrun启动方式,无第三方以及配置文件依赖。详见 msrun启动

该样例需要在2卡环境下运行。

>>> from mindspore.ops.communication import init_process_group
>>> from mindspore.ops.communication import send, recv, get_rank
>>> from mindspore import Tensor
>>> import numpy as np
>>>
# Launch 2 processes, Process 0 sends the array to Process 1.
>>> init_process_group()
>>> this_rank = get_rank()
>>> if this_rank == 0:
...     input_ = Tensor(np.ones([2, 8]).astype(np.float32))
...     send(input_, 1)
>>> if this_rank == 1:
...     x = Tensor(np.zeros([2, 8]).astype(np.float32))
...     out = recv(x, src=0)
...     print(x)
rank 1:
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]