1 /* Copyright (c) 2013-2022. 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 int allreduce__rdb(const void *sbuff, void *rbuff, int count,
11 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
13 int nprocs, rank, tag = COLL_TAG_ALLREDUCE;
14 int mask, dst, pof2, newrank, rem, newdst;
18 #ifdef MPICH2_REDUCTION
19 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
21 MPI_User_function *uop;
23 op_ptr = MPIR_ToPointer(op);
30 dtype->extent(&lb, &extent);
31 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
33 Request::sendrecv(sbuff, count, dtype, rank, 500,
34 rbuff, count, dtype, rank, 500, comm, &status);
36 // find nearest power-of-two less than or equal to comm_size
38 while (pof2 <= nprocs)
44 // In the non-power-of-two case, all even-numbered
45 // processes of rank < 2*rem send their data to
46 // (rank+1). These even-numbered processes no longer
47 // participate in the algorithm until the very end. The
48 // remaining processes form a nice power-of-two.
54 Request::send(rbuff, count, dtype, rank + 1, tag, comm);
56 // temporarily set the rank to -1 so that this
57 // process does not participate in recursive
62 Request::recv(tmp_buf, count, dtype, rank - 1, tag, comm, &status);
63 // do the reduction on received data. since the
64 // ordering is right, it doesn't matter whether
65 // the operation is commutative or not.
66 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
73 else // rank >= 2 * rem
76 // If op is user-defined or count is less than pof2, use
77 // recursive doubling algorithm. Otherwise do a reduce-scatter
78 // followed by allgather. (If op is user-defined,
79 // derived datatypes are allowed and the user could pass basic
80 // datatypes on one process and derived on another as long as
81 // the type maps are the same. Breaking up derived
82 // datatypes to do the reduce-scatter is tricky, therefore
83 // using recursive doubling in that case.)
88 newdst = newrank ^ mask;
89 // find real rank of dest
90 dst = (newdst < rem) ? newdst * 2 + 1 : newdst + rem;
92 // Send the most current data, which is in recvbuf. Recv
94 Request::sendrecv(rbuff, count, dtype, dst, tag, tmp_buf, count, dtype,
95 dst, tag, comm, &status);
97 // tmp_buf contains data received in this step.
98 // recvbuf contains data accumulated so far
100 // op is commutative OR the order is already right
101 // we assume it is commutative op
102 // if (op -> op_commute || (dst < rank))
104 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
105 } else // op is noncommutative and the order is not right
107 if(op!=MPI_OP_NULL) op->apply( rbuff, tmp_buf, &count, dtype);
109 // copy result back into recvbuf
110 Request::sendrecv(tmp_buf, count, dtype, rank, tag, rbuff, count,
111 dtype, rank, tag, comm, &status);
116 // In the non-power-of-two case, all odd-numbered processes of
117 // rank < 2 * rem send the result to (rank-1), the ranks who didn't
118 // participate above.
120 if (rank < 2 * rem) {
122 Request::send(rbuff, count, dtype, rank - 1, tag, comm);
124 Request::recv(rbuff, count, dtype, rank + 1, tag, comm, &status);
127 smpi_free_tmp_buffer(tmp_buf);
130 } // namespace simgrid::smpi