Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a doc error about actors (Tutorial_algorithms)
[simgrid.git] / src / smpi / colls / allgather / allgather-NTSLR-NB.cpp
1 /* Copyright (c) 2013-2019. 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 namespace simgrid{
10 namespace smpi{
11
12
13 // Allgather-Non-Topoloty-Scecific-Logical-Ring algorithm
14 int
15 Coll_allgather_NTSLR_NB::allgather(const void *sbuf, int scount, MPI_Datatype stype,
16                                    void *rbuf, int rcount, MPI_Datatype rtype,
17                                    MPI_Comm comm)
18 {
19   MPI_Aint rextent, sextent;
20   MPI_Status status, status2;
21   int i, to, from, rank, size;
22   int send_offset, recv_offset;
23   int tag = COLL_TAG_ALLGATHER;
24
25   rank = comm->rank();
26   size = comm->size();
27   rextent = rtype->get_extent();
28   sextent = stype->get_extent();
29   MPI_Request* rrequest_array = new MPI_Request[size];
30   MPI_Request* srequest_array = new MPI_Request[size];
31
32   // irregular case use default MPI fucntions
33   if (scount * sextent != rcount * rextent) {
34     XBT_WARN("MPI_allgather_NTSLR_NB use default MPI_allgather.");
35     Coll_allgather_default::allgather(sbuf, scount, stype, rbuf, rcount, rtype, comm);
36     return MPI_SUCCESS;
37   }
38
39   // topo non-specific
40   to = (rank + 1) % size;
41   from = (rank + size - 1) % size;
42
43   //copy a single segment from sbuf to rbuf
44   send_offset = rank * scount * sextent;
45
46   Request::sendrecv(sbuf, scount, stype, rank, tag,
47                (char *)rbuf + send_offset, rcount, rtype, rank, tag, comm, &status);
48
49
50   //start sending logical ring message
51   int increment = scount * sextent;
52
53   //post all irecv first
54   for (i = 0; i < size - 1; i++) {
55     recv_offset = ((rank - i - 1 + size) % size) * increment;
56     rrequest_array[i] = Request::irecv((char *)rbuf + recv_offset, rcount, rtype, from, tag + i, comm);
57   }
58
59
60   for (i = 0; i < size - 1; i++) {
61     send_offset = ((rank - i + size) % size) * increment;
62     srequest_array[i] = Request::isend((char *)rbuf + send_offset, scount, stype, to, tag + i, comm);
63     Request::wait(&rrequest_array[i], &status);
64     Request::wait(&srequest_array[i], &status2);
65   }
66
67   delete[] rrequest_array;
68   delete[] srequest_array;
69
70   return MPI_SUCCESS;
71 }
72
73 }
74 }