2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2006 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved.
15 * Additional copyrights may follow
20 #include "colls_private.h"
21 #include "coll_tuned_topo.h"
25 * Barrier is ment to be a synchronous operation, as some BTLs can mark
26 * a request done before its passed to the NIC and progress might not be made
27 * elsewhere we cannot allow a process to exit the barrier until its last
28 * [round of] sends are completed.
30 * It is last round of sends rather than 'last' individual send as each pair of
31 * peers can use different channels/devices/btls and the receiver of one of
32 * these sends might be forced to wait as the sender
33 * leaves the collective and does not make progress until the next mpi call
38 * Simple double ring version of barrier
40 * synchronous gurantee made by last ring of sends are synchronous
43 int smpi_coll_tuned_barrier_ompi_doublering(MPI_Comm comm
50 rank = smpi_comm_rank(comm);
51 size = smpi_comm_size(comm);
53 XBT_DEBUG("ompi_coll_tuned_barrier_ompi_doublering rank %d", rank);
55 left = ((rank-1+size)%size);
56 right = ((rank+1)%size);
58 if (rank > 0) { /* receive message from the left */
59 smpi_mpi_recv((void*)NULL, 0, MPI_BYTE, left,
60 COLL_TAG_BARRIER, comm,
64 /* Send message to the right */
65 smpi_mpi_send((void*)NULL, 0, MPI_BYTE, right,
69 /* root needs to receive from the last node */
71 smpi_mpi_recv((void*)NULL, 0, MPI_BYTE, left,
72 COLL_TAG_BARRIER, comm,
76 /* Allow nodes to exit */
77 if (rank > 0) { /* post Receive from left */
78 smpi_mpi_recv((void*)NULL, 0, MPI_BYTE, left,
79 COLL_TAG_BARRIER, comm,
83 /* send message to the right one */
84 smpi_mpi_send((void*)NULL, 0, MPI_BYTE, right,
88 /* rank 0 post receive from the last node */
90 smpi_mpi_recv((void*)NULL, 0, MPI_BYTE, left,
91 COLL_TAG_BARRIER, comm,
101 * To make synchronous, uses sync sends and sync sendrecvs
104 int smpi_coll_tuned_barrier_ompi_recursivedoubling(MPI_Comm comm
107 int rank, size, adjsize;
110 rank = smpi_comm_rank(comm);
111 size = smpi_comm_size(comm);
113 "ompi_coll_tuned_barrier_ompi_recursivedoubling rank %d",
116 /* do nearest power of 2 less than size calc */
117 for( adjsize = 1; adjsize <= size; adjsize <<= 1 );
120 /* if size is not exact power of two, perform an extra step */
121 if (adjsize != size) {
122 if (rank >= adjsize) {
123 /* send message to lower ranked node */
124 remote = rank - adjsize;
125 smpi_mpi_sendrecv(NULL, 0, MPI_BYTE, remote,
127 NULL, 0, MPI_BYTE, remote,
129 comm, MPI_STATUS_IGNORE);
131 } else if (rank < (size - adjsize)) {
133 /* receive message from high level rank */
134 smpi_mpi_recv((void*)NULL, 0, MPI_BYTE, rank+adjsize,
135 COLL_TAG_BARRIER, comm,
141 /* exchange messages */
142 if ( rank < adjsize ) {
144 while ( mask < adjsize ) {
145 remote = rank ^ mask;
147 if (remote >= adjsize) continue;
149 /* post receive from the remote node */
150 smpi_mpi_sendrecv(NULL, 0, MPI_BYTE, remote,
152 NULL, 0, MPI_BYTE, remote,
154 comm, MPI_STATUS_IGNORE);
158 /* non-power of 2 case */
159 if (adjsize != size) {
160 if (rank < (size - adjsize)) {
161 /* send enter message to higher ranked node */
162 remote = rank + adjsize;
163 smpi_mpi_send((void*)NULL, 0, MPI_BYTE, remote,
176 * To make synchronous, uses sync sends and sync sendrecvs
179 int smpi_coll_tuned_barrier_ompi_bruck(MPI_Comm comm
183 int distance, to, from;
185 rank = smpi_comm_rank(comm);
186 size = smpi_comm_size(comm);
188 "ompi_coll_tuned_barrier_ompi_bruck rank %d", rank);
190 /* exchange data with rank-2^k and rank+2^k */
191 for (distance = 1; distance < size; distance <<= 1) {
192 from = (rank + size - distance) % size;
193 to = (rank + distance) % size;
195 /* send message to lower ranked node */
196 smpi_mpi_sendrecv(NULL, 0, MPI_BYTE, to,
198 NULL, 0, MPI_BYTE, from,
200 comm, MPI_STATUS_IGNORE);
209 * To make synchronous, uses sync sends and sync sendrecvs
211 /* special case for two processes */
212 int smpi_coll_tuned_barrier_ompi_two_procs(MPI_Comm comm
217 remote = smpi_comm_rank(comm);
219 "ompi_coll_tuned_barrier_ompi_two_procs rank %d", remote);
220 remote = (remote + 1) & 0x1;
222 smpi_mpi_sendrecv(NULL, 0, MPI_BYTE, remote,
224 NULL, 0, MPI_BYTE, remote,
226 comm, MPI_STATUS_IGNORE);
227 return (MPI_SUCCESS);
232 * Linear functions are copied from the BASIC coll module
233 * they do not segment the message and are simple implementations
234 * but for some small number of nodes and/or small data sizes they
235 * are just as fast as tuned/tree based segmenting operations
236 * and as such may be selected by the decision functions
237 * These are copied into this module due to the way we select modules
238 * in V1. i.e. in V2 we will handle this differently and so will not
239 * have to duplicate code.
240 * GEF Oct05 after asking Jeff.
243 /* copied function (with appropriate renaming) starts here */
245 int smpi_coll_tuned_barrier_ompi_basic_linear(MPI_Comm comm)
248 int size = smpi_comm_size(comm);
249 int rank = smpi_comm_rank(comm);
251 /* All non-root send & receive zero-length message. */
254 smpi_mpi_send (NULL, 0, MPI_BYTE, 0,
258 smpi_mpi_recv (NULL, 0, MPI_BYTE, 0,
260 comm, MPI_STATUS_IGNORE);
263 /* The root collects and broadcasts the messages. */
266 MPI_Request* requests;
268 requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) );
269 for (i = 1; i < size; ++i) {
270 requests[i] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
271 COLL_TAG_BARRIER, comm
274 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
276 for (i = 1; i < size; ++i) {
277 requests[i] = smpi_mpi_isend(NULL, 0, MPI_BYTE, i,
282 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
291 /* copied function (with appropriate renaming) ends here */
294 * Another recursive doubling type algorithm, but in this case
295 * we go up the tree and back down the tree.
297 int smpi_coll_tuned_barrier_ompi_tree(MPI_Comm comm)
299 int rank, size, depth;
302 rank = smpi_comm_rank(comm);
303 size = smpi_comm_size(comm);
305 "ompi_coll_tuned_barrier_ompi_tree %d",
308 /* Find the nearest power of 2 of the communicator size. */
309 for(depth = 1; depth < size; depth <<= 1 );
311 for (jump=1; jump<depth; jump<<=1) {
312 partner = rank ^ jump;
313 if (!(partner & (jump-1)) && partner < size) {
314 if (partner > rank) {
315 smpi_mpi_recv (NULL, 0, MPI_BYTE, partner,
316 COLL_TAG_BARRIER, comm,
318 } else if (partner < rank) {
319 smpi_mpi_send (NULL, 0, MPI_BYTE, partner,
327 for (jump = depth; jump>0; jump>>=1) {
328 partner = rank ^ jump;
329 if (!(partner & (jump-1)) && partner < size) {
330 if (partner > rank) {
331 smpi_mpi_send (NULL, 0, MPI_BYTE, partner,
334 } else if (partner < rank) {
335 smpi_mpi_recv (NULL, 0, MPI_BYTE, partner,
336 COLL_TAG_BARRIER, comm,