Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
seriously, me
[simgrid.git] / src / smpi / smpi_coll.cpp
1 /* smpi_coll.c -- various optimized routing for collectives                   */
2
3 /* Copyright (c) 2009-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <assert.h>
12
13 #include "private.h"
14 #include "colls/colls.h"
15 #include "simgrid/sg_config.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
18                                 "Logging specific to SMPI (coll)");
19
20 s_mpi_coll_description_t mpi_coll_gather_description[] = {
21   {"default",
22    "gather default collective",
23    (void*)smpi_mpi_gather},
24 COLL_GATHERS(COLL_DESCRIPTION, COLL_COMMA),
25   {NULL, NULL, NULL}      /* this array must be NULL terminated */
26 };
27
28
29 s_mpi_coll_description_t mpi_coll_allgather_description[] = {
30   {"default",
31    "allgather default collective",
32    (void*)smpi_mpi_allgather},
33 COLL_ALLGATHERS(COLL_DESCRIPTION, COLL_COMMA),
34   {NULL, NULL, NULL}      /* this array must be NULL terminated */
35 };
36
37 s_mpi_coll_description_t mpi_coll_allgatherv_description[] = {
38   {"default",
39    "allgatherv default collective",
40    (void*)smpi_mpi_allgatherv},
41 COLL_ALLGATHERVS(COLL_DESCRIPTION, COLL_COMMA),
42   {NULL, NULL, NULL}      /* this array must be NULL terminated */
43 };
44
45 s_mpi_coll_description_t mpi_coll_allreduce_description[] = {
46   {"default",
47    "allreduce default collective",
48    (void*)smpi_mpi_allreduce},
49 COLL_ALLREDUCES(COLL_DESCRIPTION, COLL_COMMA),
50   {NULL, NULL, NULL}      /* this array must be NULL terminated */
51 };
52
53 s_mpi_coll_description_t mpi_coll_reduce_scatter_description[] = {
54   {"default",
55    "reduce_scatter default collective",
56    (void*)smpi_mpi_reduce_scatter},
57 COLL_REDUCE_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
58   {NULL, NULL, NULL}      /* this array must be NULL terminated */
59 };
60
61 s_mpi_coll_description_t mpi_coll_scatter_description[] = {
62   {"default",
63    "scatter default collective",
64    (void*)smpi_mpi_scatter},
65 COLL_SCATTERS(COLL_DESCRIPTION, COLL_COMMA),
66   {NULL, NULL, NULL}      /* this array must be NULL terminated */
67 };
68
69 s_mpi_coll_description_t mpi_coll_barrier_description[] = {
70   {"default",
71    "barrier default collective",
72    (void*)smpi_mpi_barrier},
73 COLL_BARRIERS(COLL_DESCRIPTION, COLL_COMMA),
74   {NULL, NULL, NULL}      /* this array must be NULL terminated */
75 };
76 s_mpi_coll_description_t mpi_coll_alltoall_description[] = {
77   {"default",
78    "Ompi alltoall default collective",
79    (void*)smpi_coll_tuned_alltoall_ompi2},
80 COLL_ALLTOALLS(COLL_DESCRIPTION, COLL_COMMA),
81   {"bruck",
82    "Alltoall Bruck (SG) collective",
83    (void*)smpi_coll_tuned_alltoall_bruck},
84   {"basic_linear",
85    "Alltoall basic linear (SG) collective",
86    (void*)smpi_coll_tuned_alltoall_basic_linear},
87   {NULL, NULL, NULL}      /* this array must be NULL terminated */
88 };
89
90 s_mpi_coll_description_t mpi_coll_alltoallv_description[] = {
91   {"default",
92    "Ompi alltoallv default collective",
93    (void*)smpi_coll_basic_alltoallv},
94 COLL_ALLTOALLVS(COLL_DESCRIPTION, COLL_COMMA),
95   {NULL, NULL, NULL}      /* this array must be NULL terminated */
96 };
97
98 s_mpi_coll_description_t mpi_coll_bcast_description[] = {
99   {"default",
100    "bcast default collective ",
101    (void*)smpi_mpi_bcast},
102 COLL_BCASTS(COLL_DESCRIPTION, COLL_COMMA),
103   {NULL, NULL, NULL}      /* this array must be NULL terminated */
104 };
105
106 s_mpi_coll_description_t mpi_coll_reduce_description[] = {
107   {"default",
108    "reduce default collective",
109    (void*)smpi_mpi_reduce},
110 COLL_REDUCES(COLL_DESCRIPTION, COLL_COMMA),
111   {NULL, NULL, NULL}      /* this array must be NULL terminated */
112 };
113
114
115
116 /** Displays the long description of all registered models, and quit */
117 void coll_help(const char *category, s_mpi_coll_description_t * table)
118 {
119   int i;
120   printf("Long description of the %s models accepted by this simulator:\n",
121          category);
122   for (i = 0; table[i].name; i++)
123     printf("  %s: %s\n", table[i].name, table[i].description);
124 }
125
126 int find_coll_description(s_mpi_coll_description_t * table,
127                            char *name, const char *desc)
128 {
129   int i;
130   char *name_list = NULL;
131   int selector_on=0;
132   if(name==NULL){//no argument provided, use active selector's algorithm
133     name=(char*)sg_cfg_get_string("smpi/coll_selector");
134     selector_on=1;
135   }
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);
140       return i;
141     }
142
143   if(selector_on){
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)) {
148         return i;
149     }
150   }
151   if (!table[0].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++) {
155     name_list = static_cast<char*>(xbt_realloc(name_list,
156                     strlen(name_list) + strlen(table[i].name) + 3));
157     strcat(name_list, ", ");
158     strcat(name_list, table[i].name);
159   }
160   xbt_die("Collective '%s' is invalid! Valid collectives are: %s.", name, name_list);
161   return -1;
162 }
163
164 int (*mpi_coll_gather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, int root, MPI_Comm);
165 int (*mpi_coll_allgather_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
166 int (*mpi_coll_allgatherv_fun)(void *, int, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
167 int (*mpi_coll_allreduce_fun)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
168 int (*mpi_coll_alltoall_fun)(void *, int, MPI_Datatype, void*, int, MPI_Datatype, MPI_Comm);
169 int (*mpi_coll_alltoallv_fun)(void *, int*, int*, MPI_Datatype, void*, int*, int*, MPI_Datatype, MPI_Comm);
170 int (*mpi_coll_bcast_fun)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com);
171 int (*mpi_coll_reduce_fun)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
172 int (*mpi_coll_reduce_scatter_fun)(void *sbuf, void *rbuf, int *rcounts,MPI_Datatype dtype,MPI_Op  op,MPI_Comm  comm);
173 int (*mpi_coll_scatter_fun)(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,int root, MPI_Comm comm);
174 int (*mpi_coll_barrier_fun)(MPI_Comm comm);
175
176
177 int smpi_coll_tuned_alltoall_ompi2(void *sendbuf, int sendcount,
178                                    MPI_Datatype sendtype, void *recvbuf,
179                                    int recvcount, MPI_Datatype recvtype,
180                                    MPI_Comm comm)
181 {
182   int size, sendsize;  
183   size = smpi_comm_size(comm);  
184   sendsize = smpi_datatype_size(sendtype) * sendcount;  
185   if (sendsize < 200 && size > 12) {
186     return
187         smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
188                                        recvbuf, recvcount, recvtype,
189                                        comm);
190   } else if (sendsize < 3000) {
191     return
192         smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
193                                               sendtype, recvbuf,
194                                               recvcount, recvtype, comm);
195   } else {
196     return
197         smpi_coll_tuned_alltoall_ring(sendbuf, sendcount, sendtype,
198                                       recvbuf, recvcount, recvtype,
199                                       comm);
200   }
201 }
202
203 /**
204  * Alltoall Bruck
205  *
206  * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
207  * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
208  * less...
209  **/
210 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
211                                    MPI_Datatype sendtype, void *recvbuf,
212                                    int recvcount, MPI_Datatype recvtype,
213                                    MPI_Comm comm)
214 {
215   int system_tag = 777;
216   int i, rank, size, err, count;
217   MPI_Aint lb;
218   MPI_Aint sendext = 0;
219   MPI_Aint recvext = 0;
220   MPI_Request *requests;
221
222   // FIXME: check implementation
223   rank = smpi_comm_rank(comm);
224   size = smpi_comm_size(comm);
225   XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
226   smpi_datatype_extent(sendtype, &lb, &sendext);
227   smpi_datatype_extent(recvtype, &lb, &recvext);
228   /* Local copy from self */
229   err =
230       smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
231                          sendcount, sendtype, 
232                          (char *)recvbuf + rank * recvcount * recvext,
233                          recvcount, recvtype);
234   if (err == MPI_SUCCESS && size > 1) {
235     /* Initiate all send/recv to/from others. */
236     requests = xbt_new(MPI_Request, 2 * (size - 1));
237     count = 0;
238     /* Create all receives that will be posted first */
239     for (i = 0; i < size; ++i) {
240       if (i == rank) {
241         XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
242                rank, i, recvcount);
243         continue;
244       }
245       requests[count] =
246           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
247                           recvtype, i, system_tag, comm);
248       count++;
249     }
250     /* Now create all sends  */
251     for (i = 0; i < size; ++i) {
252       if (i == rank) {
253         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
254                rank, i, sendcount);
255         continue;
256       }
257       requests[count] =
258           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
259                           sendtype, i, system_tag, comm);
260       count++;
261     }
262     /* Wait for them all. */
263     smpi_mpi_startall(count, requests);
264     XBT_DEBUG("<%d> wait for %d requests", rank, count);
265     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
266     for(i = 0; i < count; i++) {
267       if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);
268     }
269     xbt_free(requests);
270   }
271   return MPI_SUCCESS;
272 }
273
274 /**
275  * Alltoall basic_linear (STARMPI:alltoall-simple)
276  **/
277 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
278                                           MPI_Datatype sendtype,
279                                           void *recvbuf, int recvcount,
280                                           MPI_Datatype recvtype,
281                                           MPI_Comm comm)
282 {
283   int system_tag = 888;
284   int i, rank, size, err, count;
285   MPI_Aint lb = 0, sendext = 0, recvext = 0;
286   MPI_Request *requests;
287
288   /* Initialize. */
289   rank = smpi_comm_rank(comm);
290   size = smpi_comm_size(comm);
291   XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
292   smpi_datatype_extent(sendtype, &lb, &sendext);
293   smpi_datatype_extent(recvtype, &lb, &recvext);
294   /* simple optimization */
295   err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
296                            sendcount, sendtype, 
297                            (char *)recvbuf + rank * recvcount * recvext, 
298                            recvcount, recvtype);
299   if (err == MPI_SUCCESS && size > 1) {
300     /* Initiate all send/recv to/from others. */
301     requests = xbt_new(MPI_Request, 2 * (size - 1));
302     /* Post all receives first -- a simple optimization */
303     count = 0;
304     for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
305       requests[count] =
306           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount, 
307                           recvtype, i, system_tag, comm);
308       count++;
309     }
310     /* Now post all sends in reverse order
311      *   - We would like to minimize the search time through message queue
312      *     when messages actually arrive in the order in which they were posted.
313      * TODO: check the previous assertion
314      */
315     for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
316       requests[count] =
317           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
318                           sendtype, i, system_tag, comm);
319       count++;
320     }
321     /* Wait for them all. */
322     smpi_mpi_startall(count, requests);
323     XBT_DEBUG("<%d> wait for %d requests", rank, count);
324     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
325     for(i = 0; i < count; i++) {
326       if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);
327     }
328     xbt_free(requests);
329   }
330   return err;
331 }
332
333 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
334                               int *senddisps, MPI_Datatype sendtype,
335                               void *recvbuf, int *recvcounts,
336                               int *recvdisps, MPI_Datatype recvtype,
337                               MPI_Comm comm)
338 {
339   int system_tag = 889;
340   int i, rank, size, err, count;
341   MPI_Aint lb = 0, sendext = 0, recvext = 0;
342   MPI_Request *requests;
343
344   /* Initialize. */
345   rank = smpi_comm_rank(comm);
346   size = smpi_comm_size(comm);
347   XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
348   smpi_datatype_extent(sendtype, &lb, &sendext);
349   smpi_datatype_extent(recvtype, &lb, &recvext);
350   /* Local copy from self */
351   err =
352       smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext, 
353                          sendcounts[rank], sendtype,
354                          (char *)recvbuf + recvdisps[rank] * recvext, 
355                          recvcounts[rank], recvtype);
356   if (err == MPI_SUCCESS && size > 1) {
357     /* Initiate all send/recv to/from others. */
358     requests = xbt_new(MPI_Request, 2 * (size - 1));
359     count = 0;
360     /* Create all receives that will be posted first */
361     for (i = 0; i < size; ++i) {
362       if (i == rank || recvcounts[i] == 0) {
363         XBT_DEBUG
364             ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
365              rank, i, recvcounts[i]);
366         continue;
367       }
368       requests[count] =
369           smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext, 
370                           recvcounts[i], recvtype, i, system_tag, comm);
371       count++;
372     }
373     /* Now create all sends  */
374     for (i = 0; i < size; ++i) {
375       if (i == rank || sendcounts[i] == 0) {
376         XBT_DEBUG
377             ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
378              rank, i, sendcounts[i]);
379         continue;
380       }
381       requests[count] =
382           smpi_isend_init((char *)sendbuf + senddisps[i] * sendext, 
383                           sendcounts[i], sendtype, i, system_tag, comm);
384       count++;
385     }
386     /* Wait for them all. */
387     smpi_mpi_startall(count, requests);
388     XBT_DEBUG("<%d> wait for %d requests", rank, count);
389     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
390     for(i = 0; i < count; i++) {
391       if(requests[i]!=MPI_REQUEST_NULL) smpi_mpi_request_free(&requests[i]);
392     }
393     xbt_free(requests);
394   }
395   return err;
396 }