Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix warnings + refactor
[simgrid.git] / src / smpi / colls / reduce_scatter / reduce_scatter-mpich.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "../colls_private.hpp"
8
9 static inline int MPIU_Mirror_permutation(unsigned int x, int bits)
10 {
11     /* a mask for the high order bits that should be copied as-is */
12     int high_mask = ~((0x1 << bits) - 1);
13     int retval = x & high_mask;
14     int i;
15
16     for (i = 0; i < bits; ++i) {
17         unsigned int bitval = (x & (0x1 << i)) >> i; /* 0x1 or 0x0 */
18         retval |= bitval << ((bits - i) - 1);
19     }
20
21     return retval;
22 }
23 namespace simgrid{
24 namespace smpi{
25
26 int Coll_reduce_scatter_mpich_pair::reduce_scatter(void *sendbuf, void *recvbuf, int recvcounts[],
27                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
28 {
29     int   rank, comm_size, i;
30     MPI_Aint extent, true_extent, true_lb;
31     int  *disps;
32     void *tmp_recvbuf;
33     int mpi_errno = MPI_SUCCESS;
34     int total_count, dst, src;
35     int is_commutative;
36     comm_size = comm->size();
37     rank = comm->rank();
38
39     extent =datatype->get_extent();
40     datatype->extent(&true_lb, &true_extent);
41
42     if (op->is_commutative()) {
43         is_commutative = 1;
44     }
45
46     disps = (int*)xbt_malloc( comm_size * sizeof(int));
47
48     total_count = 0;
49     for (i=0; i<comm_size; i++) {
50         disps[i] = total_count;
51         total_count += recvcounts[i];
52     }
53
54     if (total_count == 0) {
55         xbt_free(disps);
56         return MPI_ERR_COUNT;
57     }
58
59         if (sendbuf != MPI_IN_PLACE) {
60             /* copy local data into recvbuf */
61             Datatype::copy(((char *)sendbuf+disps[rank]*extent),
62                                        recvcounts[rank], datatype, recvbuf,
63                                        recvcounts[rank], datatype);
64         }
65
66         /* allocate temporary buffer to store incoming data */
67         tmp_recvbuf = (void*)smpi_get_tmp_recvbuffer(recvcounts[rank]*(MAX(true_extent,extent))+1);
68         /* adjust for potential negative lower bound in datatype */
69         tmp_recvbuf = (void *)((char*)tmp_recvbuf - true_lb);
70
71         for (i=1; i<comm_size; i++) {
72             src = (rank - i + comm_size) % comm_size;
73             dst = (rank + i) % comm_size;
74
75             /* send the data that dst needs. recv data that this process
76                needs from src into tmp_recvbuf */
77             if (sendbuf != MPI_IN_PLACE)
78                 Request::sendrecv(((char *)sendbuf+disps[dst]*extent),
79                                              recvcounts[dst], datatype, dst,
80                                              COLL_TAG_SCATTER, tmp_recvbuf,
81                                              recvcounts[rank], datatype, src,
82                                              COLL_TAG_SCATTER, comm,
83                                              MPI_STATUS_IGNORE);
84             else
85                 Request::sendrecv(((char *)recvbuf+disps[dst]*extent),
86                                              recvcounts[dst], datatype, dst,
87                                              COLL_TAG_SCATTER, tmp_recvbuf,
88                                              recvcounts[rank], datatype, src,
89                                              COLL_TAG_SCATTER, comm,
90                                              MPI_STATUS_IGNORE);
91
92             if (is_commutative || (src < rank)) {
93                 if (sendbuf != MPI_IN_PLACE) {
94                   if (op != MPI_OP_NULL)
95                     op->apply(tmp_recvbuf, recvbuf, &recvcounts[rank], datatype);
96                 }
97                 else {
98                   if (op != MPI_OP_NULL)
99                     op->apply(tmp_recvbuf, ((char*)recvbuf + disps[rank] * extent), &recvcounts[rank], datatype);
100                   /* we can't store the result at the beginning of
101                      recvbuf right here because there is useful data
102                      there that other process/processes need. at the
103                      end, we will copy back the result to the
104                      beginning of recvbuf. */
105                 }
106             }
107             else {
108                 if (sendbuf != MPI_IN_PLACE) {
109                   if (op != MPI_OP_NULL)
110                     op->apply(recvbuf, tmp_recvbuf, &recvcounts[rank], datatype);
111                   /* copy result back into recvbuf */
112                   mpi_errno =
113                       Datatype::copy(tmp_recvbuf, recvcounts[rank], datatype, recvbuf, recvcounts[rank], datatype);
114                   if (mpi_errno)
115                     return (mpi_errno);
116                 }
117                 else {
118                   if (op != MPI_OP_NULL)
119                     op->apply(((char*)recvbuf + disps[rank] * extent), tmp_recvbuf, &recvcounts[rank], datatype);
120                   /* copy result back into recvbuf */
121                   mpi_errno = Datatype::copy(tmp_recvbuf, recvcounts[rank], datatype,
122                                              ((char*)recvbuf + disps[rank] * extent), recvcounts[rank], datatype);
123                   if (mpi_errno)
124                     return (mpi_errno);
125                 }
126             }
127         }
128
129         /* if MPI_IN_PLACE, move output data to the beginning of
130            recvbuf. already done for rank 0. */
131         if ((sendbuf == MPI_IN_PLACE) && (rank != 0)) {
132             mpi_errno = Datatype::copy(((char *)recvbuf +
133                                         disps[rank]*extent),
134                                        recvcounts[rank], datatype,
135                                        recvbuf,
136                                        recvcounts[rank], datatype );
137             if (mpi_errno) return(mpi_errno);
138         }
139
140         xbt_free(disps);
141         smpi_free_tmp_buffer(tmp_recvbuf);
142
143         return MPI_SUCCESS;
144 }
145
146
147 int Coll_reduce_scatter_mpich_noncomm::reduce_scatter(void *sendbuf, void *recvbuf, int recvcounts[],
148                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
149 {
150     int mpi_errno = MPI_SUCCESS;
151     int comm_size = comm->size() ;
152     int rank = comm->rank();
153     int pof2;
154     int log2_comm_size;
155     int i, k;
156     int recv_offset, send_offset;
157     int block_size, total_count, size;
158     MPI_Aint true_extent, true_lb;
159     int buf0_was_inout;
160     void *tmp_buf0;
161     void *tmp_buf1;
162     void *result_ptr;
163
164     datatype->extent(&true_lb, &true_extent);
165
166     pof2 = 1;
167     log2_comm_size = 0;
168     while (pof2 < comm_size) {
169         pof2 <<= 1;
170         ++log2_comm_size;
171     }
172
173     /* begin error checking */
174     xbt_assert(pof2 == comm_size); /* FIXME this version only works for power of 2 procs */
175
176     for (i = 0; i < (comm_size - 1); ++i) {
177         xbt_assert(recvcounts[i] == recvcounts[i+1]);
178     }
179     /* end error checking */
180
181     /* size of a block (count of datatype per block, NOT bytes per block) */
182     block_size = recvcounts[0];
183     total_count = block_size * comm_size;
184
185     tmp_buf0=( void *)smpi_get_tmp_sendbuffer( true_extent * total_count);
186     tmp_buf1=( void *)smpi_get_tmp_recvbuffer( true_extent * total_count);
187     void *tmp_buf0_save=tmp_buf0;
188     void *tmp_buf1_save=tmp_buf1;
189
190     /* adjust for potential negative lower bound in datatype */
191     tmp_buf0 = (void *)((char*)tmp_buf0 - true_lb);
192     tmp_buf1 = (void *)((char*)tmp_buf1 - true_lb);
193
194     /* Copy our send data to tmp_buf0.  We do this one block at a time and
195        permute the blocks as we go according to the mirror permutation. */
196     for (i = 0; i < comm_size; ++i) {
197         mpi_errno = Datatype::copy((char *)(sendbuf == MPI_IN_PLACE ? recvbuf : sendbuf) + (i * true_extent * block_size), block_size, datatype,
198                                    (char *)tmp_buf0 + (MPIU_Mirror_permutation(i, log2_comm_size) * true_extent * block_size), block_size, datatype);
199         if (mpi_errno) return(mpi_errno);
200     }
201     buf0_was_inout = 1;
202
203     send_offset = 0;
204     recv_offset = 0;
205     size = total_count;
206     for (k = 0; k < log2_comm_size; ++k) {
207         /* use a double-buffering scheme to avoid local copies */
208         char *incoming_data = static_cast<char*>(buf0_was_inout ? tmp_buf1 : tmp_buf0);
209         char *outgoing_data = static_cast<char*>(buf0_was_inout ? tmp_buf0 : tmp_buf1);
210         int peer = rank ^ (0x1 << k);
211         size /= 2;
212
213         if (rank > peer) {
214             /* we have the higher rank: send top half, recv bottom half */
215             recv_offset += size;
216         }
217         else {
218             /* we have the lower rank: recv top half, send bottom half */
219             send_offset += size;
220         }
221
222         Request::sendrecv(outgoing_data + send_offset*true_extent,
223                                      size, datatype, peer, COLL_TAG_SCATTER,
224                                      incoming_data + recv_offset*true_extent,
225                                      size, datatype, peer, COLL_TAG_SCATTER,
226                                      comm, MPI_STATUS_IGNORE);
227         /* always perform the reduction at recv_offset, the data at send_offset
228            is now our peer's responsibility */
229         if (rank > peer) {
230             /* higher ranked value so need to call op(received_data, my_data) */
231             if(op!=MPI_OP_NULL) op->apply(
232                    incoming_data + recv_offset*true_extent,
233                      outgoing_data + recv_offset*true_extent,
234                      &size, datatype );
235             /* buf0_was_inout = buf0_was_inout; */
236         }
237         else {
238             /* lower ranked value so need to call op(my_data, received_data) */
239             if (op != MPI_OP_NULL)
240               op->apply(outgoing_data + recv_offset * true_extent, incoming_data + recv_offset * true_extent, &size,
241                         datatype);
242             buf0_was_inout = not buf0_was_inout;
243         }
244
245         /* the next round of send/recv needs to happen within the block (of size
246            "size") that we just received and reduced */
247         send_offset = recv_offset;
248     }
249
250     xbt_assert(size == recvcounts[rank]);
251
252     /* copy the reduced data to the recvbuf */
253     result_ptr = (char *)(buf0_was_inout ? tmp_buf0 : tmp_buf1) + recv_offset * true_extent;
254     mpi_errno = Datatype::copy(result_ptr, size, datatype,
255                                recvbuf, size, datatype);
256     smpi_free_tmp_buffer(tmp_buf0_save);
257     smpi_free_tmp_buffer(tmp_buf1_save);
258     if (mpi_errno) return(mpi_errno);
259     return MPI_SUCCESS;
260 }
261
262
263
264 int Coll_reduce_scatter_mpich_rdb::reduce_scatter(void *sendbuf, void *recvbuf, int recvcounts[],
265                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
266 {
267     int   rank, comm_size, i;
268     MPI_Aint extent, true_extent, true_lb;
269     int  *disps;
270     void *tmp_recvbuf, *tmp_results;
271     int mpi_errno = MPI_SUCCESS;
272     int dis[2], blklens[2], total_count, dst;
273     int mask, dst_tree_root, my_tree_root, j, k;
274     int received;
275     MPI_Datatype sendtype, recvtype;
276     int nprocs_completed, tmp_mask, tree_root, is_commutative=0;
277     comm_size = comm->size();
278     rank = comm->rank();
279
280     extent =datatype->get_extent();
281     datatype->extent(&true_lb, &true_extent);
282
283     if ((op==MPI_OP_NULL) || op->is_commutative()) {
284         is_commutative = 1;
285     }
286
287     disps = (int*)xbt_malloc( comm_size * sizeof(int));
288
289     total_count = 0;
290     for (i=0; i<comm_size; i++) {
291         disps[i] = total_count;
292         total_count += recvcounts[i];
293     }
294
295             /* noncommutative and (non-pof2 or block irregular), use recursive doubling. */
296
297             /* need to allocate temporary buffer to receive incoming data*/
298             tmp_recvbuf= (void *) smpi_get_tmp_recvbuffer( total_count*(MAX(true_extent,extent)));
299             /* adjust for potential negative lower bound in datatype */
300             tmp_recvbuf = (void *)((char*)tmp_recvbuf - true_lb);
301
302             /* need to allocate another temporary buffer to accumulate
303                results */
304             tmp_results = (void *)smpi_get_tmp_sendbuffer( total_count*(MAX(true_extent,extent)));
305             /* adjust for potential negative lower bound in datatype */
306             tmp_results = (void *)((char*)tmp_results - true_lb);
307
308             /* copy sendbuf into tmp_results */
309             if (sendbuf != MPI_IN_PLACE)
310                 mpi_errno = Datatype::copy(sendbuf, total_count, datatype,
311                                            tmp_results, total_count, datatype);
312             else
313                 mpi_errno = Datatype::copy(recvbuf, total_count, datatype,
314                                            tmp_results, total_count, datatype);
315
316             if (mpi_errno) return(mpi_errno);
317
318             mask = 0x1;
319             i = 0;
320             while (mask < comm_size) {
321                 dst = rank ^ mask;
322
323                 dst_tree_root = dst >> i;
324                 dst_tree_root <<= i;
325
326                 my_tree_root = rank >> i;
327                 my_tree_root <<= i;
328
329                 /* At step 1, processes exchange (n-n/p) amount of
330                    data; at step 2, (n-2n/p) amount of data; at step 3, (n-4n/p)
331                    amount of data, and so forth. We use derived datatypes for this.
332
333                    At each step, a process does not need to send data
334                    indexed from my_tree_root to
335                    my_tree_root+mask-1. Similarly, a process won't receive
336                    data indexed from dst_tree_root to dst_tree_root+mask-1. */
337
338                 /* calculate sendtype */
339                 blklens[0] = blklens[1] = 0;
340                 for (j=0; j<my_tree_root; j++)
341                     blklens[0] += recvcounts[j];
342                 for (j=my_tree_root+mask; j<comm_size; j++)
343                     blklens[1] += recvcounts[j];
344
345                 dis[0] = 0;
346                 dis[1] = blklens[0];
347                 for (j=my_tree_root; (j<my_tree_root+mask) && (j<comm_size); j++)
348                     dis[1] += recvcounts[j];
349
350                 mpi_errno = Datatype::create_indexed(2, blklens, dis, datatype, &sendtype);
351                 if (mpi_errno) return(mpi_errno);
352
353                 sendtype->commit();
354
355                 /* calculate recvtype */
356                 blklens[0] = blklens[1] = 0;
357                 for (j=0; j<dst_tree_root && j<comm_size; j++)
358                     blklens[0] += recvcounts[j];
359                 for (j=dst_tree_root+mask; j<comm_size; j++)
360                     blklens[1] += recvcounts[j];
361
362                 dis[0] = 0;
363                 dis[1] = blklens[0];
364                 for (j=dst_tree_root; (j<dst_tree_root+mask) && (j<comm_size); j++)
365                     dis[1] += recvcounts[j];
366
367                 mpi_errno = Datatype::create_indexed(2, blklens, dis, datatype, &recvtype);
368                 if (mpi_errno) return(mpi_errno);
369
370                 recvtype->commit();
371
372                 received = 0;
373                 if (dst < comm_size) {
374                     /* tmp_results contains data to be sent in each step. Data is
375                        received in tmp_recvbuf and then accumulated into
376                        tmp_results. accumulation is done later below.   */
377
378                     Request::sendrecv(tmp_results, 1, sendtype, dst,
379                                                  COLL_TAG_SCATTER,
380                                                  tmp_recvbuf, 1, recvtype, dst,
381                                                  COLL_TAG_SCATTER, comm,
382                                                  MPI_STATUS_IGNORE);
383                     received = 1;
384                 }
385
386                 /* if some processes in this process's subtree in this step
387                    did not have any destination process to communicate with
388                    because of non-power-of-two, we need to send them the
389                    result. We use a logarithmic recursive-halfing algorithm
390                    for this. */
391
392                 if (dst_tree_root + mask > comm_size) {
393                     nprocs_completed = comm_size - my_tree_root - mask;
394                     /* nprocs_completed is the number of processes in this
395                        subtree that have all the data. Send data to others
396                        in a tree fashion. First find root of current tree
397                        that is being divided into two. k is the number of
398                        least-significant bits in this process's rank that
399                        must be zeroed out to find the rank of the root */
400                     j = mask;
401                     k = 0;
402                     while (j) {
403                         j >>= 1;
404                         k++;
405                     }
406                     k--;
407
408                     tmp_mask = mask >> 1;
409                     while (tmp_mask) {
410                         dst = rank ^ tmp_mask;
411
412                         tree_root = rank >> k;
413                         tree_root <<= k;
414
415                         /* send only if this proc has data and destination
416                            doesn't have data. at any step, multiple processes
417                            can send if they have the data */
418                         if ((dst > rank) &&
419                             (rank < tree_root + nprocs_completed)
420                             && (dst >= tree_root + nprocs_completed)) {
421                             /* send the current result */
422                             Request::send(tmp_recvbuf, 1, recvtype,
423                                                      dst, COLL_TAG_SCATTER,
424                                                      comm);
425                         }
426                         /* recv only if this proc. doesn't have data and sender
427                            has data */
428                         else if ((dst < rank) &&
429                                  (dst < tree_root + nprocs_completed) &&
430                                  (rank >= tree_root + nprocs_completed)) {
431                             Request::recv(tmp_recvbuf, 1, recvtype, dst,
432                                                      COLL_TAG_SCATTER,
433                                                      comm, MPI_STATUS_IGNORE);
434                             received = 1;
435                         }
436                         tmp_mask >>= 1;
437                         k--;
438                     }
439                 }
440
441                 /* The following reduction is done here instead of after
442                    the MPIC_Sendrecv_ft or MPIC_Recv_ft above. This is
443                    because to do it above, in the noncommutative
444                    case, we would need an extra temp buffer so as not to
445                    overwrite temp_recvbuf, because temp_recvbuf may have
446                    to be communicated to other processes in the
447                    non-power-of-two case. To avoid that extra allocation,
448                    we do the reduce here. */
449                 if (received) {
450                     if (is_commutative || (dst_tree_root < my_tree_root)) {
451                         {
452                           if (op != MPI_OP_NULL)
453                             op->apply(tmp_recvbuf, tmp_results, &blklens[0], datatype);
454                           if (op != MPI_OP_NULL)
455                             op->apply(((char*)tmp_recvbuf + dis[1] * extent), ((char*)tmp_results + dis[1] * extent),
456                                       &blklens[1], datatype);
457                         }
458                     }
459                     else {
460                         {
461                           if (op != MPI_OP_NULL)
462                             op->apply(tmp_results, tmp_recvbuf, &blklens[0], datatype);
463                           if (op != MPI_OP_NULL)
464                             op->apply(((char*)tmp_results + dis[1] * extent), ((char*)tmp_recvbuf + dis[1] * extent),
465                                       &blklens[1], datatype);
466                         }
467                         /* copy result back into tmp_results */
468                         mpi_errno = Datatype::copy(tmp_recvbuf, 1, recvtype,
469                                                    tmp_results, 1, recvtype);
470                         if (mpi_errno) return(mpi_errno);
471                     }
472                 }
473
474                 Datatype::unref(sendtype);
475                 Datatype::unref(recvtype);
476
477                 mask <<= 1;
478                 i++;
479             }
480
481             /* now copy final results from tmp_results to recvbuf */
482             mpi_errno = Datatype::copy(((char *)tmp_results+disps[rank]*extent),
483                                        recvcounts[rank], datatype, recvbuf,
484                                        recvcounts[rank], datatype);
485             if (mpi_errno) return(mpi_errno);
486
487     xbt_free(disps);
488     smpi_free_tmp_buffer(tmp_recvbuf);
489     smpi_free_tmp_buffer(tmp_results);
490     return MPI_SUCCESS;
491         }
492 }
493 }
494