Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7b6899e25edb7dd37b96c6af3bc665787f3ea03a
[simgrid.git] / src / smpi / colls / smpi_default_selector.cpp
1 /* selector with default/naive Simgrid algorithms. These should not be trusted for performance evaluations */
2
3 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "colls_private.hpp"
9 #include "src/smpi/include/smpi_actor.hpp"
10
11 namespace simgrid{
12 namespace smpi{
13
14 int Coll_bcast_default::bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
15 {
16   return Coll_bcast_binomial_tree::bcast(buf, count, datatype, root, comm);
17 }
18
19 int Coll_barrier_default::barrier(MPI_Comm comm)
20 {
21   return Coll_barrier_ompi_basic_linear::barrier(comm);
22 }
23
24
25 int Coll_gather_default::gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
26                      void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
27 {
28   const int system_tag = COLL_TAG_GATHER;
29   MPI_Aint lb = 0;
30   MPI_Aint recvext = 0;
31
32   int rank = comm->rank();
33   int size = comm->size();
34   if(rank != root) {
35     // Send buffer to root
36     Request::send(sendbuf, sendcount, sendtype, root, system_tag, comm);
37   } else {
38     recvtype->extent(&lb, &recvext);
39     // Local copy from root
40     Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext,
41                        recvcount, recvtype);
42     // Receive buffers from senders
43     MPI_Request *requests = xbt_new(MPI_Request, size - 1);
44     int index = 0;
45     for (int src = 0; src < size; src++) {
46       if(src != root) {
47         requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
48                                           src, system_tag, comm);
49         index++;
50       }
51     }
52     // Wait for completion of irecv's.
53     Request::startall(size - 1, requests);
54     Request::waitall(size - 1, requests, MPI_STATUS_IGNORE);
55     for (int src = 0; src < size-1; src++) {
56       Request::unref(&requests[src]);
57     }
58     xbt_free(requests);
59   }
60   return MPI_SUCCESS;
61 }
62
63 int Coll_reduce_scatter_default::reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
64                              MPI_Comm comm)
65 {
66   int rank = comm->rank();
67
68   /* arbitrarily choose root as rank 0 */
69   int size = comm->size();
70   int count = 0;
71   int *displs = xbt_new(int, size);
72   for (int i = 0; i < size; i++) {
73     displs[i] = count;
74     count += recvcounts[i];
75   }
76   void *tmpbuf = static_cast<void*>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
77
78   int ret = Coll_reduce_default::reduce(sendbuf, tmpbuf, count, datatype, op, 0, comm);
79   if(ret==MPI_SUCCESS)
80     ret = Colls::scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
81   xbt_free(displs);
82   smpi_free_tmp_buffer(tmpbuf);
83   return ret;
84 }
85
86
87 int Coll_allgather_default::allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
88                         void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
89 {
90   const int system_tag = COLL_TAG_ALLGATHER;
91   MPI_Aint lb = 0;
92   MPI_Aint recvext = 0;
93   MPI_Request *requests;
94
95   int rank = comm->rank();
96   int size = comm->size();
97   // FIXME: check for errors
98   recvtype->extent(&lb, &recvext);
99   // Local copy from self
100   Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount,
101                      recvtype);
102   // Send/Recv buffers to/from others;
103   requests = xbt_new(MPI_Request, 2 * (size - 1));
104   int index = 0;
105   for (int other = 0; other < size; other++) {
106     if(other != rank) {
107       requests[index] = Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag,comm);
108       index++;
109       requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + other * recvcount * recvext, recvcount, recvtype,
110                                         other, system_tag, comm);
111       index++;
112     }
113   }
114   // Wait for completion of all comms.
115   Request::startall(2 * (size - 1), requests);
116   Request::waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
117   for (int other = 0; other < 2*(size-1); other++) {
118     Request::unref(&requests[other]);
119   }
120   xbt_free(requests);
121   return MPI_SUCCESS;
122 }
123
124 int Coll_allgatherv_default::allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
125                          int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
126 {
127   const int system_tag = COLL_TAG_ALLGATHERV;
128   MPI_Aint lb = 0;
129   MPI_Aint recvext = 0;
130
131   int rank = comm->rank();
132   int size = comm->size();
133   recvtype->extent(&lb, &recvext);
134   // Local copy from self
135   Datatype::copy(sendbuf, sendcount, sendtype,
136                      static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
137   // Send buffers to others;
138   MPI_Request *requests = xbt_new(MPI_Request, 2 * (size - 1));
139   int index = 0;
140   for (int other = 0; other < size; other++) {
141     if(other != rank) {
142       requests[index] =
143         Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
144       index++;
145       requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
146                           recvtype, other, system_tag, comm);
147       index++;
148     }
149   }
150   // Wait for completion of all comms.
151   Request::startall(2 * (size - 1), requests);
152   Request::waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
153   for (int other = 0; other < 2*(size-1); other++) {
154     Request::unref(&requests[other]);
155   }
156   xbt_free(requests);
157   return MPI_SUCCESS;
158 }
159
160 int Coll_scatter_default::scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
161                       void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
162 {
163   const int system_tag = COLL_TAG_SCATTER;
164   MPI_Aint lb = 0;
165   MPI_Aint sendext = 0;
166   MPI_Request *requests;
167
168   int rank = comm->rank();
169   int size = comm->size();
170   if(rank != root) {
171     // Recv buffer from root
172     Request::recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
173   } else {
174     sendtype->extent(&lb, &sendext);
175     // Local copy from root
176     if(recvbuf!=MPI_IN_PLACE){
177         Datatype::copy(static_cast<char *>(sendbuf) + root * sendcount * sendext,
178                            sendcount, sendtype, recvbuf, recvcount, recvtype);
179     }
180     // Send buffers to receivers
181     requests = xbt_new(MPI_Request, size - 1);
182     int index = 0;
183     for(int dst = 0; dst < size; dst++) {
184       if(dst != root) {
185         requests[index] = Request::isend_init(static_cast<char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype,
186                                           dst, system_tag, comm);
187         index++;
188       }
189     }
190     // Wait for completion of isend's.
191     Request::startall(size - 1, requests);
192     Request::waitall(size - 1, requests, MPI_STATUS_IGNORE);
193     for (int dst = 0; dst < size-1; dst++) {
194       Request::unref(&requests[dst]);
195     }
196     xbt_free(requests);
197   }
198   return MPI_SUCCESS;
199 }
200
201
202
203 int Coll_reduce_default::reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
204                      MPI_Comm comm)
205 {
206   const int system_tag = COLL_TAG_REDUCE;
207   MPI_Aint lb = 0;
208   MPI_Aint dataext = 0;
209
210   char* sendtmpbuf = static_cast<char *>(sendbuf);
211
212   int rank = comm->rank();
213   int size = comm->size();
214   if (size <= 0)
215     return MPI_ERR_COMM;
216   //non commutative case, use a working algo from openmpi
217   if (op != MPI_OP_NULL && not op->is_commutative()) {
218     return Coll_reduce_ompi_basic_linear::reduce(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
219   }
220
221   if( sendbuf == MPI_IN_PLACE ) {
222     sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
223     Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
224   }
225
226   if(rank != root) {
227     // Send buffer to root
228     Request::send(sendtmpbuf, count, datatype, root, system_tag, comm);
229   } else {
230     datatype->extent(&lb, &dataext);
231     // Local copy from root
232     if (sendtmpbuf != nullptr && recvbuf != nullptr)
233       Datatype::copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
234     // Receive buffers from senders
235     MPI_Request *requests = xbt_new(MPI_Request, size - 1);
236     void **tmpbufs = xbt_new(void *, size - 1);
237     int index = 0;
238     for (int src = 0; src < size; src++) {
239       if (src != root) {
240         if (not smpi_process()->replaying())
241           tmpbufs[index] = xbt_malloc(count * dataext);
242          else
243            tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
244         requests[index] =
245           Request::irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
246         index++;
247       }
248     }
249     // Wait for completion of irecv's.
250     Request::startall(size - 1, requests);
251     for (int src = 0; src < size - 1; src++) {
252       index = Request::waitany(size - 1, requests, MPI_STATUS_IGNORE);
253       XBT_DEBUG("finished waiting any request with index %d", index);
254       if(index == MPI_UNDEFINED) {
255         break;
256       }else{
257         Request::unref(&requests[index]);
258       }
259       if(op) /* op can be MPI_OP_NULL that does nothing */
260         if(op!=MPI_OP_NULL) op->apply( tmpbufs[index], recvbuf, &count, datatype);
261     }
262       for(index = 0; index < size - 1; index++) {
263         smpi_free_tmp_buffer(tmpbufs[index]);
264       }
265     xbt_free(tmpbufs);
266     xbt_free(requests);
267
268   }
269   if( sendbuf == MPI_IN_PLACE ) {
270     smpi_free_tmp_buffer(sendtmpbuf);
271   }
272   return MPI_SUCCESS;
273 }
274
275 int Coll_allreduce_default::allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
276 {
277   int ret;
278   ret = Coll_reduce_default::reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
279   if(ret==MPI_SUCCESS)
280     ret = Coll_bcast_default::bcast(recvbuf, count, datatype, 0, comm);
281   return ret;
282 }
283
284 int Coll_alltoall_default::alltoall( void *sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
285 {
286   return Coll_alltoall_ompi::alltoall(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
287 }
288
289
290
291 int Coll_alltoallv_default::alltoallv(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype sendtype,
292                               void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
293 {
294   const int system_tag = 889;
295   MPI_Aint lb = 0;
296   MPI_Aint sendext = 0;
297   MPI_Aint recvext = 0;
298   MPI_Request *requests;
299
300   /* Initialize. */
301   int rank = comm->rank();
302   int size = comm->size();
303   XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
304   sendtype->extent(&lb, &sendext);
305   recvtype->extent(&lb, &recvext);
306   /* Local copy from self */
307   int err = Datatype::copy(static_cast<char *>(sendbuf) + senddisps[rank] * sendext, sendcounts[rank], sendtype,
308                                static_cast<char *>(recvbuf) + recvdisps[rank] * recvext, recvcounts[rank], recvtype);
309   if (err == MPI_SUCCESS && size > 1) {
310     /* Initiate all send/recv to/from others. */
311     requests = xbt_new(MPI_Request, 2 * (size - 1));
312     int count = 0;
313     /* Create all receives that will be posted first */
314     for (int i = 0; i < size; ++i) {
315       if (i != rank && recvcounts[i] != 0) {
316         requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i] * recvext,
317                                           recvcounts[i], recvtype, i, system_tag, comm);
318         count++;
319       }else{
320         XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
321       }
322     }
323     /* Now create all sends  */
324     for (int i = 0; i < size; ++i) {
325       if (i != rank && sendcounts[i] != 0) {
326       requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + senddisps[i] * sendext,
327                                         sendcounts[i], sendtype, i, system_tag, comm);
328       count++;
329       }else{
330         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
331       }
332     }
333     /* Wait for them all. */
334     Request::startall(count, requests);
335     XBT_DEBUG("<%d> wait for %d requests", rank, count);
336     Request::waitall(count, requests, MPI_STATUS_IGNORE);
337     for (int i = 0; i < count; i++) {
338       if(requests[i]!=MPI_REQUEST_NULL)
339         Request::unref(&requests[i]);
340     }
341     xbt_free(requests);
342   }
343   return err;
344 }
345
346 }
347 }
348