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_alltoall_description[] = {
18 "Ompi alltoall default collective",
19 smpi_coll_tuned_alltoall_ompi},
22 "Alltoall 2dmesh collective",
23 smpi_coll_tuned_alltoall_2dmesh},
25 "Alltoall 3dmesh collective",
26 smpi_coll_tuned_alltoall_3dmesh},
28 "Alltoall Bruck collective",
29 smpi_coll_tuned_alltoall_bruck},*/
31 "Alltoall pair collective",
32 smpi_coll_tuned_alltoall_pair},
33 {"pair_light_barrier",
34 "Alltoall pair_light_barrier collective",
35 smpi_coll_tuned_alltoall_pair_light_barrier},
37 "Alltoall pair_mpi_barrier collective",
38 smpi_coll_tuned_alltoall_pair_mpi_barrier},
40 "Alltoall rdb collective",
41 smpi_coll_tuned_alltoall_rdb},
43 "Alltoall ring collective",
44 smpi_coll_tuned_alltoall_ring},
45 {"ring_light_barrier",
46 "Alltoall ring_light_barrier collective",
47 smpi_coll_tuned_alltoall_ring_light_barrier},
48 {"ring_light_barrier",
49 "Alltoall ring_light_barrier collective",
50 smpi_coll_tuned_alltoall_ring_light_barrier},
52 "Alltoall ring_mpi_barrier collective",
53 smpi_coll_tuned_alltoall_ring_mpi_barrier},
55 "Alltoall ring_one_barrier collective",
56 smpi_coll_tuned_alltoall_ring_one_barrier},
58 "Alltoall simple collective",
59 smpi_coll_tuned_alltoall_simple},
62 "Alltoall Bruck (SG) collective",
63 smpi_coll_tuned_alltoall_bruck},
65 "Alltoall basic linear (SG) collective",
66 smpi_coll_tuned_alltoall_basic_linear},
68 "Alltoall pairwise (SG) collective",
69 smpi_coll_tuned_alltoall_pairwise},
71 {NULL, NULL, NULL} /* this array must be NULL terminated */
74 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
76 "allgather default collective",
79 {NULL, NULL, NULL} /* this array must be NULL terminated */
83 /** Displays the long description of all registered models, and quit */
84 void coll_help(const char *category, s_mpi_coll_description_t * table)
87 printf("Long description of the %s models accepted by this simulator:\n",
89 for (i = 0; table[i].name; i++)
90 printf(" %s: %s\n", table[i].name, table[i].description);
93 int find_coll_description(s_mpi_coll_description_t * table,
97 char *name_list = NULL;
99 for (i = 0; table[i].name; i++)
100 if (!strcmp(name, table[i].name)) {
103 name_list = strdup(table[0].name);
104 for (i = 1; table[i].name; i++) {
106 xbt_realloc(name_list,
107 strlen(name_list) + strlen(table[i].name) + 3);
108 strcat(name_list, ", ");
109 strcat(name_list, table[i].name);
111 xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
117 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
118 "Logging specific to SMPI (coll)");
129 typedef struct s_proc_tree *proc_tree_t;
134 static proc_tree_t alloc_tree(int arity)
139 tree = xbt_new(struct s_proc_tree, 1);
140 tree->PROCTREE_A = arity;
142 tree->numChildren = 0;
143 tree->child = xbt_new(int, arity);
144 for (i = 0; i < arity; i++) {
155 static void free_tree(proc_tree_t tree)
157 xbt_free(tree->child);
162 * Build the tree depending on a process rank (index) and the group size (extent)
163 * @param root the rank of the tree root
164 * @param rank the rank of the calling process
165 * @param size the total number of processes
167 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
169 int index = (rank - root + size) % size;
170 int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
174 (*tree)->root = root;
176 for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
177 (*tree)->child[i] = (firstChildIdx + i + root) % size;
178 (*tree)->numChildren++;
184 (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
191 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
192 MPI_Comm comm, proc_tree_t tree)
194 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
196 MPI_Request *requests;
198 rank = smpi_comm_rank(comm);
199 /* wait for data from my parent in the tree */
201 XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
202 rank, tree->parent, system_tag + rank);
203 smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
204 comm, MPI_STATUS_IGNORE);
206 requests = xbt_new(MPI_Request, tree->numChildren);
207 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
209 /* iniates sends to ranks lower in the tree */
210 for (i = 0; i < tree->numChildren; i++) {
211 if (tree->child[i] == -1) {
212 requests[i] = MPI_REQUEST_NULL;
214 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
215 system_tag + tree->child[i]);
217 smpi_isend_init(buf, count, datatype, tree->child[i],
218 system_tag + tree->child[i], comm);
221 smpi_mpi_startall(tree->numChildren, requests);
222 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
229 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
230 MPI_Comm comm, proc_tree_t tree)
232 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
234 MPI_Request *requests;
236 rank = smpi_comm_rank(comm);
237 // everyone sends to its parent, except root.
239 XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
240 rank, tree->parent, system_tag + rank);
241 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
244 //every one receives as many messages as it has children
245 requests = xbt_new(MPI_Request, tree->numChildren);
246 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
248 for (i = 0; i < tree->numChildren; i++) {
249 if (tree->child[i] == -1) {
250 requests[i] = MPI_REQUEST_NULL;
252 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
253 system_tag + tree->child[i]);
255 smpi_irecv_init(buf, count, datatype, tree->child[i],
256 system_tag + tree->child[i], comm);
259 smpi_mpi_startall(tree->numChildren, requests);
260 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
265 * bcast with a binary, ternary, or whatever tree ..
267 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
268 MPI_Comm comm, int arity)
270 proc_tree_t tree = alloc_tree(arity);
273 rank = smpi_comm_rank(comm);
274 size = smpi_comm_size(comm);
275 build_tree(root, rank, size, &tree);
276 tree_bcast(buf, count, datatype, comm, tree);
281 * barrier with a binary, ternary, or whatever tree ..
283 void nary_tree_barrier(MPI_Comm comm, int arity)
285 proc_tree_t tree = alloc_tree(arity);
289 rank = smpi_comm_rank(comm);
290 size = smpi_comm_size(comm);
291 build_tree(0, rank, size, &tree);
292 tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
293 tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
297 int smpi_coll_tuned_alltoall_ompi(void *sendbuf, int sendcount,
298 MPI_Datatype sendtype, void *recvbuf,
299 int recvcount, MPI_Datatype recvtype,
303 size = smpi_comm_size(comm);
304 sendsize = smpi_datatype_size(sendtype) * sendcount;
305 if (sendsize < 200 && size > 12) {
307 smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
308 recvbuf, recvcount, recvtype,
310 } else if (sendsize < 3000) {
312 smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
314 recvcount, recvtype, comm);
317 smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, sendtype,
318 recvbuf, recvcount, recvtype,
326 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
327 * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
330 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
331 MPI_Datatype sendtype, void *recvbuf,
332 int recvcount, MPI_Datatype recvtype,
335 int system_tag = 777;
336 int i, rank, size, err, count;
338 MPI_Aint sendext = 0;
339 MPI_Aint recvext = 0;
340 MPI_Request *requests;
342 // FIXME: check implementation
343 rank = smpi_comm_rank(comm);
344 size = smpi_comm_size(comm);
345 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
346 err = smpi_datatype_extent(sendtype, &lb, &sendext);
347 err = smpi_datatype_extent(recvtype, &lb, &recvext);
348 /* Local copy from self */
350 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
352 (char *)recvbuf + rank * recvcount * recvext,
353 recvcount, recvtype);
354 if (err == MPI_SUCCESS && size > 1) {
355 /* Initiate all send/recv to/from others. */
356 requests = xbt_new(MPI_Request, 2 * (size - 1));
358 /* Create all receives that will be posted first */
359 for (i = 0; i < size; ++i) {
361 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
366 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
367 recvtype, i, system_tag, comm);
370 /* Now create all sends */
371 for (i = 0; i < size; ++i) {
373 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
378 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
379 sendtype, i, system_tag, comm);
382 /* Wait for them all. */
383 smpi_mpi_startall(count, requests);
384 XBT_DEBUG("<%d> wait for %d requests", rank, count);
385 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
392 * Alltoall basic_linear (STARMPI:alltoall-simple)
394 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
395 MPI_Datatype sendtype,
396 void *recvbuf, int recvcount,
397 MPI_Datatype recvtype,
400 int system_tag = 888;
401 int i, rank, size, err, count;
402 MPI_Aint lb = 0, sendext = 0, recvext = 0;
403 MPI_Request *requests;
406 rank = smpi_comm_rank(comm);
407 size = smpi_comm_size(comm);
408 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
409 err = smpi_datatype_extent(sendtype, &lb, &sendext);
410 err = smpi_datatype_extent(recvtype, &lb, &recvext);
411 /* simple optimization */
412 err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
414 (char *)recvbuf + rank * recvcount * recvext,
415 recvcount, recvtype);
416 if (err == MPI_SUCCESS && size > 1) {
417 /* Initiate all send/recv to/from others. */
418 requests = xbt_new(MPI_Request, 2 * (size - 1));
419 /* Post all receives first -- a simple optimization */
421 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
423 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
424 recvtype, i, system_tag, comm);
427 /* Now post all sends in reverse order
428 * - We would like to minimize the search time through message queue
429 * when messages actually arrive in the order in which they were posted.
430 * TODO: check the previous assertion
432 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
434 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
435 sendtype, i, system_tag, comm);
438 /* Wait for them all. */
439 smpi_mpi_startall(count, requests);
440 XBT_DEBUG("<%d> wait for %d requests", rank, count);
441 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
450 * this algorithm performs size steps (1<=s<=size) and
451 * at each step s, a process p sends iand receive to.from a unique distinct remote process
452 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
453 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
455 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
457 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
458 MPI_Datatype sendtype, void *recvbuf,
459 int recvcount, MPI_Datatype recvtype,
462 int system_tag = 999;
463 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
465 rank = smpi_comm_rank(comm);
466 size = smpi_comm_size(comm);
467 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
468 sendsize = smpi_datatype_size(sendtype);
469 recvsize = smpi_datatype_size(recvtype);
470 /* Perform pairwise exchange - starting from 1 so the local copy is last */
471 for (step = 1; step < size + 1; step++) {
472 /* who do we talk to in this step? */
473 sendto = (rank + step) % size;
474 recvfrom = (rank + size - step) % size;
475 /* send and receive */
476 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
477 sendcount, sendtype, sendto, system_tag,
478 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
479 recvcount, recvtype, recvfrom, system_tag, comm,
485 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
486 int *senddisps, MPI_Datatype sendtype,
487 void *recvbuf, int *recvcounts,
488 int *recvdisps, MPI_Datatype recvtype,
491 int system_tag = 889;
492 int i, rank, size, err, count;
493 MPI_Aint lb = 0, sendext = 0, recvext = 0;
494 MPI_Request *requests;
497 rank = smpi_comm_rank(comm);
498 size = smpi_comm_size(comm);
499 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
500 err = smpi_datatype_extent(sendtype, &lb, &sendext);
501 err = smpi_datatype_extent(recvtype, &lb, &recvext);
502 /* Local copy from self */
504 smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext,
505 sendcounts[rank], sendtype,
506 (char *)recvbuf + recvdisps[rank] * recvext,
507 recvcounts[rank], recvtype);
508 if (err == MPI_SUCCESS && size > 1) {
509 /* Initiate all send/recv to/from others. */
510 requests = xbt_new(MPI_Request, 2 * (size - 1));
512 /* Create all receives that will be posted first */
513 for (i = 0; i < size; ++i) {
514 if (i == rank || recvcounts[i] == 0) {
516 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
517 rank, i, recvcounts[i]);
521 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
522 recvcounts[i], recvtype, i, system_tag, comm);
525 /* Now create all sends */
526 for (i = 0; i < size; ++i) {
527 if (i == rank || sendcounts[i] == 0) {
529 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
530 rank, i, sendcounts[i]);
534 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
535 sendcounts[i], sendtype, i, system_tag, comm);
538 /* Wait for them all. */
539 smpi_mpi_startall(count, requests);
540 XBT_DEBUG("<%d> wait for %d requests", rank, count);
541 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);