1 /* Copyright (c) 2013-2018. 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(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;
20 #ifdef MPICH2_REDUCTION
21 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
23 MPI_User_function *uop;
25 op_ptr = MPIR_ToPointer(op);
32 dtype->extent(&lb, &extent);
33 tmp_buf = (void *) smpi_get_tmp_sendbuffer(count * extent);
35 Request::sendrecv(sbuff, count, dtype, rank, 500,
36 rbuff, count, dtype, rank, 500, comm, &status);
38 // find nearest power-of-two less than or equal to comm_size
40 while (pof2 <= nprocs)
46 // In the non-power-of-two case, all even-numbered
47 // processes of rank < 2*rem send their data to
48 // (rank+1). These even-numbered processes no longer
49 // participate in the algorithm until the very end. The
50 // remaining processes form a nice power-of-two.
56 Request::send(rbuff, count, dtype, rank + 1, tag, comm);
58 // temporarily set the rank to -1 so that this
59 // process does not pariticipate in recursive
64 Request::recv(tmp_buf, count, dtype, rank - 1, tag, comm, &status);
65 // do the reduction on received data. since the
66 // ordering is right, it doesn't matter whether
67 // the operation is commutative or not.
68 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
75 else // rank >= 2 * rem
78 // If op is user-defined or count is less than pof2, use
79 // recursive doubling algorithm. Otherwise do a reduce-scatter
80 // followed by allgather. (If op is user-defined,
81 // derived datatypes are allowed and the user could pass basic
82 // datatypes on one process and derived on another as long as
83 // the type maps are the same. Breaking up derived
84 // datatypes to do the reduce-scatter is tricky, therefore
85 // using recursive doubling in that case.)
90 newdst = newrank ^ mask;
91 // find real rank of dest
92 dst = (newdst < rem) ? newdst * 2 + 1 : newdst + rem;
94 // Send the most current data, which is in recvbuf. Recv
96 Request::sendrecv(rbuff, count, dtype, dst, tag, tmp_buf, count, dtype,
97 dst, tag, comm, &status);
99 // tmp_buf contains data received in this step.
100 // recvbuf contains data accumulated so far
102 // op is commutative OR the order is already right
103 // we assume it is commuttive op
104 // if (op -> op_commute || (dst < rank))
106 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuff, &count, dtype);
107 } else // op is noncommutative and the order is not right
109 if(op!=MPI_OP_NULL) op->apply( rbuff, tmp_buf, &count, dtype);
111 // copy result back into recvbuf
112 Request::sendrecv(tmp_buf, count, dtype, rank, tag, rbuff, count,
113 dtype, rank, tag, comm, &status);
118 // In the non-power-of-two case, all odd-numbered processes of
119 // rank < 2 * rem send the result to (rank-1), the ranks who didn't
120 // participate above.
122 if (rank < 2 * rem) {
124 Request::send(rbuff, count, dtype, rank - 1, tag, comm);
126 Request::recv(rbuff, count, dtype, rank + 1, tag, comm, &status);
129 smpi_free_tmp_buffer(tmp_buf);