1 /* smpi_coll.c -- various optimized routing for collectives */
3 /* Copyright (c) 2009-2015. 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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
18 "Logging specific to SMPI (coll)");
20 s_mpi_coll_description_t mpi_coll_gather_description[] = {
22 "gather default collective",
24 COLL_GATHERS(COLL_DESCRIPTION, COLL_COMMA),
25 {NULL, NULL, NULL} /* this array must be NULL terminated */
29 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
31 "allgather default collective",
33 COLL_ALLGATHERS(COLL_DESCRIPTION, COLL_COMMA),
34 {NULL, NULL, NULL} /* this array must be NULL terminated */
37 s_mpi_coll_description_t mpi_coll_allgatherv_description[] = {
39 "allgatherv default collective",
41 COLL_ALLGATHERVS(COLL_DESCRIPTION, COLL_COMMA),
42 {NULL, NULL, NULL} /* this array must be NULL terminated */
45 s_mpi_coll_description_t mpi_coll_allreduce_description[] = {
47 "allreduce default collective",
49 COLL_ALLREDUCES(COLL_DESCRIPTION, COLL_COMMA),
50 {NULL, NULL, NULL} /* this array must be NULL terminated */
53 s_mpi_coll_description_t mpi_coll_reduce_scatter_description[] = {
55 "reduce_scatter default collective",
56 smpi_mpi_reduce_scatter},
57 COLL_REDUCE_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
58 {NULL, NULL, NULL} /* this array must be NULL terminated */
61 s_mpi_coll_description_t mpi_coll_scatter_description[] = {
63 "scatter default collective",
65 COLL_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
66 {NULL, NULL, NULL} /* this array must be NULL terminated */
69 s_mpi_coll_description_t mpi_coll_barrier_description[] = {
71 "barrier default collective",
73 COLL_BARRIERS(COLL_DESCRIPTION, COLL_COMMA),
74 {NULL, NULL, NULL} /* this array must be NULL terminated */
76 s_mpi_coll_description_t mpi_coll_alltoall_description[] = {
78 "Ompi alltoall default collective",
79 smpi_coll_tuned_alltoall_ompi2},
80 COLL_ALLTOALLS(COLL_DESCRIPTION, COLL_COMMA),
82 "Alltoall Bruck (SG) collective",
83 smpi_coll_tuned_alltoall_bruck},
85 "Alltoall basic linear (SG) collective",
86 smpi_coll_tuned_alltoall_basic_linear},
87 {NULL, NULL, NULL} /* this array must be NULL terminated */
90 s_mpi_coll_description_t mpi_coll_alltoallv_description[] = {
92 "Ompi alltoallv default collective",
93 smpi_coll_basic_alltoallv},
94 COLL_ALLTOALLVS(COLL_DESCRIPTION, COLL_COMMA),
95 {NULL, NULL, NULL} /* this array must be NULL terminated */
98 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
100 "bcast default collective ",
102 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
103 {NULL, NULL, NULL} /* this array must be NULL terminated */
106 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
108 "reduce default collective",
110 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
111 {NULL, NULL, NULL} /* this array must be NULL terminated */
116 /** Displays the long description of all registered models, and quit */
117 void coll_help(const char *category, s_mpi_coll_description_t * table)
120 printf("Long description of the %s models accepted by this simulator:\n",
122 for (i = 0; table[i].name; i++)
123 printf(" %s: %s\n", table[i].name, table[i].description);
126 int find_coll_description(s_mpi_coll_description_t * table,
127 char *name, const char *desc)
130 char *name_list = NULL;
132 if(name==NULL){//no argument provided, use active selector's algorithm
133 name=(char*)sg_cfg_get_string("smpi/coll_selector");
136 for (i = 0; table[i].name; i++)
137 if (!strcmp(name, table[i].name)) {
138 if (strcmp(table[i].name,"default"))
139 XBT_INFO("Switch to algorithm %s for collective %s",table[i].name,desc);
144 // collective seems not handled by the active selector, try with default one
145 name=(char*)"default";
146 for (i = 0; table[i].name; i++)
147 if (!strcmp(name, table[i].name)) {
152 xbt_die("No collective is valid for '%s'! This is a bug.",name);
153 name_list = xbt_strdup(table[0].name);
154 for (i = 1; table[i].name; i++) {
156 xbt_realloc(name_list,
157 strlen(name_list) + strlen(table[i].name) + 3);
158 strcat(name_list, ", ");
159 strcat(name_list, table[i].name);
161 xbt_die("Collective '%s' is invalid! Valid collectives are: %s.", name, name_list);
165 int (*mpi_coll_gather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, int root, MPI_Comm);
166 int (*mpi_coll_allgather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
167 int (*mpi_coll_allgatherv_fun)(void *, int, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
168 int (*mpi_coll_allreduce_fun)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
169 int (*mpi_coll_alltoall_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
170 int (*mpi_coll_alltoallv_fun)(void *, int*, int*, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
171 int (*mpi_coll_bcast_fun)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com);
172 int (*mpi_coll_reduce_fun)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
173 int (*mpi_coll_reduce_scatter_fun)(void *sbuf, void *rbuf, int *rcounts,MPI_Datatype dtype,MPI_Op op,MPI_Comm comm);
174 int (*mpi_coll_scatter_fun)(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,int root, MPI_Comm comm);
175 int (*mpi_coll_barrier_fun)(MPI_Comm comm);
178 int smpi_coll_tuned_alltoall_ompi2(void *sendbuf, int sendcount,
179 MPI_Datatype sendtype, void *recvbuf,
180 int recvcount, MPI_Datatype recvtype,
184 size = smpi_comm_size(comm);
185 sendsize = smpi_datatype_size(sendtype) * sendcount;
186 if (sendsize < 200 && size > 12) {
188 smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
189 recvbuf, recvcount, recvtype,
191 } else if (sendsize < 3000) {
193 smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
195 recvcount, recvtype, comm);
198 smpi_coll_tuned_alltoall_ring(sendbuf, sendcount, sendtype,
199 recvbuf, recvcount, recvtype,
207 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
208 * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
211 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
212 MPI_Datatype sendtype, void *recvbuf,
213 int recvcount, MPI_Datatype recvtype,
216 int system_tag = 777;
217 int i, rank, size, err, count;
219 MPI_Aint sendext = 0;
220 MPI_Aint recvext = 0;
221 MPI_Request *requests;
223 // FIXME: check implementation
224 rank = smpi_comm_rank(comm);
225 size = smpi_comm_size(comm);
226 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
227 smpi_datatype_extent(sendtype, &lb, &sendext);
228 smpi_datatype_extent(recvtype, &lb, &recvext);
229 /* Local copy from self */
231 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
233 (char *)recvbuf + rank * recvcount * recvext,
234 recvcount, recvtype);
235 if (err == MPI_SUCCESS && size > 1) {
236 /* Initiate all send/recv to/from others. */
237 requests = xbt_new(MPI_Request, 2 * (size - 1));
239 /* Create all receives that will be posted first */
240 for (i = 0; i < size; ++i) {
242 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
247 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
248 recvtype, i, system_tag, comm);
251 /* Now create all sends */
252 for (i = 0; i < size; ++i) {
254 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
259 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
260 sendtype, i, system_tag, comm);
263 /* Wait for them all. */
264 smpi_mpi_startall(count, requests);
265 XBT_DEBUG("<%d> wait for %d requests", rank, count);
266 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
267 for(i = 0; i < count; i++) {
268 if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);
276 * Alltoall basic_linear (STARMPI:alltoall-simple)
278 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
279 MPI_Datatype sendtype,
280 void *recvbuf, int recvcount,
281 MPI_Datatype recvtype,
284 int system_tag = 888;
285 int i, rank, size, err, count;
286 MPI_Aint lb = 0, sendext = 0, recvext = 0;
287 MPI_Request *requests;
290 rank = smpi_comm_rank(comm);
291 size = smpi_comm_size(comm);
292 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
293 smpi_datatype_extent(sendtype, &lb, &sendext);
294 smpi_datatype_extent(recvtype, &lb, &recvext);
295 /* simple optimization */
296 err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
298 (char *)recvbuf + rank * recvcount * recvext,
299 recvcount, recvtype);
300 if (err == MPI_SUCCESS && size > 1) {
301 /* Initiate all send/recv to/from others. */
302 requests = xbt_new(MPI_Request, 2 * (size - 1));
303 /* Post all receives first -- a simple optimization */
305 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
307 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
308 recvtype, i, system_tag, comm);
311 /* Now post all sends in reverse order
312 * - We would like to minimize the search time through message queue
313 * when messages actually arrive in the order in which they were posted.
314 * TODO: check the previous assertion
316 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
318 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
319 sendtype, i, system_tag, comm);
322 /* Wait for them all. */
323 smpi_mpi_startall(count, requests);
324 XBT_DEBUG("<%d> wait for %d requests", rank, count);
325 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
326 for(i = 0; i < count; i++) {
327 if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);
334 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
335 int *senddisps, MPI_Datatype sendtype,
336 void *recvbuf, int *recvcounts,
337 int *recvdisps, MPI_Datatype recvtype,
340 int system_tag = 889;
341 int i, rank, size, err, count;
342 MPI_Aint lb = 0, sendext = 0, recvext = 0;
343 MPI_Request *requests;
346 rank = smpi_comm_rank(comm);
347 size = smpi_comm_size(comm);
348 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
349 smpi_datatype_extent(sendtype, &lb, &sendext);
350 smpi_datatype_extent(recvtype, &lb, &recvext);
351 /* Local copy from self */
353 smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext,
354 sendcounts[rank], sendtype,
355 (char *)recvbuf + recvdisps[rank] * recvext,
356 recvcounts[rank], recvtype);
357 if (err == MPI_SUCCESS && size > 1) {
358 /* Initiate all send/recv to/from others. */
359 requests = xbt_new(MPI_Request, 2 * (size - 1));
361 /* Create all receives that will be posted first */
362 for (i = 0; i < size; ++i) {
363 if (i == rank || recvcounts[i] == 0) {
365 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
366 rank, i, recvcounts[i]);
370 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
371 recvcounts[i], recvtype, i, system_tag, comm);
374 /* Now create all sends */
375 for (i = 0; i < size; ++i) {
376 if (i == rank || sendcounts[i] == 0) {
378 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
379 rank, i, sendcounts[i]);
383 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
384 sendcounts[i], sendtype, i, system_tag, comm);
387 /* Wait for them all. */
388 smpi_mpi_startall(count, requests);
389 XBT_DEBUG("<%d> wait for %d requests", rank, count);
390 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
391 for(i = 0; i < count; i++) {
392 if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);