Debian Codegraph @

src/mpi/coll/ireduce/ireduce_intra_sched_reduce_scatter_gather.c

raw symbols SCIP
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 /* An implementation of Rabenseifner's reduce algorithm (see
9 http://www.hlrs.de/mpi/myreduce.html).
10
11 This algorithm implements the reduce in two steps: first a
12 reduce-scatter, followed by a gather to the root. A
13 recursive-halving algorithm (beginning with processes that are
14 distance 1 apart) is used for the reduce-scatter, and a binomial tree
15 algorithm is used for the gather. The non-power-of-two case is
16 handled by dropping to the nearest lower power-of-two: the first
17 few odd-numbered processes send their data to their left neighbors
18 (rank-1), and the reduce-scatter happens among the remaining
19 power-of-two processes. If the root is one of the excluded
20 processes, then after the reduce-scatter, rank 0 sends its result to
21 the root and exits; the root now acts as rank 0 in the binomial tree
22 algorithm for gather.
23
24 For the power-of-two case, the cost for the reduce-scatter is
25 lgp.alpha + n.((p-1)/p).beta + n.((p-1)/p).gamma. The cost for the
26 gather to root is lgp.alpha + n.((p-1)/p).beta. Therefore, the
27 total cost is:
28 Cost = 2.lgp.alpha + 2.n.((p-1)/p).beta + n.((p-1)/p).gamma
29
30 For the non-power-of-two case, assuming the root is not one of the
31 odd-numbered processes that get excluded in the reduce-scatter,
32 Cost = (2.floor(lgp)+1).alpha + (2.((p-1)/p) + 1).n.beta +
33 n.(1+(p-1)/p).gamma
34 */
35 int MPIR_Ireduce_intra_sched_reduce_scatter_gathercxx . . $ MPIR_Ireduce_intra_sched_reduce_scatter_gather(56be901be05c574f).(const void *sendbuf, void *recvbuf,
36 MPI_Aintcxx . . $ MPI_Aint# count, MPI_Datatypecxx . . $ MPI_Datatype# datatype, MPI_Opcxx . . $ MPI_Op# op,
37 int root, MPIR_Commcxx . . $ MPIR_Comm# * comm_ptr, MPIR_Sched_tcxx . . $ MPIR_Sched_t# s)
38 {
39 int mpi_errno = MPI_SUCCESScxx . . $ `src/include/mpi.h:655:9`!;
40 int i, j, comm_size, rank, pof2, is_commutative ATTRIBUTEcxx . . $ `src/mpl/include/mpl_base.h:39:9`!((unused));
41 int rem, dst, newrank, newdst, mask, send_idx, recv_idx, last_idx;
42 int newroot, newdst_tree_root, newroot_tree_root;
43 void *tmp_buf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
44 MPI_Aintcxx . . $ MPI_Aint# true_lb, true_extent, extent;
45 MPIR_CHKLMEM_DECLcxx . . $ `src/include/mpir_mem.h:108:9`!();
46
47 MPIR_COMM_RANK_SIZEcxx . . $ `src/include/mpir_comm.h:412:9`!(comm_ptr, rank, comm_size);
48
49 /* NOTE: this algorithm is currently only correct for commutative operations */
50 is_commutative = MPIR_Op_is_commutativecxx . . $ MPIR_Op_is_commutative(7864480464b09eea).(op);
51
52 #ifdef HAVE_ERROR_CHECKINGcxx . . $ `src/include/mpichconf.h:269:9`!
53 MPIR_Assertcxx . . $ `src/include/mpir_assert.h:39:9`!(is_commutative);
54 #endif /* HAVE_ERROR_CHECKING */
55
56 /* Create a temporary buffer */
57 MPIR_Type_get_true_extent_implcxx . . $ MPIR_Type_get_true_extent_impl(76a2ea8aac5bbe77).(datatype, &true_lb, &true_extent);
58 MPIR_Datatype_get_extent_macrocxx . . $ `src/include/mpir_datatype.h:462:9`!(datatype, extent);
59
60 tmp_buf = MPIR_Sched_alloc_statecxx . . $ `src/mpid/ch4/include/mpid_sched.h:18:9`!(s, count * (MPL_MAXcxx . . $ `src/mpl/include/mpl_base.h:105:9`!(extent, true_extent)));
61 MPIR_ERR_CHKANDJUMPcxx . . $ `src/include/mpir_err.h:1034:9`!(!tmp_buf, mpi_errno, MPI_ERR_OTHERcxx . . $ `src/include/mpi.h:679:9`!, "**nomem");
62 /* adjust for potential negative lower bound in datatype */
63 tmp_buf = (void *) ((char *) tmp_buf - true_lb);
64
65 /* get nearest power-of-two less than or equal to comm_size */
66 pof2 = MPL_pof2cxx . . $ MPL_pof2(7864480464b09eea).(comm_size);
67
68 #ifdef HAVE_ERROR_CHECKINGcxx . . $ `src/include/mpichconf.h:269:9`!
69 MPIR_Assertcxx . . $ `src/include/mpir_assert.h:39:9`!(HANDLE_IS_BUILTINcxx . . $ `src/include/mpir_objects.h:177:9`!(op));
70 MPIR_Assertcxx . . $ `src/include/mpir_assert.h:39:9`!(count >= pof2);
71 #endif /* HAVE_ERROR_CHECKING */
72
73 rem = comm_size - pof2;
74
75 /* If I'm not the root, then my recvbuf may not be valid, therefore
76 * I have to allocate a temporary one */
77 if (rank != root) {
78 recvbuf = MPIR_Sched_alloc_statecxx . . $ `src/mpid/ch4/include/mpid_sched.h:18:9`!(s, count * (MPL_MAXcxx . . $ `src/mpl/include/mpl_base.h:105:9`!(extent, true_extent)));
79 MPIR_ERR_CHKANDJUMPcxx . . $ `src/include/mpir_err.h:1034:9`!(!recvbuf, mpi_errno, MPI_ERR_OTHERcxx . . $ `src/include/mpi.h:679:9`!, "**nomem");
80 recvbuf = (void *) ((char *) recvbuf - true_lb);
81 }
82
83 if ((rank != root) || (sendbuf != MPI_IN_PLACEcxx . . $ `src/include/mpi.h:555:9`!)) {
84 mpi_errno = MPIR_Sched_copycxx . . $ `src/mpid/ch4/include/mpid_sched.h:27:9`!(sendbuf, count, datatype, recvbuf, count, datatype, s);
85 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
86 }
87
88 /* In the non-power-of-two case, all odd-numbered
89 * processes of rank < 2*rem send their data to
90 * (rank-1). These odd-numbered processes no longer
91 * participate in the algorithm until the very end. The
92 * remaining processes form a nice power-of-two.
93 *
94 * Note that in MPI_Allreduce we have the even-numbered processes
95 * send data to odd-numbered processes. That is better for
96 * non-commutative operations because it doesn't require a
97 * buffer copy. However, for MPI_Reduce, the most common case
98 * is commutative operations with root=0. Therefore we want
99 * even-numbered processes to participate the computation for
100 * the root=0 case, in order to avoid an extra send-to-root
101 * communication after the reduce-scatter. In MPI_Allreduce it
102 * doesn't matter because all processes must get the result. */
103
104 if (rank < 2 * rem) {
105 if (rank % 2 != 0) { /* odd */
106 mpi_errno = MPIR_Sched_sendcxx . . $ `src/mpid/ch4/include/mpid_sched.h:20:9`!(recvbuf, count, datatype, rank - 1, comm_ptr, s);
107 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
108 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
109
110 /* temporarily set the rank to -1 so that this
111 * process does not pariticipate in recursive
112 * doubling */
113 newrank = -1;
114 } else { /* even */
115 mpi_errno = MPIR_Sched_recvcxx . . $ `src/mpid/ch4/include/mpid_sched.h:23:9`!(tmp_buf, count, datatype, rank + 1, comm_ptr, s);
116 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
117 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
118
119 /* do the reduction on received data. */
120 /* This algorithm is used only for predefined ops
121 * and predefined ops are always commutative. */
122 mpi_errno = MPIR_Sched_reducecxx . . $ `src/mpid/ch4/include/mpid_sched.h:26:9`!(tmp_buf, recvbuf, count, datatype, op, s);
123 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
124 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
125
126 /* change the rank */
127 newrank = rank / 2;
128 }
129 } else { /* rank >= 2*rem */
130 newrank = rank - rem;
131 }
132
133 /* for the reduce-scatter, calculate the count that
134 * each process receives and the displacement within
135 * the buffer */
136
137 /* We allocate these arrays on all processes, even if newrank=-1,
138 * because if root is one of the excluded processes, we will
139 * need them on the root later on below. */
140 MPI_Aintcxx . . $ MPI_Aint# *cnts, *disps;
141 MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(cnts, pof2 * sizeof(MPI_Aint));
142 MPIR_CHKLMEM_MALLOCcxx . . $ `src/include/mpir_mem.h:118:9`!(disps, pof2 * sizeof(MPI_Aint));
143
144 last_idx = send_idx = 0; /* suppress spurious compiler warnings */
145
146 if (newrank != -1) {
147 for (i = 0; i < (pof2 - 1); i++)
148 cnts[i] = count / pof2;
149 cnts[pof2 - 1] = count - (count / pof2) * (pof2 - 1);
150
151 if (pof2)
152 disps[0] = 0;
153 for (i = 1; i < pof2; i++)
154 disps[i] = disps[i - 1] + cnts[i - 1];
155
156 mask = 0x1;
157 send_idx = recv_idx = 0;
158 last_idx = pof2;
159 while (mask < pof2) {
160 newdst = newrank ^ mask;
161 /* find real rank of dest */
162 dst = (newdst < rem) ? newdst * 2 : newdst + rem;
163
164 MPI_Aintcxx . . $ MPI_Aint# send_cnt, recv_cnt;
165 send_cnt = recv_cnt = 0;
166 if (newrank < newdst) {
167 send_idx = recv_idx + pof2 / (mask * 2);
168 for (i = send_idx; i < last_idx; i++)
169 send_cnt += cnts[i];
170 for (i = recv_idx; i < send_idx; i++)
171 recv_cnt += cnts[i];
172 } else {
173 recv_idx = send_idx + pof2 / (mask * 2);
174 for (i = send_idx; i < recv_idx; i++)
175 send_cnt += cnts[i];
176 for (i = recv_idx; i < last_idx; i++)
177 recv_cnt += cnts[i];
178 }
179
180 /* Send data from recvbuf. Recv into tmp_buf */
181 mpi_errno = MPIR_Sched_sendcxx . . $ `src/mpid/ch4/include/mpid_sched.h:20:9`!(((char *) recvbuf + disps[send_idx] * extent),
182 send_cnt, datatype, dst, comm_ptr, s);
183 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
184 /* sendrecv, no barrier here */
185 mpi_errno = MPIR_Sched_recvcxx . . $ `src/mpid/ch4/include/mpid_sched.h:23:9`!(((char *) tmp_buf + disps[recv_idx] * extent),
186 recv_cnt, datatype, dst, comm_ptr, s);
187 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
188 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
189
190 /* tmp_buf contains data received in this step.
191 * recvbuf contains data accumulated so far */
192
193 /* This algorithm is used only for predefined ops
194 * and predefined ops are always commutative. */
195 mpi_errno = MPIR_Sched_reducecxx . . $ `src/mpid/ch4/include/mpid_sched.h:26:9`!(((char *) tmp_buf + disps[recv_idx] * extent),
196 ((char *) recvbuf + disps[recv_idx] * extent),
197 recv_cnt, datatype, op, s);
198 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
199 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
200
201 /* update send_idx for next iteration */
202 send_idx = recv_idx;
203 mask <<= 1;
204
205 /* update last_idx, but not in last iteration
206 * because the value is needed in the gather
207 * step below. */
208 if (mask < pof2)
209 last_idx = recv_idx + pof2 / mask;
210 }
211 }
212
213 /* now do the gather to root */
214
215 /* Is root one of the processes that was excluded from the
216 * computation above? If so, send data from newrank=0 to
217 * the root and have root take on the role of newrank = 0 */
218
219 if (root < 2 * rem) {
220 if (root % 2 != 0) {
221 if (rank == root) { /* recv */
222 /* initialize the arrays that weren't initialized */
223 for (i = 0; i < (pof2 - 1); i++)
224 cnts[i] = count / pof2;
225 cnts[pof2 - 1] = count - (count / pof2) * (pof2 - 1);
226
227 disps[0] = 0;
228 for (i = 1; i < pof2; i++)
229 disps[i] = disps[i - 1] + cnts[i - 1];
230
231 mpi_errno = MPIR_Sched_recvcxx . . $ `src/mpid/ch4/include/mpid_sched.h:23:9`!(recvbuf, cnts[0], datatype, 0, comm_ptr, s);
232 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
233 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
234
235 newrank = 0;
236 send_idx = 0;
237 last_idx = 2;
238 } else if (newrank == 0) { /* send */
239 mpi_errno = MPIR_Sched_sendcxx . . $ `src/mpid/ch4/include/mpid_sched.h:20:9`!(recvbuf, cnts[0], datatype, root, comm_ptr, s);
240 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
241 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
242 newrank = -1;
243 }
244 newroot = 0;
245 } else
246 newroot = root / 2;
247 } else {
248 newroot = root - rem;
249 }
250
251 if (newrank != -1) {
252 j = 0;
253 mask = 0x1;
254 while (mask < pof2) {
255 mask <<= 1;
256 j++;
257 }
258 mask >>= 1;
259 j--;
260 while (mask > 0) {
261 newdst = newrank ^ mask;
262
263 /* find real rank of dest */
264 dst = (newdst < rem) ? newdst * 2 : newdst + rem;
265 /* if root is playing the role of newdst=0, adjust for
266 * it */
267 if ((newdst == 0) && (root < 2 * rem) && (root % 2 != 0))
268 dst = root;
269
270 /* if the root of newdst's half of the tree is the
271 * same as the root of newroot's half of the tree, send to
272 * newdst and exit, else receive from newdst. */
273
274 newdst_tree_root = newdst >> j;
275 newdst_tree_root <<= j;
276
277 newroot_tree_root = newroot >> j;
278 newroot_tree_root <<= j;
279
280 MPI_Aintcxx . . $ MPI_Aint# send_cnt, recv_cnt;
281 send_cnt = recv_cnt = 0;
282 if (newrank < newdst) {
283 /* update last_idx except on first iteration */
284 if (mask != pof2 / 2)
285 last_idx = last_idx + pof2 / (mask * 2);
286
287 recv_idx = send_idx + pof2 / (mask * 2);
288 for (i = send_idx; i < recv_idx; i++)
289 send_cnt += cnts[i];
290 for (i = recv_idx; i < last_idx; i++)
291 recv_cnt += cnts[i];
292 } else {
293 recv_idx = send_idx - pof2 / (mask * 2);
294 for (i = send_idx; i < last_idx; i++)
295 send_cnt += cnts[i];
296 for (i = recv_idx; i < send_idx; i++)
297 recv_cnt += cnts[i];
298 }
299
300 if (newdst_tree_root == newroot_tree_root) {
301 /* send and exit */
302 /* Send data from recvbuf. Recv into tmp_buf */
303 mpi_errno = MPIR_Sched_sendcxx . . $ `src/mpid/ch4/include/mpid_sched.h:20:9`!(((char *) recvbuf + disps[send_idx] * extent),
304 send_cnt, datatype, dst, comm_ptr, s);
305 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
306 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
307 break;
308 } else {
309 /* recv and continue */
310 mpi_errno = MPIR_Sched_recvcxx . . $ `src/mpid/ch4/include/mpid_sched.h:23:9`!(((char *) recvbuf + disps[recv_idx] * extent),
311 recv_cnt, datatype, dst, comm_ptr, s);
312 MPIR_ERR_CHECKcxx . . $ `src/include/mpir_err.h:810:9`!(mpi_errno);
313 MPIR_SCHED_BARRIERcxx . . $ `src/include/mpir_nbc.h:93:9`!(s);
314 }
315
316 if (newrank > newdst)
317 send_idx = recv_idx;
318
319 mask >>= 1;
320 j--;
321 }
322 }
323
324 fn_exit:
325 MPIR_CHKLMEM_FREEALLcxx . . $ `src/include/mpir_mem.h:129:9`!();
326 return mpi_errno;
327 fn_fail:
328 goto fn_exit;
329 }