def collective_fn(input_tensor, output_tensor, context): pygloo.broadcast( context, gloo_util.get_tensor_ptr(input_tensor), gloo_util.get_tensor_ptr(output_tensor), gloo_util.get_tensor_n_elements(input_tensor), gloo_util.get_gloo_tensor_dtype(input_tensor), root_rank, )
def test_broadcast(rank, world_size, fileStore_path): ''' rank # Rank of this process within list of participating processes world_size # Number of participating processes ''' if rank==0: if os.path.exists(fileStore_path): shutil.rmtree(fileStore_path) os.makedirs(fileStore_path) else: time.sleep(0.5) context = pygloo.rendezvous.Context(rank, world_size) attr = pygloo.transport.tcp.attr("localhost") # Perform rendezvous for TCP pairs dev = pygloo.transport.tcp.CreateDevice(attr) fileStore = pygloo.rendezvous.FileStore(fileStore_path) store = pygloo.rendezvous.PrefixStore(str(world_size), fileStore) context.connectFullMesh(store, dev) if rank == 0: sendbuf = np.array([[1,2,3],[1,2,3]], dtype=np.float32) sendptr = sendbuf.ctypes.data else: sendbuf = np.zeros((2,3), dtype=np.float32) sendptr = -1 recvbuf = np.zeros_like(sendbuf, dtype=np.float32) recvptr = recvbuf.ctypes.data # if rank == 0: # sendbuf = torch.Tensor([[1,2,3],[1,2,3]]).float() # sendptr = sendbuf.data_ptr() # else: # sendbuf = torch.zeros(2,3) # sendptr = 0 # recvbuf = torch.zeros_like(sendbuf) # recvptr = recvbuf.data_ptr() data_size = sendbuf.size if isinstance(sendbuf, np.ndarray) else sendbuf.numpy().size datatype = pygloo.glooDataType_t.glooFloat32 root = 0 pygloo.broadcast(context, sendptr, recvptr, data_size, datatype, root) print(f"rank {rank} sends {sendbuf}, receives {recvbuf}")