Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the OOP of kernel::profile
[simgrid.git] / src / smpi / colls / allgather / allgather-rdb.cpp
1 /* Copyright (c) 2013-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "../colls_private.hpp"
7 #include "smpi_status.hpp"
8
9 namespace simgrid{
10 namespace smpi{
11
12 int
13 Coll_allgather_rdb::allgather(void *sbuf, int send_count,
14                               MPI_Datatype send_type, void *rbuf,
15                               int recv_count, MPI_Datatype recv_type,
16                               MPI_Comm comm)
17 {
18   // MPI variables
19   MPI_Status status;
20   MPI_Aint send_chunk, recv_chunk;
21
22   // local int variables
23   unsigned int i, j, k, dst, send_offset, recv_offset, tree_root;
24   int dst_tree_root, rank_tree_root, last_recv_count = 0, num_procs_completed;
25   int offset, tmp_mask;
26   int tag = COLL_TAG_ALLGATHER;
27   unsigned int mask = 1;
28   int success = 0;
29   int curr_count = recv_count;
30
31   // local string variables
32   char *send_ptr = (char *) sbuf;
33   char *recv_ptr = (char *) rbuf;
34
35   // get size of the communicator, followed by rank
36   unsigned int num_procs = comm->size();
37   unsigned int rank = comm->rank();
38
39   // get size of single element's type for send buffer and recv buffer
40   send_chunk = send_type->get_extent();
41   recv_chunk = recv_type->get_extent();
42
43   // multiply size of each element by number of elements to send or recv
44   send_chunk *= send_count;
45   recv_chunk *= recv_count;
46
47   // perform a local copy
48   Request::sendrecv(send_ptr, send_count, send_type, rank, tag,
49                recv_ptr + rank * recv_chunk, recv_count, recv_type, rank, tag,
50                comm, &status);
51
52   i = 0;
53   while (mask < num_procs) {
54     dst = rank ^ mask;
55     dst_tree_root = dst >> i;
56     dst_tree_root <<= i;
57     rank_tree_root = rank >> i;
58     rank_tree_root <<= i;
59     send_offset = rank_tree_root * send_chunk;
60     recv_offset = dst_tree_root * recv_chunk;
61
62     if (dst < num_procs) {
63       Request::sendrecv(recv_ptr + send_offset, curr_count, send_type, dst,
64                    tag, recv_ptr + recv_offset, mask * recv_count,
65                    recv_type, dst, tag, comm, &status);
66       last_recv_count = Status::get_count(&status, recv_type);
67       curr_count += last_recv_count;
68     }
69
70     if (dst_tree_root + mask > num_procs) {
71       num_procs_completed = num_procs - rank_tree_root - mask;
72       /* num_procs_completed is the number of processes in this
73          subtree that have all the data. Send data to others
74          in a tree fashion. First find root of current tree
75          that is being divided into two. k is the number of
76          least-significant bits in this process's rank that
77          must be zeroed out to find the rank of the root */
78
79       j = mask;
80       k = 0;
81       while (j) {
82         j >>= 1;
83         k++;
84       }
85       k--;
86
87       offset = recv_chunk * (rank_tree_root + mask);
88       tmp_mask = mask >> 1;
89
90       while (tmp_mask) {
91         dst = rank ^ tmp_mask;
92
93         tree_root = rank >> k;
94         tree_root <<= k;
95
96         /* send only if this proc has data and destination
97            doesn't have data. at any step, multiple processes
98            can send if they have the data */
99         if ((dst > rank)
100             && (rank < tree_root + num_procs_completed)
101             && (dst >= tree_root + num_procs_completed)) {
102           Request::send(recv_ptr + offset, last_recv_count, recv_type, dst,
103                    tag, comm);
104
105           /* last_recv_cnt was set in the previous
106              receive. that's the amount of data to be
107              sent now. */
108         }
109         /* recv only if this proc. doesn't have data and sender
110            has data */
111         else if ((dst < rank)
112                  && (dst < tree_root + num_procs_completed)
113                  && (rank >= tree_root + num_procs_completed)) {
114           Request::recv(recv_ptr + offset,
115                    recv_count * num_procs_completed,
116                    recv_type, dst, tag, comm, &status);
117           // num_procs_completed is also equal to the no. of processes
118           // whose data we don't have
119           last_recv_count = Status::get_count(&status, recv_type);
120           curr_count += last_recv_count;
121         }
122         tmp_mask >>= 1;
123         k--;
124       }
125     }
126
127     mask <<= 1;
128     i++;
129   }
130
131   return success;
132 }
133
134
135 }
136 }