3 /* IMPLEMENTED BY PITCH PATARASUK
4 Non-topoloty-specific all-reduce operation designed bandwidth optimally
5 Bug fixing by Xin Yuan, 04/04/2008
9 Use -DMPICH2_REDUCTION if this code does not compile.
10 MPICH1 code also work on MPICH2 on our cluster and the performance are similar.
11 This code assume commutative and associative reduce operator (MPI_SUM, MPI_MAX, etc).
14 //#include <star-reduction.c>
17 smpi_coll_tuned_allreduce_lr(void *sbuf, void *rbuf, int rcount,
18 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
22 int rank, i, size, count;
23 int send_offset, recv_offset;
24 int remainder, remainder_flag, remainder_offset;
26 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
27 MPI_Comm_size(MPI_COMM_WORLD, &size);
29 /* make it compatible with all data type */
31 MPI_Type_extent(dtype, &extent);
33 /* when communication size is smaller than number of process (not support) */
35 return MPI_Allreduce(sbuf, rbuf, rcount, dtype, op, comm);
38 /* when communication size is not divisible by number of process:
39 call the native implementation for the remain chunk at the end of the operation */
40 else if (rcount % size != 0) {
41 remainder = rcount % size;
43 remainder_offset = (rcount / size) * size * extent;
45 remainder_flag = remainder_offset = 0;
48 /* size of each point-to-point communication is equal to the size of the whole message
49 divided by number of processes
51 count = rcount / size;
53 /* our ALL-REDUCE implementation
54 1. copy (partial of)send_buf to recv_buf
55 2. use logical ring reduce-scatter
56 3. use logical ring all-gather
60 send_offset = ((rank - 1 + size) % size) * count * extent;
61 recv_offset = ((rank - 1 + size) % size) * count * extent;
62 MPI_Sendrecv((char *) sbuf + send_offset, count, dtype, rank, tag - 1,
63 (char *) rbuf + recv_offset, count, dtype, rank, tag - 1, comm,
67 for (i = 0; i < (size - 1); i++) {
68 send_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
69 recv_offset = ((rank - 2 - i + 2 * size) % size) * count * extent;
70 // recv_offset = ((rank-i+2*size)%size)*count*extent;
71 MPI_Sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
72 tag + i, (char *) rbuf + recv_offset, count, dtype,
73 ((rank + size - 1) % size), tag + i, comm, &status);
75 // compute result to rbuf+recv_offset
76 star_reduction(op, (char *) sbuf + recv_offset, (char *) rbuf + recv_offset,
81 for (i = 0; i < (size - 1); i++) {
82 send_offset = ((rank - i + 2 * size) % size) * count * extent;
83 recv_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
84 MPI_Sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
85 tag + i, (char *) rbuf + recv_offset, count, dtype,
86 ((rank + size - 1) % size), tag + i, comm, &status);
89 /* when communication size is not divisible by number of process:
90 call the native implementation for the remain chunk at the end of the operation */
92 return MPI_Allreduce((char *) sbuf + remainder_offset,
93 (char *) rbuf + remainder_offset, remainder, dtype, op,