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"
16 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
18 "allgather default collective",
20 COLL_ALLGATHERS(COLL_DESCRIPTION, COLL_COMMA),
21 {NULL, NULL, NULL} /* this array must be NULL terminated */
24 s_mpi_coll_description_t mpi_coll_allreduce_description[] = {
26 "allreduce default collective",
28 COLL_ALLREDUCES(COLL_DESCRIPTION, COLL_COMMA),
29 {NULL, NULL, NULL} /* this array must be NULL terminated */
32 s_mpi_coll_description_t mpi_coll_alltoall_description[] = {
34 "Ompi alltoall default collective",
35 smpi_coll_tuned_alltoall_ompi},
36 COLL_ALLTOALLS(COLL_DESCRIPTION, COLL_COMMA),
38 "Alltoall Bruck (SG) collective",
39 smpi_coll_tuned_alltoall_bruck},
41 "Alltoall basic linear (SG) collective",
42 smpi_coll_tuned_alltoall_basic_linear},
44 "Alltoall pairwise (SG) collective",
45 smpi_coll_tuned_alltoall_pairwise},
46 {NULL, NULL, NULL} /* this array must be NULL terminated */
49 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
51 "allgather default collective",
53 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
54 {NULL, NULL, NULL} /* this array must be NULL terminated */
57 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
59 "allgather default collective",
61 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
62 {NULL, NULL, NULL} /* this array must be NULL terminated */
67 /** Displays the long description of all registered models, and quit */
68 void coll_help(const char *category, s_mpi_coll_description_t * table)
71 printf("Long description of the %s models accepted by this simulator:\n",
73 for (i = 0; table[i].name; i++)
74 printf(" %s: %s\n", table[i].name, table[i].description);
77 int find_coll_description(s_mpi_coll_description_t * table,
81 char *name_list = NULL;
83 for (i = 0; table[i].name; i++)
84 if (!strcmp(name, table[i].name)) {
87 name_list = strdup(table[0].name);
88 for (i = 1; table[i].name; i++) {
90 xbt_realloc(name_list,
91 strlen(name_list) + strlen(table[i].name) + 3);
92 strcat(name_list, ", ");
93 strcat(name_list, table[i].name);
95 xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
101 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
102 "Logging specific to SMPI (coll)");
104 int (*mpi_coll_allgather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
105 int (*mpi_coll_allreduce_fun)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
106 int (*mpi_coll_alltoall_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
107 int (*mpi_coll_bcast_fun)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com);
108 int (*mpi_coll_reduce_fun)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
119 typedef struct s_proc_tree *proc_tree_t;
124 static proc_tree_t alloc_tree(int arity)
129 tree = xbt_new(struct s_proc_tree, 1);
130 tree->PROCTREE_A = arity;
132 tree->numChildren = 0;
133 tree->child = xbt_new(int, arity);
134 for (i = 0; i < arity; i++) {
145 static void free_tree(proc_tree_t tree)
147 xbt_free(tree->child);
152 * Build the tree depending on a process rank (index) and the group size (extent)
153 * @param root the rank of the tree root
154 * @param rank the rank of the calling process
155 * @param size the total number of processes
157 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
159 int index = (rank - root + size) % size;
160 int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
164 (*tree)->root = root;
166 for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
167 (*tree)->child[i] = (firstChildIdx + i + root) % size;
168 (*tree)->numChildren++;
174 (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
181 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
182 MPI_Comm comm, proc_tree_t tree)
184 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
186 MPI_Request *requests;
188 rank = smpi_comm_rank(comm);
189 /* wait for data from my parent in the tree */
191 XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
192 rank, tree->parent, system_tag + rank);
193 smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
194 comm, MPI_STATUS_IGNORE);
196 requests = xbt_new(MPI_Request, tree->numChildren);
197 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
199 /* iniates sends to ranks lower in the tree */
200 for (i = 0; i < tree->numChildren; i++) {
201 if (tree->child[i] == -1) {
202 requests[i] = MPI_REQUEST_NULL;
204 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
205 system_tag + tree->child[i]);
207 smpi_isend_init(buf, count, datatype, tree->child[i],
208 system_tag + tree->child[i], comm);
211 smpi_mpi_startall(tree->numChildren, requests);
212 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
219 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
220 MPI_Comm comm, proc_tree_t tree)
222 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
224 MPI_Request *requests;
226 rank = smpi_comm_rank(comm);
227 // everyone sends to its parent, except root.
229 XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
230 rank, tree->parent, system_tag + rank);
231 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
234 //every one receives as many messages as it has children
235 requests = xbt_new(MPI_Request, tree->numChildren);
236 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
238 for (i = 0; i < tree->numChildren; i++) {
239 if (tree->child[i] == -1) {
240 requests[i] = MPI_REQUEST_NULL;
242 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
243 system_tag + tree->child[i]);
245 smpi_irecv_init(buf, count, datatype, tree->child[i],
246 system_tag + tree->child[i], comm);
249 smpi_mpi_startall(tree->numChildren, requests);
250 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
255 * bcast with a binary, ternary, or whatever tree ..
257 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
258 MPI_Comm comm, int arity)
260 proc_tree_t tree = alloc_tree(arity);
263 rank = smpi_comm_rank(comm);
264 size = smpi_comm_size(comm);
265 build_tree(root, rank, size, &tree);
266 tree_bcast(buf, count, datatype, comm, tree);
271 * barrier with a binary, ternary, or whatever tree ..
273 void nary_tree_barrier(MPI_Comm comm, int arity)
275 proc_tree_t tree = alloc_tree(arity);
279 rank = smpi_comm_rank(comm);
280 size = smpi_comm_size(comm);
281 build_tree(0, rank, size, &tree);
282 tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
283 tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
287 int smpi_coll_tuned_alltoall_ompi(void *sendbuf, int sendcount,
288 MPI_Datatype sendtype, void *recvbuf,
289 int recvcount, MPI_Datatype recvtype,
293 size = smpi_comm_size(comm);
294 sendsize = smpi_datatype_size(sendtype) * sendcount;
295 if (sendsize < 200 && size > 12) {
297 smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
298 recvbuf, recvcount, recvtype,
300 } else if (sendsize < 3000) {
302 smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
304 recvcount, recvtype, comm);
307 smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, sendtype,
308 recvbuf, recvcount, recvtype,
316 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
317 * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
320 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
321 MPI_Datatype sendtype, void *recvbuf,
322 int recvcount, MPI_Datatype recvtype,
325 int system_tag = 777;
326 int i, rank, size, err, count;
328 MPI_Aint sendext = 0;
329 MPI_Aint recvext = 0;
330 MPI_Request *requests;
332 // FIXME: check implementation
333 rank = smpi_comm_rank(comm);
334 size = smpi_comm_size(comm);
335 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
336 err = smpi_datatype_extent(sendtype, &lb, &sendext);
337 err = smpi_datatype_extent(recvtype, &lb, &recvext);
338 /* Local copy from self */
340 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
342 (char *)recvbuf + rank * recvcount * recvext,
343 recvcount, recvtype);
344 if (err == MPI_SUCCESS && size > 1) {
345 /* Initiate all send/recv to/from others. */
346 requests = xbt_new(MPI_Request, 2 * (size - 1));
348 /* Create all receives that will be posted first */
349 for (i = 0; i < size; ++i) {
351 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
356 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
357 recvtype, i, system_tag, comm);
360 /* Now create all sends */
361 for (i = 0; i < size; ++i) {
363 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
368 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
369 sendtype, i, system_tag, comm);
372 /* Wait for them all. */
373 smpi_mpi_startall(count, requests);
374 XBT_DEBUG("<%d> wait for %d requests", rank, count);
375 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
382 * Alltoall basic_linear (STARMPI:alltoall-simple)
384 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
385 MPI_Datatype sendtype,
386 void *recvbuf, int recvcount,
387 MPI_Datatype recvtype,
390 int system_tag = 888;
391 int i, rank, size, err, count;
392 MPI_Aint lb = 0, sendext = 0, recvext = 0;
393 MPI_Request *requests;
396 rank = smpi_comm_rank(comm);
397 size = smpi_comm_size(comm);
398 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
399 err = smpi_datatype_extent(sendtype, &lb, &sendext);
400 err = smpi_datatype_extent(recvtype, &lb, &recvext);
401 /* simple optimization */
402 err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
404 (char *)recvbuf + rank * recvcount * recvext,
405 recvcount, recvtype);
406 if (err == MPI_SUCCESS && size > 1) {
407 /* Initiate all send/recv to/from others. */
408 requests = xbt_new(MPI_Request, 2 * (size - 1));
409 /* Post all receives first -- a simple optimization */
411 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
413 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
414 recvtype, i, system_tag, comm);
417 /* Now post all sends in reverse order
418 * - We would like to minimize the search time through message queue
419 * when messages actually arrive in the order in which they were posted.
420 * TODO: check the previous assertion
422 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
424 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
425 sendtype, i, system_tag, comm);
428 /* Wait for them all. */
429 smpi_mpi_startall(count, requests);
430 XBT_DEBUG("<%d> wait for %d requests", rank, count);
431 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
440 * this algorithm performs size steps (1<=s<=size) and
441 * at each step s, a process p sends iand receive to.from a unique distinct remote process
442 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
443 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
445 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
447 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
448 MPI_Datatype sendtype, void *recvbuf,
449 int recvcount, MPI_Datatype recvtype,
452 int system_tag = 999;
453 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
455 rank = smpi_comm_rank(comm);
456 size = smpi_comm_size(comm);
457 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
458 sendsize = smpi_datatype_size(sendtype);
459 recvsize = smpi_datatype_size(recvtype);
460 /* Perform pairwise exchange - starting from 1 so the local copy is last */
461 for (step = 1; step < size + 1; step++) {
462 /* who do we talk to in this step? */
463 sendto = (rank + step) % size;
464 recvfrom = (rank + size - step) % size;
465 /* send and receive */
466 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
467 sendcount, sendtype, sendto, system_tag,
468 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
469 recvcount, recvtype, recvfrom, system_tag, comm,
475 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
476 int *senddisps, MPI_Datatype sendtype,
477 void *recvbuf, int *recvcounts,
478 int *recvdisps, MPI_Datatype recvtype,
481 int system_tag = 889;
482 int i, rank, size, err, count;
483 MPI_Aint lb = 0, sendext = 0, recvext = 0;
484 MPI_Request *requests;
487 rank = smpi_comm_rank(comm);
488 size = smpi_comm_size(comm);
489 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
490 err = smpi_datatype_extent(sendtype, &lb, &sendext);
491 err = smpi_datatype_extent(recvtype, &lb, &recvext);
492 /* Local copy from self */
494 smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext,
495 sendcounts[rank], sendtype,
496 (char *)recvbuf + recvdisps[rank] * recvext,
497 recvcounts[rank], recvtype);
498 if (err == MPI_SUCCESS && size > 1) {
499 /* Initiate all send/recv to/from others. */
500 requests = xbt_new(MPI_Request, 2 * (size - 1));
502 /* Create all receives that will be posted first */
503 for (i = 0; i < size; ++i) {
504 if (i == rank || recvcounts[i] == 0) {
506 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
507 rank, i, recvcounts[i]);
511 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
512 recvcounts[i], recvtype, i, system_tag, comm);
515 /* Now create all sends */
516 for (i = 0; i < size; ++i) {
517 if (i == rank || sendcounts[i] == 0) {
519 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
520 rank, i, sendcounts[i]);
524 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
525 sendcounts[i], sendtype, i, system_tag, comm);
528 /* Wait for them all. */
529 smpi_mpi_startall(count, requests);
530 XBT_DEBUG("<%d> wait for %d requests", rank, count);
531 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);