Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / smpi / colls / alltoall / alltoall-basic-linear.cpp
1 /* Copyright (c) 2013-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "../colls_private.hpp"
8
9 /*Naive and simple basic alltoall implementation. */
10
11
12 namespace simgrid{
13 namespace smpi{
14
15
16 int alltoall__basic_linear(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
17                            void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
18 {
19   int system_tag = COLL_TAG_ALLTOALL;
20   int i;
21   int count;
22   MPI_Aint lb = 0, sendext = 0, recvext = 0;
23
24   /* Initialize. */
25   int rank = comm->rank();
26   int size = comm->size();
27   XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
28   sendtype->extent(&lb, &sendext);
29   recvtype->extent(&lb, &recvext);
30   /* simple optimization */
31   int err = Datatype::copy(static_cast<const char *>(sendbuf) + rank * sendcount * sendext, sendcount, sendtype,
32                                static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
33   if (err == MPI_SUCCESS && size > 1) {
34     /* Initiate all send/recv to/from others. */
35     auto* requests = new MPI_Request[2 * (size - 1)];
36     /* Post all receives first -- a simple optimization */
37     count = 0;
38     for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
39       requests[count] = Request::irecv_init(static_cast<char *>(recvbuf) + i * recvcount * recvext, recvcount,
40                                         recvtype, i, system_tag, comm);
41       count++;
42     }
43     /* Now post all sends in reverse order
44      *   - We would like to minimize the search time through message queue
45      *     when messages actually arrive in the order in which they were posted.
46      * TODO: check the previous assertion
47      */
48     for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
49       requests[count] = Request::isend_init(static_cast<const char *>(sendbuf) + i * sendcount * sendext, sendcount,
50                                         sendtype, i, system_tag, comm);
51       count++;
52     }
53     /* Wait for them all. */
54     Request::startall(count, requests);
55     XBT_DEBUG("<%d> wait for %d requests", rank, count);
56     Request::waitall(count, requests, MPI_STATUS_IGNORE);
57     for(i = 0; i < count; i++) {
58       if(requests[i]!=MPI_REQUEST_NULL)
59         Request::unref(&requests[i]);
60     }
61     delete[] requests;
62   }
63   return err;
64 }
65
66 }
67 }