1 /* Short or medium size message and power-of-two no. of processes. Use
2 * recursive doubling algorithm */
3 #include "colls_private.h"
4 int smpi_coll_tuned_allgatherv_mpich_rdb (
11 MPI_Datatype recvtype,
14 int comm_size, rank, j, i;
16 MPI_Aint recvtype_extent, recvtype_true_extent, recvtype_true_lb;
17 int curr_cnt, dst, total_count;
19 int mask, dst_tree_root, my_tree_root, position,
20 send_offset, recv_offset, last_recv_cnt=0, nprocs_completed, k,
21 offset, tmp_mask, tree_root;
23 comm_size = smpi_comm_size(comm);
24 rank = smpi_comm_rank(comm);
27 for (i=0; i<comm_size; i++)
28 total_count += recvcounts[i];
30 if (total_count == 0) return MPI_ERR_COUNT;
32 recvtype_extent=smpi_datatype_get_extent( recvtype);
34 /* need to receive contiguously into tmp_buf because
35 displs could make the recvbuf noncontiguous */
37 smpi_datatype_extent(recvtype, &recvtype_true_lb, &recvtype_true_extent);
39 tmp_buf= (void*)xbt_malloc(total_count*(max(recvtype_true_extent,recvtype_extent)));
41 /* adjust for potential negative lower bound in datatype */
42 tmp_buf = (void *)((char*)tmp_buf - recvtype_true_lb);
44 /* copy local data into right location in tmp_buf */
46 for (i=0; i<rank; i++) position += recvcounts[i];
47 if (sendbuf != MPI_IN_PLACE)
49 smpi_datatype_copy(sendbuf, sendcount, sendtype,
50 ((char *)tmp_buf + position*
52 recvcounts[rank], recvtype);
56 /* if in_place specified, local data is found in recvbuf */
57 smpi_datatype_copy(((char *)recvbuf +
58 displs[rank]*recvtype_extent),
59 recvcounts[rank], recvtype,
60 ((char *)tmp_buf + position*
62 recvcounts[rank], recvtype);
64 curr_cnt = recvcounts[rank];
68 while (mask < comm_size) {
71 /* find offset into send and recv buffers. zero out
72 the least significant "i" bits of rank and dst to
73 find root of src and dst subtrees. Use ranks of
74 roots as index to send from and recv into buffer */
76 dst_tree_root = dst >> i;
79 my_tree_root = rank >> i;
82 if (dst < comm_size) {
84 for (j=0; j<my_tree_root; j++)
85 send_offset += recvcounts[j];
88 for (j=0; j<dst_tree_root; j++)
89 recv_offset += recvcounts[j];
91 smpi_mpi_sendrecv(((char *)tmp_buf + send_offset * recvtype_extent),
92 curr_cnt, recvtype, dst,
94 ((char *)tmp_buf + recv_offset * recvtype_extent),
95 total_count - recv_offset, recvtype, dst,
98 /* for convenience, recv is posted for a bigger amount
100 last_recv_cnt=smpi_mpi_get_count(&status, recvtype);
101 curr_cnt += last_recv_cnt;
104 /* if some processes in this process's subtree in this step
105 did not have any destination process to communicate with
106 because of non-power-of-two, we need to send them the
107 data that they would normally have received from those
108 processes. That is, the haves in this subtree must send to
109 the havenots. We use a logarithmic
110 recursive-halfing algorithm for this. */
112 /* This part of the code will not currently be
113 executed because we are not using recursive
114 doubling for non power of two. Mark it as experimental
115 so that it doesn't show up as red in the coverage
118 /* --BEGIN EXPERIMENTAL-- */
119 if (dst_tree_root + mask > comm_size) {
120 nprocs_completed = comm_size - my_tree_root - mask;
121 /* nprocs_completed is the number of processes in this
122 subtree that have all the data. Send data to others
123 in a tree fashion. First find root of current tree
124 that is being divided into two. k is the number of
125 least-significant bits in this process's rank that
126 must be zeroed out to find the rank of the root */
135 tmp_mask = mask >> 1;
138 dst = rank ^ tmp_mask;
140 tree_root = rank >> k;
143 /* send only if this proc has data and destination
144 doesn't have data. at any step, multiple processes
145 can send if they have the data */
147 (rank < tree_root + nprocs_completed)
148 && (dst >= tree_root + nprocs_completed)) {
151 for (j=0; j<(my_tree_root+mask); j++)
152 offset += recvcounts[j];
153 offset *= recvtype_extent;
155 smpi_mpi_send(((char *)tmp_buf + offset),
158 COLL_TAG_ALLGATHERV, comm);
159 /* last_recv_cnt was set in the previous
160 receive. that's the amount of data to be
163 /* recv only if this proc. doesn't have data and sender
165 else if ((dst < rank) &&
166 (dst < tree_root + nprocs_completed) &&
167 (rank >= tree_root + nprocs_completed)) {
170 for (j=0; j<(my_tree_root+mask); j++)
171 offset += recvcounts[j];
173 smpi_mpi_recv(((char *)tmp_buf + offset * recvtype_extent),
174 total_count - offset, recvtype,
175 dst, COLL_TAG_ALLGATHERV,
177 /* for convenience, recv is posted for a
178 bigger amount than will be sent */
179 last_recv_cnt=smpi_mpi_get_count(&status, recvtype);
180 curr_cnt += last_recv_cnt;
186 /* --END EXPERIMENTAL-- */
192 /* copy data from tmp_buf to recvbuf */
194 for (j=0; j<comm_size; j++) {
195 if ((sendbuf != MPI_IN_PLACE) || (j != rank)) {
196 /* not necessary to copy if in_place and
197 j==rank. otherwise copy. */
198 smpi_datatype_copy(((char *)tmp_buf + position*recvtype_extent),
199 recvcounts[j], recvtype,
200 ((char *)recvbuf + displs[j]*recvtype_extent),
201 recvcounts[j], recvtype);
203 position += recvcounts[j];