1 /* Copyright (c) 2013-2019. 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>
11 // NP pow of 2 for now
12 int Coll_allreduce_rab1::allreduce(const void *sbuff, void *rbuff,
13 int count, MPI_Datatype dtype,
14 MPI_Op op, MPI_Comm comm)
18 int tag = COLL_TAG_ALLREDUCE, send_size, newcnt, share;
19 unsigned int pof2 = 1, mask;
20 int send_idx, recv_idx, dst, send_cnt, recv_cnt;
22 int rank = comm->rank();
23 unsigned int nprocs = comm->size();
25 if((nprocs&(nprocs-1)))
26 throw std::invalid_argument("allreduce rab1 algorithm can't be used with non power of two number of processes!");
28 extent = dtype->get_extent();
31 while (pof2 <= nprocs)
35 send_idx = recv_idx = 0;
38 if ((count % nprocs)) {
39 send_size = (count + nprocs) / nprocs;
40 newcnt = send_size * nprocs;
42 unsigned char* recv = smpi_get_tmp_recvbuffer(extent * newcnt);
43 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(extent * newcnt);
44 memcpy(recv, sbuff, extent * count);
48 share = newcnt / pof2;
51 send_cnt = recv_cnt = newcnt / (pof2 / mask);
54 send_idx = recv_idx + (mask * share);
56 recv_idx = send_idx + (mask * share);
58 Request::sendrecv(recv + send_idx * extent, send_cnt, dtype, dst, tag, tmp_buf, recv_cnt, dtype, dst, tag, comm,
61 if (op != MPI_OP_NULL)
62 op->apply(tmp_buf, recv + recv_idx * extent, &recv_cnt, dtype);
64 // update send_idx for next iteration
69 memcpy(tmp_buf, recv + recv_idx * extent, recv_cnt * extent);
70 Colls::allgather(tmp_buf, recv_cnt, dtype, recv, recv_cnt, dtype, comm);
72 memcpy(rbuff, recv, count * extent);
73 smpi_free_tmp_buffer(recv);
74 smpi_free_tmp_buffer(tmp_buf);
79 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(extent * count);
80 memcpy(rbuff, sbuff, count * extent);
85 send_cnt = recv_cnt = count / (pof2 / mask);
88 send_idx = recv_idx + (mask * share);
90 recv_idx = send_idx + (mask * share);
92 Request::sendrecv((char *) rbuff + send_idx * extent, send_cnt, dtype, dst,
93 tag, tmp_buf, recv_cnt, dtype, dst, tag, comm, &status);
95 if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *) rbuff + recv_idx * extent, &recv_cnt,
98 // update send_idx for next iteration
103 memcpy(tmp_buf, (char *) rbuff + recv_idx * extent, recv_cnt * extent);
104 Colls::allgather(tmp_buf, recv_cnt, dtype, rbuff, recv_cnt, dtype, comm);
105 smpi_free_tmp_buffer(tmp_buf);