Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dvfs'
[simgrid.git] / src / smpi / colls / allreduce-lr.c
1 #include "colls_private.h"
2
3 /* IMPLEMENTED BY PITCH PATARASUK 
4    Non-topoloty-specific all-reduce operation designed bandwidth optimally 
5    Bug fixing by Xin Yuan, 04/04/2008
6 */
7
8 /* ** NOTE **
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).
12 */
13
14 //#include <star-reduction.c>
15
16 int
17 smpi_coll_tuned_allreduce_lr(void *sbuf, void *rbuf, int rcount,
18                              MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
19 {
20   int tag = COLL_TAG_ALLREDUCE;
21   MPI_Status status;
22   int rank, i, size, count;
23   int send_offset, recv_offset;
24   int remainder, remainder_flag, remainder_offset;
25
26   rank = smpi_comm_rank(MPI_COMM_WORLD);
27   size = smpi_comm_size(MPI_COMM_WORLD);
28
29   /* make it compatible with all data type */
30   MPI_Aint extent;
31   extent = smpi_datatype_get_extent(dtype);
32
33   /* when communication size is smaller than number of process (not support) */
34   if (rcount < size) {
35     XBT_WARN("MPI_allreduce_lr use default MPI_allreduce.");      
36     smpi_mpi_allreduce(sbuf, rbuf, rcount, dtype, op, comm);
37     return MPI_SUCCESS; 
38   }
39
40   /* when communication size is not divisible by number of process: 
41      call the native implementation for the remain chunk at the end of the operation */
42   if (rcount % size != 0) {
43     remainder = rcount % size;
44     remainder_flag = 1;
45     remainder_offset = (rcount / size) * size * extent;
46   } else {
47     remainder = remainder_flag = remainder_offset = 0;
48   }
49
50   /* size of each point-to-point communication is equal to the size of the whole message
51      divided by number of processes
52    */
53   count = rcount / size;
54
55   /* our ALL-REDUCE implementation
56      1. copy (partial of)send_buf to recv_buf
57      2. use logical ring reduce-scatter
58      3. use logical ring all-gather 
59    */
60
61   // copy partial data
62   send_offset = ((rank - 1 + size) % size) * count * extent;
63   recv_offset = ((rank - 1 + size) % size) * count * extent;
64   smpi_mpi_sendrecv((char *) sbuf + send_offset, count, dtype, rank, tag - 1,
65                (char *) rbuf + recv_offset, count, dtype, rank, tag - 1, comm,
66                &status);
67
68   // reduce-scatter
69   for (i = 0; i < (size - 1); i++) {
70     send_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
71     recv_offset = ((rank - 2 - i + 2 * size) % size) * count * extent;
72     //    recv_offset = ((rank-i+2*size)%size)*count*extent;
73     smpi_mpi_sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
74                  tag + i, (char *) rbuf + recv_offset, count, dtype,
75                  ((rank + size - 1) % size), tag + i, comm, &status);
76
77     // compute result to rbuf+recv_offset
78     smpi_op_apply(op, (char *) sbuf + recv_offset, (char *) rbuf + recv_offset,
79                    &count, &dtype);
80   }
81
82   // all-gather
83   for (i = 0; i < (size - 1); i++) {
84     send_offset = ((rank - i + 2 * size) % size) * count * extent;
85     recv_offset = ((rank - 1 - i + 2 * size) % size) * count * extent;
86     smpi_mpi_sendrecv((char *) rbuf + send_offset, count, dtype, ((rank + 1) % size),
87                  tag + i, (char *) rbuf + recv_offset, count, dtype,
88                  ((rank + size - 1) % size), tag + i, comm, &status);
89   }
90
91   /* when communication size is not divisible by number of process: 
92      call the native implementation for the remain chunk at the end of the operation */
93   if (remainder_flag) {
94     return mpi_coll_allreduce_fun((char *) sbuf + remainder_offset,
95                          (char *) rbuf + remainder_offset, remainder, dtype, op,
96                          comm);
97   }
98
99   return 0;
100 }