Logo AND Algorithmique Numérique Distribuée

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