Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
haha! problem with call to get_remote_mailbox...code was using communicator rank
[simgrid.git] / src / smpi / smpi_coll.c
1 /* smpi_coll.c -- various optimized routing for collectives                   */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <assert.h>
12
13 #include "private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
16                                 "Logging specific to SMPI (coll)");
17
18 struct s_proc_tree {
19   int PROCTREE_A;
20   int numChildren;
21   int *child;
22   int parent;
23   int me;
24   int root;
25   int isRoot;
26 };
27 typedef struct s_proc_tree *proc_tree_t;
28
29 /**
30  * alloc and init
31  **/
32 static proc_tree_t alloc_tree(int arity)
33 {
34   proc_tree_t tree;
35   int i;
36
37   tree = xbt_new(struct s_proc_tree, 1);
38   tree->PROCTREE_A = arity;
39   tree->isRoot = 0;
40   tree->numChildren = 0;
41   tree->child = xbt_new(int, arity);
42   for (i = 0; i < arity; i++) {
43     tree->child[i] = -1;
44   }
45   tree->root = -1;
46   tree->parent = -1;
47   return tree;
48 }
49
50 /**
51  * free
52  **/
53 static void free_tree(proc_tree_t tree)
54 {
55   xbt_free(tree->child);
56   xbt_free(tree);
57 }
58
59 /**
60  * Build the tree depending on a process rank (index) and the group size (extent)
61  * @param root the rank of the tree root
62  * @param rank the rank of the calling process
63  * @param size the total number of processes
64  **/
65 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
66 {
67   int index = (rank - root + size) % size;
68   int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
69   int i;
70
71   (*tree)->me = rank;
72   (*tree)->root = root;
73
74   for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
75     (*tree)->child[i] = (firstChildIdx + i + root) % size;
76     (*tree)->numChildren++;
77   }
78   if (rank == root) {
79     (*tree)->isRoot = 1;
80   } else {
81     (*tree)->isRoot = 0;
82     (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
83   }
84 }
85
86 /**
87  * bcast
88  **/
89 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
90                        MPI_Comm comm, proc_tree_t tree)
91 {
92   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
93   int rank, i;
94   MPI_Request *requests;
95
96   rank = smpi_comm_rank(comm);
97   /* wait for data from my parent in the tree */
98   if (!tree->isRoot) {
99     XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
100            rank, tree->parent, system_tag + rank);
101     smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
102                   comm, MPI_STATUS_IGNORE);
103   }
104   requests = xbt_new(MPI_Request, tree->numChildren);
105   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
106          tree->numChildren);
107   /* iniates sends to ranks lower in the tree */
108   for (i = 0; i < tree->numChildren; i++) {
109     if (tree->child[i] == -1) {
110       requests[i] = MPI_REQUEST_NULL;
111     } else {
112       XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
113              system_tag + tree->child[i]);
114       requests[i] =
115           smpi_isend_init(buf, count, datatype, tree->child[i],
116                           system_tag + tree->child[i], comm);
117     }
118   }
119   smpi_mpi_startall(tree->numChildren, requests);
120   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
121   xbt_free(requests);
122 }
123
124 /**
125  * anti-bcast
126  **/
127 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
128                            MPI_Comm comm, proc_tree_t tree)
129 {
130   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
131   int rank, i;
132   MPI_Request *requests;
133
134   rank = smpi_comm_rank(comm);
135   // everyone sends to its parent, except root.
136   if (!tree->isRoot) {
137     XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
138            rank, tree->parent, system_tag + rank);
139     smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
140                   comm);
141   }
142   //every one receives as many messages as it has children
143   requests = xbt_new(MPI_Request, tree->numChildren);
144   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
145          tree->numChildren);
146   for (i = 0; i < tree->numChildren; i++) {
147     if (tree->child[i] == -1) {
148       requests[i] = MPI_REQUEST_NULL;
149     } else {
150       XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
151              system_tag + tree->child[i]);
152       requests[i] =
153           smpi_irecv_init(buf, count, datatype, tree->child[i],
154                           system_tag + tree->child[i], comm);
155     }
156   }
157   smpi_mpi_startall(tree->numChildren, requests);
158   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
159   xbt_free(requests);
160 }
161
162 /**
163  * bcast with a binary, ternary, or whatever tree ..
164  **/
165 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
166                      MPI_Comm comm, int arity)
167 {
168   proc_tree_t tree = alloc_tree(arity);
169   int rank, size;
170
171   rank = smpi_comm_rank(comm);
172   size = smpi_comm_size(comm);
173   build_tree(root, rank, size, &tree);
174   tree_bcast(buf, count, datatype, comm, tree);
175   free_tree(tree);
176 }
177
178 /**
179  * barrier with a binary, ternary, or whatever tree ..
180  **/
181 void nary_tree_barrier(MPI_Comm comm, int arity)
182 {
183   proc_tree_t tree = alloc_tree(arity);
184   int rank, size;
185   char dummy = '$';
186
187   rank = smpi_comm_rank(comm);
188   size = smpi_comm_size(comm);
189   build_tree(0, rank, size, &tree);
190   tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
191   tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
192   free_tree(tree);
193 }
194
195 /**
196  * Alltoall Bruck
197  *
198  * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
199  **/
200 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
201                                    MPI_Datatype sendtype, void *recvbuf,
202                                    int recvcount, MPI_Datatype recvtype,
203                                    MPI_Comm comm)
204 {
205   int system_tag = 777;
206   int i, rank, size, err, count;
207   MPI_Aint lb;
208   MPI_Aint sendextent = 0;
209   MPI_Aint recvextent = 0;
210   MPI_Request *requests;
211
212   // FIXME: check implementation
213   rank = smpi_comm_rank(comm);
214   size = smpi_comm_size(comm);
215   XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
216   err = smpi_datatype_extent(sendtype, &lb, &sendextent);
217   err = smpi_datatype_extent(recvtype, &lb, &recvextent);
218   /* Local copy from self */
219   err =
220       smpi_datatype_copy(&((char *) sendbuf)[rank * sendextent], sendcount,
221                          sendtype, &((char *) recvbuf)[rank * recvextent],
222                          recvcount, recvtype);
223   if (err == MPI_SUCCESS && size > 1) {
224     /* Initiate all send/recv to/from others. */
225     requests = xbt_new(MPI_Request, 2 * (size - 1));
226     count = 0;
227     /* Create all receives that will be posted first */
228     for (i = 0; i < size; ++i) {
229       if (i == rank) {
230         XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
231                rank, i, recvcount);
232         continue;
233       }
234       requests[count] =
235           smpi_irecv_init(&((char *) recvbuf)[i * recvextent], recvcount,
236                           recvtype, i, system_tag, comm);
237       count++;
238     }
239     /* Now create all sends  */
240     for (i = 0; i < size; ++i) {
241       if (i == rank) {
242         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
243                rank, i, sendcount);
244         continue;
245       }
246       requests[count] =
247           smpi_isend_init(&((char *) sendbuf)[i * sendextent], sendcount,
248                           sendtype, i, system_tag, comm);
249       count++;
250     }
251     /* Wait for them all. */
252     smpi_mpi_startall(count, requests);
253     XBT_DEBUG("<%d> wait for %d requests", rank, count);
254     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
255     xbt_free(requests);
256   }
257   return MPI_SUCCESS;
258 }
259
260 /**
261  * Alltoall basic_linear
262  **/
263 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
264                                           MPI_Datatype sendtype,
265                                           void *recvbuf, int recvcount,
266                                           MPI_Datatype recvtype,
267                                           MPI_Comm comm)
268 {
269   int system_tag = 888;
270   int i, rank, size, err, count;
271   MPI_Aint lb = 0, sendext = 0, recvext = 0;
272   MPI_Request *requests;
273
274   /* Initialize. */
275   rank = smpi_comm_rank(comm);
276   size = smpi_comm_size(comm);
277   XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
278   err = smpi_datatype_extent(sendtype, &lb, &sendext);
279   err = smpi_datatype_extent(recvtype, &lb, &recvext);
280   /* simple optimization */
281   err = smpi_datatype_copy(sendbuf + rank * sendcount * sendext, sendcount, 
282                            sendtype, 
283                            recvbuf + rank * recvcount * recvext, recvcount, 
284                            recvtype);
285   if (err == MPI_SUCCESS && size > 1) {
286     /* Initiate all send/recv to/from others. */
287     requests = xbt_new(MPI_Request, 2 * (size - 1));
288     /* Post all receives first -- a simple optimization */
289     count = 0;
290     for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
291       requests[count] =
292           smpi_irecv_init(recvbuf + i * recvcount * recvext, recvcount, 
293                           recvtype, i, system_tag, comm);
294       count++;
295     }
296     /* Now post all sends in reverse order
297      *   - We would like to minimize the search time through message queue
298      *     when messages actually arrive in the order in which they were posted.
299      * TODO: check the previous assertion
300      */
301     for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
302       requests[count] =
303           smpi_isend_init(sendbuf + i * sendcount * sendext, sendcount,
304                           sendtype, i, system_tag, comm);
305       count++;
306     }
307     /* Wait for them all. */
308     smpi_mpi_startall(count, requests);
309     XBT_DEBUG("<%d> wait for %d requests", rank, count);
310     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
311     xbt_free(requests);
312   }
313   return err;
314 }
315
316 /**
317  * Alltoall pairwise
318  *
319  * this algorithm performs size steps (1<=s<=size) and
320  * at each step s, a process p sends iand receive to.from a unique distinct remote process
321  * size=5 : s=1:  4->0->1, 0->1->2, 1->2->3, ...
322  *          s=2:  3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
323  *          ....
324  * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
325  **/
326 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
327                                       MPI_Datatype sendtype, void *recvbuf,
328                                       int recvcount, MPI_Datatype recvtype,
329                                       MPI_Comm comm)
330 {
331   int system_tag = 999;
332   int rank, size, step, sendto, recvfrom, sendsize, recvsize;
333
334   rank = smpi_comm_rank(comm);
335   size = smpi_comm_size(comm);
336   XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
337   sendsize = smpi_datatype_size(sendtype);
338   recvsize = smpi_datatype_size(recvtype);
339   /* Perform pairwise exchange - starting from 1 so the local copy is last */
340   for (step = 1; step < size + 1; step++) {
341     /* who do we talk to in this step? */
342     sendto = (rank + step) % size;
343     recvfrom = (rank + size - step) % size;
344     /* send and receive */
345     smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
346                       sendcount, sendtype, sendto, system_tag,
347                       &((char *) recvbuf)[recvfrom * recvsize * recvcount],
348                       recvcount, recvtype, recvfrom, system_tag, comm,
349                       MPI_STATUS_IGNORE);
350   }
351   return MPI_SUCCESS;
352 }
353
354 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
355                               int *senddisps, MPI_Datatype sendtype,
356                               void *recvbuf, int *recvcounts,
357                               int *recvdisps, MPI_Datatype recvtype,
358                               MPI_Comm comm)
359 {
360   int system_tag = 889;
361   int i, rank, size, err, count;
362   MPI_Aint lb = 0, sendext = 0, recvext = 0;
363   MPI_Request *requests;
364
365   /* Initialize. */
366   rank = smpi_comm_rank(comm);
367   size = smpi_comm_size(comm);
368   XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
369   err = smpi_datatype_extent(sendtype, &lb, &sendext);
370   err = smpi_datatype_extent(recvtype, &lb, &recvext);
371   /* Local copy from self */
372   err =
373       smpi_datatype_copy(sendbuf + senddisps[rank] * sendext, sendcounts[rank], 
374                          sendtype,
375                          recvbuf + recvdisps[rank] * recvext, recvcounts[rank],
376                          recvtype);
377   if (err == MPI_SUCCESS && size > 1) {
378     /* Initiate all send/recv to/from others. */
379     requests = xbt_new(MPI_Request, 2 * (size - 1));
380     count = 0;
381     /* Create all receives that will be posted first */
382     for (i = 0; i < size; ++i) {
383       if (i == rank || recvcounts[i] == 0) {
384         XBT_DEBUG
385             ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
386              rank, i, recvcounts[i]);
387         continue;
388       }
389       requests[count] =
390           smpi_irecv_init(recvbuf + recvdisps[i] * recvext, recvcounts[i], 
391                           recvtype, i, system_tag, comm);
392       count++;
393     }
394     /* Now create all sends  */
395     for (i = 0; i < size; ++i) {
396       if (i == rank || sendcounts[i] == 0) {
397         XBT_DEBUG
398             ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
399              rank, i, sendcounts[i]);
400         continue;
401       }
402       requests[count] =
403           smpi_isend_init(sendbuf + senddisps[i] * sendext, sendcounts[i], 
404                           sendtype, i, system_tag, comm);
405       count++;
406     }
407     /* Wait for them all. */
408     smpi_mpi_startall(count, requests);
409     XBT_DEBUG("<%d> wait for %d requests", rank, count);
410     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
411     xbt_free(requests);
412   }
413   return err;
414 }