Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / smpi / colls / smpi_nbc_impl.cpp
1 /* Asynchronous parts of the basic collective algorithms, meant to be used both for the naive default implementation, but also for non blocking collectives */
2
3 /* Copyright (c) 2009-2023. 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::smpi {
12
13 int colls::ibarrier(MPI_Comm comm, MPI_Request* request, int external)
14 {
15   int size = comm->size();
16   int rank = comm->rank();
17   int system_tag=COLL_TAG_BARRIER-external;
18   (*request) = new Request( nullptr, 0, MPI_BYTE,
19                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
20   std::vector<MPI_Request> requests;
21
22   if (rank > 0) {
23     requests.push_back(Request::isend_init (nullptr, 0, MPI_BYTE, 0, system_tag, comm));
24     requests.push_back(Request::irecv_init(nullptr, 0, MPI_BYTE, 0, system_tag, comm));
25   }
26   else {
27     for (int i = 1; i < 2 * size - 1; i += 2) {
28       requests.push_back(Request::irecv_init(nullptr, 0, MPI_BYTE, MPI_ANY_SOURCE, system_tag, comm));
29       requests.push_back(Request::isend_init(nullptr, 0, MPI_BYTE, (i + 1) / 2, system_tag, comm));
30     }
31   }
32   (*request)->start_nbc_requests(requests);
33   return MPI_SUCCESS;
34 }
35
36 int colls::ibcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, MPI_Request* request,
37                   int external)
38 {
39   int size = comm->size();
40   int rank = comm->rank();
41   int system_tag=COLL_TAG_BCAST-external;
42   std::vector<MPI_Request> requests;
43   (*request) = new Request( nullptr, 0, MPI_BYTE,
44                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
45   if (rank != root) {
46     requests.push_back(Request::irecv_init(buf, count, datatype, root, system_tag, comm));
47   }
48   else {
49     for (int i = 0; i < size; i++) {
50       if(i!=root){
51         requests.push_back(Request::isend_init(buf, count, datatype, i, system_tag, comm));
52       }
53     }
54   }
55   (*request)->start_nbc_requests(requests);
56   return MPI_SUCCESS;
57 }
58
59 int colls::iallgather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
60                       MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request, int external)
61 {
62
63   const int system_tag = COLL_TAG_ALLGATHER-external;
64   MPI_Aint lb = 0;
65   MPI_Aint recvext = 0;
66   std::vector<MPI_Request> requests;
67
68   int rank = comm->rank();
69   int size = comm->size();
70   (*request) = new Request( nullptr, 0, MPI_BYTE,
71                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
72   // FIXME: check for errors
73   recvtype->extent(&lb, &recvext);
74   // Local copy from self
75   Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount,
76                      recvtype);
77   // Send/Recv buffers to/from others;
78   for (int other = 0; other < size; other++) {
79     if(other != rank) {
80       requests.push_back(Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm));
81       requests.push_back(Request::irecv_init(static_cast<char*>(recvbuf) + other * recvcount * recvext, recvcount,
82                                              recvtype, other, system_tag, comm));
83     }
84   }
85   (*request)->start_nbc_requests(requests);
86   return MPI_SUCCESS;
87 }
88
89 int colls::iscatter(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
90                     MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request, int external)
91 {
92   const int system_tag = COLL_TAG_SCATTER-external;
93   MPI_Aint lb = 0;
94   MPI_Aint sendext = 0;
95   std::vector<MPI_Request> requests;
96
97   int rank = comm->rank();
98   int size = comm->size();
99   (*request) = new Request( nullptr, 0, MPI_BYTE,
100                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
101   if(rank != root) {
102     // Recv buffer from root
103     requests.push_back(Request::irecv_init(recvbuf, recvcount, recvtype, root, system_tag, comm));
104   } else {
105     sendtype->extent(&lb, &sendext);
106     // Local copy from root
107     if(recvbuf!=MPI_IN_PLACE){
108         Datatype::copy(static_cast<const char *>(sendbuf) + root * sendcount * sendext,
109                            sendcount, sendtype, recvbuf, recvcount, recvtype);
110     }
111     // Send buffers to receivers
112     for(int dst = 0; dst < size; dst++) {
113       if(dst != root) {
114         requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype,
115                                           dst, system_tag, comm));
116       }
117     }
118   }
119   (*request)->start_nbc_requests(requests);
120   return MPI_SUCCESS;
121 }
122
123 int colls::iallgatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts,
124                        const int* displs, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request, int external)
125 {
126   const int system_tag = COLL_TAG_ALLGATHERV-external;
127   MPI_Aint lb = 0;
128   MPI_Aint recvext = 0;
129   std::vector<MPI_Request> requests;
130
131   int rank = comm->rank();
132   int size = comm->size();
133   (*request) = new Request( nullptr, 0, MPI_BYTE,
134                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
135   recvtype->extent(&lb, &recvext);
136   // Local copy from self
137   Datatype::copy(sendbuf, sendcount, sendtype,
138                      static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
139   // Send buffers to others;
140   for (int other = 0; other < size; other++) {
141     if(other != rank) {
142       requests.push_back(Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm));
143       requests.push_back(Request::irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
144                          recvtype, other, system_tag, comm));
145     }
146   }
147   // Wait for completion of all comms.
148   (*request)->start_nbc_requests(requests);
149   return MPI_SUCCESS;
150 }
151
152 int colls::ialltoall(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
153                      MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request, int external)
154 {
155   int system_tag   = COLL_TAG_ALLTOALL-external;
156   MPI_Aint lb      = 0;
157   MPI_Aint sendext = 0;
158   MPI_Aint recvext = 0;
159   std::vector<MPI_Request> requests;
160
161   /* Initialize. */
162   int rank = comm->rank();
163   int size = comm->size();
164   (*request) = new Request( nullptr, 0, MPI_BYTE,
165                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
166   sendtype->extent(&lb, &sendext);
167   recvtype->extent(&lb, &recvext);
168   /* simple optimization */
169   int err = Datatype::copy(static_cast<const char *>(sendbuf) + rank * sendcount * sendext, sendcount, sendtype,
170                                static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
171   if (err == MPI_SUCCESS && size > 1) {
172     /* Initiate all send/recv to/from others. */
173     /* Post all receives first -- a simple optimization */
174     for (int i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
175       requests.push_back(Request::irecv_init(static_cast<char *>(recvbuf) + i * recvcount * recvext, recvcount,
176                                         recvtype, i, system_tag, comm));
177     }
178     /* Now post all sends in reverse order
179      *   - We would like to minimize the search time through message queue
180      *     when messages actually arrive in the order in which they were posted.
181      * TODO: check the previous assertion
182      */
183     for (int i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
184       requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + i * sendcount * sendext, sendcount,
185                                         sendtype, i, system_tag, comm));
186     }
187     /* Wait for them all. */
188     (*request)->start_nbc_requests(requests);
189   }
190   return MPI_SUCCESS;
191 }
192
193 int colls::ialltoallv(const void* sendbuf, const int* sendcounts, const int* senddisps, MPI_Datatype sendtype,
194                       void* recvbuf, const int* recvcounts, const int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm,
195                       MPI_Request* request, int external)
196 {
197   const int system_tag = COLL_TAG_ALLTOALLV-external;
198   MPI_Aint lb = 0;
199   MPI_Aint sendext = 0;
200   MPI_Aint recvext = 0;
201   std::vector<MPI_Request> requests;
202
203   /* Initialize. */
204   int rank = comm->rank();
205   int size = comm->size();
206   (*request) = new Request( nullptr, 0, MPI_BYTE,
207                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
208   sendtype->extent(&lb, &sendext);
209   recvtype->extent(&lb, &recvext);
210   /* Local copy from self */
211   int err = Datatype::copy(static_cast<const char *>(sendbuf) + senddisps[rank] * sendext, sendcounts[rank], sendtype,
212                                static_cast<char *>(recvbuf) + recvdisps[rank] * recvext, recvcounts[rank], recvtype);
213   if (err == MPI_SUCCESS && size > 1) {
214     /* Initiate all send/recv to/from others. */
215     /* Create all receives that will be posted first */
216     for (int i = 0; i < size; ++i) {
217       if (i != rank) {
218         requests.push_back(Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i] * recvext,
219                                           recvcounts[i], recvtype, i, system_tag, comm));
220       }else{
221         XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
222       }
223     }
224     /* Now create all sends  */
225     for (int i = 0; i < size; ++i) {
226       if (i != rank) {
227       requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + senddisps[i] * sendext,
228                                         sendcounts[i], sendtype, i, system_tag, comm));
229       }else{
230         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
231       }
232     }
233     /* Wait for them all. */
234     (*request)->start_nbc_requests(requests);
235   }
236   return err;
237 }
238
239 int colls::ialltoallw(const void* sendbuf, const int* sendcounts, const int* senddisps, const MPI_Datatype* sendtypes,
240                       void* recvbuf, const int* recvcounts, const int* recvdisps, const MPI_Datatype* recvtypes,
241                       MPI_Comm comm, MPI_Request* request, int external)
242 {
243   const int system_tag = COLL_TAG_ALLTOALLW-external;
244   std::vector<MPI_Request> requests;
245
246   /* Initialize. */
247   int rank = comm->rank();
248   int size = comm->size();
249   (*request) = new Request( nullptr, 0, MPI_BYTE,
250                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
251   /* Local copy from self */
252   int err = (sendcounts[rank]>0 && recvcounts[rank]) ? Datatype::copy(static_cast<const char *>(sendbuf) + senddisps[rank], sendcounts[rank], sendtypes[rank],
253                                static_cast<char *>(recvbuf) + recvdisps[rank], recvcounts[rank], recvtypes[rank]): MPI_SUCCESS;
254   if (err == MPI_SUCCESS && size > 1) {
255     /* Initiate all send/recv to/from others. */
256     /* Create all receives that will be posted first */
257     for (int i = 0; i < size; ++i) {
258       if (i != rank) {
259         requests.push_back(Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i],
260                                           recvcounts[i], recvtypes[i], i, system_tag, comm));
261       }else{
262         XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
263       }
264     }
265     /* Now create all sends  */
266     for (int i = 0; i < size; ++i) {
267       if (i != rank) {
268       requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + senddisps[i] ,
269                                         sendcounts[i], sendtypes[i], i, system_tag, comm));
270       }else{
271         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
272       }
273     }
274     /* Wait for them all. */
275     (*request)->start_nbc_requests(requests);
276   }
277   return err;
278 }
279
280 int colls::igather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
281                    MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request, int external)
282 {
283   const int system_tag = COLL_TAG_GATHER-external;
284   MPI_Aint lb = 0;
285   MPI_Aint recvext = 0;
286   std::vector<MPI_Request> requests;
287
288   int rank = comm->rank();
289   int size = comm->size();
290   (*request) = new Request( nullptr, 0, MPI_BYTE,
291                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
292   if(rank != root) {
293     // Send buffer to root
294     requests.push_back(Request::isend_init(sendbuf, sendcount, sendtype, root, system_tag, comm));
295   } else {
296     recvtype->extent(&lb, &recvext);
297     // Local copy from root
298     Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext,
299                        recvcount, recvtype);
300     // Receive buffers from senders
301     for (int src = 0; src < size; src++) {
302       if(src != root) {
303         requests.push_back(Request::irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
304                                           src, system_tag, comm));
305       }
306     }
307   }
308   (*request)->start_nbc_requests(requests);
309   return MPI_SUCCESS;
310 }
311
312 int colls::igatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts,
313                     const int* displs, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request,
314                     int external)
315 {
316   int system_tag = COLL_TAG_GATHERV-external;
317   MPI_Aint lb = 0;
318   MPI_Aint recvext = 0;
319   std::vector<MPI_Request> requests;
320
321   int rank = comm->rank();
322   int size = comm->size();
323   (*request) = new Request( nullptr, 0, MPI_BYTE,
324                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
325   if (rank != root) {
326     // Send buffer to root
327     requests.push_back(Request::isend_init(sendbuf, sendcount, sendtype, root, system_tag, comm));
328   } else {
329     recvtype->extent(&lb, &recvext);
330     // Local copy from root
331     Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
332                        recvcounts[root], recvtype);
333     // Receive buffers from senders
334     for (int src = 0; src < size; src++) {
335       if(src != root) {
336         requests.push_back(Request::irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
337                           recvcounts[src], recvtype, src, system_tag, comm));
338       }
339     }
340   }
341   // Wait for completion of irecv's.
342   (*request)->start_nbc_requests(requests);
343   return MPI_SUCCESS;
344 }
345 int colls::iscatterv(const void* sendbuf, const int* sendcounts, const int* displs, MPI_Datatype sendtype,
346                      void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request,
347                      int external)
348 {
349   int system_tag = COLL_TAG_SCATTERV-external;
350   MPI_Aint lb = 0;
351   MPI_Aint sendext = 0;
352   std::vector<MPI_Request> requests;
353
354   int rank = comm->rank();
355   int size = comm->size();
356   (*request) = new Request( nullptr, 0, MPI_BYTE,
357                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
358   if(rank != root) {
359     // Recv buffer from root
360     requests.push_back(Request::irecv_init(recvbuf, recvcount, recvtype, root, system_tag, comm));
361   } else {
362     sendtype->extent(&lb, &sendext);
363     // Local copy from root
364     if(recvbuf!=MPI_IN_PLACE){
365       Datatype::copy(static_cast<const char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
366                        sendtype, recvbuf, recvcount, recvtype);
367     }
368     // Send buffers to receivers
369     for (int dst = 0; dst < size; dst++) {
370       if (dst != root) {
371         requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
372                             sendtype, dst, system_tag, comm));
373       }
374     }
375   }
376   (*request)->start_nbc_requests(requests);
377   return MPI_SUCCESS;
378 }
379
380 int colls::ireduce(const void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
381                    MPI_Comm comm, MPI_Request* request, int external)
382 {
383   const int system_tag = COLL_TAG_REDUCE-external;
384   MPI_Aint lb = 0;
385   MPI_Aint dataext = 0;
386   std::vector<MPI_Request> requests;
387
388   const void* real_sendbuf = sendbuf;
389
390   int rank = comm->rank();
391   int size = comm->size();
392
393   if (size <= 0)
394     return MPI_ERR_COMM;
395
396   unsigned char* tmp_sendbuf = nullptr;
397   if( sendbuf == MPI_IN_PLACE ) {
398     tmp_sendbuf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
399     Datatype::copy(recvbuf, count, datatype, tmp_sendbuf, count, datatype);
400     real_sendbuf = tmp_sendbuf;
401   }
402
403   if(rank == root){
404     (*request) =  new Request( recvbuf, count, datatype,
405                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC, op);
406   }
407   else
408     (*request) = new Request( nullptr, count, datatype,
409                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
410
411   if(rank != root) {
412     // Send buffer to root
413     requests.push_back(Request::isend_init(real_sendbuf, count, datatype, root, system_tag, comm));
414   } else {
415     datatype->extent(&lb, &dataext);
416     // Local copy from root
417     if (real_sendbuf != nullptr && recvbuf != nullptr)
418       Datatype::copy(real_sendbuf, count, datatype, recvbuf, count, datatype);
419     // Receive buffers from senders
420     for (int src = 0; src < size; src++) {
421       if (src != root) {
422         requests.push_back(Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, src, system_tag, comm));
423       }
424     }
425   }
426   (*request)->start_nbc_requests(requests);
427   if( sendbuf == MPI_IN_PLACE ) {
428     smpi_free_tmp_buffer(tmp_sendbuf);
429   }
430   return MPI_SUCCESS;
431 }
432
433 int colls::iallreduce(const void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm,
434                       MPI_Request* request, int external)
435 {
436
437   const int system_tag = COLL_TAG_ALLREDUCE-external;
438   MPI_Aint lb = 0;
439   MPI_Aint dataext = 0;
440   std::vector<MPI_Request> requests;
441
442   int rank = comm->rank();
443   int size = comm->size();
444   (*request) = new Request( recvbuf, count, datatype,
445                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC, op);
446   // FIXME: check for errors
447   datatype->extent(&lb, &dataext);
448   // Local copy from self
449   Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
450   // Send/Recv buffers to/from others;
451   for (int other = 0; other < size; other++) {
452     if(other != rank) {
453       requests.push_back(Request::isend_init(sendbuf, count, datatype, other, system_tag,comm));
454       requests.push_back(Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
455                                         other, system_tag, comm));
456     }
457   }
458   (*request)->start_nbc_requests(requests);
459   return MPI_SUCCESS;
460 }
461
462 int colls::iscan(const void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm,
463                  MPI_Request* request, int external)
464 {
465   int system_tag = -888-external;
466   MPI_Aint lb      = 0;
467   MPI_Aint dataext = 0;
468   std::vector<MPI_Request> requests;
469
470   int rank = comm->rank();
471   int size = comm->size();
472   (*request) = new Request( recvbuf, count, datatype,
473                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC, op);
474   datatype->extent(&lb, &dataext);
475
476   // Local copy from self
477   Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
478
479   // Send/Recv buffers to/from others
480   for (int other = 0; other < rank; other++) {
481     requests.push_back(Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm));
482   }
483   for (int other = rank + 1; other < size; other++) {
484     requests.push_back(Request::isend_init(sendbuf, count, datatype, other, system_tag, comm));
485   }
486   // Wait for completion of all comms.
487   (*request)->start_nbc_requests(requests);
488   return MPI_SUCCESS;
489 }
490
491 int colls::iexscan(const void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm,
492                    MPI_Request* request, int external)
493 {
494   int system_tag = -888-external;
495   MPI_Aint lb         = 0;
496   MPI_Aint dataext    = 0;
497   std::vector<MPI_Request> requests;
498
499   int rank = comm->rank();
500   int size = comm->size();
501   (*request) = new Request( recvbuf, count, datatype,
502                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC, op);
503   datatype->extent(&lb, &dataext);
504   if(rank != 0)
505     memset(recvbuf, 0, count*dataext);
506
507   // Send/Recv buffers to/from others
508   for (int other = 0; other < rank; other++) {
509     requests.push_back(Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm));
510   }
511   for (int other = rank + 1; other < size; other++) {
512     requests.push_back(Request::isend_init(sendbuf, count, datatype, other, system_tag, comm));
513   }
514   // Wait for completion of all comms.
515   (*request)->start_nbc_requests(requests);
516   return MPI_SUCCESS;
517 }
518
519 int colls::ireduce_scatter(const void* sendbuf, void* recvbuf, const int* recvcounts, MPI_Datatype datatype, MPI_Op op,
520                            MPI_Comm comm, MPI_Request* request, int external)
521 {
522   // Version where each process performs the reduce for its own part. Alltoall pattern for comms.
523   const int system_tag = COLL_TAG_REDUCE_SCATTER-external;
524   MPI_Aint lb = 0;
525   MPI_Aint dataext = 0;
526   std::vector<MPI_Request> requests;
527
528   int rank = comm->rank();
529   int size = comm->size();
530   int count=recvcounts[rank];
531   (*request) = new Request( recvbuf, count, datatype,
532                          rank,rank, system_tag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC, op);
533   datatype->extent(&lb, &dataext);
534
535   // Send/Recv buffers to/from others;
536   int recvdisp=0;
537   for (int other = 0; other < size; other++) {
538     if(other != rank) {
539       requests.push_back(Request::isend_init(static_cast<const char *>(sendbuf) + recvdisp * dataext, recvcounts[other], datatype, other, system_tag,comm));
540       XBT_VERB("sending with recvdisp %d", recvdisp);
541       requests.push_back(Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
542                                         other, system_tag, comm));
543     }else{
544       Datatype::copy(static_cast<const char *>(sendbuf) + recvdisp * dataext, count, datatype, recvbuf, count, datatype);
545     }
546     recvdisp+=recvcounts[other];
547   }
548   (*request)->start_nbc_requests(requests);
549   return MPI_SUCCESS;
550 }
551
552 } // namespace simgrid::smpi