broadcast

paddle.distributed. broadcast ( tensor, src, group=None, use_calc_stream=True ) [source]

Broadcast a tensor from the source to all others. As shown below, 4 GPUs each start 4 processes and GPU0 owns data 0. Through broadcast operator, the data 0 will be sent to all GPUs from GPU0.

broadcast
Parameters
  • tensor (Tensor) – The Tensor to send if current rank is the source, or the tensor to receive otherwise. Its data type should be float16, float32, float64, int32 or int64.

  • src (int) – The source rank.

  • group (Group) – The group instance return by new_group or None for global default group.

  • use_calc_stream (bool) – Wether to use calculation stream (True) or communication stream (False). Default to True.

Returns

None.

Examples

# required: distributed
import numpy as np
import paddle
from paddle.distributed import init_parallel_env

paddle.set_device('gpu:%d'%paddle.distributed.ParallelEnv().dev_id)
init_parallel_env()
if paddle.distributed.ParallelEnv().local_rank == 0:
    np_data = np.array([[4, 5, 6], [4, 5, 6]])
else:
    np_data = np.array([[1, 2, 3], [1, 2, 3]])
data = paddle.to_tensor(np_data)
paddle.distributed.broadcast(data, 1)
out = data.numpy()
# [[1, 2, 3], [1, 2, 3]]