|
1 | 1 | __all__ = [ |
2 | | - # "mpi_allgather", |
| 2 | + "mpi_allgather", |
3 | 3 | "mpi_allreduce", |
4 | 4 | # "mpi_bcast", |
5 | 5 | # "mpi_asarray", |
6 | 6 | "mpi_send", |
7 | | - # "mpi_recv", |
| 7 | + "mpi_recv", |
| 8 | + "_prepare_allgather_inputs", |
| 9 | + "_unroll_allgather_recv" |
8 | 10 | ] |
9 | 11 |
|
10 | | -from typing import Optional |
| 12 | +from typing import Optional, Tuple |
11 | 13 |
|
12 | 14 | import numpy as np |
13 | 15 | from mpi4py import MPI |
14 | 16 | from pylops.utils.backend import get_module |
15 | 17 | from pylops_mpi.utils import deps |
16 | 18 |
|
| 19 | +# TODO: return type annotation for both cupy and numpy |
| 20 | +def _prepare_allgather_inputs(send_buf, send_buf_shapes, engine): |
| 21 | + r""" Prepare send_buf and recv_buf for NCCL allgather (nccl_allgather) |
| 22 | +
|
| 23 | + Buffered Allgather (MPI and NCCL) requires the sending buffer to have the same size for every device. |
| 24 | + Therefore, padding is required when the array is not evenly partitioned across |
| 25 | + all the ranks. The padding is applied such that the each dimension of the sending buffers |
| 26 | + is equal to the max size of that dimension across all ranks. |
| 27 | +
|
| 28 | + Similarly, each receiver buffer (recv_buf) is created with size equal to :math:n_rank \cdot send_buf.size |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + send_buf : :obj: `numpy.ndarray` or `cupy.ndarray` or array-like |
| 33 | + The data buffer from the local GPU to be sent for allgather. |
| 34 | + send_buf_shapes: :obj:`list` |
| 35 | + A list of shapes for each GPU send_buf (used to calculate padding size) |
| 36 | + engine : :obj:`str` |
| 37 | + Engine used to store array (``numpy`` or ``cupy``) |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + send_buf: :obj:`cupy.ndarray` |
| 42 | + A buffer containing the data and padded elements to be sent by this rank. |
| 43 | + recv_buf : :obj:`cupy.ndarray` |
| 44 | + An empty, padded buffer to gather data from all GPUs. |
| 45 | + """ |
| 46 | + ncp = get_module(engine) |
| 47 | + sizes_each_dim = list(zip(*send_buf_shapes)) |
| 48 | + send_shape = tuple(map(max, sizes_each_dim)) |
| 49 | + pad_size = [ |
| 50 | + (0, s_shape - l_shape) for s_shape, l_shape in zip(send_shape, send_buf.shape) |
| 51 | + ] |
| 52 | + |
| 53 | + send_buf = ncp.pad( |
| 54 | + send_buf, pad_size, mode="constant", constant_values=0 |
| 55 | + ) |
| 56 | + |
| 57 | + ndev = len(send_buf_shapes) |
| 58 | + recv_buf = ncp.zeros(ndev * send_buf.size, dtype=send_buf.dtype) |
| 59 | + |
| 60 | + return send_buf, recv_buf |
| 61 | + |
| 62 | + |
| 63 | +def _unroll_allgather_recv(recv_buf, padded_send_buf_shape, send_buf_shapes) -> list: |
| 64 | + r"""Unrolll recv_buf after Buffered Allgather (MPI and NCCL) |
| 65 | +
|
| 66 | + Remove the padded elements in recv_buff, extract an individual array from each device and return them as a list of arrays |
| 67 | + Each GPU may send array with a different shape, so the return type has to be a list of array |
| 68 | + instead of the concatenated array. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + recv_buf: :obj:`cupy.ndarray` or array-like |
| 73 | + The data buffer returned from nccl_allgather call |
| 74 | + padded_send_buf_shape: :obj:`tuple`:int |
| 75 | + The size of send_buf after padding used in nccl_allgather |
| 76 | + send_buf_shapes: :obj:`list` |
| 77 | + A list of original shapes for each GPU send_buf prior to padding |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + chunks: :obj:`list` |
| 82 | + A list of `cupy.ndarray` from each GPU with the padded element removed |
| 83 | + """ |
| 84 | + ndev = len(send_buf_shapes) |
| 85 | + # extract an individual array from each device |
| 86 | + chunk_size = np.prod(padded_send_buf_shape) |
| 87 | + chunks = [ |
| 88 | + recv_buf[i * chunk_size:(i + 1) * chunk_size] for i in range(ndev) |
| 89 | + ] |
| 90 | + |
| 91 | + # Remove padding from each array: the padded value may appear somewhere |
| 92 | + # in the middle of the flat array and thus the reshape and slicing for each dimension is required |
| 93 | + for i in range(ndev): |
| 94 | + slicing = tuple(slice(0, end) for end in send_buf_shapes[i]) |
| 95 | + chunks[i] = chunks[i].reshape(padded_send_buf_shape)[slicing] |
| 96 | + |
| 97 | + return chunks |
17 | 98 |
|
18 | 99 | def mpi_allreduce(base_comm: MPI.Comm, |
19 | 100 | send_buf, recv_buf=None, |
@@ -57,7 +138,27 @@ def mpi_allreduce(base_comm: MPI.Comm, |
57 | 138 | # For MIN and MAX which require recv_buf |
58 | 139 | base_comm.Allreduce(send_buf, recv_buf, op) |
59 | 140 | return recv_buf |
60 | | - |
| 141 | + |
| 142 | + |
| 143 | +def mpi_allgather(base_comm: MPI.Comm, |
| 144 | + send_buf, recv_buf=None, |
| 145 | + engine: Optional[str] = "numpy", |
| 146 | + ) -> np.ndarray: |
| 147 | + |
| 148 | + if deps.cuda_aware_mpi_enabled or engine == "numpy": |
| 149 | + send_shapes = base_comm.allgather(send_buf.shape) |
| 150 | + (padded_send, padded_recv) = _prepare_allgather_inputs(send_buf, send_shapes, engine=engine) |
| 151 | + recv_buffer_to_use = recv_buf if recv_buf else padded_recv |
| 152 | + base_comm.Allgather(padded_send, recv_buffer_to_use) |
| 153 | + return _unroll_allgather_recv(recv_buffer_to_use, padded_send.shape, send_shapes) |
| 154 | + |
| 155 | + else: |
| 156 | + # CuPy with non-CUDA-aware MPI |
| 157 | + if recv_buf is None: |
| 158 | + return base_comm.allgather(send_buf) |
| 159 | + base_comm.Allgather(send_buf, recv_buf) |
| 160 | + return recv_buf |
| 161 | + |
61 | 162 |
|
62 | 163 | def mpi_send(base_comm: MPI.Comm, |
63 | 164 | send_buf, dest, count, tag=0, |
|
0 commit comments