1 /* smpi_coll.c -- various optimized routing for collectives */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
14 #include "colls/colls.h"
15 #include "simgrid/sg_config.h"
17 s_mpi_coll_description_t mpi_coll_gather_description[] = {
19 "gather default collective",
21 COLL_GATHERS(COLL_DESCRIPTION, COLL_COMMA),
22 {NULL, NULL, NULL} /* this array must be NULL terminated */
26 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
28 "allgather default collective",
30 COLL_ALLGATHERS(COLL_DESCRIPTION, COLL_COMMA),
31 {NULL, NULL, NULL} /* this array must be NULL terminated */
34 s_mpi_coll_description_t mpi_coll_allgatherv_description[] = {
36 "allgatherv default collective",
38 COLL_ALLGATHERVS(COLL_DESCRIPTION, COLL_COMMA),
39 {NULL, NULL, NULL} /* this array must be NULL terminated */
42 s_mpi_coll_description_t mpi_coll_allreduce_description[] = {
44 "allreduce default collective",
46 COLL_ALLREDUCES(COLL_DESCRIPTION, COLL_COMMA),
47 {NULL, NULL, NULL} /* this array must be NULL terminated */
50 s_mpi_coll_description_t mpi_coll_reduce_scatter_description[] = {
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 */
58 s_mpi_coll_description_t mpi_coll_scatter_description[] = {
60 "scatter default collective",
62 COLL_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
63 {NULL, NULL, NULL} /* this array must be NULL terminated */
66 s_mpi_coll_description_t mpi_coll_alltoall_description[] = {
68 "Ompi alltoall default collective",
69 smpi_coll_tuned_alltoall_ompi2},
70 COLL_ALLTOALLS(COLL_DESCRIPTION, COLL_COMMA),
72 "Alltoall Bruck (SG) collective",
73 smpi_coll_tuned_alltoall_bruck},
75 "Alltoall basic linear (SG) collective",
76 smpi_coll_tuned_alltoall_basic_linear},
77 {NULL, NULL, NULL} /* this array must be NULL terminated */
80 s_mpi_coll_description_t mpi_coll_alltoallv_description[] = {
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 */
88 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
90 "bcast default collective",
92 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
93 {NULL, NULL, NULL} /* this array must be NULL terminated */
96 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
98 "reduce default collective",
100 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
101 {NULL, NULL, NULL} /* this array must be NULL terminated */
106 /** Displays the long description of all registered models, and quit */
107 void coll_help(const char *category, s_mpi_coll_description_t * table)
110 printf("Long description of the %s models accepted by this simulator:\n",
112 for (i = 0; table[i].name; i++)
113 printf(" %s: %s\n", table[i].name, table[i].description);
116 int find_coll_description(s_mpi_coll_description_t * table,
120 char *name_list = NULL;
122 if(name==NULL){//no argument provided, use active selector's algorithm
123 name=(char*)sg_cfg_get_string("smpi/coll_selector");
126 for (i = 0; table[i].name; i++)
127 if (!strcmp(name, table[i].name)) {
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)) {
139 name_list = strdup(table[0].name);
140 for (i = 1; table[i].name; i++) {
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);
147 xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
151 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
152 "Logging specific to SMPI (coll)");
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);
173 typedef struct s_proc_tree *proc_tree_t;
178 static proc_tree_t alloc_tree(int arity)
183 tree = xbt_new(struct s_proc_tree, 1);
184 tree->PROCTREE_A = arity;
186 tree->numChildren = 0;
187 tree->child = xbt_new(int, arity);
188 for (i = 0; i < arity; i++) {
199 static void free_tree(proc_tree_t tree)
201 xbt_free(tree->child);
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
211 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
213 int index = (rank - root + size) % size;
214 int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
218 (*tree)->root = root;
220 for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
221 (*tree)->child[i] = (firstChildIdx + i + root) % size;
222 (*tree)->numChildren++;
228 (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
235 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
236 MPI_Comm comm, proc_tree_t tree)
238 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
240 MPI_Request *requests;
242 rank = smpi_comm_rank(comm);
243 /* wait for data from my parent in the tree */
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);
250 requests = xbt_new(MPI_Request, tree->numChildren);
251 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
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;
258 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
259 system_tag + tree->child[i]);
261 smpi_isend_init(buf, count, datatype, tree->child[i],
262 system_tag + tree->child[i], comm);
265 smpi_mpi_startall(tree->numChildren, requests);
266 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
273 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
274 MPI_Comm comm, proc_tree_t tree)
276 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
278 MPI_Request *requests;
280 rank = smpi_comm_rank(comm);
281 // everyone sends to its parent, except root.
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,
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,
292 for (i = 0; i < tree->numChildren; i++) {
293 if (tree->child[i] == -1) {
294 requests[i] = MPI_REQUEST_NULL;
296 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
297 system_tag + tree->child[i]);
299 smpi_irecv_init(buf, count, datatype, tree->child[i],
300 system_tag + tree->child[i], comm);
303 smpi_mpi_startall(tree->numChildren, requests);
304 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
309 * bcast with a binary, ternary, or whatever tree ..
311 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
312 MPI_Comm comm, int arity)
314 proc_tree_t tree = alloc_tree(arity);
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);
325 * barrier with a binary, ternary, or whatever tree ..
327 void nary_tree_barrier(MPI_Comm comm, int arity)
329 proc_tree_t tree = alloc_tree(arity);
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);
341 int smpi_coll_tuned_alltoall_ompi2(void *sendbuf, int sendcount,
342 MPI_Datatype sendtype, void *recvbuf,
343 int recvcount, MPI_Datatype recvtype,
347 size = smpi_comm_size(comm);
348 sendsize = smpi_datatype_size(sendtype) * sendcount;
349 if (sendsize < 200 && size > 12) {
351 smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
352 recvbuf, recvcount, recvtype,
354 } else if (sendsize < 3000) {
356 smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
358 recvcount, recvtype, comm);
361 smpi_coll_tuned_alltoall_ring(sendbuf, sendcount, sendtype,
362 recvbuf, recvcount, recvtype,
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
374 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
375 MPI_Datatype sendtype, void *recvbuf,
376 int recvcount, MPI_Datatype recvtype,
379 int system_tag = 777;
380 int i, rank, size, err, count;
382 MPI_Aint sendext = 0;
383 MPI_Aint recvext = 0;
384 MPI_Request *requests;
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 */
394 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
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));
402 /* Create all receives that will be posted first */
403 for (i = 0; i < size; ++i) {
405 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
410 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
411 recvtype, i, system_tag, comm);
414 /* Now create all sends */
415 for (i = 0; i < size; ++i) {
417 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
422 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
423 sendtype, i, system_tag, comm);
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);
436 * Alltoall basic_linear (STARMPI:alltoall-simple)
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,
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;
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,
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 */
465 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
467 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
468 recvtype, i, system_tag, comm);
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
476 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
478 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
479 sendtype, i, system_tag, comm);
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);
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,
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;
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 */
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));
518 /* Create all receives that will be posted first */
519 for (i = 0; i < size; ++i) {
520 if (i == rank || recvcounts[i] == 0) {
522 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
523 rank, i, recvcounts[i]);
527 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
528 recvcounts[i], recvtype, i, system_tag, comm);
531 /* Now create all sends */
532 for (i = 0; i < size; ++i) {
533 if (i == rank || sendcounts[i] == 0) {
535 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
536 rank, i, sendcounts[i]);
540 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
541 sendcounts[i], sendtype, i, system_tag, comm);
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);