def collective_fn(input_tensor, output_tensor, context): pygloo.reduce( 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), gloo_util.get_gloo_reduce_op(reduce_options.reduceOp), root_rank)
def test_reduce(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) sendbuf = np.array([[1, 2, 3], [1, 2, 3]], dtype=np.float32) recvbuf = np.zeros_like(sendbuf, dtype=np.float32) sendptr = sendbuf.ctypes.data recvptr = recvbuf.ctypes.data # sendbuf = torch.Tensor([[1,2,3],[1,2,3]]).float() # recvbuf = torch.zeros_like(sendbuf) # sendptr = sendbuf.data_ptr() # recvptr = recvbuf.data_ptr() data_size = sendbuf.size if isinstance( sendbuf, np.ndarray) else sendbuf.numpy().size datatype = pygloo.glooDataType_t.glooFloat32 op = pygloo.ReduceOp.SUM root = 0 pygloo.reduce(context, sendptr, recvptr, data_size, datatype, op, root) print(f"rank {rank} sends {sendbuf}, receives {recvbuf}")