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(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(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<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<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(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
147 int *recvcounts, 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( 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;
182 MPI_Aint lb = 0, sendext = 0, recvext = 0;
185 int rank = comm->rank();
186 int size = comm->size();
187 (*request) = new Request( nullptr, 0, MPI_BYTE,
188 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
189 sendtype->extent(&lb, &sendext);
190 recvtype->extent(&lb, &recvext);
191 /* simple optimization */
192 int err = Datatype::copy(static_cast<char *>(sendbuf) + rank * sendcount * sendext, sendcount, sendtype,
193 static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
194 if (err == MPI_SUCCESS && size > 1) {
195 /* Initiate all send/recv to/from others. */
196 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
197 /* Post all receives first -- a simple optimization */
199 for (int i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
200 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + i * recvcount * recvext, recvcount,
201 recvtype, i, system_tag, comm);
204 /* Now post all sends in reverse order
205 * - We would like to minimize the search time through message queue
206 * when messages actually arrive in the order in which they were posted.
207 * TODO: check the previous assertion
209 for (int i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
210 requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + i * sendcount * sendext, sendcount,
211 sendtype, i, system_tag, comm);
214 /* Wait for them all. */
215 Request::startall(count, requests);
216 (*request)->set_nbc_requests(requests, count);
221 int Colls::ialltoallv(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype sendtype,
222 void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request *request){
223 const int system_tag = COLL_TAG_ALLTOALLV;
225 MPI_Aint sendext = 0;
226 MPI_Aint recvext = 0;
229 int rank = comm->rank();
230 int size = comm->size();
231 (*request) = new Request( nullptr, 0, MPI_BYTE,
232 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
233 sendtype->extent(&lb, &sendext);
234 recvtype->extent(&lb, &recvext);
235 /* Local copy from self */
236 int err = Datatype::copy(static_cast<char *>(sendbuf) + senddisps[rank] * sendext, sendcounts[rank], sendtype,
237 static_cast<char *>(recvbuf) + recvdisps[rank] * recvext, recvcounts[rank], recvtype);
238 if (err == MPI_SUCCESS && size > 1) {
239 /* Initiate all send/recv to/from others. */
240 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
242 /* Create all receives that will be posted first */
243 for (int i = 0; i < size; ++i) {
245 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i] * recvext,
246 recvcounts[i], recvtype, i, system_tag, comm);
249 XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
252 /* Now create all sends */
253 for (int i = 0; i < size; ++i) {
255 requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + senddisps[i] * sendext,
256 sendcounts[i], sendtype, i, system_tag, comm);
259 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
262 /* Wait for them all. */
263 Request::startall(count, requests);
264 (*request)->set_nbc_requests(requests, count);
269 int Colls::ialltoallw(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype* sendtypes,
270 void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype* recvtypes, MPI_Comm comm, MPI_Request *request){
271 const int system_tag = COLL_TAG_ALLTOALLV;
274 int rank = comm->rank();
275 int size = comm->size();
276 (*request) = new Request( nullptr, 0, MPI_BYTE,
277 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
278 /* Local copy from self */
279 int err = (sendcounts[rank]>0 && recvcounts[rank]) ? Datatype::copy(static_cast<char *>(sendbuf) + senddisps[rank], sendcounts[rank], sendtypes[rank],
280 static_cast<char *>(recvbuf) + recvdisps[rank], recvcounts[rank], recvtypes[rank]): MPI_SUCCESS;
281 if (err == MPI_SUCCESS && size > 1) {
282 /* Initiate all send/recv to/from others. */
283 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
285 /* Create all receives that will be posted first */
286 for (int i = 0; i < size; ++i) {
288 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i],
289 recvcounts[i], recvtypes[i], i, system_tag, comm);
292 XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
295 /* Now create all sends */
296 for (int i = 0; i < size; ++i) {
298 requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + senddisps[i] ,
299 sendcounts[i], sendtypes[i], i, system_tag, comm);
302 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
305 /* Wait for them all. */
306 Request::startall(count, requests);
307 (*request)->set_nbc_requests(requests, count);
312 int Colls::igather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
313 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
315 const int system_tag = COLL_TAG_GATHER;
317 MPI_Aint recvext = 0;
319 int rank = comm->rank();
320 int size = comm->size();
321 (*request) = new Request( nullptr, 0, MPI_BYTE,
322 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
324 // Send buffer to root
325 MPI_Request* requests = new MPI_Request[1];
326 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
327 (*request)->set_nbc_requests(requests, 1);
329 recvtype->extent(&lb, &recvext);
330 // Local copy from root
331 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext,
332 recvcount, recvtype);
333 // Receive buffers from senders
334 MPI_Request* requests = new MPI_Request[size - 1];
336 for (int src = 0; src < size; src++) {
338 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
339 src, system_tag, comm);
343 // Wait for completion of irecv's.
344 Request::startall(size - 1, requests);
345 (*request)->set_nbc_requests(requests, size - 1);
350 int Colls::igatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
351 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
353 int system_tag = COLL_TAG_GATHERV;
355 MPI_Aint recvext = 0;
357 int rank = comm->rank();
358 int size = comm->size();
359 (*request) = new Request( nullptr, 0, MPI_BYTE,
360 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
362 // Send buffer to root
363 MPI_Request* requests = new MPI_Request[1];
364 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
365 (*request)->set_nbc_requests(requests, 1);
367 recvtype->extent(&lb, &recvext);
368 // Local copy from root
369 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
370 recvcounts[root], recvtype);
371 // Receive buffers from senders
372 MPI_Request* requests = new MPI_Request[size - 1];
374 for (int src = 0; src < size; src++) {
376 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
377 recvcounts[src], recvtype, src, system_tag, comm);
381 // Wait for completion of irecv's.
382 Request::startall(size - 1, requests);
383 (*request)->set_nbc_requests(requests, size - 1);
387 int Colls::iscatterv(void *sendbuf, int *sendcounts, int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount,
388 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
390 int system_tag = COLL_TAG_SCATTERV;
392 MPI_Aint sendext = 0;
394 int rank = comm->rank();
395 int size = comm->size();
396 (*request) = new Request( nullptr, 0, MPI_BYTE,
397 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
399 // Recv buffer from root
400 MPI_Request* requests = new MPI_Request[1];
401 requests[0]=Request::irecv(recvbuf, recvcount, recvtype, root, system_tag, comm);
402 (*request)->set_nbc_requests(requests, 1);
404 sendtype->extent(&lb, &sendext);
405 // Local copy from root
406 if(recvbuf!=MPI_IN_PLACE){
407 Datatype::copy(static_cast<char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
408 sendtype, recvbuf, recvcount, recvtype);
410 // Send buffers to receivers
411 MPI_Request *requests = new MPI_Request[size - 1];
413 for (int dst = 0; dst < size; dst++) {
415 requests[index] = Request::isend_init(static_cast<char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
416 sendtype, dst, system_tag, comm);
420 // Wait for completion of isend's.
421 Request::startall(size - 1, requests);
422 (*request)->set_nbc_requests(requests, size - 1);
427 int Colls::ireduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
428 MPI_Comm comm, MPI_Request* request)
430 const int system_tag = COLL_TAG_REDUCE;
432 MPI_Aint dataext = 0;
434 char* sendtmpbuf = static_cast<char *>(sendbuf);
436 int rank = comm->rank();
437 int size = comm->size();
442 if( sendbuf == MPI_IN_PLACE ) {
443 sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
444 Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
448 (*request) = new Request( recvbuf, count, datatype,
449 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
452 (*request) = new Request( nullptr, count, datatype,
453 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT);
456 // Send buffer to root
457 MPI_Request* requests = new MPI_Request[1];
458 requests[0]=Request::isend(sendtmpbuf, count, datatype, root, system_tag, comm);
459 (*request)->set_nbc_requests(requests, 1);
461 datatype->extent(&lb, &dataext);
462 // Local copy from root
463 if (sendtmpbuf != nullptr && recvbuf != nullptr)
464 Datatype::copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
465 // Receive buffers from senders
466 MPI_Request *requests = new MPI_Request[size - 1];
468 for (int src = 0; src < size; src++) {
471 Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, src, system_tag, comm);
475 // Wait for completion of irecv's.
476 Request::startall(size - 1, requests);
477 (*request)->set_nbc_requests(requests, size - 1);
479 if( sendbuf == MPI_IN_PLACE ) {
480 smpi_free_tmp_buffer(sendtmpbuf);
485 int Colls::iallreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
486 MPI_Op op, MPI_Comm comm, MPI_Request* request)
489 const int system_tag = COLL_TAG_ALLREDUCE;
491 MPI_Aint dataext = 0;
493 int rank = comm->rank();
494 int size = comm->size();
495 (*request) = new Request( recvbuf, count, datatype,
496 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
497 // FIXME: check for errors
498 datatype->extent(&lb, &dataext);
499 // Local copy from self
500 Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
501 // Send/Recv buffers to/from others;
502 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
504 for (int other = 0; other < size; other++) {
506 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag,comm);
508 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
509 other, system_tag, comm);
513 Request::startall(2 * (size - 1), requests);
514 (*request)->set_nbc_requests(requests, 2 * (size - 1));
518 int Colls::iscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
520 int system_tag = -888;
522 MPI_Aint dataext = 0;
524 int rank = comm->rank();
525 int size = comm->size();
526 (*request) = new Request( recvbuf, count, datatype,
527 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
528 datatype->extent(&lb, &dataext);
530 // Local copy from self
531 Datatype::copy(sendbuf, count, datatype, recvbuf, count, datatype);
533 // Send/Recv buffers to/from others
534 MPI_Request *requests = new MPI_Request[size - 1];
536 for (int other = 0; other < rank; other++) {
537 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm);
540 for (int other = rank + 1; other < size; other++) {
541 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag, comm);
544 // Wait for completion of all comms.
545 Request::startall(size - 1, requests);
546 (*request)->set_nbc_requests(requests, size - 1);
550 int Colls::iexscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
552 int system_tag = -888;
554 MPI_Aint dataext = 0;
555 int rank = comm->rank();
556 int size = comm->size();
557 (*request) = new Request( recvbuf, count, datatype,
558 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
559 datatype->extent(&lb, &dataext);
561 memset(recvbuf, 0, count*dataext);
563 // Send/Recv buffers to/from others
564 MPI_Request *requests = new MPI_Request[size - 1];
566 for (int other = 0; other < rank; other++) {
567 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype, other, system_tag, comm);
570 for (int other = rank + 1; other < size; other++) {
571 requests[index] = Request::isend_init(sendbuf, count, datatype, other, system_tag, comm);
574 // Wait for completion of all comms.
575 Request::startall(size - 1, requests);
576 (*request)->set_nbc_requests(requests, size - 1);
580 int Colls::ireduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
581 MPI_Comm comm, MPI_Request* request){
582 //Version where each process performs the reduce for its own part. Alltoall pattern for comms.
583 const int system_tag = COLL_TAG_REDUCE_SCATTER;
585 MPI_Aint dataext = 0;
587 int rank = comm->rank();
588 int size = comm->size();
589 int count=recvcounts[rank];
590 (*request) = new Request( recvbuf, count, datatype,
591 rank,rank, system_tag, comm, MPI_REQ_PERSISTENT, op);
592 datatype->extent(&lb, &dataext);
594 // Send/Recv buffers to/from others;
595 MPI_Request* requests = new MPI_Request[2 * (size - 1)];
598 for (int other = 0; other < size; other++) {
600 requests[index] = Request::isend_init(static_cast<char *>(sendbuf) + recvdisp * dataext, recvcounts[other], datatype, other, system_tag,comm);
601 XBT_VERB("sending with recvdisp %d", recvdisp);
603 requests[index] = Request::irecv_init(smpi_get_tmp_sendbuffer(count * dataext), count, datatype,
604 other, system_tag, comm);
607 Datatype::copy(static_cast<char *>(sendbuf) + recvdisp * dataext, count, datatype, recvbuf, count, datatype);
609 recvdisp+=recvcounts[other];
611 Request::startall(2 * (size - 1), requests);
612 (*request)->set_nbc_requests(requests, 2 * (size - 1));