Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / allreduce / allreduce-rab1.cpp
1 /* Copyright (c) 2013-2023. 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 //#include <star-reduction.c>
9 namespace simgrid::smpi {
10 // NP pow of 2 for now
11 int allreduce__rab1(const void *sbuff, void *rbuff,
12                     int count, MPI_Datatype dtype,
13                     MPI_Op op, MPI_Comm comm)
14 {
15   MPI_Status status;
16   MPI_Aint extent;
17   int tag = COLL_TAG_ALLREDUCE, send_size, newcnt, share;
18   unsigned int pof2 = 1, mask;
19   int send_idx, recv_idx, dst, send_cnt, recv_cnt;
20
21   int rank = comm->rank();
22   unsigned int nprocs = comm->size();
23
24   if((nprocs&(nprocs-1)))
25     throw std::invalid_argument("allreduce rab1 algorithm can't be used with non power of two number of processes!");
26
27   extent = dtype->get_extent();
28
29   pof2 = 1;
30   while (pof2 <= nprocs)
31     pof2 <<= 1;
32   pof2 >>= 1;
33
34   send_idx = recv_idx = 0;
35
36   // uneven count
37   if ((count % nprocs)) {
38     send_size = (count + nprocs) / nprocs;
39     newcnt = send_size * nprocs;
40
41     unsigned char* recv    = smpi_get_tmp_recvbuffer(extent * newcnt);
42     unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(extent * newcnt);
43     memcpy(recv, sbuff, extent * count);
44
45
46     mask = pof2 / 2;
47     share = newcnt / pof2;
48     while (mask > 0) {
49       dst = rank ^ mask;
50       send_cnt = recv_cnt = newcnt / (pof2 / mask);
51
52       if (rank < dst)
53         send_idx = recv_idx + (mask * share);
54       else
55         recv_idx = send_idx + (mask * share);
56
57       Request::sendrecv(recv + send_idx * extent, send_cnt, dtype, dst, tag, tmp_buf, recv_cnt, dtype, dst, tag, comm,
58                         &status);
59
60       if (op != MPI_OP_NULL)
61         op->apply(tmp_buf, recv + recv_idx * extent, &recv_cnt, dtype);
62
63       // update send_idx for next iteration
64       send_idx = recv_idx;
65       mask >>= 1;
66     }
67
68     memcpy(tmp_buf, recv + recv_idx * extent, recv_cnt * extent);
69     colls::allgather(tmp_buf, recv_cnt, dtype, recv, recv_cnt, dtype, comm);
70
71     memcpy(rbuff, recv, count * extent);
72     smpi_free_tmp_buffer(recv);
73     smpi_free_tmp_buffer(tmp_buf);
74
75   }
76
77   else {
78     unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(extent * count);
79     memcpy(rbuff, sbuff, count * extent);
80     mask = pof2 / 2;
81     share = count / pof2;
82     while (mask > 0) {
83       dst = rank ^ mask;
84       send_cnt = recv_cnt = count / (pof2 / mask);
85
86       if (rank < dst)
87         send_idx = recv_idx + (mask * share);
88       else
89         recv_idx = send_idx + (mask * share);
90
91       Request::sendrecv((char *) rbuff + send_idx * extent, send_cnt, dtype, dst,
92                    tag, tmp_buf, recv_cnt, dtype, dst, tag, comm, &status);
93
94       if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *) rbuff + recv_idx * extent, &recv_cnt,
95                      dtype);
96
97       // update send_idx for next iteration
98       send_idx = recv_idx;
99       mask >>= 1;
100     }
101
102     memcpy(tmp_buf, (char *) rbuff + recv_idx * extent, recv_cnt * extent);
103     colls::allgather(tmp_buf, recv_cnt, dtype, rbuff, recv_cnt, dtype, comm);
104     smpi_free_tmp_buffer(tmp_buf);
105   }
106
107   return MPI_SUCCESS;
108 }
109 } // namespace simgrid::smpi