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 int Coll_allreduce_rdb::allreduce(const void *sbuff, void *rbuff, int count,
12 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
14 int nprocs, rank, tag = COLL_TAG_ALLREDUCE;
15 int mask, dst, pof2, newrank, rem, newdst;
19 #ifdef MPICH2_REDUCTION
20 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
22 MPI_User_function *uop;
24 op_ptr = MPIR_ToPointer(op);
31 dtype->extent(&lb, &extent);
32 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
34 Request::sendrecv(sbuff, count, dtype, rank, 500,
35 rbuff, count, dtype, rank, 500, comm, &status);
37 // find nearest power-of-two less than or equal to comm_size
39 while (pof2 <= nprocs)
45 // In the non-power-of-two case, all even-numbered
46 // processes of rank < 2*rem send their data to
47 // (rank+1). These even-numbered processes no longer
48 // participate in the algorithm until the very end. The
49 // remaining processes form a nice power-of-two.
55 Request::send(rbuff, count, dtype, rank + 1, tag, comm);
57 // temporarily set the rank to -1 so that this
58 // process does not pariticipate in recursive
63 Request::recv(tmp_buf, count, dtype, rank - 1, tag, comm, &status);
64 // do the reduction on received data. since the
65 // ordering is right, it doesn't matter whether
66 // the operation is commutative or not.
67 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
74 else // rank >= 2 * rem
77 // If op is user-defined or count is less than pof2, use
78 // recursive doubling algorithm. Otherwise do a reduce-scatter
79 // followed by allgather. (If op is user-defined,
80 // derived datatypes are allowed and the user could pass basic
81 // datatypes on one process and derived on another as long as
82 // the type maps are the same. Breaking up derived
83 // datatypes to do the reduce-scatter is tricky, therefore
84 // using recursive doubling in that case.)
89 newdst = newrank ^ mask;
90 // find real rank of dest
91 dst = (newdst < rem) ? newdst * 2 + 1 : newdst + rem;
93 // Send the most current data, which is in recvbuf. Recv
95 Request::sendrecv(rbuff, count, dtype, dst, tag, tmp_buf, count, dtype,
96 dst, tag, comm, &status);
98 // tmp_buf contains data received in this step.
99 // recvbuf contains data accumulated so far
101 // op is commutative OR the order is already right
102 // we assume it is commuttive op
103 // if (op -> op_commute || (dst < rank))
105 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
106 } else // op is noncommutative and the order is not right
108 if(op!=MPI_OP_NULL) op->apply( rbuff, tmp_buf, &count, dtype);
110 // copy result back into recvbuf
111 Request::sendrecv(tmp_buf, count, dtype, rank, tag, rbuff, count,
112 dtype, rank, tag, comm, &status);
117 // In the non-power-of-two case, all odd-numbered processes of
118 // rank < 2 * rem send the result to (rank-1), the ranks who didn't
119 // participate above.
121 if (rank < 2 * rem) {
123 Request::send(rbuff, count, dtype, rank - 1, tag, comm);
125 Request::recv(rbuff, count, dtype, rank + 1, tag, comm, &status);
128 smpi_free_tmp_buffer(tmp_buf);