Logo AND Algorithmique Numérique Distribuée

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