1 /* Copyright (c) 2013-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* Short or medium size message and power-of-two no. of processes. Use
8 * recursive doubling algorithm */
9 #include "../colls_private.h"
14 int Coll_allgatherv_mpich_rdb::allgatherv (
17 MPI_Datatype sendtype,
21 MPI_Datatype recvtype,
26 MPI_Aint recvtype_extent, recvtype_true_extent, recvtype_true_lb;
27 unsigned int curr_cnt, dst, total_count;
28 void *tmp_buf, *tmp_buf_rl;
29 unsigned int mask, dst_tree_root, my_tree_root, position,
30 send_offset, recv_offset, last_recv_cnt=0, nprocs_completed, k,
31 offset, tmp_mask, tree_root;
33 unsigned int comm_size = comm->size();
34 unsigned int rank = comm->rank();
37 for (i=0; i<comm_size; i++)
38 total_count += recvcounts[i];
43 recvtype_extent=recvtype->get_extent();
45 /* need to receive contiguously into tmp_buf because
46 displs could make the recvbuf noncontiguous */
48 recvtype->extent(&recvtype_true_lb, &recvtype_true_extent);
50 tmp_buf_rl= (void*)smpi_get_tmp_sendbuffer(total_count*(MAX(recvtype_true_extent,recvtype_extent)));
52 /* adjust for potential negative lower bound in datatype */
53 tmp_buf = (void *)((char*)tmp_buf_rl - recvtype_true_lb);
55 /* copy local data into right location in tmp_buf */
57 for (i=0; i<rank; i++)
58 position += recvcounts[i];
59 if (sendbuf != MPI_IN_PLACE)
61 Datatype::copy(sendbuf, sendcount, sendtype,
62 ((char *)tmp_buf + position*
64 recvcounts[rank], recvtype);
68 /* if in_place specified, local data is found in recvbuf */
69 Datatype::copy(((char *)recvbuf +
70 displs[rank]*recvtype_extent),
71 recvcounts[rank], recvtype,
72 ((char *)tmp_buf + position*
74 recvcounts[rank], recvtype);
76 curr_cnt = recvcounts[rank];
80 while (mask < comm_size) {
83 /* find offset into send and recv buffers. zero out
84 the least significant "i" bits of rank and dst to
85 find root of src and dst subtrees. Use ranks of
86 roots as index to send from and recv into buffer */
88 dst_tree_root = dst >> i;
91 my_tree_root = rank >> i;
94 if (dst < comm_size) {
96 for (j=0; j<my_tree_root; j++)
97 send_offset += recvcounts[j];
100 for (j=0; j<dst_tree_root; j++)
101 recv_offset += recvcounts[j];
103 Request::sendrecv(((char *)tmp_buf + send_offset * recvtype_extent),
104 curr_cnt, recvtype, dst,
106 ((char *)tmp_buf + recv_offset * recvtype_extent),
107 total_count - recv_offset, recvtype, dst,
110 /* for convenience, recv is posted for a bigger amount
112 last_recv_cnt=smpi_mpi_get_count(&status, recvtype);
113 curr_cnt += last_recv_cnt;
116 /* if some processes in this process's subtree in this step
117 did not have any destination process to communicate with
118 because of non-power-of-two, we need to send them the
119 data that they would normally have received from those
120 processes. That is, the haves in this subtree must send to
121 the havenots. We use a logarithmic
122 recursive-halfing algorithm for this. */
124 /* This part of the code will not currently be
125 executed because we are not using recursive
126 doubling for non power of two. Mark it as experimental
127 so that it doesn't show up as red in the coverage
130 /* --BEGIN EXPERIMENTAL-- */
131 if (dst_tree_root + mask > comm_size) {
132 nprocs_completed = comm_size - my_tree_root - mask;
133 /* nprocs_completed is the number of processes in this
134 subtree that have all the data. Send data to others
135 in a tree fashion. First find root of current tree
136 that is being divided into two. k is the number of
137 least-significant bits in this process's rank that
138 must be zeroed out to find the rank of the root */
147 tmp_mask = mask >> 1;
150 dst = rank ^ tmp_mask;
152 tree_root = rank >> k;
155 /* send only if this proc has data and destination
156 doesn't have data. at any step, multiple processes
157 can send if they have the data */
159 (rank < tree_root + nprocs_completed)
160 && (dst >= tree_root + nprocs_completed)) {
163 for (j=0; j<(my_tree_root+mask); j++)
164 offset += recvcounts[j];
165 offset *= recvtype_extent;
167 Request::send(((char *)tmp_buf + offset),
170 COLL_TAG_ALLGATHERV, comm);
171 /* last_recv_cnt was set in the previous
172 receive. that's the amount of data to be
175 /* recv only if this proc. doesn't have data and sender
177 else if ((dst < rank) &&
178 (dst < tree_root + nprocs_completed) &&
179 (rank >= tree_root + nprocs_completed)) {
182 for (j=0; j<(my_tree_root+mask); j++)
183 offset += recvcounts[j];
185 Request::recv(((char *)tmp_buf + offset * recvtype_extent),
186 total_count - offset, recvtype,
187 dst, COLL_TAG_ALLGATHERV,
189 /* for convenience, recv is posted for a
190 bigger amount than will be sent */
191 last_recv_cnt=smpi_mpi_get_count(&status, recvtype);
192 curr_cnt += last_recv_cnt;
198 /* --END EXPERIMENTAL-- */
204 /* copy data from tmp_buf to recvbuf */
206 for (j=0; j<comm_size; j++) {
207 if ((sendbuf != MPI_IN_PLACE) || (j != rank)) {
208 /* not necessary to copy if in_place and
209 j==rank. otherwise copy. */
210 Datatype::copy(((char *)tmp_buf + position*recvtype_extent),
211 recvcounts[j], recvtype,
212 ((char *)recvbuf + displs[j]*recvtype_extent),
213 recvcounts[j], recvtype);
215 position += recvcounts[j];
218 smpi_free_tmp_buffer(tmp_buf_rl);