1 /* Copyright (c) 2013-2023. The SimGrid Team.
2 * All rights reserved. */
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. */
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)
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;
21 int rank = comm->rank();
22 unsigned int nprocs = comm->size();
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!");
27 extent = dtype->get_extent();
30 while (pof2 <= nprocs)
34 send_idx = recv_idx = 0;
37 if ((count % nprocs)) {
38 send_size = (count + nprocs) / nprocs;
39 newcnt = send_size * nprocs;
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);
47 share = newcnt / pof2;
50 send_cnt = recv_cnt = newcnt / (pof2 / mask);
53 send_idx = recv_idx + (mask * share);
55 recv_idx = send_idx + (mask * share);
57 Request::sendrecv(recv + send_idx * extent, send_cnt, dtype, dst, tag, tmp_buf, recv_cnt, dtype, dst, tag, comm,
60 if (op != MPI_OP_NULL)
61 op->apply(tmp_buf, recv + recv_idx * extent, &recv_cnt, dtype);
63 // update send_idx for next iteration
68 memcpy(tmp_buf, recv + recv_idx * extent, recv_cnt * extent);
69 colls::allgather(tmp_buf, recv_cnt, dtype, recv, recv_cnt, dtype, comm);
71 memcpy(rbuff, recv, count * extent);
72 smpi_free_tmp_buffer(recv);
73 smpi_free_tmp_buffer(tmp_buf);
78 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(extent * count);
79 memcpy(rbuff, sbuff, count * extent);
84 send_cnt = recv_cnt = count / (pof2 / mask);
87 send_idx = recv_idx + (mask * share);
89 recv_idx = send_idx + (mask * share);
91 Request::sendrecv((char *) rbuff + send_idx * extent, send_cnt, dtype, dst,
92 tag, tmp_buf, recv_cnt, dtype, dst, tag, comm, &status);
94 if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *) rbuff + recv_idx * extent, &recv_cnt,
97 // update send_idx for next iteration
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);
109 } // namespace simgrid::smpi