Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define buffer on the stack here.
[simgrid.git] / src / smpi / colls / allreduce-smp-binomial.c
1 #include "colls_private.h"
2 /* IMPLEMENTED BY PITCH PATARASUK 
3    Non-topoloty-specific (however, number of cores/node need to be changed) 
4    all-reduce operation designed for smp clusters
5    It uses 2-layer communication: binomial for both intra-communication 
6    inter-communication*/
7
8 /* change number of core per smp-node
9    we assume that number of core per process will be the same for all implementations */
10 #ifndef NUM_CORE
11 #define NUM_CORE 8
12 #endif
13
14 /* ** NOTE **
15    Use -DMPICH2 if this code does not compile.
16    MPICH1 code also work on MPICH2 on our cluster and the performance are similar.
17    This code assume commutative and associative reduce operator (MPI_SUM, MPI_MAX, etc).
18 */
19
20 //#include <star-reduction.c>
21
22 /*
23 This fucntion performs all-reduce operation as follow.
24 1) binomial_tree reduce inside each SMP node
25 2) binomial_tree reduce intra-communication between root of each SMP node
26 3) binomial_tree bcast intra-communication between root of each SMP node
27 4) binomial_tree bcast inside each SMP node
28 */
29 int smpi_coll_tuned_allreduce_smp_binomial(void *send_buf, void *recv_buf,
30                                            int count, MPI_Datatype dtype,
31                                            MPI_Op op, MPI_Comm comm)
32 {
33   int comm_size, rank;
34   void *tmp_buf;
35   int tag = COLL_TAG_ALLREDUCE;
36   int mask, src, dst;
37   int num_core = NUM_CORE;
38   MPI_Status status;
39
40   comm_size=smpi_comm_size(comm);
41   rank=smpi_comm_rank(comm);
42   MPI_Aint extent, lb;
43   smpi_datatype_extent(dtype, &lb, &extent);
44   tmp_buf = (void *) xbt_malloc(count * extent);
45
46   /* compute intra and inter ranking */
47   int intra_rank, inter_rank;
48   intra_rank = rank % num_core;
49   inter_rank = rank / num_core;
50
51   /* size of processes participate in intra communications =>
52      should be equal to number of machines */
53   int inter_comm_size = (comm_size + num_core - 1) / num_core;
54
55   /* copy input buffer to output buffer */
56   smpi_mpi_sendrecv(send_buf, count, dtype, rank, tag,
57                recv_buf, count, dtype, rank, tag, comm, &status);
58
59   /* start binomial reduce intra communication inside each SMP node */
60   mask = 1;
61   while (mask < num_core) {
62     if ((mask & intra_rank) == 0) {
63       src = (inter_rank * num_core) + (intra_rank | mask);
64       if (src < comm_size) {
65         smpi_mpi_recv(tmp_buf, count, dtype, src, tag, comm, &status);
66         smpi_op_apply(op, tmp_buf, recv_buf, &count, &dtype);
67       }
68     } else {
69       dst = (inter_rank * num_core) + (intra_rank & (~mask));
70       smpi_mpi_send(recv_buf, count, dtype, dst, tag, comm);
71       break;
72     }
73     mask <<= 1;
74   }
75
76   /* start binomial reduce inter-communication between each SMP nodes: 
77      each node only have one process that can communicate to other nodes */
78   if (intra_rank == 0) {
79     mask = 1;
80     while (mask < inter_comm_size) {
81       if ((mask & inter_rank) == 0) {
82         src = (inter_rank | mask) * num_core;
83         if (src < comm_size) {
84           smpi_mpi_recv(tmp_buf, count, dtype, src, tag, comm, &status);
85           smpi_op_apply(op, tmp_buf, recv_buf, &count, &dtype);
86         }
87       } else {
88         dst = (inter_rank & (~mask)) * num_core;
89         smpi_mpi_send(recv_buf, count, dtype, dst, tag, comm);
90         break;
91       }
92       mask <<= 1;
93     }
94   }
95
96   /* start binomial broadcast inter-communication between each SMP nodes: 
97      each node only have one process that can communicate to other nodes */
98   if (intra_rank == 0) {
99     mask = 1;
100     while (mask < inter_comm_size) {
101       if (inter_rank & mask) {
102         src = (inter_rank - mask) * num_core;
103         smpi_mpi_recv(recv_buf, count, dtype, src, tag, comm, &status);
104         break;
105       }
106       mask <<= 1;
107     }
108     mask >>= 1;
109
110     while (mask > 0) {
111       if (inter_rank < inter_comm_size) {
112         dst = (inter_rank + mask) * num_core;
113         if (dst < comm_size) {
114           smpi_mpi_send(recv_buf, count, dtype, dst, tag, comm);
115         }
116       }
117       mask >>= 1;
118     }
119   }
120
121   /* start binomial broadcast intra-communication inside each SMP nodes */
122   int num_core_in_current_smp = num_core;
123   if (inter_rank == (inter_comm_size - 1)) {
124     num_core_in_current_smp = comm_size - (inter_rank * num_core);
125   }
126   mask = 1;
127   while (mask < num_core_in_current_smp) {
128     if (intra_rank & mask) {
129       src = (inter_rank * num_core) + (intra_rank - mask);
130       smpi_mpi_recv(recv_buf, count, dtype, src, tag, comm, &status);
131       break;
132     }
133     mask <<= 1;
134   }
135   mask >>= 1;
136
137   while (mask > 0) {
138     dst = (inter_rank * num_core) + (intra_rank + mask);
139     if (dst < comm_size) {
140       smpi_mpi_send(recv_buf, count, dtype, dst, tag, comm);
141     }
142     mask >>= 1;
143   }
144
145   free(tmp_buf);
146   return MPI_SUCCESS;
147 }