Logo AND Algorithmique Numérique Distribuée

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