Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50ee98edc4474840ffe6133b83dccbdf6854feda
[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 #include "colls/colls.h"
15 #include "simgrid/sg_config.h"
16
17 s_mpi_coll_description_t mpi_coll_gather_description[] = {
18   {"default",
19    "gather default collective",
20    smpi_mpi_gather},
21 COLL_GATHERS(COLL_DESCRIPTION, COLL_COMMA),
22   {NULL, NULL, NULL}      /* this array must be NULL terminated */
23 };
24
25
26 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
27   {"default",
28    "allgather default collective",
29    smpi_mpi_allgather},
30 COLL_ALLGATHERS(COLL_DESCRIPTION, COLL_COMMA),
31   {NULL, NULL, NULL}      /* this array must be NULL terminated */
32 };
33
34 s_mpi_coll_description_t mpi_coll_allgatherv_description[] = {
35   {"default",
36    "allgatherv default collective",
37    smpi_mpi_allgatherv},
38 COLL_ALLGATHERVS(COLL_DESCRIPTION, COLL_COMMA),
39   {NULL, NULL, NULL}      /* this array must be NULL terminated */
40 };
41
42 s_mpi_coll_description_t mpi_coll_allreduce_description[] = {
43   {"default",
44    "allreduce default collective",
45    smpi_mpi_allreduce},
46 COLL_ALLREDUCES(COLL_DESCRIPTION, COLL_COMMA),
47   {NULL, NULL, NULL}      /* this array must be NULL terminated */
48 };
49
50 s_mpi_coll_description_t mpi_coll_reduce_scatter_description[] = {
51   {"default",
52    "reduce_scatter default collective",
53    smpi_mpi_reduce_scatter},
54 COLL_REDUCE_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
55   {NULL, NULL, NULL}      /* this array must be NULL terminated */
56 };
57
58 s_mpi_coll_description_t mpi_coll_scatter_description[] = {
59   {"default",
60    "scatter default collective",
61    smpi_mpi_scatter},
62 COLL_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
63   {NULL, NULL, NULL}      /* this array must be NULL terminated */
64 };
65
66 s_mpi_coll_description_t mpi_coll_alltoall_description[] = {
67   {"default",
68    "Ompi alltoall default collective",
69    smpi_coll_tuned_alltoall_ompi2},
70 COLL_ALLTOALLS(COLL_DESCRIPTION, COLL_COMMA),
71   {"bruck",
72    "Alltoall Bruck (SG) collective",
73    smpi_coll_tuned_alltoall_bruck},
74   {"basic_linear",
75    "Alltoall basic linear (SG) collective",
76    smpi_coll_tuned_alltoall_basic_linear},
77   {NULL, NULL, NULL}      /* this array must be NULL terminated */
78 };
79
80 s_mpi_coll_description_t mpi_coll_alltoallv_description[] = {
81   {"default",
82    "Ompi alltoallv default collective",
83    smpi_coll_basic_alltoallv},
84 COLL_ALLTOALLVS(COLL_DESCRIPTION, COLL_COMMA),
85   {NULL, NULL, NULL}      /* this array must be NULL terminated */
86 };
87
88 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
89   {"default",
90    "bcast default collective",
91    smpi_mpi_bcast},
92 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
93   {NULL, NULL, NULL}      /* this array must be NULL terminated */
94 };
95
96 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
97   {"default",
98    "reduce default collective",
99    smpi_mpi_reduce},
100 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
101   {NULL, NULL, NULL}      /* this array must be NULL terminated */
102 };
103
104
105
106 /** Displays the long description of all registered models, and quit */
107 void coll_help(const char *category, s_mpi_coll_description_t * table)
108 {
109   int i;
110   printf("Long description of the %s models accepted by this simulator:\n",
111          category);
112   for (i = 0; table[i].name; i++)
113     printf("  %s: %s\n", table[i].name, table[i].description);
114 }
115
116 int find_coll_description(s_mpi_coll_description_t * table,
117                            char *name)
118 {
119   int i;
120   char *name_list = NULL;
121   int selector_on=0;
122   if(name==NULL){//no argument provided, use active selector's algorithm
123     name=(char*)sg_cfg_get_string("smpi/coll_selector");
124     selector_on=1;
125   }
126   for (i = 0; table[i].name; i++)
127     if (!strcmp(name, table[i].name)) {
128       return i;
129     }
130
131   if(selector_on){
132     // collective seems not handled by the active selector, try with default one
133     name=(char*)"default";
134     for (i = 0; table[i].name; i++)
135       if (!strcmp(name, table[i].name)) {
136         return i;
137     }
138   }
139   name_list = strdup(table[0].name);
140   for (i = 1; table[i].name; i++) {
141     name_list =
142         xbt_realloc(name_list,
143                     strlen(name_list) + strlen(table[i].name) + 3);
144     strcat(name_list, ", ");
145     strcat(name_list, table[i].name);
146   }
147   xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
148   return -1;
149 }
150
151 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
152                                 "Logging specific to SMPI (coll)");
153
154 int (*mpi_coll_gather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, int root, MPI_Comm);
155 int (*mpi_coll_allgather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
156 int (*mpi_coll_allgatherv_fun)(void *, int, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
157 int (*mpi_coll_allreduce_fun)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
158 int (*mpi_coll_alltoall_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
159 int (*mpi_coll_alltoallv_fun)(void *, int*, int*, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
160 int (*mpi_coll_bcast_fun)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com);
161 int (*mpi_coll_reduce_fun)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
162 int (*mpi_coll_reduce_scatter_fun)(void *sbuf, void *rbuf, int *rcounts,MPI_Datatype dtype,MPI_Op  op,MPI_Comm  comm);
163 int (*mpi_coll_scatter_fun)(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,int root, MPI_Comm comm);
164 struct s_proc_tree {
165   int PROCTREE_A;
166   int numChildren;
167   int *child;
168   int parent;
169   int me;
170   int root;
171   int isRoot;
172 };
173 typedef struct s_proc_tree *proc_tree_t;
174
175 /**
176  * alloc and init
177  **/
178 static proc_tree_t alloc_tree(int arity)
179 {
180   proc_tree_t tree;
181   int i;
182
183   tree = xbt_new(struct s_proc_tree, 1);
184   tree->PROCTREE_A = arity;
185   tree->isRoot = 0;
186   tree->numChildren = 0;
187   tree->child = xbt_new(int, arity);
188   for (i = 0; i < arity; i++) {
189     tree->child[i] = -1;
190   }
191   tree->root = -1;
192   tree->parent = -1;
193   return tree;
194 }
195
196 /**
197  * free
198  **/
199 static void free_tree(proc_tree_t tree)
200 {
201   xbt_free(tree->child);
202   xbt_free(tree);
203 }
204
205 /**
206  * Build the tree depending on a process rank (index) and the group size (extent)
207  * @param root the rank of the tree root
208  * @param rank the rank of the calling process
209  * @param size the total number of processes
210  **/
211 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
212 {
213   int index = (rank - root + size) % size;
214   int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
215   int i;
216
217   (*tree)->me = rank;
218   (*tree)->root = root;
219
220   for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
221     (*tree)->child[i] = (firstChildIdx + i + root) % size;
222     (*tree)->numChildren++;
223   }
224   if (rank == root) {
225     (*tree)->isRoot = 1;
226   } else {
227     (*tree)->isRoot = 0;
228     (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
229   }
230 }
231
232 /**
233  * bcast
234  **/
235 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
236                        MPI_Comm comm, proc_tree_t tree)
237 {
238   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
239   int rank, i;
240   MPI_Request *requests;
241
242   rank = smpi_comm_rank(comm);
243   /* wait for data from my parent in the tree */
244   if (!tree->isRoot) {
245     XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
246            rank, tree->parent, system_tag + rank);
247     smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
248                   comm, MPI_STATUS_IGNORE);
249   }
250   requests = xbt_new(MPI_Request, tree->numChildren);
251   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
252          tree->numChildren);
253   /* iniates sends to ranks lower in the tree */
254   for (i = 0; i < tree->numChildren; i++) {
255     if (tree->child[i] == -1) {
256       requests[i] = MPI_REQUEST_NULL;
257     } else {
258       XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
259              system_tag + tree->child[i]);
260       requests[i] =
261           smpi_isend_init(buf, count, datatype, tree->child[i],
262                           system_tag + tree->child[i], comm);
263     }
264   }
265   smpi_mpi_startall(tree->numChildren, requests);
266   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
267   xbt_free(requests);
268 }
269
270 /**
271  * anti-bcast
272  **/
273 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
274                            MPI_Comm comm, proc_tree_t tree)
275 {
276   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
277   int rank, i;
278   MPI_Request *requests;
279
280   rank = smpi_comm_rank(comm);
281   // everyone sends to its parent, except root.
282   if (!tree->isRoot) {
283     XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
284            rank, tree->parent, system_tag + rank);
285     smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
286                   comm);
287   }
288   //every one receives as many messages as it has children
289   requests = xbt_new(MPI_Request, tree->numChildren);
290   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
291          tree->numChildren);
292   for (i = 0; i < tree->numChildren; i++) {
293     if (tree->child[i] == -1) {
294       requests[i] = MPI_REQUEST_NULL;
295     } else {
296       XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
297              system_tag + tree->child[i]);
298       requests[i] =
299           smpi_irecv_init(buf, count, datatype, tree->child[i],
300                           system_tag + tree->child[i], comm);
301     }
302   }
303   smpi_mpi_startall(tree->numChildren, requests);
304   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
305   xbt_free(requests);
306 }
307
308 /**
309  * bcast with a binary, ternary, or whatever tree ..
310  **/
311 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
312                      MPI_Comm comm, int arity)
313 {
314   proc_tree_t tree = alloc_tree(arity);
315   int rank, size;
316
317   rank = smpi_comm_rank(comm);
318   size = smpi_comm_size(comm);
319   build_tree(root, rank, size, &tree);
320   tree_bcast(buf, count, datatype, comm, tree);
321   free_tree(tree);
322 }
323
324 /**
325  * barrier with a binary, ternary, or whatever tree ..
326  **/
327 void nary_tree_barrier(MPI_Comm comm, int arity)
328 {
329   proc_tree_t tree = alloc_tree(arity);
330   int rank, size;
331   char dummy = '$';
332
333   rank = smpi_comm_rank(comm);
334   size = smpi_comm_size(comm);
335   build_tree(0, rank, size, &tree);
336   tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
337   tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
338   free_tree(tree);
339 }
340
341 int smpi_coll_tuned_alltoall_ompi2(void *sendbuf, int sendcount,
342                                    MPI_Datatype sendtype, void *recvbuf,
343                                    int recvcount, MPI_Datatype recvtype,
344                                    MPI_Comm comm)
345 {
346   int size, sendsize;   
347   size = smpi_comm_size(comm);  
348   sendsize = smpi_datatype_size(sendtype) * sendcount;  
349   if (sendsize < 200 && size > 12) {
350     return
351         smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
352                                        recvbuf, recvcount, recvtype,
353                                        comm);
354   } else if (sendsize < 3000) {
355     return
356         smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
357                                               sendtype, recvbuf,
358                                               recvcount, recvtype, comm);
359   } else {
360     return
361         smpi_coll_tuned_alltoall_ring(sendbuf, sendcount, sendtype,
362                                       recvbuf, recvcount, recvtype,
363                                       comm);
364   }
365 }
366
367 /**
368  * Alltoall Bruck
369  *
370  * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
371  * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
372  * less...
373  **/
374 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
375                                    MPI_Datatype sendtype, void *recvbuf,
376                                    int recvcount, MPI_Datatype recvtype,
377                                    MPI_Comm comm)
378 {
379   int system_tag = 777;
380   int i, rank, size, err, count;
381   MPI_Aint lb;
382   MPI_Aint sendext = 0;
383   MPI_Aint recvext = 0;
384   MPI_Request *requests;
385
386   // FIXME: check implementation
387   rank = smpi_comm_rank(comm);
388   size = smpi_comm_size(comm);
389   XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
390   err = smpi_datatype_extent(sendtype, &lb, &sendext);
391   err = smpi_datatype_extent(recvtype, &lb, &recvext);
392   /* Local copy from self */
393   err =
394       smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
395                          sendcount, sendtype, 
396                          (char *)recvbuf + rank * recvcount * recvext,
397                          recvcount, recvtype);
398   if (err == MPI_SUCCESS && size > 1) {
399     /* Initiate all send/recv to/from others. */
400     requests = xbt_new(MPI_Request, 2 * (size - 1));
401     count = 0;
402     /* Create all receives that will be posted first */
403     for (i = 0; i < size; ++i) {
404       if (i == rank) {
405         XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
406                rank, i, recvcount);
407         continue;
408       }
409       requests[count] =
410           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
411                           recvtype, i, system_tag, comm);
412       count++;
413     }
414     /* Now create all sends  */
415     for (i = 0; i < size; ++i) {
416       if (i == rank) {
417         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
418                rank, i, sendcount);
419         continue;
420       }
421       requests[count] =
422           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
423                           sendtype, i, system_tag, comm);
424       count++;
425     }
426     /* Wait for them all. */
427     smpi_mpi_startall(count, requests);
428     XBT_DEBUG("<%d> wait for %d requests", rank, count);
429     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
430     xbt_free(requests);
431   }
432   return MPI_SUCCESS;
433 }
434
435 /**
436  * Alltoall basic_linear (STARMPI:alltoall-simple)
437  **/
438 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
439                                           MPI_Datatype sendtype,
440                                           void *recvbuf, int recvcount,
441                                           MPI_Datatype recvtype,
442                                           MPI_Comm comm)
443 {
444   int system_tag = 888;
445   int i, rank, size, err, count;
446   MPI_Aint lb = 0, sendext = 0, recvext = 0;
447   MPI_Request *requests;
448
449   /* Initialize. */
450   rank = smpi_comm_rank(comm);
451   size = smpi_comm_size(comm);
452   XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
453   err = smpi_datatype_extent(sendtype, &lb, &sendext);
454   err = smpi_datatype_extent(recvtype, &lb, &recvext);
455   /* simple optimization */
456   err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
457                            sendcount, sendtype, 
458                            (char *)recvbuf + rank * recvcount * recvext, 
459                            recvcount, recvtype);
460   if (err == MPI_SUCCESS && size > 1) {
461     /* Initiate all send/recv to/from others. */
462     requests = xbt_new(MPI_Request, 2 * (size - 1));
463     /* Post all receives first -- a simple optimization */
464     count = 0;
465     for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
466       requests[count] =
467           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount, 
468                           recvtype, i, system_tag, comm);
469       count++;
470     }
471     /* Now post all sends in reverse order
472      *   - We would like to minimize the search time through message queue
473      *     when messages actually arrive in the order in which they were posted.
474      * TODO: check the previous assertion
475      */
476     for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
477       requests[count] =
478           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
479                           sendtype, i, system_tag, comm);
480       count++;
481     }
482     /* Wait for them all. */
483     smpi_mpi_startall(count, requests);
484     XBT_DEBUG("<%d> wait for %d requests", rank, count);
485     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
486     xbt_free(requests);
487   }
488   return err;
489 }
490
491 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
492                               int *senddisps, MPI_Datatype sendtype,
493                               void *recvbuf, int *recvcounts,
494                               int *recvdisps, MPI_Datatype recvtype,
495                               MPI_Comm comm)
496 {
497   int system_tag = 889;
498   int i, rank, size, err, count;
499   MPI_Aint lb = 0, sendext = 0, recvext = 0;
500   MPI_Request *requests;
501
502   /* Initialize. */
503   rank = smpi_comm_rank(comm);
504   size = smpi_comm_size(comm);
505   XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
506   err = smpi_datatype_extent(sendtype, &lb, &sendext);
507   err = smpi_datatype_extent(recvtype, &lb, &recvext);
508   /* Local copy from self */
509   err =
510       smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext, 
511                          sendcounts[rank], sendtype,
512                          (char *)recvbuf + recvdisps[rank] * recvext, 
513                          recvcounts[rank], recvtype);
514   if (err == MPI_SUCCESS && size > 1) {
515     /* Initiate all send/recv to/from others. */
516     requests = xbt_new(MPI_Request, 2 * (size - 1));
517     count = 0;
518     /* Create all receives that will be posted first */
519     for (i = 0; i < size; ++i) {
520       if (i == rank || recvcounts[i] == 0) {
521         XBT_DEBUG
522             ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
523              rank, i, recvcounts[i]);
524         continue;
525       }
526       requests[count] =
527           smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext, 
528                           recvcounts[i], recvtype, i, system_tag, comm);
529       count++;
530     }
531     /* Now create all sends  */
532     for (i = 0; i < size; ++i) {
533       if (i == rank || sendcounts[i] == 0) {
534         XBT_DEBUG
535             ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
536              rank, i, sendcounts[i]);
537         continue;
538       }
539       requests[count] =
540           smpi_isend_init((char *)sendbuf + senddisps[i] * sendext, 
541                           sendcounts[i], sendtype, i, system_tag, comm);
542       count++;
543     }
544     /* Wait for them all. */
545     smpi_mpi_startall(count, requests);
546     XBT_DEBUG("<%d> wait for %d requests", rank, count);
547     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
548     xbt_free(requests);
549   }
550   return err;
551 }