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},
43 {NULL, NULL, NULL} /* this array must be NULL terminated */
46 s_mpi_coll_description_t mpi_coll_alltoallv_description[] = {
48 "Ompi alltoallv default collective",
49 smpi_coll_basic_alltoallv},
50 COLL_ALLTOALLVS(COLL_DESCRIPTION, COLL_COMMA),
51 {NULL, NULL, NULL} /* this array must be NULL terminated */
54 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
56 "allgather default collective",
58 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
59 {NULL, NULL, NULL} /* this array must be NULL terminated */
62 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
64 "allgather default collective",
66 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
67 {NULL, NULL, NULL} /* this array must be NULL terminated */
72 /** Displays the long description of all registered models, and quit */
73 void coll_help(const char *category, s_mpi_coll_description_t * table)
76 printf("Long description of the %s models accepted by this simulator:\n",
78 for (i = 0; table[i].name; i++)
79 printf(" %s: %s\n", table[i].name, table[i].description);
82 int find_coll_description(s_mpi_coll_description_t * table,
86 char *name_list = NULL;
88 for (i = 0; table[i].name; i++)
89 if (!strcmp(name, table[i].name)) {
92 name_list = strdup(table[0].name);
93 for (i = 1; table[i].name; i++) {
95 xbt_realloc(name_list,
96 strlen(name_list) + strlen(table[i].name) + 3);
97 strcat(name_list, ", ");
98 strcat(name_list, table[i].name);
100 xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
106 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
107 "Logging specific to SMPI (coll)");
109 int (*mpi_coll_allgather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
110 int (*mpi_coll_allreduce_fun)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
111 int (*mpi_coll_alltoall_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
112 int (*mpi_coll_alltoallv_fun)(void *, int*, int*, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
113 int (*mpi_coll_bcast_fun)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com);
114 int (*mpi_coll_reduce_fun)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
125 typedef struct s_proc_tree *proc_tree_t;
130 static proc_tree_t alloc_tree(int arity)
135 tree = xbt_new(struct s_proc_tree, 1);
136 tree->PROCTREE_A = arity;
138 tree->numChildren = 0;
139 tree->child = xbt_new(int, arity);
140 for (i = 0; i < arity; i++) {
151 static void free_tree(proc_tree_t tree)
153 xbt_free(tree->child);
158 * Build the tree depending on a process rank (index) and the group size (extent)
159 * @param root the rank of the tree root
160 * @param rank the rank of the calling process
161 * @param size the total number of processes
163 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
165 int index = (rank - root + size) % size;
166 int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
170 (*tree)->root = root;
172 for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
173 (*tree)->child[i] = (firstChildIdx + i + root) % size;
174 (*tree)->numChildren++;
180 (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
187 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
188 MPI_Comm comm, proc_tree_t tree)
190 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
192 MPI_Request *requests;
194 rank = smpi_comm_rank(comm);
195 /* wait for data from my parent in the tree */
197 XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
198 rank, tree->parent, system_tag + rank);
199 smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
200 comm, MPI_STATUS_IGNORE);
202 requests = xbt_new(MPI_Request, tree->numChildren);
203 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
205 /* iniates sends to ranks lower in the tree */
206 for (i = 0; i < tree->numChildren; i++) {
207 if (tree->child[i] == -1) {
208 requests[i] = MPI_REQUEST_NULL;
210 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
211 system_tag + tree->child[i]);
213 smpi_isend_init(buf, count, datatype, tree->child[i],
214 system_tag + tree->child[i], comm);
217 smpi_mpi_startall(tree->numChildren, requests);
218 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
225 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
226 MPI_Comm comm, proc_tree_t tree)
228 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
230 MPI_Request *requests;
232 rank = smpi_comm_rank(comm);
233 // everyone sends to its parent, except root.
235 XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
236 rank, tree->parent, system_tag + rank);
237 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
240 //every one receives as many messages as it has children
241 requests = xbt_new(MPI_Request, tree->numChildren);
242 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
244 for (i = 0; i < tree->numChildren; i++) {
245 if (tree->child[i] == -1) {
246 requests[i] = MPI_REQUEST_NULL;
248 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
249 system_tag + tree->child[i]);
251 smpi_irecv_init(buf, count, datatype, tree->child[i],
252 system_tag + tree->child[i], comm);
255 smpi_mpi_startall(tree->numChildren, requests);
256 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
261 * bcast with a binary, ternary, or whatever tree ..
263 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
264 MPI_Comm comm, int arity)
266 proc_tree_t tree = alloc_tree(arity);
269 rank = smpi_comm_rank(comm);
270 size = smpi_comm_size(comm);
271 build_tree(root, rank, size, &tree);
272 tree_bcast(buf, count, datatype, comm, tree);
277 * barrier with a binary, ternary, or whatever tree ..
279 void nary_tree_barrier(MPI_Comm comm, int arity)
281 proc_tree_t tree = alloc_tree(arity);
285 rank = smpi_comm_rank(comm);
286 size = smpi_comm_size(comm);
287 build_tree(0, rank, size, &tree);
288 tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
289 tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
293 int smpi_coll_tuned_alltoall_ompi(void *sendbuf, int sendcount,
294 MPI_Datatype sendtype, void *recvbuf,
295 int recvcount, MPI_Datatype recvtype,
299 size = smpi_comm_size(comm);
300 sendsize = smpi_datatype_size(sendtype) * sendcount;
301 if (sendsize < 200 && size > 12) {
303 smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
304 recvbuf, recvcount, recvtype,
306 } else if (sendsize < 3000) {
308 smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
310 recvcount, recvtype, comm);
313 smpi_coll_tuned_alltoall_ring(sendbuf, sendcount, sendtype,
314 recvbuf, recvcount, recvtype,
322 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
323 * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
326 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
327 MPI_Datatype sendtype, void *recvbuf,
328 int recvcount, MPI_Datatype recvtype,
331 int system_tag = 777;
332 int i, rank, size, err, count;
334 MPI_Aint sendext = 0;
335 MPI_Aint recvext = 0;
336 MPI_Request *requests;
338 // FIXME: check implementation
339 rank = smpi_comm_rank(comm);
340 size = smpi_comm_size(comm);
341 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
342 err = smpi_datatype_extent(sendtype, &lb, &sendext);
343 err = smpi_datatype_extent(recvtype, &lb, &recvext);
344 /* Local copy from self */
346 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
348 (char *)recvbuf + rank * recvcount * recvext,
349 recvcount, recvtype);
350 if (err == MPI_SUCCESS && size > 1) {
351 /* Initiate all send/recv to/from others. */
352 requests = xbt_new(MPI_Request, 2 * (size - 1));
354 /* Create all receives that will be posted first */
355 for (i = 0; i < size; ++i) {
357 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
362 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
363 recvtype, i, system_tag, comm);
366 /* Now create all sends */
367 for (i = 0; i < size; ++i) {
369 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
374 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
375 sendtype, i, system_tag, comm);
378 /* Wait for them all. */
379 smpi_mpi_startall(count, requests);
380 XBT_DEBUG("<%d> wait for %d requests", rank, count);
381 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
388 * Alltoall basic_linear (STARMPI:alltoall-simple)
390 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
391 MPI_Datatype sendtype,
392 void *recvbuf, int recvcount,
393 MPI_Datatype recvtype,
396 int system_tag = 888;
397 int i, rank, size, err, count;
398 MPI_Aint lb = 0, sendext = 0, recvext = 0;
399 MPI_Request *requests;
402 rank = smpi_comm_rank(comm);
403 size = smpi_comm_size(comm);
404 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
405 err = smpi_datatype_extent(sendtype, &lb, &sendext);
406 err = smpi_datatype_extent(recvtype, &lb, &recvext);
407 /* simple optimization */
408 err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
410 (char *)recvbuf + rank * recvcount * recvext,
411 recvcount, recvtype);
412 if (err == MPI_SUCCESS && size > 1) {
413 /* Initiate all send/recv to/from others. */
414 requests = xbt_new(MPI_Request, 2 * (size - 1));
415 /* Post all receives first -- a simple optimization */
417 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
419 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
420 recvtype, i, system_tag, comm);
423 /* Now post all sends in reverse order
424 * - We would like to minimize the search time through message queue
425 * when messages actually arrive in the order in which they were posted.
426 * TODO: check the previous assertion
428 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
430 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
431 sendtype, i, system_tag, comm);
434 /* Wait for them all. */
435 smpi_mpi_startall(count, requests);
436 XBT_DEBUG("<%d> wait for %d requests", rank, count);
437 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
443 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
444 int *senddisps, MPI_Datatype sendtype,
445 void *recvbuf, int *recvcounts,
446 int *recvdisps, MPI_Datatype recvtype,
449 int system_tag = 889;
450 int i, rank, size, err, count;
451 MPI_Aint lb = 0, sendext = 0, recvext = 0;
452 MPI_Request *requests;
455 rank = smpi_comm_rank(comm);
456 size = smpi_comm_size(comm);
457 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
458 err = smpi_datatype_extent(sendtype, &lb, &sendext);
459 err = smpi_datatype_extent(recvtype, &lb, &recvext);
460 /* Local copy from self */
462 smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext,
463 sendcounts[rank], sendtype,
464 (char *)recvbuf + recvdisps[rank] * recvext,
465 recvcounts[rank], recvtype);
466 if (err == MPI_SUCCESS && size > 1) {
467 /* Initiate all send/recv to/from others. */
468 requests = xbt_new(MPI_Request, 2 * (size - 1));
470 /* Create all receives that will be posted first */
471 for (i = 0; i < size; ++i) {
472 if (i == rank || recvcounts[i] == 0) {
474 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
475 rank, i, recvcounts[i]);
479 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
480 recvcounts[i], recvtype, i, system_tag, comm);
483 /* Now create all sends */
484 for (i = 0; i < size; ++i) {
485 if (i == rank || sendcounts[i] == 0) {
487 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
488 rank, i, sendcounts[i]);
492 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
493 sendcounts[i], sendtype, i, system_tag, comm);
496 /* Wait for them all. */
497 smpi_mpi_startall(count, requests);
498 XBT_DEBUG("<%d> wait for %d requests", rank, count);
499 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);