| 1 | /* |
| 2 | * Copyright (C) by Argonne National Laboratory |
| 3 | * See COPYRIGHT in top-level directory |
| 4 | */ |
| 5 | |
| 6 | #include "mpiimpl.h"cxx . . $ `<file>/src/include/mpiimpl.h`/ |
| 7 | |
| 8 | /* |
| 9 | === BEGIN_MPI_T_CVAR_INFO_BLOCK === |
| 10 | |
| 11 | cvars: |
| 12 | - name : MPIR_CVAR_ALLTOALL_BRUCKS_KVAL |
| 13 | category : COLLECTIVE |
| 14 | type : int |
| 15 | default : 2 |
| 16 | class : none |
| 17 | verbosity : MPI_T_VERBOSITY_USER_BASIC |
| 18 | scope : MPI_T_SCOPE_ALL_EQ |
| 19 | description : >- |
| 20 | radix (k) value for generic transport brucks based alltoall |
| 21 | |
| 22 | === END_MPI_T_CVAR_INFO_BLOCK === |
| 23 | */ |
| 24 | |
| 25 | /* Brucks Pack, UnPack (PUP) utility function |
| 26 | * This functions packs (unpacks) non-contiguous (contiguous) |
| 27 | * data from (to) rbuf to (from) pupbuf. It goes to every offset |
| 28 | * that has the value "digitval" at the "phase"th digit in the |
| 29 | * base k representation of the offset. The argument pow_k_phase |
| 30 | * is MPL_ipow(k, phase) where phase corresponds to the phase in |
| 31 | * the brucks algorithm. */ |
| 32 | static int |
| 33 | brucks_sched_pupcxx . . $ brucks_sched_pup(bb5d5551f8e6d3fa).(int pack, void *rbuf, void *pupbuf, MPI_Datatypecxx . . $ MPI_Datatype# rtype, MPI_Aintcxx . . $ MPI_Aint# count, |
| 34 | int pow_k_phase, int k, int digitval, int comm_size, int *pupsize) |
| 35 | { |
| 36 | MPI_Aintcxx . . $ MPI_Aint# type_extent, type_lb, type_true_extent; |
| 37 | int offset, nconsecutive_occurrences, delta; |
| 38 | int mpi_errno = MPI_SUCCESScxx . . $ `src/include/mpi.h:655:9`!; |
| 39 | |
| 40 | MPIR_Datatype_get_extent_macrocxx . . $ `src/include/mpir_datatype.h:462:9`!(rtype, type_extent); |
| 41 | MPIR_Type_get_true_extent_implcxx . . $ MPIR_Type_get_true_extent_impl(76a2ea8aac5bbe77).(rtype, &type_lb, &type_true_extent); |
| 42 | type_extent = MPL_MAXcxx . . $ `src/mpl/include/mpl_base.h:105:9`!(type_extent, type_true_extent); |
| 43 | |
| 44 | /* first offset where the phase'th bit has value digitval */ |
| 45 | offset = pow_k_phase * digitval; |
| 46 | /* number of consecutive occurrences of digitval */ |
| 47 | nconsecutive_occurrences = pow_k_phase; |
| 48 | /* distance between non-consecutive occurrences of digitval */ |
| 49 | delta = (k - 1) * pow_k_phase; |
| 50 | |
| 51 | *pupsize = 0; /* points to the first empty location in pupbuf */ |
| 52 | while (offset < comm_size) { |
| 53 | if (pack) { |
| 54 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).((char *) rbuf + offset * count * type_extent, count, rtype, |
| 55 | (char *) pupbuf + *pupsize, count, rtype); |
| 56 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 57 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 58 | (MPL_DBG_FDEST, "packing rbuf+%ld to pupbuf+%d\n", |
| 59 | offset * count * type_extent, *pupsize)); |
| 60 | } else { |
| 61 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).((char *) pupbuf + *pupsize, count, rtype, |
| 62 | (char *) rbuf + offset * count * type_extent, count, rtype); |
| 63 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 64 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 65 | (MPL_DBG_FDEST, "unpacking from pupbuf+%d to rbuf+%ld\n", *pupsize, |
| 66 | offset * count * type_extent)); |
| 67 | } |
| 68 | |
| 69 | offset += 1; |
| 70 | nconsecutive_occurrences -= 1; |
| 71 | |
| 72 | if (nconsecutive_occurrences == 0) { /* consecutive occurrences are over */ |
| 73 | offset += delta; |
| 74 | nconsecutive_occurrences = pow_k_phase; |
| 75 | } |
| 76 | |
| 77 | *pupsize += count * type_extent; |
| 78 | } |
| 79 | fn_exit: |
| 80 | return mpi_errno; |
| 81 | fn_fail: |
| 82 | goto fn_exit; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* Algorithm: Bruck's algorithm with radix k |
| 87 | * |
| 88 | * This algorithm is used for small message sizes. It is a modified version |
| 89 | * from the IEEE TPDS Nov 97 paper by Jehoshua Bruck et al. It modifies |
| 90 | * the original bruck algorithm to work with any radix k(k >= 2). It takes |
| 91 | * logk(p) 'phases' to complete and performs (k - 1) sends and recvs |
| 92 | * per 'phase'. When working with radix k, it reads the index of each |
| 93 | * data location in base k. At each substep j, where 0 < j < k, in a phase, |
| 94 | * it packs and sends (recvs and unpacks) the data whose 'phase'th least |
| 95 | * significant digit is j. |
| 96 | * For cases when p is a perfect power of k, |
| 97 | * Cost = logk(p) * alpha + (n/p) * (p - 1) * logk(p) * beta |
| 98 | * where n is the total amount of data a process needs to send to all |
| 99 | * other processes. The algorithm also works for non power of k cases. |
| 100 | * */ |
| 101 | |
| 102 | int MPIR_Alltoall_intra_k_bruckscxx . . $ MPIR_Alltoall_intra_k_brucks(613430dbefb15b08).(const void *sendbuf, |
| 103 | MPI_Aintcxx . . $ MPI_Aint# sendcnt, |
| 104 | MPI_Datatypecxx . . $ MPI_Datatype# sendtype, |
| 105 | void *recvbuf, |
| 106 | MPI_Aintcxx . . $ MPI_Aint# recvcnt, |
| 107 | MPI_Datatypecxx . . $ MPI_Datatype# recvtype, MPIR_Commcxx . . $ MPIR_Comm# * comm, int k, int coll_attr) |
| 108 | { |
| 109 | int mpi_errno = MPI_SUCCESScxx . . $ `src/include/mpi.h:655:9`!; |
| 110 | int i, j; |
| 111 | int rank, size; |
| 112 | int nphases, max; |
| 113 | int p_of_k; /* largest power of k that is smaller than 'size' */ |
| 114 | int is_inplace; |
| 115 | MPI_Aintcxx . . $ MPI_Aint# s_extent, s_lb, r_extent, r_lb; |
| 116 | MPI_Aintcxx . . $ MPI_Aint# s_true_extent, r_true_extent; |
| 117 | int delta, src, dst; |
| 118 | void **tmp_sbuf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, **tmp_rbuf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 119 | int packsize; |
| 120 | void *tmp_buf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 121 | const void *senddata; |
| 122 | MPIR_Requestcxx . . $ MPIR_Request# **reqs; |
| 123 | int num_reqs = 0; |
| 124 | |
| 125 | MPIR_CHKLMEM_DECLcxx . . $ `src/include/mpir_mem.h:108:9`!(); |
| 126 | |
| 127 | MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(reqs, (2 * (k - 1) * sizeof(MPIR_Request *))); |
| 128 | |
| 129 | is_inplace = (sendbuf == MPI_IN_PLACEcxx . . $ `src/include/mpi.h:555:9`!); |
| 130 | |
| 131 | MPIR_COMM_RANK_SIZEcxx . . $ `src/include/mpir_comm.h:412:9`!(comm, rank, size); |
| 132 | |
| 133 | nphases = 0; |
| 134 | max = size - 1; |
| 135 | |
| 136 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 137 | (MPL_DBG_FDEST, "alltoall_blocking_brucks: num_ranks: %d, k: %d \n", size, k)); |
| 138 | /* calculate the number of bits required to represent a rank in base k */ |
| 139 | while (max) { |
| 140 | nphases++; |
| 141 | max /= k; |
| 142 | } |
| 143 | |
| 144 | /* calculate largest power of k that is smaller than 'size'. |
| 145 | * This is used for allocating temporary space for sending |
| 146 | * and receiving data. */ |
| 147 | p_of_k = 1; |
| 148 | for (i = 0; i < nphases - 1; i++) { |
| 149 | p_of_k *= k; |
| 150 | } |
| 151 | |
| 152 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, (MPL_DBG_FDEST, "Num phases: %d\n", nphases)); |
| 153 | |
| 154 | if (is_inplace) { |
| 155 | sendcnt = recvcnt; |
| 156 | sendtype = recvtype; |
| 157 | } |
| 158 | |
| 159 | MPIR_Datatype_get_extent_macrocxx . . $ `src/include/mpir_datatype.h:462:9`!(sendtype, s_extent); |
| 160 | MPIR_Type_get_true_extent_implcxx . . $ MPIR_Type_get_true_extent_impl(76a2ea8aac5bbe77).(sendtype, &s_lb, &s_true_extent); |
| 161 | s_extent = MPL_MAXcxx . . $ `src/mpl/include/mpl_base.h:105:9`!(s_extent, s_true_extent); |
| 162 | |
| 163 | MPIR_Datatype_get_extent_macrocxx . . $ `src/include/mpir_datatype.h:462:9`!(recvtype, r_extent); |
| 164 | MPIR_Type_get_true_extent_implcxx . . $ MPIR_Type_get_true_extent_impl(76a2ea8aac5bbe77).(recvtype, &r_lb, &r_true_extent); |
| 165 | r_extent = MPL_MAXcxx . . $ `src/mpl/include/mpl_base.h:105:9`!(r_extent, r_true_extent); |
| 166 | |
| 167 | #ifdef MPL_USE_DBG_LOGGING |
| 168 | { |
| 169 | MPI_Aint s_type_size; |
| 170 | MPIR_Datatype_get_size_macro(sendtype, s_type_size); |
| 171 | MPL_DBG_MSG_FMT(MPIR_DBG_COLL, VERBOSE, |
| 172 | (MPL_DBG_FDEST, |
| 173 | "send_type_size: %ld, send_type_extent: %ld, send_count: %ld\n", |
| 174 | s_type_size, s_extent, sendcnt)); |
| 175 | } |
| 176 | #endif |
| 177 | /* temporary buffer used for rotation, so used as sendbuf when inplace is true */ |
| 178 | MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(tmp_buf, recvcnt * size * r_extent); |
| 179 | |
| 180 | if (is_inplace) { |
| 181 | mpi_errno = |
| 182 | MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).(recvbuf, size * recvcnt, recvtype, tmp_buf, size * recvcnt, recvtype); |
| 183 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 184 | senddata = tmp_buf; |
| 185 | } else { |
| 186 | senddata = sendbuf; |
| 187 | } |
| 188 | |
| 189 | /* Step 1: rotate the data locally */ |
| 190 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).((void *) ((char *) senddata + rank * sendcnt * s_extent), |
| 191 | (size - rank) * sendcnt, sendtype, recvbuf, |
| 192 | (size - rank) * recvcnt, recvtype); |
| 193 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 194 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).(senddata, rank * sendcnt, sendtype, |
| 195 | (void *) ((char *) recvbuf + (size - rank) * recvcnt * r_extent), |
| 196 | rank * recvcnt, recvtype); |
| 197 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 198 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, (MPL_DBG_FDEST, "Step 1 data rotation scheduled\n")); |
| 199 | |
| 200 | /* Step 2: Allocate buffer space for packing/receiving data for every phase */ |
| 201 | delta = 1; |
| 202 | |
| 203 | MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(tmp_sbuf, sizeof(void *) * (k - 1)); |
| 204 | MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(tmp_rbuf, sizeof(void *) * (k - 1)); |
| 205 | |
| 206 | for (j = 0; j < k - 1; j++) { |
| 207 | tmp_sbuf[j] = (void *) MPL_malloccxx . . $ MPL_malloc(47e2c277390bf45).(r_extent * recvcnt * p_of_k, MPL_MEM_COLLcxx . . $ MPL_MEM_COLL.); |
| 208 | MPIR_ERR_CHKANDJUMPcxx . . $ `src/include/mpir_err.h:1034:9`!(!tmp_sbuf[j], mpi_errno, MPI_ERR_OTHERcxx . . $ `src/include/mpi.h:679:9`!, "**nomem"); |
| 209 | tmp_rbuf[j] = (void *) MPL_malloccxx . . $ MPL_malloc(47e2c277390bf45).(r_extent * recvcnt * p_of_k, MPL_MEM_COLLcxx . . $ MPL_MEM_COLL.); |
| 210 | MPIR_ERR_CHKANDJUMPcxx . . $ `src/include/mpir_err.h:1034:9`!(!tmp_rbuf[j], mpi_errno, MPI_ERR_OTHERcxx . . $ `src/include/mpi.h:679:9`!, "**nomem"); |
| 211 | } |
| 212 | |
| 213 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 214 | (MPL_DBG_FDEST, "Allocated temporary buffer space for packing\n")); |
| 215 | |
| 216 | packsize = 0; |
| 217 | |
| 218 | /* Post (k - 1) sends/recvs for each of the nphases */ |
| 219 | for (i = 0; i < nphases; i++) { |
| 220 | num_reqs = 0; |
| 221 | for (j = 1; j < k; j++) { /* for every non-zero value of digitval */ |
| 222 | if (delta * j >= size) /* if the first location exceeds comm size, nothing is to be sent */ |
| 223 | break; |
| 224 | |
| 225 | src = (rank - delta * j + size) % size; |
| 226 | dst = (rank + delta * j) % size; |
| 227 | |
| 228 | /* pack data to be sent */ |
| 229 | mpi_errno = |
| 230 | brucks_sched_pupcxx . . $ brucks_sched_pup(bb5d5551f8e6d3fa).(1, recvbuf, tmp_sbuf[j - 1], recvtype, recvcnt, delta, k, j, |
| 231 | size, &packsize); |
| 232 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 233 | |
| 234 | mpi_errno = |
| 235 | MPIC_Irecvcxx . . $ MPIC_Irecv(d5a3955da26b940f).(tmp_rbuf[j - 1], packsize, MPIR_BYTE_INTERNALcxx . . $ `src/include/mpichconf.h:778:9`!, src, MPIR_ALLTOALL_TAGcxx . . $ `src/include/mpir_tags.h:19:9`!, |
| 236 | comm, &reqs[num_reqs++]); |
| 237 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 238 | |
| 239 | mpi_errno = |
| 240 | MPIC_Isendcxx . . $ MPIC_Isend(92f04c3ea3bbad99).(tmp_sbuf[j - 1], packsize, MPIR_BYTE_INTERNALcxx . . $ `src/include/mpichconf.h:778:9`!, dst, MPIR_ALLTOALL_TAGcxx . . $ `src/include/mpir_tags.h:19:9`!, |
| 241 | comm, &reqs[num_reqs++], coll_attr); |
| 242 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 243 | } |
| 244 | |
| 245 | MPIC_Waitallcxx . . $ MPIC_Waitall(96058919363a54c1).(num_reqs, reqs, MPI_STATUSES_IGNOREcxx . . $ `src/include/mpi.h:561:9`!); |
| 246 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 247 | |
| 248 | for (j = 1; j < k; j++) { |
| 249 | if (delta * j >= size) /* if the first location exceeds comm size, nothing is to be sent */ |
| 250 | break; |
| 251 | |
| 252 | /* Unpack received data */ |
| 253 | mpi_errno = |
| 254 | brucks_sched_pupcxx . . $ brucks_sched_pup(bb5d5551f8e6d3fa).(0, recvbuf, tmp_rbuf[j - 1], recvtype, recvcnt, delta, k, j, |
| 255 | size, &packsize); |
| 256 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 257 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 258 | (MPL_DBG_FDEST, "phase %d, digit %d unpacking scheduled\n", i, j)); |
| 259 | } |
| 260 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, (MPL_DBG_FDEST, "phase %d scheduled\n", i)); |
| 261 | |
| 262 | delta *= k; |
| 263 | } |
| 264 | |
| 265 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, (MPL_DBG_FDEST, "Step 2 scheduled\n")); |
| 266 | |
| 267 | /* Step 3: rotate the buffer */ |
| 268 | /* TODO: MPICH implementation does some lower_bound adjustment here for |
| 269 | * derived datatypes, skipping that for now, will come back |
| 270 | * to it later on - will require adding API for getting true_lb */ |
| 271 | |
| 272 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).((void *) ((char *) recvbuf + (rank + 1) * recvcnt * r_extent), |
| 273 | (size - rank - 1) * recvcnt, recvtype, tmp_buf, |
| 274 | (size - rank - 1) * recvcnt, recvtype); |
| 275 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 276 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).(recvbuf, (rank + 1) * recvcnt, recvtype, |
| 277 | (void *) ((char *) tmp_buf + |
| 278 | (size - rank - 1) * recvcnt * r_extent), |
| 279 | (rank + 1) * recvcnt, recvtype); |
| 280 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 281 | |
| 282 | /* invert the buffer now to get the result in desired order */ |
| 283 | for (i = 0; i < size; i++) { |
| 284 | mpi_errno = MPIR_Localcopycxx . . $ MPIR_Localcopy(cf422982ceb8df2).((char *) tmp_buf + i * recvcnt * r_extent, recvcnt, recvtype, |
| 285 | (void *) ((char *) recvbuf + |
| 286 | (size - i - 1) * recvcnt * r_extent), recvcnt, |
| 287 | recvtype); |
| 288 | MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno); |
| 289 | } |
| 290 | |
| 291 | MPL_DBG_MSG_FMTcxx . . $ `src/mpl/include/mpl_dbg.h:100:9`!(MPIR_DBG_COLL, VERBOSE, |
| 292 | (MPL_DBG_FDEST, "Step 3: data rearrangement scheduled\n")); |
| 293 | |
| 294 | for (i = 0; i < k - 1; i++) { |
| 295 | MPL_freecxx . . $ `src/mpl/include/mpl_trmem.h:308:9`!(tmp_sbuf[i]); |
| 296 | MPL_freecxx . . $ `src/mpl/include/mpl_trmem.h:308:9`!(tmp_rbuf[i]); |
| 297 | } |
| 298 | |
| 299 | MPIR_CHKLMEM_FREEALLcxx . . $ `src/include/mpir_mem.h:129:9`!(); |
| 300 | |
| 301 | fn_exit: |
| 302 | return mpi_errno; |
| 303 | fn_fail: |
| 304 | goto fn_exit; |
| 305 | } |