1 /* Asynchronous parts of the basic collective algorithms, meant to be used both for the naive default implementation, but also for non blocking collectives */
3 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved. */
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. */
8 #include "colls_private.hpp"
9 #include "src/smpi/include/smpi_actor.hpp"
15 int Colls::ibarrier(MPI_Comm comm, MPI_Request* request)
17 int size = comm->size();
18 int rank = comm->rank();
19 (*request) = new Request( nullptr, 0, MPI_BYTE,
20 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
22 MPI_Request* requests = new MPI_Request[2];
23 requests[0] = Request::isend (nullptr, 0, MPI_BYTE, 0,
26 requests[1] = Request::irecv (nullptr, 0, MPI_BYTE, 0,
29 (*request)->set_nbc_requests(requests, 2);
32 MPI_Request* requests = new MPI_Request[(size - 1) * 2];
33 for (int i = 1; i < 2 * size - 1; i += 2) {
34 requests[i - 1] = Request::irecv(nullptr, 0, MPI_BYTE, MPI_ANY_SOURCE, COLL_TAG_BARRIER, comm);
35 requests[i] = Request::isend(nullptr, 0, MPI_BYTE, (i + 1) / 2, COLL_TAG_BARRIER, comm);
37 (*request)->set_nbc_requests(requests, 2*(size-1));
42 int Colls::ibcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, MPI_Request* request)
44 int size = comm->size();
45 int rank = comm->rank();
46 (*request) = new Request( nullptr, 0, MPI_BYTE,
47 rank,rank, COLL_TAG_BCAST, comm, MPI_REQ_PERSISTENT);
49 MPI_Request* requests = new MPI_Request[1];
50 requests[0] = Request::irecv (buf, count, datatype, root,
53 (*request)->set_nbc_requests(requests, 1);
56 MPI_Request* requests = new MPI_Request[size - 1];
58 for (int i = 0; i < size; i++) {
60 requests[n] = Request::isend(buf, count, datatype, i,
67 (*request)->set_nbc_requests(requests, size-1);
72 int Colls::iallgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
73 void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
76 const int system_tag = COLL_TAG_ALLGATHER;
80 int rank = comm->rank();
81 int size = comm->size();
82 (*request) = new Request( nullptr, 0, MPI_BYTE,
83 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
84 // FIXME: check for errors
85 recvtype->extent(&lb, &recvext);
86 // Local copy from self
87 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount,
89 // Send/Recv buffers to/from others;
90 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
92 for (int other = 0; other < size; other++) {
94 requests[index] = Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag,comm);
96 requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + other * recvcount * recvext, recvcount, recvtype,
97 other, system_tag, comm);
101 Request::startall(2 * (size - 1), requests);
102 (*request)->set_nbc_requests(requests, 2 * (size - 1));
106 int Colls::iscatter(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
107 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
109 const int system_tag = COLL_TAG_SCATTER;
111 MPI_Aint sendext = 0;
113 int rank = comm->rank();
114 int size = comm->size();
115 (*request) = new Request( nullptr, 0, MPI_BYTE,
116 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
118 MPI_Request* requests = new MPI_Request[1];
119 // Recv buffer from root
120 requests[0] = Request::irecv(recvbuf, recvcount, recvtype, root, system_tag, comm);
121 (*request)->set_nbc_requests(requests, 1);
123 sendtype->extent(&lb, &sendext);
124 // Local copy from root
125 if(recvbuf!=MPI_IN_PLACE){
126 Datatype::copy(static_cast<const char *>(sendbuf) + root * sendcount * sendext,
127 sendcount, sendtype, recvbuf, recvcount, recvtype);
129 // Send buffers to receivers
130 MPI_Request* requests = new MPI_Request[size - 1];
132 for(int dst = 0; dst < size; dst++) {
134 requests[index] = Request::isend_init(static_cast<const char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype,
135 dst, system_tag, comm);
139 // Wait for completion of isend's.
140 Request::startall(size - 1, requests);
141 (*request)->set_nbc_requests(requests, size - 1);
146 int Colls::iallgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
147 const int *recvcounts, const int *displs, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
149 const int system_tag = COLL_TAG_ALLGATHERV;
151 MPI_Aint recvext = 0;
153 int rank = comm->rank();
154 int size = comm->size();
155 (*request) = new Request( nullptr, 0, MPI_BYTE,
156 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
157 recvtype->extent(&lb, &recvext);
158 // Local copy from self
159 Datatype::copy(sendbuf, sendcount, sendtype,
160 static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
161 // Send buffers to others;
162 MPI_Request *requests = new MPI_Request[2 * (size - 1)];
164 for (int other = 0; other < size; other++) {
167 Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
169 requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
170 recvtype, other, system_tag, comm);
174 // Wait for completion of all comms.
175 Request::startall(2 * (size - 1), requests);
176 (*request)->set_nbc_requests(requests, 2 * (size - 1));
180 int Colls::ialltoall( const void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request){
181 int system_tag = COLL_TAG_ALLTOALL;
183 MPI_Aint sendext = 0;
184 MPI_Aint recvext = 0;
187 int rank = comm->rank();
188 int size = comm->size();
189 (*request) = new Request( nullptr, 0, MPI_BYTE,
190 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
191 sendtype->extent(&lb, &sendext);
192 recvtype->extent(&lb, &recvext);
193 /* simple optimization */
194 int err = Datatype::copy(static_cast<const char *>(sendbuf) + rank * sendcount * sendext, sendcount, sendtype,
195 static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
196 if (err == MPI_SUCCESS && size > 1) {
197 /* Initiate all send/recv to/from others. */
198 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
199 /* Post all receives first -- a simple optimization */
201 for (int i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
202 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + i * recvcount * recvext, recvcount,
203 recvtype, i, system_tag, comm);
206 /* Now post all sends in reverse order
207 * - We would like to minimize the search time through message queue
208 * when messages actually arrive in the order in which they were posted.
209 * TODO: check the previous assertion
211 for (int i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
212 requests[count] = Request::isend_init(static_cast<const char *>(sendbuf) + i * sendcount * sendext, sendcount,
213 sendtype, i, system_tag, comm);
216 /* Wait for them all. */
217 Request::startall(count, requests);
218 (*request)->set_nbc_requests(requests, count);
223 int Colls::ialltoallv(const void *sendbuf, const int *sendcounts, const int *senddisps, MPI_Datatype sendtype,
224 void *recvbuf, const int *recvcounts, const int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request *request){
225 const int system_tag = COLL_TAG_ALLTOALLV;
227 MPI_Aint sendext = 0;
228 MPI_Aint recvext = 0;
231 int rank = comm->rank();
232 int size = comm->size();
233 (*request) = new Request( nullptr, 0, MPI_BYTE,
234 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
235 sendtype->extent(&lb, &sendext);
236 recvtype->extent(&lb, &recvext);
237 /* Local copy from self */
238 int err = Datatype::copy(static_cast<const char *>(sendbuf) + senddisps[rank] * sendext, sendcounts[rank], sendtype,
239 static_cast<char *>(recvbuf) + recvdisps[rank] * recvext, recvcounts[rank], recvtype);
240 if (err == MPI_SUCCESS && size > 1) {
241 /* Initiate all send/recv to/from others. */
242 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
244 /* Create all receives that will be posted first */
245 for (int i = 0; i < size; ++i) {
247 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i] * recvext,
248 recvcounts[i], recvtype, i, system_tag, comm);
251 XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
254 /* Now create all sends */
255 for (int i = 0; i < size; ++i) {
257 requests[count] = Request::isend_init(static_cast<const char *>(sendbuf) + senddisps[i] * sendext,
258 sendcounts[i], sendtype, i, system_tag, comm);
261 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
264 /* Wait for them all. */
265 Request::startall(count, requests);
266 (*request)->set_nbc_requests(requests, count);
271 int Colls::ialltoallw(const void *sendbuf, const int *sendcounts, const int *senddisps, const MPI_Datatype* sendtypes,
272 void *recvbuf, const int *recvcounts, const int *recvdisps, const MPI_Datatype* recvtypes, MPI_Comm comm, MPI_Request *request){
273 const int system_tag = COLL_TAG_ALLTOALLV;
276 int rank = comm->rank();
277 int size = comm->size();
278 (*request) = new Request( nullptr, 0, MPI_BYTE,
279 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
280 /* Local copy from self */
281 int err = (sendcounts[rank]>0 && recvcounts[rank]) ? Datatype::copy(static_cast<const char *>(sendbuf) + senddisps[rank], sendcounts[rank], sendtypes[rank],
282 static_cast<char *>(recvbuf) + recvdisps[rank], recvcounts[rank], recvtypes[rank]): MPI_SUCCESS;
283 if (err == MPI_SUCCESS && size > 1) {
284 /* Initiate all send/recv to/from others. */
285 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
287 /* Create all receives that will be posted first */
288 for (int i = 0; i < size; ++i) {
290 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i],
291 recvcounts[i], recvtypes[i], i, system_tag, comm);
294 XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
297 /* Now create all sends */
298 for (int i = 0; i < size; ++i) {
300 requests[count] = Request::isend_init(static_cast<const char *>(sendbuf) + senddisps[i] ,
301 sendcounts[i], sendtypes[i], i, system_tag, comm);
304 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
307 /* Wait for them all. */
308 Request::startall(count, requests);
309 (*request)->set_nbc_requests(requests, count);
314 int Colls::igather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
315 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
317 const int system_tag = COLL_TAG_GATHER;
319 MPI_Aint recvext = 0;
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);
326 // Send buffer to root
327 MPI_Request* requests = new MPI_Request[1];
328 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
329 (*request)->set_nbc_requests(requests, 1);
331 recvtype->extent(&lb, &recvext);
332 // Local copy from root
333 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext,
334 recvcount, recvtype);
335 // Receive buffers from senders
336 MPI_Request* requests = new MPI_Request[size - 1];
338 for (int src = 0; src < size; src++) {
340 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
341 src, system_tag, comm);
345 // Wait for completion of irecv's.
346 Request::startall(size - 1, requests);
347 (*request)->set_nbc_requests(requests, size - 1);
352 int Colls::igatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int *recvcounts, const int *displs,
353 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
355 int system_tag = COLL_TAG_GATHERV;
357 MPI_Aint recvext = 0;
359 int rank = comm->rank();
360 int size = comm->size();
361 (*request) = new Request( nullptr, 0, MPI_BYTE,
362 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
364 // Send buffer to root
365 MPI_Request* requests = new MPI_Request[1];
366 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
367 (*request)->set_nbc_requests(requests, 1);
369 recvtype->extent(&lb, &recvext);
370 // Local copy from root
371 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
372 recvcounts[root], recvtype);
373 // Receive buffers from senders
374 MPI_Request* requests = new MPI_Request[size - 1];
376 for (int src = 0; src < size; src++) {
378 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
379 recvcounts[src], recvtype, src, system_tag, comm);
383 // Wait for completion of irecv's.
384 Request::startall(size - 1, requests);
385 (*request)->set_nbc_requests(requests, size - 1);
389 int Colls::iscatterv(const void *sendbuf, const int *sendcounts, const int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount,
390 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
392 int system_tag = COLL_TAG_SCATTERV;
394 MPI_Aint sendext = 0;
396 int rank = comm->rank();
397 int size = comm->size();
398 (*request) = new Request( nullptr, 0, MPI_BYTE,
399 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
401 // Recv buffer from root
402 MPI_Request* requests = new MPI_Request[1];
403 requests[0]=Request::irecv(recvbuf, recvcount, recvtype, root, system_tag, comm);
404 (*request)->set_nbc_requests(requests, 1);
406 sendtype->extent(&lb, &sendext);
407 // Local copy from root
408 if(recvbuf!=MPI_IN_PLACE){
409 Datatype::copy(static_cast<const char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
410 sendtype, recvbuf, recvcount, recvtype);
412 // Send buffers to receivers
413 MPI_Request *requests = new MPI_Request[size - 1];
415 for (int dst = 0; dst < size; dst++) {
417 requests[index] = Request::isend_init(static_cast<const char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
418 sendtype, dst, system_tag, comm);
422 // Wait for completion of isend's.
423 Request::startall(size - 1, requests);
424 (*request)->set_nbc_requests(requests, size - 1);
429 int Colls::ireduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
430 MPI_Comm comm, MPI_Request* request)
432 const int system_tag = COLL_TAG_REDUCE;
434 MPI_Aint dataext = 0;
436 const void* real_sendbuf = sendbuf;
438 int rank = comm->rank();
439 int size = comm->size();
444 unsigned char* tmp_sendbuf = nullptr;
445 if( sendbuf == MPI_IN_PLACE ) {
446 tmp_sendbuf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
447 Datatype::copy(recvbuf, count, datatype, tmp_sendbuf, count, datatype);
448 real_sendbuf = tmp_sendbuf;
452 (*request) = new Request( recvbuf, count, datatype,
453 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
456 (*request) = new Request( nullptr, count, datatype,
457 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
460 // Send buffer to root
461 MPI_Request* requests = new MPI_Request[1];
462 requests[0] = Request::isend(real_sendbuf, count, datatype, root, system_tag, comm);
463 (*request)->set_nbc_requests(requests, 1);
465 datatype->extent(&lb, &dataext);
466 // Local copy from root
467 if (real_sendbuf != nullptr && recvbuf != nullptr)
468 Datatype::copy(real_sendbuf, count, datatype, recvbuf, count, datatype);
469 // Receive buffers from senders
470 MPI_Request *requests = new MPI_Request[size - 1];
472 for (int src = 0; src < size; src++) {
475 Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, src, system_tag, comm);
479 // Wait for completion of irecv's.
480 Request::startall(size - 1, requests);
481 (*request)->set_nbc_requests(requests, size - 1);
483 if( sendbuf == MPI_IN_PLACE ) {
484 smpi_free_tmp_buffer(tmp_sendbuf);
489 int Colls::iallreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
490 MPI_Op op, MPI_Comm comm, MPI_Request* request)
493 const int system_tag = COLL_TAG_ALLREDUCE;
495 MPI_Aint dataext = 0;
497 int rank = comm->rank();
498 int size = comm->size();
499 (*request) = new Request( recvbuf, count, datatype,
500 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
501 // FIXME: check for errors
502 datatype->extent(&lb, &dataext);
503 // Local copy from self
504 Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
505 // Send/Recv buffers to/from others;
506 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
508 for (int other = 0; other < size; other++) {
510 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag,comm);
512 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
513 other, system_tag, comm);
517 Request::startall(2 * (size - 1), requests);
518 (*request)->set_nbc_requests(requests, 2 * (size - 1));
522 int Colls::iscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
524 int system_tag = -888;
526 MPI_Aint dataext = 0;
528 int rank = comm->rank();
529 int size = comm->size();
530 (*request) = new Request( recvbuf, count, datatype,
531 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
532 datatype->extent(&lb, &dataext);
534 // Local copy from self
535 Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
537 // Send/Recv buffers to/from others
538 MPI_Request *requests = new MPI_Request[size - 1];
540 for (int other = 0; other < rank; other++) {
541 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm);
544 for (int other = rank + 1; other < size; other++) {
545 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag, comm);
548 // Wait for completion of all comms.
549 Request::startall(size - 1, requests);
550 (*request)->set_nbc_requests(requests, size - 1);
554 int Colls::iexscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
556 int system_tag = -888;
558 MPI_Aint dataext = 0;
559 int rank = comm->rank();
560 int size = comm->size();
561 (*request) = new Request( recvbuf, count, datatype,
562 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
563 datatype->extent(&lb, &dataext);
565 memset(recvbuf, 0, count*dataext);
567 // Send/Recv buffers to/from others
568 MPI_Request *requests = new MPI_Request[size - 1];
570 for (int other = 0; other < rank; other++) {
571 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm);
574 for (int other = rank + 1; other < size; other++) {
575 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag, comm);
578 // Wait for completion of all comms.
579 Request::startall(size - 1, requests);
580 (*request)->set_nbc_requests(requests, size - 1);
584 int Colls::ireduce_scatter(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op,
585 MPI_Comm comm, MPI_Request* request){
586 //Version where each process performs the reduce for its own part. Alltoall pattern for comms.
587 const int system_tag = COLL_TAG_REDUCE_SCATTER;
589 MPI_Aint dataext = 0;
591 int rank = comm->rank();
592 int size = comm->size();
593 int count=recvcounts[rank];
594 (*request) = new Request( recvbuf, count, datatype,
595 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
596 datatype->extent(&lb, &dataext);
598 // Send/Recv buffers to/from others;
599 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
602 for (int other = 0; other < size; other++) {
604 requests[index] = Request::isend_init(static_cast<const char *>(sendbuf) + recvdisp * dataext, recvcounts[other], datatype, other, system_tag,comm);
605 XBT_VERB("sending with recvdisp %d", recvdisp);
607 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
608 other, system_tag, comm);
611 Datatype::copy(static_cast<const char *>(sendbuf) + recvdisp * dataext, count, datatype, recvbuf, count, datatype);
613 recvdisp+=recvcounts[other];
615 Request::startall(2 * (size - 1), requests);
616 (*request)->set_nbc_requests(requests, 2 * (size - 1));