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)
18 int size = comm->size();
19 int rank = comm->rank();
20 MPI_Request* requests;
21 (*request) = new Request( nullptr, 0, MPI_BYTE,
22 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
24 requests = new MPI_Request[2];
25 requests[0] = Request::isend (nullptr, 0, MPI_BYTE, 0,
28 requests[1] = Request::irecv (nullptr, 0, MPI_BYTE, 0,
31 (*request)->set_nbc_requests(requests, 2);
34 requests = new MPI_Request[(size-1)*2];
35 for (i = 1; i < 2*size-1; i+=2) {
36 requests[i-1] = Request::irecv(nullptr, 0, MPI_BYTE, MPI_ANY_SOURCE,
37 COLL_TAG_BARRIER, comm
39 requests[i] = Request::isend(nullptr, 0, MPI_BYTE, (i+1)/2,
44 (*request)->set_nbc_requests(requests, 2*(size-1));
49 int Colls::ibcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, MPI_Request* request)
52 int size = comm->size();
53 int rank = comm->rank();
54 MPI_Request* requests;
55 (*request) = new Request( nullptr, 0, MPI_BYTE,
56 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
58 requests = new MPI_Request[1];
59 requests[0] = Request::irecv (buf, count, datatype, root,
62 (*request)->set_nbc_requests(requests, 1);
65 requests = new MPI_Request[size-1];
67 for (i = 0; i < size; i++) {
69 requests[n] = Request::isend(buf, count, datatype, i,
76 (*request)->set_nbc_requests(requests, size-1);
81 int Colls::iallgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
82 void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
85 const int system_tag = COLL_TAG_ALLGATHER;
88 MPI_Request *requests;
90 int rank = comm->rank();
91 int size = comm->size();
92 (*request) = new Request( nullptr, 0, MPI_BYTE,
93 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
94 // FIXME: check for errors
95 recvtype->extent(&lb, &recvext);
96 // Local copy from self
97 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount,
99 // Send/Recv buffers to/from others;
100 requests = new MPI_Request[2 * (size - 1)];
102 for (int other = 0; other < size; other++) {
104 requests[index] = Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag,comm);
106 requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + other * recvcount * recvext, recvcount, recvtype,
107 other, system_tag, comm);
111 Request::startall(2 * (size - 1), requests);
112 (*request)->set_nbc_requests(requests, 2 * (size - 1));
116 int Colls::iscatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
117 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
119 const int system_tag = COLL_TAG_SCATTER;
121 MPI_Aint sendext = 0;
122 MPI_Request *requests;
124 int rank = comm->rank();
125 int size = comm->size();
126 (*request) = new Request( nullptr, 0, MPI_BYTE,
127 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
129 requests = new MPI_Request[1];
130 // Recv buffer from root
131 requests[0] = Request::irecv(recvbuf, recvcount, recvtype, root, system_tag, comm);
132 (*request)->set_nbc_requests(requests, 1);
134 sendtype->extent(&lb, &sendext);
135 // Local copy from root
136 if(recvbuf!=MPI_IN_PLACE){
137 Datatype::copy(static_cast<char *>(sendbuf) + root * sendcount * sendext,
138 sendcount, sendtype, recvbuf, recvcount, recvtype);
140 // Send buffers to receivers
141 requests = new MPI_Request[size - 1];
143 for(int dst = 0; dst < size; dst++) {
145 requests[index] = Request::isend_init(static_cast<char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype,
146 dst, system_tag, comm);
150 // Wait for completion of isend's.
151 Request::startall(size - 1, requests);
152 (*request)->set_nbc_requests(requests, size - 1);
157 int Colls::iallgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
158 int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
160 const int system_tag = COLL_TAG_ALLGATHERV;
162 MPI_Aint recvext = 0;
164 int rank = comm->rank();
165 int size = comm->size();
166 (*request) = new Request( nullptr, 0, MPI_BYTE,
167 rank,rank, COLL_TAG_BARRIER, comm, MPI_REQ_PERSISTENT);
168 recvtype->extent(&lb, &recvext);
169 // Local copy from self
170 Datatype::copy(sendbuf, sendcount, sendtype,
171 static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
172 // Send buffers to others;
173 MPI_Request *requests = new MPI_Request[2 * (size - 1)];
175 for (int other = 0; other < size; other++) {
178 Request::isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
180 requests[index] = Request::irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
181 recvtype, other, system_tag, comm);
185 // Wait for completion of all comms.
186 Request::startall(2 * (size - 1), requests);
187 (*request)->set_nbc_requests(requests, 2 * (size - 1));
191 int Colls::ialltoall( void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request){
192 int system_tag = COLL_TAG_ALLTOALL;
195 MPI_Aint lb = 0, sendext = 0, recvext = 0;
196 MPI_Request *requests;
199 int rank = comm->rank();
200 int size = comm->size();
201 (*request) = new Request( nullptr, 0, MPI_BYTE,
202 rank,rank, COLL_TAG_ALLTOALL, comm, MPI_REQ_PERSISTENT);
203 sendtype->extent(&lb, &sendext);
204 recvtype->extent(&lb, &recvext);
205 /* simple optimization */
206 int err = Datatype::copy(static_cast<char *>(sendbuf) + rank * sendcount * sendext, sendcount, sendtype,
207 static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
208 if (err == MPI_SUCCESS && size > 1) {
209 /* Initiate all send/recv to/from others. */
210 requests = new MPI_Request[2 * (size - 1)];
211 /* Post all receives first -- a simple optimization */
213 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
214 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + i * recvcount * recvext, recvcount,
215 recvtype, i, system_tag, comm);
218 /* Now post all sends in reverse order
219 * - We would like to minimize the search time through message queue
220 * when messages actually arrive in the order in which they were posted.
221 * TODO: check the previous assertion
223 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
224 requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + i * sendcount * sendext, sendcount,
225 sendtype, i, system_tag, comm);
228 /* Wait for them all. */
229 Request::startall(count, requests);
230 (*request)->set_nbc_requests(requests, count);
235 int Colls::ialltoallv(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype sendtype,
236 void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request *request){
237 const int system_tag = COLL_TAG_ALLTOALLV;
239 MPI_Aint sendext = 0;
240 MPI_Aint recvext = 0;
241 MPI_Request *requests;
244 int rank = comm->rank();
245 int size = comm->size();
246 (*request) = new Request( nullptr, 0, MPI_BYTE,
247 rank,rank, COLL_TAG_ALLTOALLV, comm, MPI_REQ_PERSISTENT);
248 sendtype->extent(&lb, &sendext);
249 recvtype->extent(&lb, &recvext);
250 /* Local copy from self */
251 int err = Datatype::copy(static_cast<char *>(sendbuf) + senddisps[rank] * sendext, sendcounts[rank], sendtype,
252 static_cast<char *>(recvbuf) + recvdisps[rank] * recvext, recvcounts[rank], recvtype);
253 if (err == MPI_SUCCESS && size > 1) {
254 /* Initiate all send/recv to/from others. */
255 requests = new MPI_Request[2 * (size - 1)];
257 /* Create all receives that will be posted first */
258 for (int i = 0; i < size; ++i) {
259 if (i != rank && recvcounts[i] != 0) {
260 requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + recvdisps[i] * recvext,
261 recvcounts[i], recvtype, i, system_tag, comm);
264 XBT_DEBUG("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
267 /* Now create all sends */
268 for (int i = 0; i < size; ++i) {
269 if (i != rank && sendcounts[i] != 0) {
270 requests[count] = Request::isend_init(static_cast<char *>(sendbuf) + senddisps[i] * sendext,
271 sendcounts[i], sendtype, i, system_tag, comm);
274 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
277 /* Wait for them all. */
278 Request::startall(count, requests);
279 (*request)->set_nbc_requests(requests, count);
284 int Colls::igather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
285 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
287 const int system_tag = COLL_TAG_GATHER;
289 MPI_Aint recvext = 0;
290 MPI_Request *requests;
292 int rank = comm->rank();
293 int size = comm->size();
294 (*request) = new Request( nullptr, 0, MPI_BYTE,
295 rank,rank, COLL_TAG_GATHER, comm, MPI_REQ_PERSISTENT);
297 // Send buffer to root
298 requests = new MPI_Request[1];
299 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
300 (*request)->set_nbc_requests(requests, 1);
302 recvtype->extent(&lb, &recvext);
303 // Local copy from root
304 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext,
305 recvcount, recvtype);
306 // Receive buffers from senders
307 requests = new MPI_Request[size - 1];
309 for (int src = 0; src < size; src++) {
311 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
312 src, system_tag, comm);
316 // Wait for completion of irecv's.
317 Request::startall(size - 1, requests);
318 (*request)->set_nbc_requests(requests, size - 1);
323 int Colls::igatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
324 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
326 int system_tag = COLL_TAG_GATHERV;
328 MPI_Aint recvext = 0;
329 MPI_Request *requests;
331 int rank = comm->rank();
332 int size = comm->size();
333 (*request) = new Request( nullptr, 0, MPI_BYTE,
334 rank,rank, COLL_TAG_GATHERV, comm, MPI_REQ_PERSISTENT);
336 // Send buffer to root
337 requests = new MPI_Request[1];
338 requests[0]=Request::isend(sendbuf, sendcount, sendtype, root, system_tag, comm);
339 (*request)->set_nbc_requests(requests, 1);
341 recvtype->extent(&lb, &recvext);
342 // Local copy from root
343 Datatype::copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
344 recvcounts[root], recvtype);
345 // Receive buffers from senders
346 requests = new MPI_Request[size - 1];
348 for (int src = 0; src < size; src++) {
350 requests[index] = Request::irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
351 recvcounts[src], recvtype, src, system_tag, comm);
355 // Wait for completion of irecv's.
356 Request::startall(size - 1, requests);
357 (*request)->set_nbc_requests(requests, size - 1);
361 int Colls::iscatterv(void *sendbuf, int *sendcounts, int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount,
362 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request *request)
364 int system_tag = COLL_TAG_SCATTERV;
366 MPI_Aint sendext = 0;
367 MPI_Request* requests;
369 int rank = comm->rank();
370 int size = comm->size();
371 (*request) = new Request( nullptr, 0, MPI_BYTE,
372 rank,rank, COLL_TAG_SCATTERV, comm, MPI_REQ_PERSISTENT);
374 // Recv buffer from root
375 requests = new MPI_Request[1];
376 requests[0]=Request::irecv(recvbuf, recvcount, recvtype, root, system_tag, comm);
377 (*request)->set_nbc_requests(requests, 1);
379 sendtype->extent(&lb, &sendext);
380 // Local copy from root
381 if(recvbuf!=MPI_IN_PLACE){
382 Datatype::copy(static_cast<char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
383 sendtype, recvbuf, recvcount, recvtype);
385 // Send buffers to receivers
386 MPI_Request *requests = new MPI_Request[size - 1];
388 for (int dst = 0; dst < size; dst++) {
390 requests[index] = Request::isend_init(static_cast<char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
391 sendtype, dst, system_tag, comm);
395 // Wait for completion of isend's.
396 Request::startall(size - 1, requests);
397 (*request)->set_nbc_requests(requests, size - 1);