Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / smpi / colls / allreduce / allreduce-lr.cpp
1 /* Copyright (c) 2013-2019. 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-topoloty-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
24 Coll_allreduce_lr::allreduce(void *sbuf, void *rbuf, int rcount,
25                              MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
26 {
27   int tag = COLL_TAG_ALLREDUCE;
28   MPI_Status status;
29   int rank, i, size, count;
30   int send_offset, recv_offset;
31   int remainder, remainder_flag, remainder_offset;
32
33   rank = comm->rank();
34   size = comm->size();
35
36   /* make it compatible with all data type */
37   MPI_Aint extent;
38   extent = dtype->get_extent();
39
40   /* when communication size is smaller than number of process (not support) */
41   if (rcount < size) {
42     XBT_WARN("MPI_allreduce_lr use default MPI_allreduce.");
43     Coll_allreduce_default::allreduce(sbuf, rbuf, rcount, dtype, op, comm);
44     return MPI_SUCCESS;
45   }
46
47   /* when communication size is not divisible by number of process:
48      call the native implementation for the remain chunk at the end of the operation */
49   if (rcount % size != 0) {
50     remainder = rcount % size;
51     remainder_flag = 1;
52     remainder_offset = (rcount / size) * size * extent;
53   } else {
54     remainder = remainder_flag = remainder_offset = 0;
55   }
56
57   /* size of each point-to-point communication is equal to the size of the whole message
58      divided by number of processes
59    */
60   count = rcount / size;
61
62   /* our ALL-REDUCE implementation
63      1. copy (partial of)send_buf to recv_buf
64      2. use logical ring reduce-scatter
65      3. use logical ring all-gather
66    */
67
68   // copy partial data
69   send_offset = ((rank - 1 + size) % size) * count * extent;
70   recv_offset = ((rank - 1 + size) % size) * count * extent;
71   Request::sendrecv((char *) sbuf + send_offset, count, dtype, rank, tag - 1,
72                (char *) rbuf + recv_offset, count, dtype, rank, tag - 1, comm,
73                &status);
74
75   // reduce-scatter
76   for (i = 0; i < (size - 1); i++) {
77     send_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
78     recv_offset = ((rank - 2 - i + 2 * size) % size) * count * extent;
79     //    recv_offset = ((rank-i+2*size)%size)*count*extent;
80     Request::sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
81                  tag + i, (char *) rbuf + recv_offset, count, dtype,
82                  ((rank + size - 1) % size), tag + i, comm, &status);
83
84     // compute result to rbuf+recv_offset
85     if(op!=MPI_OP_NULL) op->apply( (char *) sbuf + recv_offset, (char *) rbuf + recv_offset,
86                    &count, dtype);
87   }
88
89   // all-gather
90   for (i = 0; i < (size - 1); i++) {
91     send_offset = ((rank - i + 2 * size) % size) * count * extent;
92     recv_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
93     Request::sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
94                  tag + i, (char *) rbuf + recv_offset, count, dtype,
95                  ((rank + size - 1) % size), tag + i, comm, &status);
96   }
97
98   /* when communication size is not divisible by number of process:
99      call the native implementation for the remain chunk at the end of the operation */
100   if (remainder_flag) {
101     return Colls::allreduce((char *) sbuf + remainder_offset,
102                          (char *) rbuf + remainder_offset, remainder, dtype, op,
103                          comm);
104   }
105
106   return 0;
107 }
108 }
109 }