Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / smpi / colls / allreduce / allreduce-lr.cpp
1 /* Copyright (c) 2013-2022. 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 /* IMPLEMENTED BY PITCH PATARASUK
10    Non-topology-specific all-reduce operation designed bandwidth optimally
11    Bug fixing by Xin Yuan, 04/04/2008
12 */
13
14 /* ** NOTE **
15    Use -DMPICH2_REDUCTION if this code does not compile.
16    MPICH1 code also work on MPICH2 on our cluster and the performance are similar.
17    This code assume commutative and associative reduce operator (MPI_SUM, MPI_MAX, etc).
18 */
19
20 //#include <star-reduction.c>
21 namespace simgrid {
22 namespace smpi {
23 int allreduce__lr(const void *sbuf, void *rbuf, int rcount,
24                   MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
25 {
26   int tag = COLL_TAG_ALLREDUCE;
27   MPI_Status status;
28   int rank, i, size, count;
29   int send_offset, recv_offset;
30   int remainder, remainder_flag, remainder_offset;
31
32   rank = comm->rank();
33   size = comm->size();
34
35   /* make it compatible with all data type */
36   MPI_Aint extent;
37   extent = dtype->get_extent();
38
39   if (rcount < size) {
40     XBT_INFO("MPI_allreduce_lr: communication size smaller than number of process, use default MPI_allreduce.");
41     allreduce__default(sbuf, rbuf, rcount, dtype, op, comm);
42     return MPI_SUCCESS;
43   }
44
45   /* when communication size is not divisible by number of process:
46      call the native implementation for the remain chunk at the end of the operation */
47   if (rcount % size != 0) {
48     remainder = rcount % size;
49     remainder_flag = 1;
50     remainder_offset = (rcount / size) * size * extent;
51   } else {
52     remainder = remainder_flag = remainder_offset = 0;
53   }
54
55   /* size of each point-to-point communication is equal to the size of the whole message
56      divided by number of processes
57    */
58   count = rcount / size;
59
60   /* our ALL-REDUCE implementation
61      1. copy (partial of)send_buf to recv_buf
62      2. use logical ring reduce-scatter
63      3. use logical ring all-gather
64    */
65
66   // copy partial data
67   send_offset = ((rank - 1 + size) % size) * count * extent;
68   recv_offset = ((rank - 1 + size) % size) * count * extent;
69   Request::sendrecv((char *) sbuf + send_offset, count, dtype, rank, tag - 1,
70                (char *) rbuf + recv_offset, count, dtype, rank, tag - 1, comm,
71                &status);
72
73   // reduce-scatter
74   for (i = 0; i < (size - 1); i++) {
75     send_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
76     recv_offset = ((rank - 2 - i + 2 * size) % size) * count * extent;
77     //    recv_offset = ((rank-i+2*size)%size)*count*extent;
78     Request::sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
79                  tag + i, (char *) rbuf + recv_offset, count, dtype,
80                  ((rank + size - 1) % size), tag + i, comm, &status);
81
82     // compute result to rbuf+recv_offset
83     if(op!=MPI_OP_NULL) op->apply( (char *) sbuf + recv_offset, (char *) rbuf + recv_offset,
84                    &count, dtype);
85   }
86
87   // all-gather
88   for (i = 0; i < (size - 1); i++) {
89     send_offset = ((rank - i + 2 * size) % size) * count * extent;
90     recv_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
91     Request::sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
92                  tag + i, (char *) rbuf + recv_offset, count, dtype,
93                  ((rank + size - 1) % size), tag + i, comm, &status);
94   }
95
96   /* when communication size is not divisible by number of process:
97      call the native implementation for the remain chunk at the end of the operation */
98   if (remainder_flag) {
99     return colls::allreduce((char*)sbuf + remainder_offset, (char*)rbuf + remainder_offset, remainder, dtype, op, comm);
100   }
101
102   return 0;
103 }
104 }
105 }