1 /* Copyright (c) 2013-2019. The SimGrid Team. All rights reserved. */
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. */
6 /* Short or medium size message and power-of-two no. of processes. Use
7 * recursive doubling algorithm */
9 #include "../colls_private.hpp"
10 #include "smpi_status.hpp"
16 int Coll_allgatherv_mpich_rdb::allgatherv (
19 MPI_Datatype sendtype,
21 const int *recvcounts,
23 MPI_Datatype recvtype,
28 MPI_Aint recvtype_extent, recvtype_true_extent, recvtype_true_lb;
29 unsigned int curr_cnt, dst, total_count;
30 unsigned int mask, dst_tree_root, my_tree_root, position,
31 send_offset, recv_offset, last_recv_cnt=0, nprocs_completed, k,
32 offset, tmp_mask, tree_root;
34 unsigned int comm_size = comm->size();
35 unsigned int rank = comm->rank();
38 for (i=0; i<comm_size; i++)
39 total_count += recvcounts[i];
44 recvtype_extent=recvtype->get_extent();
46 /* need to receive contiguously into tmp_buf because
47 displs could make the recvbuf noncontiguous */
49 recvtype->extent(&recvtype_true_lb, &recvtype_true_extent);
51 unsigned char* tmp_buf_rl = smpi_get_tmp_sendbuffer(total_count * std::max(recvtype_true_extent, recvtype_extent));
53 /* adjust for potential negative lower bound in datatype */
54 unsigned char* tmp_buf = tmp_buf_rl - recvtype_true_lb;
56 /* copy local data into right location in tmp_buf */
58 for (i=0; i<rank; i++)
59 position += recvcounts[i];
60 if (sendbuf != MPI_IN_PLACE)
62 Datatype::copy(sendbuf, sendcount, sendtype, tmp_buf + position * recvtype_extent, recvcounts[rank], recvtype);
66 /* if in_place specified, local data is found in recvbuf */
67 Datatype::copy(static_cast<char*>(recvbuf) + displs[rank] * recvtype_extent, recvcounts[rank], recvtype,
68 tmp_buf + position * recvtype_extent, recvcounts[rank], recvtype);
70 curr_cnt = recvcounts[rank];
74 while (mask < comm_size) {
77 /* find offset into send and recv buffers. zero out
78 the least significant "i" bits of rank and dst to
79 find root of src and dst subtrees. Use ranks of
80 roots as index to send from and recv into buffer */
82 dst_tree_root = dst >> i;
85 my_tree_root = rank >> i;
88 if (dst < comm_size) {
90 for (j=0; j<my_tree_root; j++)
91 send_offset += recvcounts[j];
94 for (j=0; j<dst_tree_root; j++)
95 recv_offset += recvcounts[j];
97 Request::sendrecv(tmp_buf + send_offset * recvtype_extent, curr_cnt, recvtype, dst, COLL_TAG_ALLGATHERV,
98 tmp_buf + recv_offset * recvtype_extent, total_count - recv_offset, recvtype, dst,
99 COLL_TAG_ALLGATHERV, comm, &status);
100 /* for convenience, recv is posted for a bigger amount
102 last_recv_cnt=Status::get_count(&status, recvtype);
103 curr_cnt += last_recv_cnt;
106 /* if some processes in this process's subtree in this step
107 did not have any destination process to communicate with
108 because of non-power-of-two, we need to send them the
109 data that they would normally have received from those
110 processes. That is, the haves in this subtree must send to
111 the havenots. We use a logarithmic
112 recursive-halfing algorithm for this. */
114 /* This part of the code will not currently be
115 executed because we are not using recursive
116 doubling for non power of two. Mark it as experimental
117 so that it doesn't show up as red in the coverage
120 /* --BEGIN EXPERIMENTAL-- */
121 if (dst_tree_root + mask > comm_size) {
122 nprocs_completed = comm_size - my_tree_root - mask;
123 /* nprocs_completed is the number of processes in this
124 subtree that have all the data. Send data to others
125 in a tree fashion. First find root of current tree
126 that is being divided into two. k is the number of
127 least-significant bits in this process's rank that
128 must be zeroed out to find the rank of the root */
137 tmp_mask = mask >> 1;
140 dst = rank ^ tmp_mask;
142 tree_root = rank >> k;
145 /* send only if this proc has data and destination
146 doesn't have data. at any step, multiple processes
147 can send if they have the data */
149 (rank < tree_root + nprocs_completed)
150 && (dst >= tree_root + nprocs_completed)) {
153 for (j=0; j<(my_tree_root+mask); j++)
154 offset += recvcounts[j];
155 offset *= recvtype_extent;
157 Request::send(tmp_buf + offset, last_recv_cnt, recvtype, dst, COLL_TAG_ALLGATHERV, comm);
158 /* last_recv_cnt was set in the previous
159 receive. that's the amount of data to be
162 /* recv only if this proc. doesn't have data and sender
164 else if ((dst < rank) &&
165 (dst < tree_root + nprocs_completed) &&
166 (rank >= tree_root + nprocs_completed)) {
169 for (j=0; j<(my_tree_root+mask); j++)
170 offset += recvcounts[j];
172 Request::recv(tmp_buf + offset * recvtype_extent, total_count - offset, recvtype, dst, COLL_TAG_ALLGATHERV,
174 /* for convenience, recv is posted for a
175 bigger amount than will be sent */
176 last_recv_cnt=Status::get_count(&status, recvtype);
177 curr_cnt += last_recv_cnt;
183 /* --END EXPERIMENTAL-- */
189 /* copy data from tmp_buf to recvbuf */
191 for (j=0; j<comm_size; j++) {
192 if ((sendbuf != MPI_IN_PLACE) || (j != rank)) {
193 /* not necessary to copy if in_place and
194 j==rank. otherwise copy. */
195 Datatype::copy(tmp_buf + position * recvtype_extent, recvcounts[j], recvtype,
196 static_cast<char*>(recvbuf) + displs[j] * recvtype_extent, recvcounts[j], recvtype);
198 position += recvcounts[j];
201 smpi_free_tmp_buffer(tmp_buf_rl);