2 //#include <star-reduction.c>
4 int smpi_coll_tuned_allreduce_rdb(void *sbuff, void *rbuff, int count,
5 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
7 int nprocs, rank, type_size, tag = 543;
8 int mask, dst, pof2, newrank, rem, newdst;
13 #ifdef MPICH2_REDUCTION
14 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
16 MPI_User_function *uop;
17 struct MPIR_OP *op_ptr;
18 op_ptr = MPIR_ToPointer(op);
22 MPI_Comm_size(comm, &nprocs);
23 MPI_Comm_rank(comm, &rank);
25 MPI_Type_extent(dtype, &extent);
26 tmp_buf = (void *) malloc(count * extent);
28 printf("Could not allocate memory for tmp_buf\n");
32 MPI_Sendrecv(sbuff, count, dtype, rank, 500,
33 rbuff, count, dtype, rank, 500, comm, &status);
35 MPI_Type_size(dtype, &type_size);
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 MPI_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 MPI_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 star_reduction(op, 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 MPI_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 star_reduction(op, tmp_buf, rbuff, &count, &dtype);
106 } else // op is noncommutative and the order is not right
108 star_reduction(op, rbuff, tmp_buf, &count, &dtype);
110 // copy result back into recvbuf
111 MPI_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 MPI_Send(rbuff, count, dtype, rank - 1, tag, comm);
125 MPI_Recv(rbuff, count, dtype, rank + 1, tag, comm, &status);